How can I get my TextView to the left and the Radiobutton to the right with this code?

I have the following layout xml. I am trying to place the TextView to the left, and the Radiobutton to the right. How can I accomplish this?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TextView
      android:id="@+id/txtVehName"
      android:hint="@string/VEH_NAME"
      android:textSize="18sp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
    </TextView>
    <RadioButton
      android:id="@+id/rbDefault"
      android:text=""
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    </RadioButton>
</LinearLayout>

thanks

patrick


Asked by: Sawyer732 | Posted: 20-01-2022






Answer 1

Forget LinearLayout, use RelativeLayout. This should put the TextBox on the left and the RadioButton on the right like you ask.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TextView
      android:id="@+id/txtVehName"
      android:hint="@string/VEH_NAME"
      android:textSize="18sp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
    </TextView>
    <RadioButton
      android:id="@+id/rbDefault"
      android:text=""
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/txtVehName">
    </RadioButton>
</RelativeLayout>

Eclipsed4utoo is 100% correct, you should be using dp instead of sp because they will render visually the same size across all devices.

Answered by: Kimberly208 | Posted: 21-02-2022



Answer 2

This should place the txtVehName against the left wall, and the rbDefault against the right wall. Is that what you were trying to accomplish?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TextView
      android:id="@+id/txtVehName"
      android:hint="@string/VEH_NAME"
      android:textSize="18sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentBottom="true">
    </TextView>
    <RadioButton
      android:id="@+id/rbDefault"
      android:text=""
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentBottom="true">
    </RadioButton>
</RelativeLayout>

Answered by: Anna802 | Posted: 21-02-2022



Answer 3

Thanks everyone for your answers.

CaseyB - not sure why your solution won't work for me. here is the displayed output from your suggestion:

alt text http://ikonicsoft.com/img/layout_weight.jpg

Here is the xml that seems to work for me.

<RelativeLayout  
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"> 
  <TextView 
      android:id="@+id/txtVehName" 
      android:hint="@string/VEH_NAME" 
      android:textSize="18dp" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_marginTop="10dip"
      > 
    </TextView> 
    <RadioButton 
      android:id="@+id/rbDefault" 
      android:text="" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      > 
    </RadioButton> 
</RelativeLayout>

here is the emulater display output

alt text http://ikonicsoft.com/img/layout_relative.jpg

Eclipsed4utoo - dp instead of sp...thanks

Answered by: Adrian977 | Posted: 21-02-2022



Answer 4

You need to add android:layout_weight="1" to your TextView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TextView
      android:id="@+id/txtVehName"
      android:hint="@string/VEH_NAME"
      android:textSize="18sp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1">
    </TextView>
    <RadioButton
      android:id="@+id/rbDefault"
      android:text=""
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    </RadioButton>
</LinearLayout>

Answered by: Cherry759 | Posted: 21-02-2022



Similar questions

Passing a radiobutton value to the textview in android

I have two activities or "screens". The second activity has an AlertDialogBox with radiobuttons for selecting a single option. I want that after an option has been selected from the second activity's DialogBox, it should be displayed on the first activity screen in a text-view. That is, suppose the user chooses "Green" from the dialogbox in the second activity, then "Green" should be displayed on t...


android - How to change text in the TextView according to the RadioButton selected?

In my application i have three Edittexts and one RadioGroup with two RadioButtons and a TextView I need to do some calculation between the values of the **three EditTexts and display the result in the TextView.... I have put


java - Showing text from string through TextView after pressing RadioButton

I'm trying to show automaticly "This is the correct answer" or "Try again" just right after the radio button is pressed. My question is: How to add two strings &lt;string name="Good_answer"&gt;That is the correct answer&lt;/string&gt; &lt;string name="Wrong_answer"&gt;Try again&lt;/string&gt; to this textView &lt;TextVi...


radio button - Add margin between a RadioButton and its label in Android?

Is it possible to add a little bit of space between a RadioButton and the label while still using Android's built-in components? By default the text looks a little scrunched. &lt;RadioButton android:id="@+id/rb1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="My Text"/&gt; I've tried a couple of things: Specifying margin ...


radio button - android radiobutton disabled

If I create a checkbox view and call setEnabled(false), the checkbox look is grayed but if I create a radiobutton, the view is inactive but it is not grayed. Any idea ?


Passing a radiobutton value to the textview in android

I have two activities or "screens". The second activity has an AlertDialogBox with radiobuttons for selecting a single option. I want that after an option has been selected from the second activity's DialogBox, it should be displayed on the first activity screen in a text-view. That is, suppose the user chooses "Green" from the dialogbox in the second activity, then "Green" should be displayed on t...


radio button - android - RadioButton and RadioGroup

is there any way by which i can save the checked state of the radiobutton whose effect remains even after i have call radiogroupinstance.clearCheck()


android - RadioButton as flat button?

Is there a way to style a RadioButton as a flat button? I have four options: | btn1 | btn2 | btn3 | btn4 | instead of having the little radio circle next to each, I'd like to show each as just a plain flat button. Is that possible without having to draw them myself? Thanks


android - RadioButton in ListView

I want to create a ListView with a RadioButton in every row. I'm using a CursorAdapter and I'm binding it in the right way, but I cannot manage correctly the RadioButton. I want that at any moment only one RadioButton to be checked (so when I check one, I would like the one that was checked before to uncheck). Furthermore, when I put the RadioButton on the row, I cannot manage anymore the setOnItemClickListener. Do you kno...


android - Populate radiobutton group with data from squlite database

I am writing a game app for Android, where a question word with four different alternatives will show that the user can choose between. I have a database and i can insert from the app, but what would be the easiest way to use a select statement, and then parse the response and populate the five different positions in the radio group? I have done the Notepad tutorial on the dev. site, and it seems a bit too ...


android - Is there a way to get the text from a checked radiobutton from a radiogroup?

I have a static radiogroup in my XML layout, but I have created all of the radiobuttons dynamically as an effect of user input. Since none of these have ID's, is there a way I can call a method on a whole radiogroup to just return the text (or label) of the checked radio button? All the labels are URL's so if I can get the URL of the checked radio button then I can pass it to a method i created to open the web browser. ...


android - Unexpected RadioButton behavior

Here is my layout: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFFFF"&gt; &lt;RadioGroup android:id="@+id/choices_group" android:layout_width="fill_parent" ...


android - How to make UI components disappear when a certain RadioButton is selected

I have created a layout xml file where I have two RadioButtons. By default, RadioButton 1 is selected and I am displaying a DatePicker component on the screen but when the user selects RadioButton2 the DatePicker should disappear from the screen. How can I handle the scenario? Should I make the change in layout / Java code?






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top