22

In my Android app I've got a couple radion buttons which should get a different color and become bold upon selection. I managed to get the different color by defining a radio_pick_color.xml file in drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- checked -->
     <item android:state_checked="true"
           android:color="#00FF15" /> 
     <!-- default -->
     <item android:color="#4000FF" /> 
</selector>

and referencing this file in my main.xml file:

android:textColor="@drawable/radio_picker_color"

I now want to do the same for having the text bold. So I made another file called radio_picker_style.xml in which I wanted to define the style like this:

<item android:state_checked="true"
       android:style="bold" /> 

Unfortunately eclipse complaints that no resource identifier can be found for attribute 'style' in package 'android'. I also tried with android:textStyle, but from within a selector item it also doesn't know the android:textStyle attribute.

Does anybody know how I can get the selected radio button in bold?

==EDIT== The relevant part of my main.xml file:

<RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/option_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/option_1"
        android:textColor="@drawable/radio_picker_color"
        />

    <RadioButton
        android:id="@+id/option_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/option_2" 
        android:textColor="@drawable/radio_picker_color"
        />
</RadioGroup>

And the radio_picker_style.xml which I tried to put in drawable folder, but which says "Attribute is missing the Android namespace prefix":

<?xml version="1.0" encoding="utf-8"?>
<style name="mystyle">  
    <item name="android:textColor">#ffffff</item>
    <item name="android:textStyle">bold</item>
</style>
Zain
  • 37,492
  • 7
  • 60
  • 84
kramer65
  • 50,427
  • 120
  • 308
  • 488
  • possible duplicate of [Android customized button; changing text color](http://stackoverflow.com/questions/4692642/android-customized-button-changing-text-color) – ozbek Oct 01 '13 at 11:36
  • 3
    @shoerat - It is absolutely not a dublicate of the question you suggest. I know how to set colors, but getting the text bold is what I don't succeed in. Do you know how to make the selected text bold? – kramer65 Oct 01 '13 at 12:31
  • The only feasible solution I see is to do that during runtime... – ozbek Oct 02 '13 at 08:47

3 Answers3

22

The style has no selector: using one will either break the build/runtime or will have no effect and be neglected. There are only two types of selectors: for colors and for drawables.

So, there is only one option: apply bold style on the fly, possibly, by listening to check state changes.

=== Example ===

MainActivity.java:

package com.example.radiobuttontest;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
    private RadioGroup mGroup2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGroup2 = (RadioGroup)findViewById(R.id.radioGroup2);

        RadioButton button1 = (RadioButton)findViewById(R.id.option_1);
        button1.setOnCheckedChangeListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        RadioButton checked = (RadioButton)findViewById(mGroup2.getCheckedRadioButtonId());
        checked.setTypeface(Typeface.DEFAULT_BOLD);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        buttonView.setTypeface(isChecked ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
    }
}

layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radioGroup2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/option_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/option_1"
        android:textColor="@drawable/radio_picker_color" />

    <RadioButton
        android:id="@+id/option_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/option_2"
        android:textColor="@drawable/radio_picker_color" />

</RadioGroup>

drawable/radio_picker_color.xml [I changed colors you have used (they are very faint) in the question with "#0099CC" for demonstration purposes]:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- checked -->
     <item android:state_checked="true"
         android:color="#0099CC" />
     <!-- default -->
     <item android:color="#0099CC" />
</selector>

Result:

with bold effect

without bold effect

Hope this helps.

ozbek
  • 20,955
  • 5
  • 61
  • 84
-4

You can do something like this

Create your own style e.g. mystyle.xml

<style name="mystyle">  
    <item name="android:textColor">#ffffff</item>
    <item name="android:textStyle">bold</item>
</style>

Now call this in your selector like this

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
      android:state_checked="true"
      style="@style/mystyle" />
</selector>
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • 1
    I created radio_selected.xml and copy pasted your code in there, but it says that "Attribute is missing the Android namespace prefix", so I changed all the occurrences of "name" to "android:name", but it then still says that the attribute is missing the android namespace prefix. Furthermore, it also says "Unexpected text found in layout file: #fff". Any ideas what I'm doing wrong here? – kramer65 Oct 01 '13 at 12:02
  • post your complete xml – Jitender Dev Oct 01 '13 at 12:09
  • I added some more code to my question. Does that tell you more? – kramer65 Oct 01 '13 at 12:17
-8

Use

android:textStyle = "bold"
SHANK
  • 2,978
  • 23
  • 31
  • 1
    Excuse me, I should've mentioned that this also doesn't work when you're within a selector item. It also says that it doesn't have the textStyle attribute. – kramer65 Oct 01 '13 at 11:44