Abnormal Behavior with RadioButton - android

I am using RadioButton in my layout. Where i have provided background to the RadioButton. Below is the code
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:background="#drawable/rb_spain"
android:id="#+id/am_rb_spain" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:background="#drawable/rb_germany"
android:layout_marginLeft="#dimen/margin_15"
android:layout_marginRight="#dimen/margin_15"
android:id="#+id/am_rb_german" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:background="#drawable/rb_english"
android:id="#+id/am_rb_english" />
</RadioGroup>
RadioButton background drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="#drawable/ic_spain" />
<item android:state_checked="true" android:drawable="#drawable/ic_spain_pressed" /> //pressed image is Large in size
<item android:drawable="#drawable/ic_spain" /> <!-- default -->
</selector>
I have saved the selection in TinyDB so that app will remember my selection whenever i open my app. But whenever i open my app the default selected RadioButton background Appears Large.
I have taken all the images of same size in drawable's with same padding.
First flag is Default selected here. But it appear to be slightly large in size
Now i have pressed 2nd flag. But first flag is not coming to its normal state.

You should change your android:button="#null" to android:button="#drawable/rb_germany" and remove background from each RadioButton. It will run fine.
Your updated RadioButton should be like:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/rb_spain"
android:id="#+id/am_rb_spain" />

Related

Making Custom RadioButton with image on center

I'm trying to my custom RadioButton that I trying to make to look like this:
So I did custom drawables that respond to the RadioButton status and used it as background. This is alright.
The problem is that I'm not been able to center the images that I set through the android:button atribute.
Here is how I'm trying to use it in my layout.
<RadioGroup
android:id="#+id/presenter_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp"
android:gravity="center"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/cluster_centered"
android:gravity="center"
android:padding="8dp"
android:background="#drawable/presenter_radiobutton_left"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:background="#drawable/presenter_radiobutton_right"
android:button="#drawable/fire"
/>
</RadioGroup>
With this I'm getting this as result:
I've already tried to define a drawable that sets the gravity to center, but nothing changed. Here is the custom drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/cluster" android:gravity="center"/>
</item>
</selector>
So, how could I center the button images?
PS: I can't set each kind of button as background because it'll be dynamic in the future, so the only thing that could be in background is the blue shape.
My solution was to set android:button=#null, then set the image that I want into the android:drawableLeft attribute. So my RadioButton code is like this:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:background="#drawable/presenter_radiobutton_left"
android:button="#null"
android:drawableLeft="#drawable/fire"
/>
"on center" solution is
...
<RadioButton>
...
android:button="#null"
android:foreground="#drawable/your_selector_for_center_drawable"
android:background="#drawable/your_selector_for_background_drawable"
android:foregroundGravity="center"

Change the Color of RadioButton When Clicked

I have a RadioGroup having 3 Radio Buttons. By default , when I click the radio button, it turns Blue. I need to change the color, say , when I click the first one, it should turn to Yellow, the 2nd one Blue and the 3rd one to Green.
I have seen some tutorials to customize by styling the radio buttons, like Is it possible to change the radio button icon in an android radio button group but it hides all the buttons.
Here is my XML .
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/studentImage"
android:layout_alignParentRight="true"
android:layout_alignRight="#+id/studentImage"
android:layout_alignTop="#+id/studentImage"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
<RadioButton
android:id="#+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
<RadioButton
android:id="#+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
</RadioGroup>
You can use a statelist drawable like I wrote below:
<?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/color_pressed" /> <!-- pressed -->
<item android:drawable="#drawable/default_button" /> <!-- default -->
</selector>
color_pressed and deafult_button would be your drawables for each button's state, you could have multiple selectors for each radio button

How do I remove this rectangular of image button

There is a default rectangular background in my Image Button. I want to remove this
This is related codes:
<ImageButton
android:layout_width="62dp"
android:layout_weight="1"
android:layout_height="60dp"
android:id="#+id/im16"
android:layout_gravity="fill_horizontal"
android:src="#drawable/off1"/>
Just try this
<ImageButton
android:layout_width="62dp"
android:layout_weight="1"
android:layout_height="60dp"
android:id="#+id/im16"
android:layout_gravity="fill_horizontal"
android:background="#drawable/off1"/>
just change your android:src to android:background
You will want to set the background of the imageButton to be something invisible. I do this by making the following XML drawable and setting it to the ImageButton's background. This will let the button still show a color when it is clicked, but blank otherwise.
blank_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/some_color" />
<item android:drawable="#android:color/transparent" />
</selector>
<ImageButton
android:layout_width="62dp"
android:layout_weight="1"
android:layout_height="60dp"
android:id="#+id/im16"
android:layout_gravity="fill_horizontal"
android:src="#drawable/off1"
android:background="#drawable/blank_selector" />
best solution is
android:background="#null"

Changing background or foreground button color dynamically in android

Suppose I have a footer like the following in my app, defined in a XML file such as footer.xml:
<LinearLayout android:id="#+id/llfooter"
android:layout_weight="1" android:layout_width="fill_parent"
android:orientation="horizontal" android:layout_height="0dp"
android:visibility="visible" android:background="#drawable/fbg"
android:weightSum="5.0" android:gravity="center"
android:layout_margin="0dp">
<Button android:id="#+id/home" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_weight="1"
android:background="#drawable/home" android:textColor="#FFFFFF"
android:padding="10px"></Button>
<Button android:id="#+id/issue" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_weight="1"
android:background="#android:drawable/ic_menu_send" android:textColor="#FFFFFF"
android:padding="10px"></Button>
<Button android:id="#+id/browse" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_weight="1"
android:background="#android:drawable/ic_menu_slideshow" android:textColor="#FFFFFF"
android:padding="10px"></Button>
<Button android:id="#+id/search" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_weight="1"
android:background="#drawable/search" android:textColor="#FFFFFF"
android:padding="10px"></Button>
<Button android:layout_height="wrap_content" android:id="#+id/favorite"
android:background="#drawable/favorite" android:layout_width="wrap_content"
android:focusable="true"
android:clickable="true"
android:layout_weight="1"
android:textColor="#FFFFFF" android:padding="10px"></Button>
</LinearLayout>
Now, the problem is that home, issue, browse, etc. are PNG icons, and when I tap on them, user can't have feedback of touching, because they stay unchanged.
I would like to change background colour on pressing them (e.g. just a bit lighter). I know I can write down XML drawables () one per button, such as the following
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/bgalt" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/bgalt" />
<item android:drawable="#drawable/bgnorm" />
</selector>
.. but if I have 10 buttons (say, 5 for footer, 5 for header) I should create other 10 buttons with altered background (so more work with graph editor and .apk heavier because of more raster icons.. ).
Is there a way to create (even in java) a ligher color "onClick" and normal color "onRelease" instead, with only one icon per feature in resources?
Any suggestions?
Tnx in advance.
Gabo
Use an ImageButton, and set the android:src parameter to the button drawable with a transparent background, then set the android:background value to a selector drawable that changes color when selected for example.
That way you have a set of drawables for your icons and one drawable only for the background which changes according to the state of your button
you can get button bitmap in onTouch in the code and change color, but it's bad idea.
selector is best solution.

Set background for item in list view

I am working with list view in android.
I want to set background color for each item in list view.
If I use setbackgroudcolor function, it displays incorrect color. It seems there is the mix color of application background and new color.
If I use setbackgroundresource fund, it displays ok. However, when I click on an item, color is not change.
i need to set background for for each item on list view, and when click on an item, background is changed to other color or background
My code: OnView
// Set background
RelativeLayout root = (RelativeLayout) vi.findViewById(R.id.p60001_layout_info);
if (position % 2 == 0){
root.setBackgroundResource(R.drawable.cell_g_02);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}else{
root.setBackgroundResource(R.drawable.cell_g_01);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}
Row layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/p60001_layout_info"
android:layout_width="match_parent"
android:layout_height="30dp">
<TextView
android:id="#+id/p60001_item_info"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="15dp" android:layout_marginLeft="10dp" android:fadeScrollbars="false" android:textColor="#color/txt_normal" android:scrollHorizontally="true" android:gravity="center_vertical">
</TextView>
<ImageView
android:id="#+id/icon"
android:layout_width="22px"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/arrow" android:layout_marginRight="15dp"/>
<ImageView
android:id="#+id/p60001_image_notice_num"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_marginRight="30dp"
android:layout_toLeftOf="#+id/icon"
android:layout_marginTop="5dp" android:src="#drawable/fukidashi" android:visibility="invisible" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:longClickable="false"/>
<TextView
android:id="#+id/p60001_item_notices"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/icon"
android:ems="10" android:layout_marginRight="40dp" android:background="#android:color/transparent" android:freezesText="false" android:focusableInTouchMode="false" android:focusable="false" android:clickable="false" android:selectAllOnFocus="false" android:textSize="20px" android:layout_marginTop="4dp" android:visibility="invisible">
<requestFocus />
</TextView>
</RelativeLayout>
If you are using custom adapter concept and the above is your getView code.
you must a convert view(xml file of each row) change the background of it like below.
if (position % 2 == 0){
converterView.setBackgroundResource(R.drawable.cell_g_02);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}else{
converterView.setBackgroundResource(R.drawable.cell_g_01);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}
if you need to change the color on focus or pressed you use the below xml as background.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/red" />
<item android:state_selected="true"
android:drawable="#color/red" />
</selector>
also if need to set background for clicked items then a way to do this is::::
to have a bool variable(=false)in your pojo(bean) where you are storing the data of each item.
so when ever you click on item make that variable to true. And in getView() just check whether that variable is true or false and do the need full accordingly.
Try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use selected state color -->
<item android:drawable="#drawable/your_drawable_when_selected"
android:state_selected="true" />
<!-- When not selected, use default item color-->
<item android:drawable="#drawable/your_drawable_when_unselected" />
</selector>

Categories

Resources