RelativeLayout Stack Buttons - android

I would like to align two buttons on the bottom of a relative layout that is wrapped inside of a linear layout.
I am unable to get the view favorites button to be on top of the search button. There is a big space as you can see in this image:
Here is my code:
<?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">
<TextView
android:text="#string/SearchRestaurantsCity"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tvRestaurantSearchCity" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/etRestaurantSearchCity" />
<TextView
android:text="#string/SearchRestaurantsState"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tvRestaurantSearchState" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/etRestaurantSearchState" />
<TextView
android:text="#string/SearchRestaurantsCategories"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tvRestaurantSearchCategories" />
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spRestaurantSearchCategories" />
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:text="#string/btnViewFavoriteRestaurants"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/btnViewFavoriteRestaurants"
style="#style/ButtonText"
android:padding="5dp"
android:layout_below="btnSearchRestaurants" />
<Button
android:text="#string/btnSearchRestaurants"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/btnSearchRestaurants"
style="#style/ButtonText"
android:layout_alignParentBottom="true"
android:padding="5dp" />
</RelativeLayout>

There are 2 things wrong with the XML.
For starters, you can't refer to the button btnSearchRestaurants before it's defined.
Secondly, you've defined btnSearchRestaurants to align-parent-bottom, and then tried to define that btnViewFavoriteRestaurants would be below that button. It should be above it (as per your description).

If you're trying to put the first button I can see inside RelativeLayout, I'd try to change the following line:
android:layout_below="btnSearchRestaurants" />
for this one:
android:layout_above="#+id/btnSearchRestaurants" />
Part of the space is reserved for all the elements of the layout I can see inside the LinearLayout,and the + is because the button btnSearchRestaurants is below so it is not created yet

I think the problem is at
android:layout_below="btnSearchRestaurants"
It should be
android:layout_below="#id/btnSearchRestaurants"

In your xml code I think you should correct this line
android:layout_below="btnSearchRestaurants"
as you want it to be above the Search Restaurants button it should be
android:layout_above="#id/btnSearchRestaurants"
I changed below to above for obvious reasons (you want it above the other button not below it) and added #id/ because you will be searching for the button id.

Related

TextView and EditText overflow

I've got a need to display and TextView (label), EditText(Input) and TextView(suffix) in a line.
The issue I have is - if the label TextView is too long it The EditText and Suffix TextView and not drawn.
Here's my code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="0"
android:padding="5dp"
android:text="Label"
android:id="#+id/label_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_weight="1"
android:id="#+id/input_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#color/dark_grey"
android:textColor="#color/black"
android:hint="text.." />
<TextView
android:padding="5dp"
android:layout_weight="0"
android:text="Suffix.."
android:background="#color/lite_grey"
android:id="#+id/suffix_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
So what I want is - even if the label is over 2-3 lines, for an EditText to appear followed by another TextView.
Any pointers would be appreciated.
Thanks!
Edit:
I've added some 'diagrams' so you can understand what I want to do a bit better.
The label to the EditText can be arbitrary length(2/3 lines), so whenever the label finishes I want an EditText to start (which is followed by another TextView showing a suffix..)
..
![This shows a 2 line label and TextView..]
Edit 2: Thanks for the responses guys!! I think #questioner has really understood what I want to do i.e. I want your 3 views to move to other lines so that they behave like they were one view - and as he has suggested i'll have to find a library for this!
How about this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="5dp"
android:text="Label"
android:id="#+id/label_text_view"
android:layout_width="100dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"/>
<EditText
android:layout_weight="1"
android:id="#+id/input_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColorHint="#color/dark_grey"
android:textColor="#color/black"
android:hint="text.."/>
<TextView
android:padding="5dp"
android:text="Suffix.."
android:layout_gravity="center_vertical"
android:background="#color/lite_grey"
android:id="#+id/suffix_text_view"
android:layout_width="100dp"
android:layout_height="wrap_content"/>
</LinearLayout>
If you want your 3 views to move to other lines so that they behave like they were one view - you should use external library for that.
Why not use a RelativeLayout instead of LinearLayout and then use android:layout_below="#+id/..." or android:layout_above="#+id/..." also android:layout_toRightOf="#+id/..." or android:layout_toLeftOf="#+id/..."
Set
android:layout_weight="1"
for each view so that they all occupy the same amount of horizontal space.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
Try this or please expand a little more.

TextView overlap in ListView implementation android with 2 textviews

I am implementing listview with 2 textviews , and for reference used Basic Layout and Formatting
Instead of stock names which are short i am using long text, so when i use a long text, it overlaps with the second .
So how to get it right, so that the 2 textviews don't overlap each other.
You can used following code to solve your problem:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:id="#+id/ticker_symbol"
android:text="sdasd sadas"
android:layout_weight=".5"/>
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:id="#+id/ticker_price"
android:text="dfsd"
android:layout_weight=".5"
android:gravity="right" />
</LinearLayout>
Output:
To avoid overlapping, you could set the second textview to align to right (android:layout_toRightOf).
For example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/ticker_symbol"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/ticker_price"
android:layout_toRightOf="#id/ticker_symbol"
android:layout_alignParentRight="true" />
</RelativeLayout>
I guess you mean due to long length of text it goes to next line or on next TextView
So i suggest you to use textView property android:ellipsized="end" and also use android:single_line="true" instead this you can directly specify the maxline=1
I'm not sure with names spelled correctly so check while typing
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/ticker_symbol"
android:ellipsezed="end"
android:singe_line="true"
android:layout_alignParentLeft="true" />
Hope this explanation woks for you let me know..

How to add margin between radiobutton ant it's drawable?

This my current layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#F5F5DC"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton android:id="#+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/flag_dk"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:drawablePadding="10dip"
android:text="Danish" />
<RadioButton android:id="#+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/flag_en"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dip"
android:drawablePadding="10dip"
android:textColor="#000000"
android:text="English" />
</RadioGroup>
</LinearLayout>
</ScrollView>
I get this:
I would like to have some space (margin) between radiobutton an the flag (it's drawable). Is it even possible?
You can do like the following:
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="match_parent"
android:layout_height="#dimen/view_size"
android:button="#null"
android:drawablePadding="100dp"
android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
android:text="label" />
In my case, where I have a custom drawable shape, adding android:paddingDrwable attribure did not work but adding android:paddingLeft="5dp" did.
Where you are setting the image and the text give blank spaces before it.
for example: radiobutton.setText(" "+Text);.You will see the desired output.
I don't think it is possible to add space between the image and the radio button using the android apis. However, the answer to your question I think is to add some space in the image itself.
Use Relative Layout instead of Linear Layout it gives android:layout_margingleft & android:layout_margingRight to drop your radio buton & Images as well.
Also give android:padding="10dip"
Try it it will definitely work!!!!

How to set different Layouts in 1 XML file in Android

Hi friends i have set my layout thru TabLayout within LinearLayout but still my button in the end couldn't be set properly as TabLayout divides all the fields in number of columns so can anyone help how to set 2 Layouts in one XML file
means all the fields in TabLayout and remaining both Login and Register buttons in Linear Layout ore some other so that they can set properly.
Thanks in advance.
Layout
Username ! EditTextBox
Password ! EditTextBox
Login ! Register
Button ! Button
I want my Layout in above Format so i have used TabLaout but in that case it is causing my button's view as well as it is stretching one button than other one as EditText is larger than TextView
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="26sp"
android:text="#string/login_text"/>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:layout_width="120px"
android:text="User name"
/>
<EditText
android:id="#+id/txtUserName"
android:singleLine="true"
android:maxLength="20"
android:width="195px"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="120px"
android:text="Password"
/>
<EditText
android:id="#+id/txtPassword"
android:width="195px"
android:maxLength="20"
android:singleLine="true"
android:password="true"
/>
</TableRow>
<TableRow>
<TextView />
<CheckBox android:id="#+id/chkRememberPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Remember Password"
/>
</TableRow>
<TableRow>
<Button
android:id="#+id/buttonRegister"
android:text="Register"
android:layout_width="124px"
android:layout_height="wrap_content"
android:layout_below="#+id/chkRememberPassword"
android:layout_alignRight="#+id/chkRememberPassword"
/>
<Button
android:id="#+id/buttonSignIn"
android:text="Log In"
android:layout_width="124px"
android:layout_height="wrap_content"
android:layout_below="#+id/chkRememberPassword"
android:layout_alignLeft="#+id/chkRememberPassword"
/>
</TableRow>
<TextView
android:id="#+id/statusError"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableLayout>
</LinearLayout>
I suggest you to use wrap_content in TableLayout & then you can have your buttons in LinearLayout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
Its not possible to set more than one layout in a single xml file... And for the same reason Android has layout, layout-landscape, etc. type of folder system..
What you can do is as explained by 100rabh... Make the layout universal by applying relative layout and fill parent settings...
Otherwise as i explained you need to make 2 xml layout files and place the layout for landscape in the layout-landscape folder thereby the respective layout will automatically be set according to the orientation of the phone...
Hope this helps u...
You need to create 2 XML files:
One in the normal layout folder
A second in a res/layout-land to define the layout for the landscape orientation. You'll need to create the res/layout-land folder if it does not already exist.

How to create column-like layout for ListView rows?

I have a relative layout which looks like this:
Here is the code:
<?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="fill_parent"
>
<TextView
android:id="#+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:text="Symbol"
/>
<TextView
android:id="#+id/priceText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_toRightOf="#+id/nameText"
android:gravity="right"
android:text="100"
/>
<TextView
android:id="#+id/changeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_toRightOf="#+id/priceText"
android:gravity="right"
android:text="1.3"
/>
</RelativeLayout>
How can I get the company name to line up next to the number 1.3?
Following up on Mayra's answer, here's one way to do it using layout_weight:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:id="#+id/left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:padding="5dip"
android:layout_weight="0"
android:text="Code"
/>
<TextView
android:id="#+id/middle_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:padding="5dip"
android:layout_weight="1"
android:text="Name of Company"
/>
<TextView
android:id="#+id/right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:gravity="right|center_vertical"
android:layout_weight="0"
android:text="1.3"
/>
</TableRow>
</TableLayout>
It's not clear exactly what you want to have happen, but here are a few options:
If you want columns to be aligned across rows, you might consider using a TableLayout.
You can also use the "weight" atribute to affect what percentage of the screen each TextView takes up. If you assign a value of 1 on the left-most text view, and 0 on the other 2, this will cause the left text view to take up all the extra space, and thus push the middle text view to the right. This will probably cause the middle text view to look right aligned though. You could instead give the left one 20%, the middle one 50% and the right one 30% by assign 2, 5 and 3.
You could also just set an explicit size for the text views (in dpi), but this might be problamatic with different sized screens.
I agree with Mayra! Use the TableView, you'll get the formatting your looking for as well as keep almost all functionality of a ListView (scolling, and list oriented functionality)

Categories

Resources