Navigation View app:menu build gradle error - gradle

I am trying to use Navigation View in Drawer Layout, but I keep getting errors when building the file.
Here's what my xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_items"
/>
</android.support.v4.widget.DrawerLayout>
This leads to a build gradle error. My menu looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/menu_bookmark"
android:title="Bookmark" />
<item android:id="#+id/menu_save"
android:title="Save" />
<item android:id="#+id/menu_search"
android:title="Search" />
<item android:id="#+id/menu_share"
android:title="Share" />
</group>
</menu>
When I remove app:menu from the navigationView,the error goes away. What could be wrong?

My fix was that I needed to add app:headerLayout="#layout/my_header_layout" as well.

Related

Android Navigation Component: How do I style toolbar based on navigation graph

I have the following nav_graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nav_graph"
app:startDestination="#id/actualFragment">
<fragment
android:id="#+id/actualFragment"
android:name="com.online.view.ui.ActualFragment"
android:label="Актуальное"
tools:layout="#layout/fragment_actual" >
<action
android:id="#+id/action_actualFragment_to_actualEventFragment"
app:destination="#id/actualEventFragment"
app:enterAnim="#anim/nav_default_pop_enter_anim"
app:exitAnim="#anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="#+id/actualEventFragment"
android:name="com.online.view.ui.detail.ActualEventFragment"
android:label="Актуальное"
tools:layout="#layout/actual_event_fragment" />
</navigation>
and I have MainActivity with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent" android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"
android:background="#color/white"
android:paddingLeft="8dp"
app:contentInsetStart="8dp"
app:contentInsetStartWithNavigation="0dp"
app:titleTextAppearance="#style/TabTitles"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:theme="#style/ToolbarStyle"
/>
<fragment
android:id="#+id/content"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="#+id/bottom_tabs"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.587"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:navGraph="#navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_tabs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="#drawable/bottom_nav_colors"
app:itemTextAppearanceActive="#style/bottomBarTextSelected"
app:itemTextAppearanceInactive="#style/bottomBarText"
app:itemIconTint="#drawable/bottom_nav_colors"
android:alpha="1.0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/tabs_navigation"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Now I want to have different styling for activity toolbar when top level fragment is loaded and when its child is loaded (showing back arrow in toolbar).
QUESTION: How can I achieve this different styling for toolbar (appbar) if I use Android Navigation Component and I navigate from root fragment to child fragment using the following code:
Bundle bundle = new Bundle();
bundle.putString("id", ei.id);
MainActivity)context).getNavController().navigate(R.id.action_actualFragment_to_actualEventFragment, bundle);
NavigationUI uses an AppBarConfiguration object to manage the behavior of the Navigation button. The Navigation button’s behavior changes depending on whether the user is at a top-level destination. Have a look at their documentation Update UI components with NavigationUI

Replacing ActionBar title by custom image not working

I wanted to replace ActionBar title(just title not icons) by an image (for example image I want the icon to remain the same but replace the title with another image )
.So I created a custom layout with ImageView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent">
<ImageView
android:id="#+id/actionbar_img"
android:src="#drawable/ic_protect_go_white"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
then on my Oncreate(), I wrote the code:-
RequestWindowFeature(WindowFeatures.ActionBarOverlay);
SetContentView(Resource.Layout.MainView);
Window.SetFeatureInt(WindowFeatures.ActionBarOverlay, Resource.Layout.CustomActionBar_Title);
Here is my theme
<style name="AppTheme" parent="#Theme.AppCompat.Light.NoActionBar">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
but nothing is working and there is no error. What am I missing?
Replacing ActionBar title by custom image not working
You could try using Toolbar to implement this feature :
The Toolbar is an action bar component that provides more flexibility than the default action bar.
Create a Custom Theme
Open the Resources/values/styles.xml, replace its contents with the following XML :
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:colorPrimary">#5A8622</item>
</style>
</resources>
Open the Resources/values-21/styles.xml, replace its contents with the following XML :
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!--
Base application theme for API 21+. This theme replaces
MyTheme from res/values/styles.xml on API 21+ devices.
-->
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
Define a Toolbar Layout
In the Resources/layout directory, create a new file called toolbar.xml. Replace its contents with the following XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
Include the Toolbar Layout
Edit the layout file Resources/layout/MainView.axml and replace its contents with the following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
4. Find and Activate the Toolbar
if you want "the icon to remain", you could find the NavigationIcon ic_action_menu in this site, you can find in Clipart part.
var toolbar = FindViewById<Toolbar> (Resource.Id.toolbar);
toolbar.SetNavigationIcon(Resource.Drawable.ic_action_menu); //the icon to remain
//"replace ActionBar title(just title not icons) by an image", I think this is what you want
toolbar.SetLogo(Resource.Drawable.Icon);
SupportActionBar.Title = "";
//Toolbar will now take on default actionbar characteristics
SetSupportActionBar(toolbar);
For more information, you could read the document or this example.
Effect :
Update:
In the Resources/menu subdirectory, create a new XML file called home.xml and replace the contents with the following XML:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_logo"
android:icon="#drawable/logo" //assume this is your image, it will adjust its size according to your toolbar's size
local:showAsAction="ifRoom"
android:title="Logo" />
<item
android:id="#+id/menu_share"
android:icon="#drawable/ic_action_social_share"
local:showAsAction="ifRoom"
android:title="Share" />
<item
android:id="#+id/menu_settings"
local:showAsAction="never"
android:title="Settings" />
</menu>
Inflate the Menus in your Activity:
// The options menu in which you place your items.
// This is the menu for the Toolbar/ActionBar to use
public override bool OnCreateOptionsMenu (IMenu menu)
{
MenuInflater.Inflate (Resource.Menu.home, menu);
return base.OnCreateOptionsMenu (menu);
}

Data Binding Error Trying to Pass ViewModel into Include Layout with Abstract Variable Type

*EDIT: To answer my own question, I had to add EditorViewModel as an import to the parent abstract class in the outer layout, and cast the viewModel to the parent class using app:viewModel="#{((EditorViewModel)viewModel)}", that was it! I swear I don't recall doing this cast before though...*
My problem results because the included layout is defining a type which is the parent abstract class, and NOT the child concrete class, of the viewModel the outer layout is trying to share with the included layout.
I've confirmed that changing the included layout's type to the child concrete class' type fixes the issue, however, it should work with the abstract class type...
Here is how I define my viewModel variable, this is the concrete class in the type below:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.ootpapps.gpeofflinedatacollection.viewmodels.EquipmentEditorViewModel" />
</data>
...
A few statements later I include the layout, attempting to share the viewModel from above:
<include
layout="#layout/layout_spinner_location"
app:viewModel="#{viewModel}" />
Within the included layout the variable viewModel is defined as follows:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.ootpapps.gpeofflinedatacollection.viewmodels.EditorViewModel" />
</data>
And of course here's the EquipmentEditorViewModel showing it extends the abstract parent class EditorViewModel (defined as the parent class type above in the included layout):
public class EquipmentEditorViewModel extends EditorViewModel<Equipment> {
The error I receive is:
****/ data binding error ****msg:Cannot find the setter for attribute 'app:viewModel' with parameter type com.ootpapps.gpeofflinedatacollection.viewmodels.EquipmentEditorViewModel on com.ootpapps.gpeofflinedatacollection.databinding.LayoutSpinnerLocationBinding.
file:C:\Users\Ryan\AndroidstudioProjects\GPEOfflineDataCollection\app\src\main\res\layout\content_equipment_editor.xml
loc:31:33 - 31:41
****\ data binding error ****
As I mentioned above, changing the type in layout_spinner_location to "EquipmentEditorViewModel" fixes the error, HOWEVER, I need to use the abstract type in order to re-use this view as it doesn't always use an EquipmentEditorViewModel, sometimes it needs a "ToolEditorViewModel" or a "MeasurmentEditorViewModel" all of which extend EditorViewModel.
Thank you very much for your help. Maybe I will get lucky and George Mount will stop by.
Just for clarity if anyone comes across this.
Added 3 approaches to tackle the problem, #3 seems to be the winner
This seems to be an issue with the new gradle 3 and upward with the following error: Cannot find the setter for attribute 'app:viewModel'
Before
parent xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.myapp.android.viewmodel.base.PageWebViewViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<WebView
android:id="#+id/webview_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="#{viewModel.contentVisibility}"
app:baseUrl="#{null}"
app:builtInZoomControlsEnabled="#{false}"
app:cacheMode="#{viewModel.webViewCacheMode}"
app:data="#{viewModel.webViewData}"
app:domStorageEnabled="#{true}"
app:encoding="#{`UTF-8`}"
app:headers="#{viewModel.headers}"
app:javaScriptEnabled="#{true}"
app:loadUrl="#{viewModel.webViewUrl}"
app:mimeType="#{`text/html; charset=utf-8`}"
app:webChromeClient="#{viewModel.webChromeClient}"
app:webInterface="#{viewModel.webInterface}"
app:webInterfaceName="#{viewModel.webInterfaceName}"
app:webViewClient="#{viewModel.webViewClient}" />
<include
layout="#layout/request_error_view"
app:viewModel="#{viewModel}" />
<include
android:id="#+id/lyt_loading"
layout="#layout/loading_layout"
app:viewModel="#{viewModel}" />
</FrameLayout>
</layout>
include/child xml (layout/request_error_view)
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="viewModel"
type="com.myapp.android.viewmodel.base.BaseViewModel" />
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/send_request_error_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="#{viewModel.errorVisibility}">
<TextView
android:id="#+id/text_error_sending_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/error_sending_request"
android:textColor="#color/window_primary_text"
android:textSize="#dimen/text_size_large" />
<Button
android:id="#+id/button_try_again"
style="#style/SuperbalistButton.Green.NoInset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/standard_content_margin"
android:onClick="#{viewModel::onClickReload}"
android:text="#string/try_again" />
</LinearLayout>
</layout>
After #1 (2nd variable approach, does not seem to work)
parent xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.myapp.android.viewmodel.base.PageWebViewViewModel" />
<variable
name="viewModelBase"
type="com.myapp.android.viewmodel.base.BaseViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<WebView
android:id="#+id/webview_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="#{viewModel.contentVisibility}"
app:baseUrl="#{null}"
app:builtInZoomControlsEnabled="#{false}"
app:cacheMode="#{viewModel.webViewCacheMode}"
app:data="#{viewModel.webViewData}"
app:domStorageEnabled="#{true}"
app:encoding="#{`UTF-8`}"
app:headers="#{viewModel.headers}"
app:javaScriptEnabled="#{true}"
app:loadUrl="#{viewModel.webViewUrl}"
app:mimeType="#{`text/html; charset=utf-8`}"
app:webChromeClient="#{viewModel.webChromeClient}"
app:webInterface="#{viewModel.webInterface}"
app:webInterfaceName="#{viewModel.webInterfaceName}"
app:webViewClient="#{viewModel.webViewClient}" />
<include
layout="#layout/request_error_view"
app:viewModel="#{viewModelBase}" />
<include
android:id="#+id/lyt_loading"
layout="#layout/loading_layout"
app:viewModel="#{viewModelBase}" />
</FrameLayout>
</layout>
After #2 (casting approach, works)
parent xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.myapp.android.viewmodel.base.PageWebViewViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<WebView
android:id="#+id/webview_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="#{viewModel.contentVisibility}"
app:baseUrl="#{null}"
app:builtInZoomControlsEnabled="#{false}"
app:cacheMode="#{viewModel.webViewCacheMode}"
app:data="#{viewModel.webViewData}"
app:domStorageEnabled="#{true}"
app:encoding="#{`UTF-8`}"
app:headers="#{viewModel.headers}"
app:javaScriptEnabled="#{true}"
app:loadUrl="#{viewModel.webViewUrl}"
app:mimeType="#{`text/html; charset=utf-8`}"
app:webChromeClient="#{viewModel.webChromeClient}"
app:webInterface="#{viewModel.webInterface}"
app:webInterfaceName="#{viewModel.webInterfaceName}"
app:webViewClient="#{viewModel.webViewClient}" />
<include
layout="#layout/request_error_view"
app:viewModel="#{((com.myapp.android.viewmodel.base.BaseViewModel) viewModel)}" />
<include
android:id="#+id/lyt_loading"
layout="#layout/loading_layout"
app:viewModel="#{((com.myapp.android.viewmodel.base.BaseViewModel) viewModel)}" />
</FrameLayout>
</layout>
After #3 (import and cast approach, works)
parent xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="com.myapp.android.viewmodel.base.BaseViewModel" />
<variable
name="viewModel"
type="com.myapp.android.viewmodel.base.PageWebViewViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<WebView
android:id="#+id/webview_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="#{viewModel.contentVisibility}"
app:baseUrl="#{null}"
app:builtInZoomControlsEnabled="#{false}"
app:cacheMode="#{viewModel.webViewCacheMode}"
app:data="#{viewModel.webViewData}"
app:domStorageEnabled="#{true}"
app:encoding="#{`UTF-8`}"
app:headers="#{viewModel.headers}"
app:javaScriptEnabled="#{true}"
app:loadUrl="#{viewModel.webViewUrl}"
app:mimeType="#{`text/html; charset=utf-8`}"
app:webChromeClient="#{viewModel.webChromeClient}"
app:webInterface="#{viewModel.webInterface}"
app:webInterfaceName="#{viewModel.webInterfaceName}"
app:webViewClient="#{viewModel.webViewClient}" />
<include
layout="#layout/request_error_view"
app:viewModel="#{(BaseViewModel) viewModel)}" />
<include
android:id="#+id/lyt_loading"
layout="#layout/loading_layout"
app:viewModel="#{((BaseViewModel) viewModel)}" />
</FrameLayout>
</layout>
This method works perfectly
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
<include
android:id="#+id/include"
layout="#layout/buttons_layout" />
</layout>
buttons_layout
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="#{() -> viewmodel.onClickChangeName()}"
android:text="Change Name"
app:layout_constraintTop_toBottomOf="#id/include"
app:layout_constraintStart_toStartOf="parent"
/>
</layout>
MainActivity
lateinit var dataBinding: ActivityMainBinding
lateinit var viewModel: viewModel
dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
viewModel = ViewModelProvider(this).get(ViewModel::class.java)
dataBinding.include.viewModel = viewModel
dataBinding.viewModel = viewModel
dataBinding.executePendingBindings()

xamsvg SvgImageView xamarin android no display

I try to test the XamSVG for android but no display and in debug the error is:
Failed to set EGL_SWAP_BEHAVIOR on surface 0xdb72a380,
error=EGL_SUCCESS.
How to solve this ?
I added the .svg file in res/raw/home.svg
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<xamsvg.SvgImageView
local:svg="#raw/home"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</RelativeLayout>

How to use selector with xamarin radiobutton to set background colors?

I have two radio buttons, which looks like a normal buttons. I would like to change their background color by using radio_button.xml with selector inside.
If I use little .png image to set background then it works fine.
But I tried similar code to use just colors (integers) and it doesn't work. What I'm doing wrong?
Code with background image:
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:text="LEFT"
android:id="#+id/radioLeft"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/radio_button"
android:checked="true" />
<RadioButton
android:text="RIGHT"
android:id="#+id/radioRight"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/radio_button" />
</RadioGroup>
radio_button.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/gray_dot" />
<item android:state_checked="false"
android:drawable="#drawable/blue_dot" />
</selector>
button_colors.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<color name="button_black">#000000</color>
<color name="button_white">#ffffff</color>
<color name="button_blue">#00afdb</color>
<color name="button_gray">#dcdcdc</color>
</resources>
I tried to replace
android:drawable="#drawable/blue_dot"
with
android:background="#color/button_blue"
but it's not working. How to fix?
I used shape xml with color to avoid background images.
radio_button.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/rb_checked" />
<item android:state_checked="false"
android:drawable="#drawable/rb_unchecked" />
</selector>
rb_checked.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/button_blue" />
</shape>
Main.axml
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:checked="true"
android:text="LEFT"
android:id="#+id/radioLeft"
android:button="#android:color/transparent"
android:background="#drawable/radio_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
<RadioButton
android:text="RIGHT"
android:id="#+id/radioRight"
android:button="#android:color/transparent"
android:background="#drawable/radio_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
</RadioGroup>
The trick that worked for me was
radioLeft.ButtonDrawable.SetTint(GetColor(Resource.Color.button_blue));
Or whatever color you wish to set, depending on the checked state, eg
radioLeft.ButtonDrawable.SetTint(GetColor(radioLeft.Checked ? Resource.Color.button_blue : Resource.Color.button_gray));

Resources