Android: center background of radio button - android

I need to use custom radio buttons with background image, and space them evenly horizontally, I can achieve the spacing but cannot position the background image in the center of each button. My layout is as below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="58dp">
<RadioGroup
android:id="#+id/group_bottom_bar"
android:layout_width="match_parent"
android:layout_height="52dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/v4_btn_trip"
style="#style/BottomTabBarButtonStyle"
android:button="#drawable/v4_tabbar_radio_trip" />
<RadioButton
android:id="#+id/v4_btn_agenda"
style="#style/BottomTabBarButtonStyle"
android:button="#drawable/v4_tabbar_radio_agenda" />
<RadioButton
android:id="#+id/v4_btn_favorite"
style="#style/BottomTabBarButtonStyle"
android:button="#drawable/v4_tabbar_radio_favorite" />
<RadioButton
android:id="#+id/v4_btn_settings"
style="#style/BottomTabBarButtonStyle"
android:button="#drawable/v4_tabbar_radio_settings" />
</RadioGroup>
</RelativeLayout>
The style is per below:
<style name="BottomTabBarButtonStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_weight">1</item>
I already set the gravity to center_horizontal.
I tried to replace android:button with android:background but that just stretched the image.
One of the selector files (v4_tabbar_radio_trip)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_bottom_tab_trip_active" android:state_checked="true" android:state_pressed="true" />
<item android:drawable="#drawable/ic_bottom_tab_trip_active" android:state_pressed="true" />
<item android:drawable="#drawable/ic_bottom_tab_trip_active" android:state_checked="true" />
<item android:drawable="#drawable/ic_bottom_tab_trip" />
</selector>
This is its current state.
Update: The expected output is that the background image (the two dots) is in the middle of the greyed area (which is the radio button), not aligned to the left. Similarly for other icons

You can try this layout xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="58dp" >
<RadioGroup
android:id="#+id/group_bottom_bar"
android:layout_width="match_parent"
android:layout_height="52dp"
android:orientation="horizontal" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioButton
android:id="#+id/v4_btn_trip"
android:layout_gravity="center"
android:button="#drawable/v4_tabbar_radio_trip" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioButton
android:id="#+id/v4_btn_agenda"
android:layout_gravity="center"
android:button="#drawable/v4_tabbar_radio_agenda" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioButton
android:id="#+id/v4_btn_favorite"
android:layout_gravity="center"
android:button="#drawable/v4_tabbar_radio_favorite" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioButton
android:id="#+id/v4_btn_settings"
android:layout_gravity="center"
android:button="#drawable/v4_tabbar_radio_settings" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
I know this is not good way of handling the things but it can do a trick for you and it is feasible for all screens.
Try it. Hope this helps you.

try to write
style="#style/BottomTabBarButtonStyle"
after the
android:button="#drawable/v4_tabbar_radio_settings"
in layout xml file.

Try out this!!
<LinearLayout
android:id="#+id/ll_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:weightSum="2" >
<RadioGroup
android:id="#+id/radioGrouprecharge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radioPreapaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:button="#android:drawable/btn_radio" />
<RadioButton
android:id="#+id/radIoPostpaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_horizontal" />
</RadioGroup>
</LinearLayout>

You are centering your button, but the drawable is always left-aligned.
The solution appears to be create a new radio group. Here is an example.

Related

Custom dialog becomes the size of the first control in android

I have the following layout for my custom log in dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/padding_large">
<ImageButton android:src="#drawable/ic_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dialog.login.settings"
android:padding="#dimen/margin_medium" />
<TextView android:text="#string/dialog.login.text.user_name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/dialog.login.user_name"
android:layout_width="match_parent"
android:gravity="center"
android:ems="20"
android:layout_height="wrap_content"
android:inputType="text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:text="#string/dialog.login.text.password"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/dialog.login.password"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox android:id="#+id/dialog.login.show_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dialog.login.check.show_password"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_margin="#dimen/margin_medium"
android:checked="false" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="#+id/dialog.login.cancel"
android:drawableStart="#drawable/ic_exit_black"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/margin_medium"
android:text="#string/dialog.login.button.cancel" />
<Button android:id="#+id/dialog.login.connect"
android:text="#string/dialog.login.button.connect"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginStart="#dimen/margin_medium"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
when I create the dialog and setcontentview the width of the dialog becomes equal to that of the image button on the first row
if I remove that , it becomes the size of the EditText (dialog.login.user_name) whose ems is 20
when I make the dialog's constructor call the base dialog constructor with a theme of R.style.Theme.Material.Dialog it does get the correct (about 3/4 of the screen) but the problem is that it ignores my primary and accent colours (which is normal)
Is there a way to have the dialog have the Theme.Material.Dialog size but still keep my own primary/accent colours?
thanks in advance for any help you can provide
After a lot of searching it seems that the solution is to actually replace the dialogTheme attribute of my main theme, so the solution was:
<style name="app.theme"
parent="android:Theme.Material.Light.NoActionBar.Fullscreen">
<item name="android:dialogTheme">#style/app.dialog</item>
......
</style>
<style name="app.dialog"
parent="android:Theme.Material.Light.Dialog">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary.dark</item>
<item name="android:colorAccent">#color/accent</item>
</style>

android Separator after last item of ListView

I have two listviews and two different adapters for them. one shows the bottom divider but the other list does not show divider after the last item.
The one which shows divider after last item, only has listview in the layout. other one which does not shows divider has other views along with the listview.
both listviews have the same style which is below.
<style name="st_comman_data_list">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">#color/app_background_color</item>
<item name="android:cacheColorHint">#color/app_background_color</item>
<item name="android:drawSelectorOnTop">true</item>
<item name="android:fadeScrollbars">true</item>
<item name="android:fadingEdge">none</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:listSelector">#drawable/list_states_color</item>
<item name="android:scrollbarFadeDuration">1000</item>
<item name="android:fastScrollEnabled">true</item>
</style>
Edit
layout xml for correct divider
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/app_background_color_light"
android:orientation="vertical" >
<ListView
android:id="#+id/gift_home_ui_home_list"
style="#style/st_comman_data_list"
android:layout_height="match_parent" />
xml which don't shows divider
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/app_background_color"
android:cacheColorHint="#color/app_background_color"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_header"
android:gravity="center"
android:visibility="gone" >
<Button
android:id="#+id/tab_video_main_btn_all"
style="#style/st_btn_top_header_gry"
android:layout_marginRight="2dp"
android:text="#string/st_tab_main_btn_all" />
<Button
android:id="#+id/tab_video_main_btn_my_videos"
style="#style/st_btn_top_header_gry"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="#string/st_tab_video_main_btn_my_videos"
android:visibility="visible" />
<Button
android:id="#+id/tab_video_main_btn_saved"
style="#style/st_btn_top_header_gry"
android:layout_marginLeft="2dp"
android:text="#string/st_tab_main_btn_saved" />
</LinearLayout>
<View style="#style/st_single_black_line" />
<TableLayout
android:id="#+id/video_search_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/background_header_small"
android:focusableInTouchMode="false"
android:gravity="left"
android:stretchColumns="0"
android:visibility="gone" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="#dimen/margin_basic_very_large"
android:background="#drawable/audio_search_bg"
android:gravity="left|center_vertical"
android:paddingLeft="#dimen/padding_basic_2dp"
android:paddingRight="#dimen/padding_basic_2dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<ImageButton
android:id="#+id/video_btn_search_clear"
style="#style/st_search_ui_ic_search"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:src="#drawable/ic_clear_search"
android:visibility="gone" />
<ImageButton
android:id="#+id/video_btn_search"
style="#style/st_search_ui_ic_search"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_search" />
<EditText
android:id="#+id/video_search_edit_text"
style="#style/st_search_ui_search_bar"
android:layout_toLeftOf="#id/video_btn_search_clear"
android:layout_toRightOf="#id/video_btn_search"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="#string/hint_search_by_video_title_or_description"
android:imeOptions="actionDone"
android:singleLine="true"
android:windowSoftInputMode="stateVisible|adjustResize" />
</RelativeLayout>
</TableRow>
</TableLayout>
<ListView
android:id="#+id/no_more_text_list"
style="#style/st_no_more_text_list"
android:visibility="gone" />
<ListView
android:id="#+id/video_chanel_list"
style="#style/st_comman_data_list" />
<View
android:id="#+id/invisible"
android:layout_width="match_parent"
android:layout_height="1dp" />
Solutions which i have tried:
set height as "match_parent"
add a view under the list view and make it invisible
set footer divider as enabled (which is true by default)
Someone have any ides why it is behaving differently?

Remove custom toggle button text space android

Why is my toggle button taking the wrong width and have the space of text?
I want this toggle button to be exactly the same width as the other buttons
Here is the code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#242424"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/ten_dp"
android:layout_weight="30"
android:background="#drawable/action_bar_btn_bg"
android:text="Close"
android:textColor="#color/abc_primary_text_material_dark" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="70"
android:gravity="right"
android:orientation="horizontal"
android:padding="#dimen/ten_dp" >
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/five_dp"
android:background="#drawable/action_bar_btn_bg"
android:button="#drawable/btn_fav"
android:gravity="center"
android:textOn="#null"
android:textOff="#null" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/five_dp"
android:background="#drawable/action_bar_btn_bg"
android:src="#drawable/ic_action_left" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_bar_btn_bg"
android:src="#drawable/ic_action_right" />
</LinearLayout>
</LinearLayout>
#drawable/action_bar_btn_bg
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="#drawable/action_bar_btn_bg_normal"/>
#drawable/btn_fav
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_action_star_normal" android:state_checked="false" />
<item android:drawable="#drawable/ic_action_star_active" android:state_checked="true"/>
Please help me and tell me where I am going wrong?
Thank you.
Try adding a weight sum tag in the linear layout
android:weightSum = "3"
Then add weight attributes to the toggle and image buttons
android:weight="1"
EDIT: since you want to center the image within the toggleButton,
I recommend trying this in addition:
<ToggleButton
android:id="#+id/YourID
android:layout_width="0"
android:layout_height="75dp"
android:layout_weight="1"
android:checked="false"
android:textOn=""
android:textOff=""
android:drawableTop="#drawable/yourImage"
android:layout_gravity="center"
android:textColor="#yourColor"

How to add an image to a button in android

I was wondering how I put an image to a button.
I have tried to use "android:icon="#drawable/search.png"
and I add this image to the drawable-hdpi folder but the image doesn't show up. I am developing an application for the nexus 4 so I don't know what size the image should be. The image I am trying to add is a normal search icon used in the contact manager of the nexus 4.
The code I have written so far.
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl_group_coworkers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coworkers"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/lbl_group_family"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="#+id/lbl_group_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friends"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.70"
android:icon="#drawable/search.png" />
<Button
android:id="#+id/Button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addGroup"
android:icon="#drawable/addgroup.png"/>
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.11"
android:text="#string/Button3" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/Button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addGroup"
android:text="TEXT"
android:background="#drawable/addgroup"/>
To add background and image and text at the same time.
Just replace
android:background="#drawable/addgroup"
with
android:background="#android:color/transparent"
android:drawableTop="#drawable/addgroup"
android:text="TEXT"
You can also use attributes in your Button Layout with properties defining image source to placed inside the button
android:drawableLeft
android:drawableRight
android:drawableBottom
android:drawableTop
use the attribute
android:background="#drawable/...."
You can set the background of the Button using :
android:background="#drawable/..."
Please make drawable folder in res and create buttonanimation.xml in drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/add_incident_resetfocus" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/add_incident_resetfocus" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/add_incident_resetfocus" android:state_focused="true"/>
<item android:drawable="#drawable/add_incident_reset" android:state_focused="false" android:state_pressed="false"/>
</selector>
And use this buttonanimation in layout button background.
<Button
android:id="#+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonanimation"
/>

Centering CheckBox Views

If you're interested in RadioButtons in addition to (or instead of) CheckBoxes, see this question instead.
Despite the presence of
<item name="android:layout_gravity">center_horizontal</item>
in the style file, the two checkboxes are not centered, but appear "left-justified".
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/cb1"
style="#style/CB_style" />
<CheckBox
android:id="#+id/cb2"
style="#style/CB_style" />
</LinearLayout>
res/values/styles.xml
<style name="CB_style" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:checked">false</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
You have android:layout_weight = 1. So your checkboxes fill all width of screen.
Remove android:layout_weight from style and add margin between checkboxes.
Gravity in Checkbox don't affect the inner tick button, only text.
EDIT
Ok, try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="1"/>
<CheckBox
android:id="#+id/cb1"
style="#style/CB_style" />
<TextView
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="1"/>
<CheckBox
android:id="#+id/cb2"
style="#style/CB_style" />
<TextView
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="1"/>
</LinearLayout>
And remove layout_weight from style.
Another way is create custom checkbox. But I think it's too complicated for this problem.
Here's something that may help:
android:gravity vs android:layout_gravity
android:gravity is usually internal, usually asking the view itself what to do with the pixels it has
android:layout_gravity is usually external, usually asking the parent ViewGroup (LinearLayout/RelativeLayout/FrameLayout) how to position the view with extra pixels that the view is not using
If you are using a view with wrap_content, you probably want to use layout_gravity. If you are using a view with match_parent, you probably want to try gravity.
Sometimes wrapping another ViewGroup around a troublesome View can help with positioning. Views and ViewGroups go through an intricate "screen space" negotiating phase, and different Views (Buttons/TextView/ImageView/etc) and ViewGroups (LinearLayout/RelativeLayout/TableLayout/etc) have different rules and negotiating powers
This is why sometimes pairing a troublesome View with another parent like a FrameLayout or LinearLayout can make it behave all of the sudden
This is a little messy but it works, and yes it works as well for 3 or more checkbox inputs:
<LinearLayout
android:padding="0dip"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="name"
android:layout_height="40dip"
android:textAlignment="center" android:layout_gravity="center"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dip"
android:layout_height="1dip" android:layout_weight="1" android:text=" " />
<CheckBox
android:id="#+id/chk1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleX="1.5" android:scaleY="1.5"
>
</CheckBox>
<TextView
android:layout_width="0dip"
android:layout_height="1dip" android:layout_weight="1" android:text=" " />
<CheckBox
android:id="#+id/chk2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleX="1.5" android:scaleY="1.5"
>
</CheckBox>
<TextView
android:layout_width="0dip"
android:layout_height="1dip" android:layout_weight="1" android:text=" " />
</LinearLayout>
Try it in style
<item name="android:layout_width">0dp</item>
I hope it will work.. thanks..
<style name="TextlessCheckBox" parent="android:Widget.Material.CompoundButton.CheckBox">
<item name="android:button">#null</item>
<item name="android:foreground">?android:listChoiceIndicatorMultiple</item>
<item name="android:foregroundGravity">center</item>
</style>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#ff0000"
android:gravity="center" >
<CheckBox
android:id="#+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#00FF00"
android:gravity="center" >
<CheckBox
android:id="#+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ok, last try for today ;) you should post a picture next time, HOW it should look... well, now you ll have your LinearLayout divided into two similar wide part (red & green), and in every block your checkbox is in the center...did i got it right this time?!
Just add this to your LinearLayout :
android:gravity="center_horizontal"
This line specifies that anything inside this layout will be in the center.
The Full layout code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<CheckBox
android:id="#+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox ONE"
/>
<CheckBox
android:id="#+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox TWO" />
</LinearLayout>
Here is the output i got :

Categories

Resources