Question

I am having 1 layout which is common in all 3 tabs (Fragment) in my project. So what I did is I have created separate layout for that common structure and included the layout in all the fragments layout. The layout is basically having 4 buttons and on click of those buttons I am changing the background color of button and displaying data accordingly.

This is the 1st tab in which its coming perfect.

This is the second tab where all the buttons are getting the blue color.

Code :-

@Override
    public View onCreateView(final LayoutInflater inflater,
            final ViewGroup container, final Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment,
                container, false);
        mLvIncomingCalls = (ListView) rootView.findViewById(R.id.lv);

        mBtnDay = (Button) rootView.findViewById(R.id.btnDay);
        mBtnWeek = (Button) rootView.findViewById(R.id.btnWeek);
        mBtnMonth = (Button) rootView.findViewById(R.id.btnMonth);
        mBtnAllTime = (Button) rootView.findViewById(R.id.btnAllTime);

        mBtnDay.setOnClickListener(btnDayClickListener);
        mBtnWeek.setOnClickListener(btnWeekClickListener);
        mBtnMonth.setOnClickListener(btnMonthClickListener);
        mBtnAllTime.setOnClickListener(btnAllTimeClickListener);

        mBtnDay.setBackgroundColor(getResources().getColor(
                android.R.color.holo_blue_light));

        return rootView;
    }

Now as seen in the snap shot, the problem is that in second tab by default all the button get the blue color. I am not getting why its getting blue for all buttons, as in all the fragment I am adding the above code which by default makes Day button color to blue only.

Do I need to create separate layout for all?

Anyone has any idea about it please kindly guide me.

Was it helpful?

Solution

in onCreateView you can set other button colors to gray. This way it should definitely not happen. The reason why it's happening is a mystery, I believe the system somehow recycles the Drawable incorrectly.

OTHER TIPS

Put onTabChangedListener

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
            if (tabId.equals("first")) {
                // here you can set "setBackgroundColor"
            }else if (tabId.equals("second")) {
            ......
            ......
        }
    }
}

I think it will helpful.
Thank you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top