-2

How to change colour of Button? When I click "Equipment" button color will bi changed. And then when i clicked another button like "Technician" button Previous button("Equipment") colour set as default button color and "Technician" button color changed.

here is my code

   public void onButtonTabClick(View v)
    {
        Fragment fragment = null;
        switch (v.getId())
        {
            case R.id.button_equipment:

              fragment = new EquipmentFragment();

               break;
            case R.id.button_tech:

                fragment = new TechnicianFragment();

                break;

            case R.id.button_timeline:
                fragment = new TimeLineFragment();

                break;
        }

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.show_fragment, fragment);
        transaction.commit();
    }

enter image description here

Mohsin
  • 9
  • 5

2 Answers2

2

You just need to write specific code for setting background color of buttons.

public void onEquipmentPressed(){
    equipmentButton.setBackgroundColor(getResources().getColor(R.color.color_id));
    technicans.setBackgroundColor(getResources().getColor(R.color.default_color_id));
    timeline.setBackgroundColor(getResources().getColor(R.color.default_color_id));

}


public void onTechnicansPressed(){
    equipmentButton.setBackgroundColor(getResources().getColor(R.color.default_color_id));
    technicans.setBackgroundColor(getResources().getColor(R.color.color_id));
    timeline.setBackgroundColor(getResources().getColor(R.color.default_color_id));

}

and so on

Asset Bekbossynov
  • 1,633
  • 3
  • 13
  • 25
0

You don't need to declare many method to handle button's state like @Asset Bekbossynov. You can write code in this way:

private View mLastClickView;
public void onButtonTabClick(View v)
{
    // add these code
    if (mLastClickView != null) {
        mLastClickView.setBackgroundColor(getResources().getColor(R.color.unselected));
    }
    v.setBackgroundColor(getResources().getColor(R.color.selected));
    mLastClickView = v;

    Fragment fragment = null;
    switch (v.getId())
    {
        case R.id.button_equipment:
            fragment = new EquipmentFragment();

            break;
        case R.id.button_tech:

            fragment = new TechnicianFragment();

            break;

        case R.id.button_timeline:
            fragment = new TimeLineFragment();

            break;
    }

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.show_fragment, fragment);
    transaction.commit();
}
orzangleli
  • 178
  • 7