Nesting Relative layout inside Linear Layout - android

I have the following code in my layout.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent">
<EditText android:hint="#string/feed_url"
android:id="#+id/feedUrl" android:textSize="14dp" android:inputType="textUri"
android:layout_marginRight="45dp" android:layout_height="wrap_content" android:layout_width="fill_parent">
</EditText>
<Button android:id="#+id/getGraph"
android:text="#string/get"
android:layout_toRightOf="#id/feedUrl"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" android:width="45dp" android:layout_width="wrap_content">
</Button>
</RelativeLayout>
<WebView android:id="#+id/wv1" android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
In the eclipse Plugin layout creator, the EditText and button are shown properly. (see the below screenshot)
But on the device and emulator, the button is not hidden. (see the below screenshot)
Any idea why the button is getting hidden in the device?

Try the following changes to your code. You have to define the button first, since it is of fixed width, and then place the EditText to fill the rest of the space, placed to the left of the button. The button also has to be defined first (prior to Android 2.2, that is).
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<Button
android:id="#+id/getGraph"
android:text="#string/get"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<EditText
android:hint="#string/feed_url"
android:id="#+id/feedUrl"
android:textSize="14dp"
android:inputType="textUri"
android:layout_marginRight="45dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toLeftOf="#id/getGraph"
/>
</RelativeLayout>

I cannot see the button in the layout builder... the reason, I think is that the EditText width is fill parent so it fills parent pushing the button out. If you must use RelativeLayout, you need to define width of the EditText explicitly. Otherwise you may want to switch to horizontal LinearLayout and set weight of the edit box to "1".
Also, there are invalid options for RelativeLayout:
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal

You can also use LinearLayout instead of RelativeLayout to archive this. You can use android:layout_weight to make Button not be overlaid by EditText. Following is the sample xml that may work:
<LinearLayout android:orientation="horizontal"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<EditText android:hint="#string/feed_url"
android:id="#+id/feedUrl" android:textSize="14dp"
android:inputType="textUri" android:layout_marginRight="45dp"
android:layout_height="wrap_content" android:layout_width="fill_parent" />
<Button android:id="#+id/getGraph" android:text="#string/get"
android:layout_height="wrap_content" android:width="45dp"
android:layout_width="wrap_content"
android:layout_weight="1" />
</LinearLayout>

Related

First time programmer confused as to why the EditText and Button fields aren't going where I want them to

I am coding in XML on android studios, and I am having trouble getting the EditText to be centered at the top and the Button to be centered in the center. I do not know a lot about code so the other things I've read about Relative vs Linear Layout have been very confusing. I've never coded prior to this, so I'm very sorry if most of it is wrong.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/touch" >
<EditText android:id="#+id/edit_message"
android:layout_width="225dp"
android:layout_height="225dp"
android:layout_gravity="center"
android:hint="#string/edit_message"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:textSize="62sp"
android:text="#string/begin_app"/>
</LinearLayout>
Use the below code in your layout:
android:layout_alignParentTop will align the view on Top
android:layout_alignParentTop will align the view on center of parent
android:layout_centerHorizontal="true" and android:layout_centerVertical="true" will align the view in center of the parent
Layout code with RelativeLayout
<?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"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
In android studio just go to the layout xml file and click Design, you can drag and drop the widgets wherever you want and the system will generate the code for you with the placement you desired. Its great for starters.

Android ExpandableListAdapter View layout not displaying as desired :-(

I have an XML layout for a view representing one item as displayed by a ExpandableListAdapter. (The XML that is duplicated once for each list item, not the main activity layout). I initially tried a LinearLayout but this hid the final of the three widgets (a image button). I understand why that doesn't work but I then tried a RelativeLayout but the text view does not show.
I'll display the relative layout first along with a screenshot and also append my initial layout and screenshot at the bottom FWIW. Thanks for any help.
<?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="55dip"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/chkParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:checked="false"/>
<EditText
android:id="#+id/edtParent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/chkParent"
android:textSize="17dip"
android:inputType="text"
android:hint="#string/strItem"/>
<ImageButton
android:id="#+id/btnExpand"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_toLeftOf="#id/edtParent"
android:src="#drawable/dark_expand"
android:clickable="true"
android:hint="#string/strViewSubItems"
android:contentDescription="#string/strViewSubItems"
android:background="#null"/>
</RelativeLayout>
Original linear layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/chkParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
<EditText
android:id="#+id/edtParent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:inputType="text"
android:hint="#string/strItem"/>
<ImageButton
android:id="#+id/btnExpand"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/dark_expand"
android:clickable="true"
android:hint="#string/strViewSubItems"
android:contentDescription="#string/strViewSubItems"
android:background="#null"/>
</LinearLayout>
Pretty sure your toLeftOf attributes should be toRightOf attributes. Give that a shot and see what happens.
Actually, your EditText should be set toRightOf="#id/chkParent" and toLeftOf="#id/btnExpand. Your ImageButton doesn't even need the toRightOf modifier, the alignParentRight attribute should cover it.

Make a component fill all available space on screen

I want something like this:
But I don't know how to resize the TextView so it gets all the available space on screen which is not ocuppied by the EditText or the Buttons. May I do it in the code, or in the xml?
At XML I tried putting the TextView into a FrameLayout, but it makes no difference. Currently looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/consola"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars = "vertical"
android:text="#string/hello"/>
</FrameLayout>
<EditText
android:id="#+id/comando"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:text="Conectar"
android:id="#+id/boton_conectar"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Enviar"
android:id="#+id/boton_enviar"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
At the code I'm just checking if the Buttons are pushed with Listeners. One of them, when pushed, gets the text at the EditText, and appends it to the TextView. It works, and TextView gets higher, while EditText and Buttons downs one line. If I go on appending lines, finaly EditText and Buttons get out of the screen. I want to avoid this behaviour, and accomplish to get this 3 widgets sticked to the bottom of the screen.
Use the android:layout_weight=1 attribute, like the buttons on the bottom of the form. That will assign most of the space to it and anything that's left to the rest of the elements.
It's all about the weight. This should give you what you want:
<?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">
<TextView android:text="TextView"
android:id="#+id/textView1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</TextView>
<EditText android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:text="Button"
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
</Button>
<Button android:text="Button"
android:id="#+id/button2"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1">
</Button>
</LinearLayout>
</LinearLayout>
(Side note: When using weight, setting the corresponding height/width to 0dp sometimes gets around some weird behavior.)
Try android:fillViewport="true".

Android: EditText not filling ScrollView and RelativeLayout

I have been trying to wrap my editText with ScrollView to auto scroll it when the content is updated. I found the problem that the EditText wont cover the entire scroll view even with both width and height are set to fill_parent. Please enlighten me. Thank you.
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="send"
/>
<EditText
android:id="#+id/msgBox"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/sendButton"
android:gravity="left"
android:longClickable="false"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/msgBox">
<EditText
android:id="#+id/chatBox"
android:editable="false"
android:gravity="left|top"
android:cursorVisible="false"
android:longClickable="false"
android:clickable="false"
android:autoLink="all"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</ScrollView>
</RelativeLayout>
Try adding android:fillViewport="true" to ScrollView properties. That should make it adjust its size every time something inside it changes. Also, use android:layout_height=wrap_content for EditText. It doesn't make sense to fill a parent that itself can re-size

Placing two view in one row using RelativeLayout

I want the EditText to fill all the available space to the right side until the button placed on the right of it. I'm using RelativeLayout to construct such a view. But in result I don't have my button visible, the only EditText. Please advice how to make both of them visible.
<?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="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Task description">
</EditText>
<Button
android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_toRightOf="#id/edit_text">
</Button>
</RelativeLayout>
</LinearLayout>
You're putting your button first. You have to place the item you want fixed before the others.
<Button
android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_alignParentRight="true"
/>
<EditText
android:id="#+id/edit_text"
android:layout="align_parent_left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/ok_button"
android:hint="Task description"
/>
The way you have it, your edittext knows nothing about the button that follows it, so why should it not take up the entire width of the parent just like you told it to?

Categories

Resources