-2

I have use class RadioButtonCenter at

https://github.com/pizza/MaterialTabs/blob/master/sample/src/io/karim/materialtabs/sample/ui/RadioButtonCenter.java

It worked good in below API 24. But in API 24, It not display correct.

when i click tab new. tab old always active. When I intent to activity diff and back. It display correct.

I did not find the problem. Please. Help me!

This is xml

  <RadioGroup
    android:id="@+id/bottom_navigation_on_main"
    android:layout_width="match_parent"
    android:layout_height="@dimen/item_height"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:weightSum="4">

    <RadioButtonCenter
        android:id="@+id/tab_home"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/selector_tab_main"
        android:button="@null"
        android:clickable="true"
        android:gravity="center"
        app:radioDrawable="@drawable/selector_tab_home" />

    <RadioButtonCenter
        android:id="@+id/tab_search"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:clickable="true"
        android:background="@drawable/selector_tab_main"
        android:gravity="center"
        app:radioDrawable="@drawable/selector_tab_search" />
   </RadioGroup>

This is selector

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_home_on" android:state_checked="true" 
 />
<item android:drawable="@drawable/ic_home_on" android:state_activated="true" 
 />
<item android:drawable="@drawable/ic_home_off" />
 </selector>
keyur patel
  • 99
  • 1
  • 7
Ha Nguyen
  • 91
  • 8

1 Answers1

0

I found the problem. I add requestLayout(); and now It worked !

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (buttonDrawable != null) {
        if (Build.VERSION.SDK_INT >= 24){
            requestLayout();
        }
        buttonDrawable.setState(getDrawableState());
        final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
        final int height = buttonDrawable.getIntrinsicHeight();

        int y = 0;

        switch (verticalGravity) {
            case Gravity.BOTTOM:
                y = getHeight() - height;
                break;
            case Gravity.CENTER_VERTICAL:
                y = (getHeight() - height) / 2;
                break;
        }

        int buttonWidth = buttonDrawable.getIntrinsicWidth();
        int buttonLeft = (getWidth() - buttonWidth) / 2;
        buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height);
        buttonDrawable.draw(canvas);
    }
}
Ha Nguyen
  • 91
  • 8