I have a selector where I set a circle as the background for the state_selected = true but I want to change the color when I click on the object. How can I do it?
This is how my drawables are set up:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#D0021B"/>
<stroke android:width="2dp" android:color="#910012" />
<size
android:width="50dp"
android:height="50dp" />
<corners android:radius="50dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/black" android:state_selected="false" />
<item android:drawable="#drawable/nav_circle" android:state_selected="true" />
</selector>
You can do it programmatically:
public static void colorDrawable(View view, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
if (wrappedDrawable != null) {
DrawableCompat.setTint(wrappedDrawable.mutate(), color);
setBackgroundDrawable(view, wrappedDrawable);
}
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}
if you want to change the color at the moment it is pressed, you can use state_pressed.
<item android:state_pressed="true"
android:drawable="#color/pressedColor"/> <!-- pressed state -->
<item android:state_focused="true"
android:drawable="#color/pressedColor"/> <!-- focused state -->
<item android:drawable="#color/colorPrimary"/>
you can also use another drawable under the android:drawable attribute
<item android:state_pressed="true"
android:drawable="#drawable/button_solid"/>
however, if you want to change the background drawable when it is pressed, you can change the button background to a different xml drawable
button.setBackground(getDrawable(R.drawable.selectorDrawable2));
Maybe this will help you :
i=(ImageView)findViewById(R.id.image);
i.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
}
});
ImageView xml code :
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/nav_circle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/image" />
Related
I need TextInputEditText bottom line color like below.
Default color => Gray
If user enter text, line color should remain as Blue. Currently, if i
enter input in first edittext and go to other edittext, first one
becoming gray again.
Edittext is Empty => Gray
I also require default hint animation of TextInputLayout, So, can't use EditText. I implemented this by using TextWatcher like here but not working.
Here is my code
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/EditScreenTextInputLayoutStyle"
app:hintTextAppearance="#style/etHintText">
<android.support.design.widget.TextInputEditText
android:id="#+id/etAddress"
style="#style/et_14_blk_sngl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="text"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
Style :
<style name="EditScreenTextInputLayoutStyle">
<item name="colorControlNormal">#color/gray</item>
<item name="colorControlActivated">#color/blue</item>
<item name="colorControlHighlight">#color/blue</item></style>
And,
private void UpdateLineColor()
{
if (!TextUtils.IsEmpty(this.Text))
{
DrawableCompat.SetTint(this.Background, ContextCompat.GetColor(this.Context, Resource.Color.blue));
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Lollipop)
{
ColorStateList colorStateList = ColorStateList.ValueOf(Resources.GetColor(Resource.Color.blue));
this.BackgroundTintList = colorStateList;
ViewCompat.SetBackgroundTintList(this, colorStateList);
}
this.Background.SetColorFilter(Resources.GetColor(Resource.Color.blue), PorterDuff.Mode.SrcAtop);
}
else
{
DrawableCompat.SetTint(this.Background, ContextCompat.GetColor(this.Context, Resource.Color.gray));
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Lollipop)
{
ColorStateList colorStateList = ColorStateList.ValueOf(Resources.GetColor(Resource.Color.gray));
this.BackgroundTintList = colorStateList;
ViewCompat.SetBackgroundTintList(this, colorStateList);
}
this.Background.SetColorFilter(Resources.GetColor(Resource.Color.gray), PorterDuff.Mode.SrcAtop);
}
}
you could custon the TextInputEditText's background,like this:
custom et_underline_selected.axml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="0dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#00f" /> // color blue
<padding android:bottom="4dp" />
</shape>
</item>
</layer-list>
et_underline_unselected.axml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="0dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:color="#0f0"
android:width="1dp" />
<padding android:bottom="4dp" />
</shape>
</item>
</layer-list>
edittext_bg_selector.axml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/et_underline_unselected"/>
<item android:state_focused="false"
android:drawable="#drawable/et_underline_selected"/>
</selector>
these three files put in Resources/drawable
then in your layout.axml:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/etHintText">
<android.support.design.widget.TextInputEditText
android:id="#+id/etAddress"
style="#style/et_14_blk_sngl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="text"
android:background="#drawable/edittext_bg_selector"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
finally in your activity.cs:
TextInputEditText etAddress = FindViewById<TextInputEditText>(Resource.Id.etAddress);
etAddress.FocusChange += (s, e) =>
{
if (e.HasFocus)
{
etAddress.SetBackgroundResource(Resource.Drawable.et_underline_selected);
}
else
{
if (etAddress.Text.Length > 0)
{
etAddress.SetBackgroundResource(Resource.Drawable.et_underline_selected);
}
else
{
etAddress.SetBackgroundResource(Resource.Drawable.et_underline_unselected);
}
}
};
is this effect you need ?
Replace your style with:
<style name="EditScreenTextInputLayoutStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#color/gray</item>
<item name="colorControlActivated">#color/blue</item>
<item name="colorControlHighlight">#color/blue</item>
</style>
what i basically wanna do is this:
When i click on the button i want its Text color to appear in a different color.
What i tried is this:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#color/red" />
<item
android:state_pressed="false"
android:color="#000" />
</selector>
and then i did use this selector as drawable on the button android:textColor
but this doesn solve it since it only changes its color while i press the button.
I want it like this:
Default: black
on click: blue
on click again: black
any ideas how to do that? :S
this is my shape for the button (if it matters):
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="-1dp"
android:insetLeft="-1dp"
android:insetRight="-1dp">
<selector>
<item android:state_pressed="false">
<shape android:shape="rectangle" >
<corners
android:radius="0dp"
/>
<solid
android:color="#color/background_grey"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="30dp"
/>
<stroke
android:width="1dp"
android:color="#ffb4b4b4"
/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle" >
<corners
android:radius="0dp"
/>
<solid
android:color="#color/pq_blue"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="30dp"
/>
<stroke
android:width="1dp"
android:color="#ffb4b4b4"
/>
</shape>
</item>
</selector>
</inset>
thx in advance
EDIT
so i tried to do it programatically and tied the folowing just to see if it changes color´s ..but yea..it doesn´t (it seems like my onCLick event doesnt work):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_item, container, false);
final Button likeButton = (Button)rootView.findViewById(R.id.btLike);
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String test = "tester";
if(BUTTON_STATE==BUTTON_STATE_ONCE){
likeButton.setTextColor(getResources().getColor(R.color.pq_blue));
BUTTON_STATE = BUTTON_STATE_TWICE;
}else{
likeButton.setTextColor(getResources().getColor(R.color.red));
BUTTON_STATE = BUTTON_STATE_ONCE;
}
}
});
return rootView;
}
}
NOTE: i do all tht stuff in onCreateView since im in a Fragment of my ActionBarActivity(with tabs) if im doing it in the onCreate i get a null pointer exception at findViewById ( since it searches for the ID in my mainActivity, if im right?)
so yea..any ideas?
Your textselector.xml -
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#color/red" /> <!--selected text colour-->
<item
android:state_focused="true"
android:color="#color/red" />
<item
android:color="#color/blue" /> <!--unselected text colour-->
</selector>
Your button in layout.xml -
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
android:textColor="#drawable/textselector" <!-- SET textselector HERE -->
android:background="#drawable/button_color"/>
You can use this code to do it programmatically:
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ColorStateList myList=myButton.getTextColors();
int myColor=myList.getDefaultColor();
switch(myColor)
{
case Color.BLACK:
myButton.setTextColor(Color.BLUE);
break;
case Color.BLUE:
myButton.setTextColor(Color.BLACK);
break;
}
}
});
If You want to have a behaviour like:
First Click: Button text black
Second Click: Button text blue
third Click: button text black again
I don´t think it´s possible with the selector, also not with state focused. Because if any other view will be clicked, the button is not focused anymore and will loose the textcolor, goes back to default. You have to do it in a programmatically way:
First, set the default textColor to what You want: black inside Your xml. So than You have the color on no press. make a globa variabel to save the state:
private int BUTTON_STATE = 0;
private final int BUTTON_STATE_ONCE = 0;
private final int BUTTON_STATE_TWICE = 1;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(BUTTON_STATE==BUTTON_STATE_ONCE){
button.setTextColor(Color.BLUE);
BUTTON_STATE = BUTTON_STATE_TWICE;
}else if(BUTTON_STATE==BUTTON_STATE_TWICE){
button.setTextColor(Color.BLACK);
BUTTON_STATE = BUTTON_STATE_ONCE;
}
}
});
That´s just a possible solution, there are many ways..
EDIT
for Your code:
Create that global variables like i did in my example above, and use them in the if/else statement:
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(BUTTON_STATE==BUTTON_STATE_ONCE){
likeButton.setTextColor(getResources().getColor(R.color.pqBlue));
BUTTON_STATE = BUTTON_STATE_TWICE;
}else if(BUTTON_STATE==BUTTON_STATE_TWICE){
likeButton.setTextColor(getResources().getColor(R.color.pqBlack));
BUTTON_STATE = BUTTON_STATE_ONCE;
}
}
});
Try adding text_effect.xml drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/white" /> <!-- pressed -->
<item android:color="#color/black" /> <!-- default -->
</selector>
add this line in button control
android:textColor="#drawable/text_effect"
It will work. Enjoy:)
If you do not mandatorily need a simple Button, you could use a ToggleButton that is made for binary-state handling... With a ToggleButton your selector would look like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/red" />
<!-- Default State -->
<item android:color="#000" />
</selector>
It seems like you wanna implement something like toggle button.
Instead of button u can use toggle button.
Though if you wanna use button only then u need to do some changes in your java code as well as xml code also
Create a file def_btn.xml it will look like this..
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="0dp"
/>
<solid
android:color="#color/background_grey"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="30dp"
/>
<stroke
android:width="1dp"
android:color="#ffb4b4b4"
/>
</shape>
Create another file press_btn.xml it will look like this..
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="0dp"
/>
<solid
android:color="#color/pq_blue"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="30dp"
/>
<stroke
android:width="1dp"
android:color="#ffb4b4b4"
/>
</shape>
Inside your activity declare a private boolean variable(say isPressed) by default isPressed is false. & for default button background will be def_btn.xml
Now write following in button's onClick event.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isPressed = !isPressed;
if(isPressed){
btn.setBackgroundResource(R.drawable.def_btn);
}else{
btn.setBackgroundResource(R.drawable.press_btn);
}
}
});
That's it..
Hi I am new to android development. I want to create onclick effects to textview. When I click on the textview it will blink or something effects make. I tried it with change color, but it's not working. How can I make blink effect on textview onclick ??
please help me with example code. thanks in advance :)
The easiest way is to set this background in the TextView:
android:background="?attr/selectableItemBackground"
And if you want to set a different color for the background, set that attr as foreground instead of background.
try this. it worked for me.
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
create a xml with name something like txt_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector>
then add in texview xml
android:background="#drawable/txt_bg"
android:clickable="true"
hope it will help.
try below code:-
<Button
android:id="#+id/action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_margin="5dp"
android:background="#drawable/btn_click"
android:gravity="center"
android:textColor="#color/white"
android:textSize="12sp" />
btn_click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_hover" android:state_pressed="true"/>
<item android:drawable="#drawable/button"/>
</selector>
or below also
btn_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#000000" />
<gradient
android:angle="270"
android:centerColor="#1a000000"
android:endColor="#33000000"
android:startColor="#android:color/transparent" >
</gradient>
<corners android:radius="5dp" />
</shape>
btn.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:color="#000000"
android:width="1dp"
/>
<gradient
android:angle="270"
android:centerColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:startColor="#android:color/transparent" >
</gradient>
<corners android:radius="5dp" />
</shape>
btn_click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_hover" android:state_pressed="true"/>
<item android:drawable="#drawable/btn"/>
</selector>
public class TesteBlinkActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
blink();
}
private void blink(){
final Handler handler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
int timeToBlink = 1000; //in milissegunds
try{Thread.sleep(timeToBlink);}catch (Exception e) {}
handler.post(new Runnable() {
#Override
public void run() {
TextView txt = (TextView) findViewById(R.id.usage);
if(txt.getVisibility() == View.VISIBLE){
txt.setVisibility(View.INVISIBLE);
}else{
txt.setVisibility(View.VISIBLE);
}
blink();
}
});
}
}).start();
}
What I am trying to achieve: When the button is pressed for the first time, it should get highlighted/ activated / pressed. On the second click on the button it should get unactivated/ not pressed.
and later I want to check that if the button isPressed, do something.
What I tried:
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.day_button:
if (v.isPressed() == true) {
v.setPressed(false);
} else if (v.isPressed() == false) {
v.setPressed(true);
}
return true;
I tried this with day_but.isPressed == true also.
you can try with this way change the background state of button when click set the images as a background
Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});
YOu can use toggle button. For more info http://developer.android.com/guide/topics/ui/controls/togglebutton.html
<ToggleButton
android:id="#+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"
android:onClick="onToggleClicked"/>
or there is another approach you can follow. you can apply style in your button.
<Button
android:id="#+id/button1"
style="#button_style/<StyleName>"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
Create a button_style.xml file in drawable directory
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector>
You can also define a Selector for your Button to customize highlighting,so you create a xml file then :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_enabled="true"
android:state_focused="false" android:drawable="#drawable/enable_notpressed" />
<item android:state_pressed="true" android:state_enabled="true"
android:drawable="#drawable/enable_pressed" />
<item android:state_pressed="false" android:state_enabled="false"
android:state_focused="false" android:drawable="#drawable/disabled" />
</selector>
then assign it to your button as background param.
use selector like:
make a new xml file in drawable folder and paste this code.and change values accordingly.
<padding
android:left="10dp"
android:top="2dp"
android:right="10dp"
android:bottom="2dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#003040"
android:endColor="#003040"
android:angle="180" />
<corners
android:radius="8dp" />
<padding
android:left="10dp"
android:top="2dp"
android:right="10dp"
android:bottom="2dp" />
</shape>
</item>
</selector>
!!happy coding!!
XML CODE:
<ToggleButton
android:id="#+id/btspeaker"
android:layout_width="100dp"
android:layout_height="70dp"
android:layout_marginRight="20dp"
android:background="#drawable/bgspeaker"
android:button="#null"
android:textOff=""
android:textOn="" />
In the drawable:
bgspeaker.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/speaker_btn_select" android:state_checked="true"/>
<item android:drawable="#drawable/speaker_btn" android:state_checked="false"/>
<item android:drawable="#drawable/speaker_btn"></item>
</selector>
In the activity:
if (v.getId() == R.id.btspeaker) {
if (btspeaker.isChecked()) {
Toast.makeText(context, "Pressed/Selected",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context,"UnSelected",
Toast.LENGTH_LONG).show();
}
}
I would like to achieve this specific type of radio buttons in my layout:
= different graphics for the first item, middle ones, and last one, which have different rounded corners. I can imagine doing this with different styles for the 3 types of buttons (using custom styles, stateful drawables).
I'm implementing this using custom toggle buttons. I would like to take advantage of drawable selector for using different drawables for the first and the last items, so I use:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_first="true"
android:drawable="#drawable/radio_left_act"/>
<item android:state_checked="true" android:state_last="true"
android:drawable="#drawable/radio_right_act"/>
<item android:state_checked="true"
android:drawable="#drawable/radio_middle_act"/>
<item android:state_checked="false" android:state_first="true"
android:drawable="#drawable/radio_left_inact"/>
<item android:state_checked="false" android:state_last="true"
android:drawable="#drawable/radio_right_inact"/>
<item android:state_checked="false"
android:drawable="#drawable/radio_middle_inact"/>
</selector>
But now I have a problem, that states state_first, state_last are not set automatically in my LinearLayout, so I have to set them manually every time the buttons are clicked. Is there some way, some layout, where these states are set automatically? Thank you for any help.
Found nothing special, so here is a "default" solution, with custom toggle buttons. Here are 3 different styles (put to styles.xml) for first, middle ones and last buttons:
<!-- Toggle button styles -->
<style name="CustomToggle">
<item name="android:paddingTop">9dp</item>
<item name="android:paddingBottom">9dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
</style>
<style name="FirstToggle" parent="#style/CustomToggle">
<item name="android:background">#drawable/radio_first</item>
</style>
<style name="MiddleToggle" parent="#style/CustomToggle">
<item name="android:background">#drawable/radio_middle</item>
</style>
<style name="LastToggle" parent="#style/CustomToggle">
<item name="android:background">#drawable/radio_last</item>
</style>
And a short code for activity handling the events of toggle buttons, so only 1 button is checked in the same time, and checked button is disabled:
public class AktivityActivity extends Activity
{
ArrayList<ToggleButton> toggle_buttons;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.aktivity);
initToggleButtons();
}
private void initToggleButtons()
{
toggle_buttons = new ArrayList<ToggleButton>();
toggle_buttons.add((ToggleButton) findViewById(R.id.toggle_1));
toggle_buttons.add((ToggleButton) findViewById(R.id.toggle_2));
toggle_buttons.add((ToggleButton) findViewById(R.id.toggle_3));
// Listen on all toggle buttons
for (ToggleButton toggle_button : toggle_buttons)
toggle_button.setOnCheckedChangeListener(check_listener);
// Check first toggle button
updateToggleButtons(toggle_buttons.get(0));
}
// Only one toggle can be checked, and checked button must be disabled
private void updateToggleButtons(ToggleButton checked_button)
{
for (ToggleButton toggle_button : toggle_buttons)
{
toggle_button.setChecked(toggle_button == checked_button);
toggle_button.setEnabled(toggle_button != checked_button);
}
}
// Toggle buttons change listener
OnCheckedChangeListener check_listener = new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
updateToggleButtons((ToggleButton) buttonView);
}
};
}
Maybe it can be usefull for somebody...
You should check out the Wordpress Android project. They use a "ToggleButton" that gives similar functionality. For the .xml look here. To download the complete source go here.
It wont be the same as what you want, as they just have toggle buttons, but that can most likely be adapted for your needed style of radio buttons (if it's not already built in).
The Wordpress Android project helped me learn a lot. Everything from theming, custom buttons, custom layouts, toggle buttons, xmlrpc, and more.
The simplest way I found for doing this is:
1) Extend RadioButton class as follows:
import android.content.Context;
import android.view.ViewGroup;
import android.widget.RadioButton;
public class RoundedButton extends RadioButton {
private static final int[] STATE_ONLY_ONE = new int[] {
android.R.attr.state_first,
android.R.attr.state_last,
};
private static final int[] STATE_FIRST = new int[] {
android.R.attr.state_first
};
private static final int[] STATE_LAST = new int[] {
android.R.attr.state_last
};
public RoundedButton(Context context) {
super(context);
}
#Override
protected int[] onCreateDrawableState(int extraSpace) {
ViewGroup parent = (ViewGroup) getParent();
if (parent == null) {
return super.onCreateDrawableState(extraSpace);
}
final int size = parent.getChildCount();
final boolean isFirst = (parent.getChildAt(0) == this);
final boolean isLast = (parent.getChildAt(size-1) == this);
int[] states = super.onCreateDrawableState(extraSpace + 2);
if (isFirst && isLast) {
mergeDrawableStates(states, STATE_ONLY_ONE);
} else if (isFirst) {
mergeDrawableStates(states, STATE_FIRST);
} else if (isLast) {
mergeDrawableStates(states, STATE_LAST);
}
return states;
}
}
2) Create One XML file in "res/drawable/rbtn_selector.xml" add below XML code for Radio Button background.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First Checked -->
<item android:state_first="true" android:state_checked="true">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_selected_start"
android:endColor="#color/radio_button_selected_end"
android:type="linear" />
<!--<solid android:color="#android:color/holo_blue_dark" />-->
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_selected" />
</shape>
</item>
<!-- First Unchecked -->
<item android:state_first="true" android:state_checked="false">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_unselected_start"
android:endColor="#color/radio_button_unselected_end"
android:type="linear" />
<!--<solid android:color="#android:color/holo_purple"/>-->
<corners android:topLeftRadius="10dp" android:topRightRadius="#dimen/radio_button_radius"/>
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_unselected" />
</shape>
</item>
<!-- Last Checked -->
<item android:state_last="true" android:state_checked="true">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_selected_start"
android:endColor="#color/radio_button_selected_end"
android:type="linear" />
<!--<solid android:color="#android:color/holo_green_dark" />-->
<corners android:bottomLeftRadius="#dimen/radio_button_radius" android:bottomRightRadius="#dimen/radio_button_radius"/>
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_selected" />
</shape>
</item>
<!-- Last Unchecked -->
<item android:state_last="true" android:state_checked="false">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_unselected_start"
android:endColor="#color/radio_button_unselected_end"
android:type="linear" />
<!--<solid android:color="#android:color/holo_red_dark"/>-->
<corners android:bottomLeftRadius="#dimen/radio_button_radius" android:bottomRightRadius="#dimen/radio_button_radius"/>
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_unselected" />
</shape>
</item>
<!-- Default Checked -->
<item android:state_checked="true">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_selected_start"
android:endColor="#color/radio_button_selected_end"
android:type="linear" />
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_selected" />
<!--<solid android:color="#android:color/holo_orange_dark" />-->
</shape>
</item>
<!-- Default Unchecked -->
<item android:state_checked="false">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_unselected_start"
android:endColor="#color/radio_button_unselected_end"
android:type="linear" />
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_unselected" />
<!--<solid android:color="#android:color/holo_green_light"/>-->
</shape>
</item>
</selector>
3) Create One XML file in "res/drawable/rbtn_textcolor_selector.xml" add below XML code for Radio Buttons Text selector color.(Text Color Selector xml file)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/radio_text_selected"/>
<item android:color="#color/radio_text_unselected"/>
</selector>
4) Set your style to the button:
4.1) Programmatically add some RoundedButton to an exixsting RadioGroup:
RoundedButton newRadioButton = new RoundedButton(this.getActivity());
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
newRadioButton.setBackgroundDrawable(this.getActivity().getResources().getDrawable(R.drawable.rbtn_selector));
} else {
newRadioButton.setBackground(this.getActivity().getResources().getDrawable(R.drawable.rbtn_selector));
}
newRadioButton.setTextColor(this.getActivity().getResources().getColorStateList(R.color.rbtn_textcolor_selector));
4.2) Xml:
<RoundedButton
android:id="#+id/bt_id_1"
android:background="#drawable/rbtn_selector"
android:textColor="#drawable/rbtn_textcolor_selector" />
5) Choose your own colors and dimensions the one I used in the example are:
<color name="radio_text_selected">#FFF</color>
<color name="radio_text_unselected">#222</color>
<color name="radio_button_selected_start">#5393c5</color>
<color name="radio_button_selected_end">#6facd5</color>
<color name="radio_button_unselected_start">#f9f9f9</color>
<color name="radio_button_unselected_end">#eee</color>
<color name="radio_button_border_selected">#2373a5</color>
<color name="radio_button_border_unselected">#aaa</color>
and:
<dimen name="radio_button_radius">10dp</dimen>
<dimen name="radio_button_border">0.7dp</dimen>