Button selector is not working when applied in Fragment
. It's working in an AppCompatDialogFragment
but not in a regular Fragment
that's instantiated via FragmentTransaction
. Here's my instantiation code:
var transaction = SupportFragmentManager.BeginTransaction();
var grpFragment = new GroupsFragment();
transaction.Replace(Resource.Id.fragment_frame, grpFragment, GroupsFragment.Tag);
transaction.Commit();
GroupsFragment.OnViewCreated
:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.fragment_groups, container, false)!;
Initialize(view);
return view;
}
In the fragment_groups.xml
layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/platin_white">
...
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_button"
android:text="Cancel"/>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
In the selector_button.xml
:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true">
<shape android:shape="rectangle">
<solid android:color="@color/danger_dark" />
<corners android:radius="10dp" />
<padding android:left="20dp" android:right="20dp" android:top="10dp" android:bottom="10dp" />
</shape>
</item>
<item android:state_enabled="true">
<shape android:shape="rectangle">
<solid android:color="@color/danger" />
<corners android:radius="10dp" />
<padding android:left="20dp" android:right="20dp" android:top="10dp" android:bottom="10dp" />
</shape>
</item>
</selector>
Any ideas on how I can fix it?
I'm not sure whether to call this a solution or a workaround but, I managed to solve the selector issue by using AndroidX's AppCompatButton
instead of the traditional Button
.
Just replaced this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_button"
android:text="Cancel"/>
By the following:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_button"
android:text="Cancel"/>
I also changed the parent of my custom Button
styles to Widget.AppCompat.Button
instead of @android:style/Widget.Button
.