Make the radio button icon to center - android

I have 3 RadioButton like below
The . text is getting the centre, not the icon. I need both the icon and text to center
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content" >
<RadioButton
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:layout_gravity="center"
android:layout_weight="1"
android:text="Test2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:layout_gravity="center"
android:layout_weight="1"
android:text="Test3"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>

You can try below code
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:text="Test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:text="Test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:text="Test3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RadioGroup>

Related

How Can I edit my buttons

I tried to use a LinearLayout with 4 buttons but the last one was cut. How can I edit my code to get button's width corresponding to all smartphones'resolutions
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GREEN" />
</LinearLayout>
Thanks
make width="0dp" in all buttons and add
android:layout_weight="1"
to all buttons
First,set android:weightSum="4" in the LinearLayout.
And set android:layout_width="Odp" and android:layout_weight="1" in your Button .
<?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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
</LinearLayout>
Use weightSum to get this equally divided layout by adjusting layout_weight
Try this once:
<?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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width=match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
</LinearLayout>

Android Layout for calendar control

I am looking to build a layout as above using linear/relative layouts with textbox.
Any help is appreciated.
Add background style to root layout for rounded corner and done
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#999999"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#FFFFFF"
android:text="25"
android:padding="5dp"
android:textColor="#000000"
android:textSize="60sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Sept \n2016"
android:padding="5dp"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#999999"
android:layout_margin="20dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#999999"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#FFFFFF"
android:padding="10dp"
android:text="25"
android:textColor="#000000"
android:textSize="60sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Sept \n2016"
android:textColor="#android:color/white"
android:padding="10dp"
android:textStyle="bold"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>

How to adjust space between radio button in android

In my android application using radio buttons to select screen view. I want radio button equally aligned horizontal. I tried layout xml shown below.
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Views"
android:paddingLeft="10dp"
android:textSize="20sp"
/>
<RadioGroup
android:id="#+id/radioView"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:measureWithLargestChild="true"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioViewSingle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/single"
android:layout_weight="1"
android:checked="true" />
<RadioButton
android:id="#+id/radioView2by2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view2x2" />
<RadioButton
android:id="#+id/radioView3by3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view3x3" />
<RadioButton
android:id="#+id/radioView4by4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view4x4" />
</RadioGroup>
</LinearLayout>
output obtained using above xml code
But i expecting below output
Thanks in advance
Instead of using android:drawableRight, use android:drawableLeft.
I just tested this and it is working as expected
Remove android:layout_weight="1" for all RadioButtons and set android:layout_width="0dp" to android:layout_width="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Views"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/radioView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="true"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioViewSingle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:drawableRight="#android:drawable/ic_menu_search" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView2by2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:drawableRight="#android:drawable/ic_menu_camera" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView3by3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#android:drawable/ic_menu_always_landscape_portrait" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView4by4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:drawableRight="#android:drawable/ic_menu_call" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
You can put each RadioButton to one layout and give all layout_width="0dp" and give layout_weight="1" as showen below:
<RadioGroup
android:id="#+id/radioView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:measureWithLargestChild="true"
android:orientation="horizontal">
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:checked="true"
android:drawableRight="#drawable/single"
android:id="#+id/radioViewSingle"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view2x2"
android:id="#+id/radioView2by2"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view3x3"
android:id="#+id/radioView3by3"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view4x4"
android:id="#+id/radioView4by4"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
</RadioGroup>

Align multiline text in Radio group android

I am working on a quiz application, in this application i have to show 4 radio buttons with some multi-line text just like in snapshot. For this i have used radio-group, but the problem is that radio buttons are not aligned with the text properly as shown in snapshot. It becomes hosh-pash. Please somebody provide some suggestions. Thanks in advance
here is my layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/txtQ"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/screen"
android:orientation="vertical"
tools:context=".MainActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textQno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:id="#+id/textRem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:gravity="right"
android:text="1" />
</LinearLayout>
<TextView
android:id="#+id/textQuis"
android:layout_width="fill_parent"
android:layout_height="144dp"
android:text="TextView" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbQ1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/rbQ2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/rbQ3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/rbQ4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="fill" >
<Button
android:id="#+id/bMainPre"
style="#style/ButtonText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_blue"
android:text="#string/bPrev" />
<Button
android:id="#+id/bMainNext"
style="#style/ButtonText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_blue"
android:text="#string/bNext" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill" >
<Button
android:id="#+id/bshowAnswer"
style="#style/ButtonText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_blue"
android:text="#string/bShow" />
<Button
android:id="#+id/bSubAns"
style="#style/ButtonText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_blue"
android:text="#string/bsumit" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Do this to add aligned text to a radiobutton:
<RadioButton
android:id="#+id/rbQ1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="YOUR TEXT HERE"
android:gravity="center"
android:layout_gravity="center"
android:layout_margin="5dip" />

button not displaying right side of the layout in android

Hi in this Layout i have two button's named as back and home same names i mentioned as id's for both.Now left side i want to display back button and right side corner i want to display home.But home button not displaying at the right corner.Now if i mention margin left it's not showing properly.Can any one please help me
xml
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#FF0000"
android:orientation="horizontal">
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"
/>
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
/>
<Button
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/home"
android:layout_marginLeft="60dp"
/>
</LinearLayout>
</LinearLayout>
Try this,
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#FF0000"
android:orientation="horizontal"
android:weightSum="100" >
<Button
android:id="#+id/back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="20"
android:background="#drawable/back" />
<TextView
android:id="#+id/tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="60"
android:gravity="center"
android:text="Hello"
android:textStyle="bold" />
<Button
android:id="#+id/home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="60dp"
android:layout_weight="20"
android:background="#drawable/home" />
</LinearLayout>
</LinearLayout>
Try below code xml file:
<?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="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#FF0000"
android:orientation="horizontal">
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#drawable/back"
/>
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textStyle="bold"
/>
<Button
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/home"
android:layout_alignParentRight="true"
android:layout_marginLeft="60dp"
/>
</RelativeLayout>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#FF0000"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"/>
</LinearLayout>
<Button
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/home"/>
</LinearLayout>
why don't you use relative layout for that, see that
<?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="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#FF0000"
android:orientation="horizontal" >
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/account_settings" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold" />
<Button
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/account_settings"
android:gravity="center_horizontal" />
</RelativeLayout>
</LinearLayout>
if you want to do this with linear layout use weight sum
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#FF0000"
android:weightSum="3"
android:orientation="horizontal" >
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/account_settings" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textStyle="bold" />
<Button
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_weight="1"
android:background="#drawable/account_settings" />
</LinearLayout>
</LinearLayout>

Categories

Resources