button needs to fill parent radiobutton - android

I have a radio group with two radiobuttons. each button has a custom selector. I would like for the drawable in the selector to fill out the width of the radio button. Under different resolutions the button looks different. I am not using a nine-patch so this makes sense but even if i was I am not sure how to make the button drawable stretch. Below is the code for the radio group and a selector. widths are different from fiddling around.
The layout xml:
<RadioButton
android:id="#+id/radiomale"
android:layout_width="126dp"
android:layout_height="50dp"
android:minHeight="50dp"
android:minWidth="126dp"
android:button="#layout/signupmale"
/>
<RadioButton
android:id="#+id/radiofemale"
android:layout_width="128dp"
android:layout_height="50dp"
android:minHeight="50dp"
android:minWidth="128dp"
android:button="#layout/signupfemale" />
</RadioGroup>
The Selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/genre_male" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/genre_male" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/genre_male" />
<item android:state_checked="false" android:drawable="#drawable/genre_male" android:scaleType="fitXY" />
<item android:state_checked="true" android:drawable="#drawable/genre_male_pressed" android:scaleType="fitXY"/>
</selector>
The problem is when testing under different resolutions gaps form between the buttons. Thanks for any help.

RadioGroup is actually a subclass of LinearLayout so what you could do is use layout_weight of the children to control the spacing. This might be a little helpful: Linear Layout and weight in Android
and this Android layout_weight comportment
<RadioGroup
android:layout_width="match_parent"
android:layout_height="45dip"
android:weightSum="1"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_weight=".5"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_weight=".5"
/>
</RadioGroup>
You are going to need to use ninepatch and maybe use padding left or right if there is text on the radio buttons.

Related

Android Custom Switch

I'm trying to make a custom switch like this:
with these properites:
text on both sides always shown.
different colors for on and off.
and these are two problems I faced since the switch only shows the text on the chosen side , and I can't seem to find a place where I can specify two different colors?
can I achieve this using the regular switch in android studio or must I use some library?
Thank you.
After researching I found a way that gives me exactly what I needed, this is what I got:
in case of anyone looking for a way to do it, this is how:
based on this post answer , which worked great for me.
this is what I did, I created two drawables one for On and another for Off :
switch_on.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_checked="true" android:drawable="#color/colorGray"/>
<item android:drawable="#color/colorPrimary" android:state_checked="true" />
<item android:drawable="#color/colorPrimaryDark" android:state_pressed="true" />
<item android:drawable="#color/transparent" />
</selector>
switch_off.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_checked="true" android:drawable="#color/colorGray"/>
<item android:drawable="#color/gray_light" android:state_checked="true" />
<item android:drawable="#color/black_overlay" android:state_pressed="true" />
<item android:drawable="#color/transparent" />
</selector>
Next , created a drawable for the outline of the switch.
outline.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#80ffffff" />
<stroke
android:width="1dp"
android:color="#color/gray_light" />
</shape>
one thing that I added is a drawable for the text color, because the color of the text changes depending on whether it's checked or not, this is it :
switch_text.xml
<?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/colorWhite"/>
<item android:state_checked="true" android:color="#color/colorWhite"/>
<item android:color="#color/gray_light"/>
</selector>
and finally, created RadioGroup in my layout this way:
<RadioGroup
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/outline"
android:checkedButton="#+id/off"
android:orientation="horizontal">
<RadioButton
android:id="#+id/off"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#drawable/switch_off"
android:button="#null"
android:gravity="center"
android:padding="#dimen/fab_margin"
android:text="#string/off"
android:textColor="#drawable/switch_text" />
<RadioButton
android:id="#+id/on"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:layout_marginEnd="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#drawable/switch_on"
android:button="#null"
android:gravity="center"
android:padding="#dimen/fab_margin"
android:text="#string/on"
android:textColor="#drawable/switch_text" />
</RadioGroup>
Notice the usage of each drawable in the right place:
android:background="#drawable/outline" for the RadioGroup
android:background="#drawable/switch_off" for the first RadioButton
android:background="#drawable/switch_on" for the second RadioButton
android:textColor="#drawable/switch_text" for both Radio Buttons
And that's all.
Use ToggleButton(change height/width as per image ratio) & selector, here's the code
<ToggleButton
android:id="#+id/toggle_"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:background="#drawable/on_off"
android:textOff=""
android:textOn=""/>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/ic_on" />
<item android:state_checked="false"
android:drawable="#drawable/ic_off" />
</selector>
To create ON and OFF labels for the switch you can use the attributes android:textOn and android:textOff with the switch declaration.
To ensure, that the text lebals are always displayed, especially for API Level bigger API21, also use this attribute: android:showText="true".
Then your switch should look like this:
<Switch
android:id="#+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:showText="true"
/>
In order to change the default color, you have to specify a seperate design for it like that:
1. In file values\styles.xml define a style like that:
<style name="CustomSwitchTheme" parent="Theme.AppCompat.Light">
<item name="android:colorControlActivated">#FF0000</item>
</style>
It is important, that you also reference the correct parent theme.
2. After defining the new switch style, you can link your custom style to the switch with the attribute
android:theme="#style/CustomSwitchTheme"
Finally your Switch should look like that:
<Switch
android:id="#+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:showText="true"
android:theme="#style/CustomSwitchTheme"
/>

How can I style the buttons like these in my Android application?

I would like to style the buttons in my Android application like this:
Whats the easiest way to create the buttons like above?
Currently I simply have a rectangle, here is the xml:
<Button
android:id="#+id/Main_DownloadCenter"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="90dp"
android:layout_marginRight="90dp"
android:background="#drawable/main_button_about_state"
android:text="txt" >
</Button>
What I need to do now is add the extra small rectangles that are on both sides of the main big rectangle. Meaning add a small_rect_grey and after that small_rect_aqua .. how can I achieve that?
Check out the Styling Your Button section:
http://developer.android.com/guide/topics/ui/controls/button.html#Style
Mainly borderless and custom background
button_custom.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_default" />
</selector>
activity_main.xml
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button"
android:onClick="action"
android:background="#drawable/button_custom" />

Android ToggleButton

By following this article I was able to create a toggle button that's made out of images. My toggle doesnt have any text, just on/off images.
When my toggle button is created it's being stretched and loses it's proportions, how do I make it retain it's original size?
These are the images im using:
The code:
main.xml:
<ToggleButton
android:id="#+id/changeNumerals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:checked="true"
android:background="#drawable/toggle_bg"
android:textOn=""
android:textOff=""
/>
drawable/toggle.xml:
<?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/toggle_off" />
<item android:state_checked="true" android:drawable="#drawable/toggle_on" />
</selector>
drawable/toggle_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background" android:drawable="#android:color/transparent" />
<item android:id="#+android:id/toggle" android:drawable="#drawable/toggle" />
</layer-list>
Try following attributes for <ToggleButton>
android:background="#android:color/transparent"
android:button="#drawable/toggle_bg"
It should work. Good luck :)
The dimensions are "hardcoded" but they'll scale with the size of your screen.
So the xml could look like:
android:layout_width="64dp"
android:layout_height="24dp"
The official documentation for dps is here
To support versions from GINGERBREAD up to the current ones, I ended up using:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/filterPhotos"
android:checked="true"
android:textOn=""
android:textOff=""
android:paddingLeft="5dp"
android:drawableRight="#drawable/toggle_filter_photos"
android:background="#null"/>
and the drawable toggle_filter_photos.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/picture_filter_icons"
android:state_checked="true" />
<item android:drawable="#drawable/picture_inactive_filter_icons" />
</selector>
The other solutions above didn't work well for GINGERBREAD and newer as either the icons wouldn't show up at all (with android:button on GINGERBREAD) or the aspect ratio was lost (with android:background on JELLY_BEAN_MR1 (4.2.2)).

how to set border to layout

I want set a border to Relativelayout. I make relativelayout clickable and use selector for changing selected color, also I use shape for rounding corners of layout. But when I select the layou the shape takes all background including the border. I can't find a way for solving this problem.
This is my main.xml
<RelativeLayout android:id="#+id/map_refresh"
android:layout_height="fill_parent" android:layout_width="wrap_content"
android:background="#drawable/settings_selector_up"
android:padding="15dp"
android:layout_marginLeft="15dip" android:layout_marginRight="15dip" android:layout_marginTop="5dip">
<TextView android:id="#+id/Text1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Map refresh period">
</TextView>
<TextView android:id="#+id/TextView09"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="1 min"
android:layout_alignParentRight="true"
android:paddingRight="5dp">
</TextView>
</RelativeLayout>
</LinearLayout>
this is my settings_selector_up.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:background="#drawable/settings_border_roundup" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/settings_border_roundup" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="#drawable/settings_border_roundup" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/settings_border_roundup" />
<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true"
android:drawable="#drawable/selector_pressed" >
</item>
<item android:state_pressed="true" android:drawable="#drawable/list_selector_pressed" />
</selector>
And this is my selector_pressed
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E6E6E6"></solid>
<corners android:radius ="10dp" />
</shape>
Is there a way receive same result as we go for example using listselector in listView (in listview when I select the listitem only background color changed but border stayed unchanged).
Put the whole thing in another parent layout that handles the drawing of the border, like this:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="your background 9-patch or shape of the border"
android:padding="some padding value so the border looks ok">
< Your RelativeLayout without the border stuff >
</FrameLayout>

what is the best way to set image to imageview

Hi I have created a relative layout. and align the imageview to the right of relativelayout. I set the background of relativelayout using selector. if i select thye relativelayout its color wil be green otherwise its color will be white. Now I want to set the image depending on the selection of scrollview. If scrollview is selected then i want to show imge1 if not then i want to show image2. How can I achieve this?
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selector">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow_right"
android:layout_alignParentRight="true" />
</RelativeLayout>
selector.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/grbk" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/grbk" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/grbk" />
<item
android:drawable="#drawable/whbk" />
</selector>
Thanks
Sunil
In the same way as for background. You should add android:duplicateParentState="true"
<ImageView
android:duplicateParentState="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow_selector"
android:layout_alignParentRight="true" />
first set the color to all the list list view item and then put the selector in the list view ..i gess u know how to set the selector in list view
We can create another selector for imageview and pass that selector as background to imageview.
imageselector.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/arow_selected" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/arow_selected" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/arow_selected" />
<item
android:drawable="#drawable/arow_unselect" />
</selector>
main.xml
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/imageselector"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dip"/>
</RelativeLayout>
Here is my suggestion..
once i set the background to the imageview, then when i want to change the transparent, i can do nothing...after check some website, i found i need to set the imageview's resourse instead of background...

Categories

Resources