skip to Main Content

I’m new to android studio and I’m trying to remove a button from my videos app

I have 4 buttons in my app Android : Recent,Featured,Popular,Random and Live.

I would like to delete the Live button, but I am not able to

If necessary I can take a screenshot of my android studio.

I don’t necessarily need to remove the button if I have another way to just hide it, that would be a big help too

I followed my code:

If necessary I can take a screenshot of my android studio.

package com.app.materialvideo.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.duolingo.open.rtlviewpager.RtlViewPager;
import com.app.materialvideo.Config;
import com.app.materialvideo.R;
import com.app.materialvideo.utils.CustomTabLayout;
import com.app.materialvideo.utils.SharedPref;

import static com.app.materialvideo.utils.Constant.FILTER_LIVE;
import static com.app.materialvideo.utils.Constant.FILTER_video;
import static com.app.materialvideo.utils.Constant.ORDER_FEATURED;
import static com.app.materialvideo.utils.Constant.ORDER_LIVE;
import static com.app.materialvideo.utils.Constant.ORDER_POPULAR;
import static com.app.materialvideo.utils.Constant.ORDER_RANDOM;
import static com.app.materialvideo.utils.Constant.ORDER_RECENT;

public class FragmentTabLayout extends Fragment {

    public RelativeLayout tab_background;
    public CustomTabLayout smartTabLayout;
    public ViewPager viewPager;
    public RtlViewPager viewPagerRTL;
    public int tab_count = 5;
    SharedPref sharedPref;
    View view;

    public FragmentTabLayout() {

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (Config.ENABLE_RTL_MODE) {
            view = inflater.inflate(R.layout.fragment_tab_layout_rtl, container, false);
        } else {
            view = inflater.inflate(R.layout.fragment_tab_layout, container, false);
        }
        sharedPref = new SharedPref(getActivity());
        tab_background = view.findViewById(R.id.tab_background);
        smartTabLayout = view.findViewById(R.id.tabs);
        if (sharedPref.getIsDarkTheme()) {
            tab_background.setBackgroundColor(getResources().getColor(R.color.colorToolbarDark));
            smartTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccentDark));
        } else {
            tab_background.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        }
        initViewPager();
        return view;

    }

    public void initViewPager() {
        if (Config.ENABLE_RTL_MODE) {
            viewPagerRTL = view.findViewById(R.id.view_pager_rtl);
            viewPagerRTL.setOffscreenPageLimit(tab_count);
            viewPagerRTL.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), tab_count));
            smartTabLayout.post(() -> smartTabLayout.setViewPager(viewPagerRTL));

        } else {
            viewPager = view.findViewById(R.id.view_pager);
            viewPager.setOffscreenPageLimit(tab_count);
            viewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), tab_count));
            smartTabLayout.post(() -> smartTabLayout.setViewPager(viewPager));
        }
    }

    public class ViewPagerAdapter extends FragmentStatePagerAdapter {

        int noOfItems;

        public ViewPagerAdapter(FragmentManager fm, int noOfItems) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            this.noOfItems = noOfItems;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return Fragmentvideo.newInstance(ORDER_RECENT, FILTER_video);
            } else if (position == 1) {
                return Fragmentvideo.newInstance(ORDER_FEATURED, FILTER_video);
            } else if (position == 2) {
                return Fragmentvideo.newInstance(ORDER_POPULAR, FILTER_video);
            } else if (position == 3) {
                return Fragmentvideo.newInstance(ORDER_RANDOM, FILTER_video);
            }  {
                return Fragmentvideo.newInstance(ORDER_LIVE, FILTER_LIVE);
            }
        }

        @Override
        public int getCount() {
            return noOfItems;
        }

        @Override
        public String getPageTitle(int position) {
            if (position == 0) {
                return getResources().getString(R.string.menu_recent);
            } else if (position == 1) {
                return getResources().getString(R.string.menu_featured);
            } else if (position == 2) {
                return getResources().getString(R.string.menu_popular);
            } else if (position == 3) {
                return getResources().getString(R.string.menu_random);
            }  {
                return getResources().getString(R.string.menu_live);
            }
        }
    }

}

2

Answers


  1. If you know id of the button.

     view.findViewById(R.id.btn_firs).setVisibility(View.GONE);
    
    Login or Signup to reply.
  2. From my understanding, you don’t want to remove a classic button, but a Tab button used inside a TabLayout.

    If you want to remove it , easiest way would be to remove the last condition in your if else statements that deal with displaying and handling this button.

    From the code you posted should be

    Here

    {
                return getResources().getString(R.string.menu_live);
    }
    

    and here, as i think these two deal with the "Live" button.

    {
                return Fragmentvideo.newInstance(ORDER_LIVE, FILTER_LIVE);
    }
    

    As well as reduce the value passed to the ViewPager Adapter "noOfItems" variable by 1. In your situation, I think the variable is "tabCount" and that should be set to 4 ( down from 5). I see it’s used in many places and changing this should deal with all cases.

    If hiding is also good, then this code should also work.

    Just use it inside onCreateView(). The code is in Kotlin and works on a default TabLayout, but i am not sure if it will work as i see you use a custom one.

    tabLayout?.getTabAt(4)?.view?.visibility = View.GONE
    

    You need to pass the index of the tab you want to hide, so in your case, you have 5 tabs, so the index is 4.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search