-5

How to programmatically change the radiobutton color which is inside the popupmenu

private void showPopupMenu(View view) {
    PopupMenu popupMenu = new PopupMenu(getContext(), view);
    popupMenu.getMenuInflater().inflate(R.menu.menu_publish_more_popup, popupMenu.getMenu());
    MenuItem postItem = popupMenu.getMenu().findItem(R.id.action_post);
    postItem.setChecked(true);
    RadioButton radioButton = (RadioButton) (postItem.getActionView());
    if (Build.VERSION.SDK_INT >= 21) {
        ColorStateList colorStateList = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_enabled}, //disabled
                        new int[]{android.R.attr.state_enabled} //enabled
                },
                new int[]{
                        Color.GREEN
                        , Color.RED
                }
        );
        radioButton.setButtonTintList(colorStateList);//set the color tint list
        radioButton.invalidate(); //could not be necessary
    }
    popupMenu.show();
}

xml code radio button is in redbox of picture

tcqq
  • 41
  • 8

3 Answers3

0

Define colorstatelist first

ColorStateList colorStateList = new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_enabled} //enabled
                },
                new int[] {getResources().getColor(R.color.colorPrimary) }
        );

then assign them

AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
radioButton.setSupportButtonTintList(colorStateList);

AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
cbSelected.setSupportButtonTintList(colorStateList);

or else if this doesn't work then use this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background)));
}
male.setHighlightColor(getResources().getColor(R.color.background));
Reevanth Lama
  • 37
  • 1
  • 1
  • 4
0

Try this code:

PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.getMenuInflater().inflate(R.menu.menu_publish_more_popup, popupMenu.getMenu());
         popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
                     public boolean onMenuItemClick(MenuItem item) {  
                      if(item.getTitle().equals("Create Post")){
    if(Build.VERSION.SDK_INT>=21)
    {

        ColorStateList colorStateList = new ColorStateList(
                new int[][]{

                        new int[]{-android.R.attr.state_enabled}, //disabled
                        new int[]{android.R.attr.state_enabled} //enabled
                },
                new int[] {

                        Color.WHITE//disabled
                        ,Color.RED//enabled

                }
            );                       


        radio.setButtonTintList(colorStateList);//set the color tint list
        radio.invalidate(); //could not be necessary
    }
    } 
                      return true;  
                     }  
                    });  

                    popup.show();//showing popup menu  
tcqq
  • 41
  • 8
TREAF ALSHEMERI
  • 409
  • 5
  • 12
  • java.lang.ClassCastException: android.support.v7.view.menu.MenuItemImpl cannot be cast to android.widget.RadioButton – tcqq Jan 23 '18 at 10:57
  • ColorStateList colorStateList = new ColorStateList( new int[][]{new int[]{android.R.attr.state_enabled} //enabled }, new int[] {getResources().getColor(R.color.red) } ); RadioButton button = (RadioButton) popupMenu.getMenu().findItem(R.id.action_post); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { button.setButtonTintList(colorStateList); } – tcqq Jan 23 '18 at 11:02
  • Hi, how to get RadioButton? – tcqq Jan 24 '18 at 16:51
  • Hi , in onMenuItemClick just add this line to check if the button contains the same title of your radioButton : if(item.getTitle() == "Create Post") – TREAF ALSHEMERI Jan 24 '18 at 16:55
  • 【RadioButton radioButton = (RadioButton) popupMenu.getMenu().findItem(R.id.action_post);】I use this code, but radioButton == null. – tcqq Jan 24 '18 at 16:56
  • "Hi , in onMenuItemClick just add this line to check if the button contains the same title of your radioButton : if(item.getTitle() == "Create Post")" -Yes, use equals is right. – tcqq Jan 24 '18 at 16:59
  • If use "==" result is false, if use "equal" result is true. – tcqq Jan 24 '18 at 18:08
  • Yes, but now how can I get to radiobutton? – tcqq Jan 24 '18 at 18:12
0

You can try,

MenuItem menuitem=popupMenu.getMenu().findItem(R.id.action_post);
RadioButton radioButton=  (RadioButton)(MenuItemCompat.getActionView(menuitem));//menuitem.getActionView();
radioButton.setButtonTintList(colorstateList);

And in your menu, add the actionViewClass,

<item
android:id="@+id/action_post"
android:actionViewClass="android.widget.RadioButton"
/>

Your colorStateList should be like,

 ColorStateList colorStateList = new ColorStateList(
            new int[][]{
                    new int[]{}, //otherwise for all cases
                    new int[]{android.R.attr.state_checked} //checked
            },
            new int[]{
                    Color.GREEN,Color.RED
            }
    );
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
  • ColorStateList colorStateList = new ColorStateList( new int[][]{ new int[]{android.R.attr.state_enabled} //enabled }, new int[]{getResources().getColor(R.color.red)} ); MenuItem menuitem = popupMenu.getMenu().findItem(R.id.action_post); RadioButton radioButton = (RadioButton) (menuitem.getActionView()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { radioButton.setButtonTintList(colorStateList); } – tcqq Jan 23 '18 at 11:52
  • Using the code above will be error:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setButtonTintList(android.content.res.ColorStateList)' on a null object reference – tcqq Jan 23 '18 at 11:54
  • Get the RadioButton is null. – tcqq Jan 23 '18 at 12:05
  • An error occurred, I debug to find radioButton = null – tcqq Jan 24 '18 at 16:31
  • @裴佳瑞 could you please post your popup menu creation code. If you have used setactionview() then getactionview() will work fine..I think, it is better to choose popup window than menu – Jyoti JK Jan 26 '18 at 12:01
  • mMaterialButtonGo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupMenu(mMaterialButtonGo); } }); } private void showPopupMenu(View view) { PopupMenu popupMenu = new PopupMenu(getContext(), view); popupMenu.getMenuInflater().inflate(R.menu.menu_publish_more_popup, popupMenu.getMenu()); popupMenu.show(); MenuItem postItem = popupMenu.getMenu().findItem(R.id.action_post); postItem.setChecked(true); } – tcqq Jan 28 '18 at 23:11
  • Add `actionviewclass` in your menu as `android.widget.RadioButton` .Then try getactionview() method – Jyoti JK Jan 29 '18 at 03:51
  • **Now 'RadioButton' can be get**, but I tested setting the color answer above, but the color of 'RadioButton' has not been changed, how should I change the color of 'RadioButton'? – tcqq Jan 29 '18 at 05:17
  • You have to create custom drawables and use it as `radioButton.setButtonDrawable(R.drawable.your_drawable);` if your min api is <21. – Jyoti JK Jan 29 '18 at 05:39
  • try [this](https://stackoverflow.com/questions/18352259/android-ring-shape-for-radio-button) – Jyoti JK Jan 29 '18 at 05:45
  • This is to change the color in the form of xml, but now the application allows the user to pass in the color code to set the theme, so I have to programmatically change the color – tcqq Jan 29 '18 at 06:35
  • Yes, my android version is greater than 21, the code is called, but the color does not work – tcqq Jan 29 '18 at 06:37
  • What should I do to programmatically change the color of 'RadioButton'? – tcqq Jan 29 '18 at 06:39
  • ColorStateList will work fine. Please check your code or else edit your question with complete code – Jyoti JK Jan 30 '18 at 04:29
  • instead of using states as enable/disable, try checked – Jyoti JK Jan 31 '18 at 04:38
  • No, now `radiobutton` the color will not be changed, what am I doing wrong? How should I change is correct? – tcqq Jan 31 '18 at 13:47