Spreading text to match the parent width - java

Please have a look at the following code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/textView1"
android:src="#drawable/add_2" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</RelativeLayout>
This generates the following output
To be honest, this is not what I want. The text should "spread" to match the screen width, until it find the image view. I think improving the space between letters might be the solution.
But I would like to know whether there is any better solution, which we can do in Code or XML.

Hey i did some changes in your code. I use LinerLayout instead of Relative Layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3.0f"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:layout_marginLeft="21dp"
/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>

If I get you right, you want the EditText below "Sales Inquiry".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/add_2"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>
Note I replaced the ImageView with drawableRight. It is called compoundDrawable and it could be set to any location near the text of TextView.
There is no need to use RelativeLayout here. Vertical LinearLayout should do the trick.
layout_width="match_parent"
Tells that the View should be spread across the width of the layout.
But if you want EditText in the same line as "Sales Inquiry" then it's done line this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:drawableRight="#drawable/add_2"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>
Use horizontal LinearLayout, set layout_width of first TextView to wrap_content.
The EditText should use layout_weight="1" (only works in LinearLayout) that means it will take as much space available. When using layout_weight in horizontal LinearLayout, set the layout_width to 0dp. If it's vertical LinearLayout, layout_height should be 0dp.

Related

align reyclerview elements android

I have a recyclerview with a TextView and an EditText building the rows with the TextView on the left and the EditText right of it.
Currently view looks as follows:
Aaa: value1
Abbsbc: value2
Cskhkdhd: value3
HJhk: value4
I would like the view to look this way:
Aaa: value1
Abbsbc: value2
Cskhkdhd: value3
HJhk: value4
the layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/brand_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/brand_selector">
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider"/>
<TextView
android:id="#+id/object"
android:layout_width="wrap_content"
android:layout_height="#dimen/edit_box_height"
android:layout_below="#id/divider"
android:gravity="center_vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/noData"/>
<EditText
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="#dimen/edit_box_height"
android:layout_below="#id/divider"
android:layout_toRightOf="#id/object"
android:background="#android:color/transparent"
android:hint="Enter value"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black"
android:textColorHint="#color/divider"/>
</RelativeLayout>
Add this line in Textview:
android:textAlignment="viewEnd"
android:gravity="center_vertical|end"
Also I think this will still lead to uneven alignment if the length of textview text varies in rows, So its better if you use Linear Layout as parent layout. I have redone the xml accordingly:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/brand_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/brand_selector">
<TextView
android:id="#+id/object"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="#dimen/edit_box_height"
android:textAlignment="viewEnd"
android:gravity="center_vertical|end"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/noData"/>
<EditText
android:id="#+id/value"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="#dimen/edit_box_height"
android:layout_below="#id/divider"
android:layout_toRightOf="#id/object"
android:background="#android:color/transparent"
android:hint="Enter value"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black"
android:textColorHint="#color/divider"/>
</LinearLayout>
Try LinearLayout with weight:
<RelativeLayout
android:id="#+id/brand_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/brand_selector">
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/divider"
android:orientation="horizontal">
<TextView
android:id="#+id/object"
android:layout_width="0dp"
android:layout_height="#dimen/edit_box_height"
android:layout_weight="0.5"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/noData"
android:gravity="end"/>
<EditText
android:id="#+id/value"
android:layout_width="0dp"
android:layout_height="#dimen/edit_box_height"
android:layout_weight="0.5"
android:background="#android:color/transparent"
android:hint="Enter value"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black"
android:textColorHint="#color/divider"
android:paddingStart="10dp"/>
</LinearLayout>
</RelativeLayout>
EDIT: I added a gravity END to align on the right on the textView and a Startpadding on the EditText to leave space between label and value

Android : buttons not visible in scrollview

I am making an android app to enter user information, for that I have two button and number picker, my button are customize and are not showing in either preview in android studio neither in emulator
Here is my code
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app.tarun.dc2.Fragments.AddressFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
>
<!--Layout for Buttons-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<com.app.tarun.dc2.CustomViews.SquareLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="25dp"
android:layout_weight="1">
<ImageButton
android:src="#drawable/ic_add"
android:id="#+id/medicineEditAddButton"
android:scaleType="fitCenter"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/green_button_style"/>
</com.app.tarun.dc2.CustomViews.SquareLayout>
<NumberPicker
android:layout_width="0dp"
android:id="#+id/medicineEditNumberPicker"
android:layout_height="wrap_content"
android:layout_weight="1">
</NumberPicker>
<com.app.tarun.dc2.CustomViews.SquareLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="25dp"
android:layout_weight="1">
<ImageButton
android:src="#drawable/ic_continue"
android:id="#+id/medicineEditContinueButton"
android:scaleType="fitCenter"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/yellow_button_style"/>
</com.app.tarun.dc2.CustomViews.SquareLayout>
</LinearLayout>
<!--Horizontal Linear Layout for EDITTEXT-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="First Name"
android:inputType="text"
android:layout_marginTop="25dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Last Name"
android:inputType="text"
android:layout_marginTop="25dp"/>
</LinearLayout>
<!--Horizontal Linear Layout for EDITTEXT end here(First name and last name-->
</LinearLayout></ScrollView>
Change your custom layout's width from 0dp to wrap content/match_parent depending on your need. I think that may solve your problem. I have your edited layout in below.
<com.app.tarun.dc2.CustomViews.SquareLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="25dp"
android:layout_weight="1">
<ImageButton
android:src="#drawable/ic_add"
android:id="#+id/medicineEditAddButton"
android:scaleType="fitCenter"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/green_button_style"/>
</com.app.tarun.dc2.CustomViews.SquareLayout>
<NumberPicker
android:layout_width="wrap_content"
android:id="#+id/medicineEditNumberPicker"
android:layout_height="wrap_content"
android:layout_weight="1">
</NumberPicker>
<com.app.tarun.dc2.CustomViews.SquareLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="25dp"
android:layout_weight="1">
<ImageButton
android:src="#drawable/ic_continue"
android:id="#+id/medicineEditContinueButton"
android:scaleType="fitCenter"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/yellow_button_style"/>
</com.app.tarun.dc2.CustomViews.SquareLayout>
Try including your scroll view in a layout. Here is how i have achieved it for a project of mine:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.com.atr.LoginActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/txthello"></TextView>
<EditText
android:id="#+id/txtApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txthello"
android:layout_marginTop="18dp"
android:hint="Enter Application Code"/>
<EditText
android:id="#+id/txtModuleId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtApp"
android:layout_marginTop="18dp"
android:hint="Enter Module Id"/>
<EditText
android:id="#+id/txtModuleCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtModuleId"
android:layout_marginTop="18dp"
android:hint="Enter Module Code"/>
<EditText
android:id="#+id/txtModuleType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtModuleCode"
android:layout_marginTop="18dp"
android:hint="Enter Module Type"/>
<EditText
android:id="#+id/txtSubModuleId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtModuleType"
android:layout_marginTop="18dp"
android:hint="Enter Sub Module Id"/>
<EditText
android:id="#+id/txtSubModuleCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtSubModuleId"
android:layout_marginTop="18dp"
android:hint="Enter Sub Module Code"/>
<EditText
android:id="#+id/txtUserId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtSubModuleCode"
android:layout_marginTop="18dp"
android:hint="Enter User Id"/>
<EditText
android:id="#+id/txtDateFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtUserId"
android:layout_marginTop="18dp"
android:hint="From Date (mmm-dd-yyyy)"/>
<EditText
android:id="#+id/txtDateTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtDateFrom"
android:layout_marginTop="18dp"
android:hint="To Date (mmm-dd-yyyy)"/>
<Button
android:id="#+id/btnCallService"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_below="#+id/txtDateTo"
android:layout_toRightOf="#+id/txtDateTo"
android:layout_marginTop="18dp"
android:layout_alignParentLeft="true"
android:text="Send"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Just to give you a basic idea :)
Add a android:weightSum="[any int]" on your first LinearLayout
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects.
So here you have to take LinearLayout or RelativeLayout and then you have to put different hierarchy of component.
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app.tarun.dc2.Fragments.AddressFragment">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
>
<!--Layout for Buttons-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<com.app.tarun.dc2.CustomViews.SquareLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="25dp"
android:layout_weight="1">
<ImageButton
android:src="#drawable/ic_add"
android:id="#+id/medicineEditAddButton"
android:scaleType="fitCenter"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/green_button_style"/>
</com.app.tarun.dc2.CustomViews.SquareLayout>
<NumberPicker
android:layout_width="0dp"
android:id="#+id/medicineEditNumberPicker"
android:layout_height="wrap_content"
android:layout_weight="1">
</NumberPicker>
<com.app.tarun.dc2.CustomViews.SquareLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="25dp"
android:layout_weight="1">
<ImageButton
android:src="#drawable/ic_continue"
android:id="#+id/medicineEditContinueButton"
android:scaleType="fitCenter"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/yellow_button_style"/>
</com.app.tarun.dc2.CustomViews.SquareLayout>
</LinearLayout>
<!--Horizontal Linear Layout for EDITTEXT-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="First Name"
android:inputType="text"
android:layout_marginTop="25dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Last Name"
android:inputType="text"
android:layout_marginTop="25dp"/>
</LinearLayout>
<!--Horizontal Linear Layout for EDITTEXT end here(First name and last name-->
</LinearLayout>
</LinearLayout></ScrollView>
Hope so this code is work for you.
And you can also visit this ScrollView

Android - Center View in Other View inside Layout

Hi I am having trouble centering my text view in an image view. I want it to look kinda like this but have it actually vertically centered and stay vertically centered if the the font size changes.
And here is what I have in XML I don't care if I need to change the relative layout to another kind that doesn't matter to me I just want to be able to center this. Heres the xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.spencer.app.ProfileActivity"
android:id="#+id/layout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="140dp"
android:minWidth="140dp"
android:id="#+id/logoBackground"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="25dp"
android:contentDescription="#string/content_description_useless"
android:background="#ffffd0cd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/capital_u"
android:id="#+id/u_textView"
android:textSize="80sp"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_alignTop="#+id/logoBackground"
android:layout_alignLeft="#+id/logoBackground"
android:layout_alignStart="#+id/logoBackground" />
</RelativeLayout>
Thanks for the help in advance.
try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.spencer.app.ProfileActivity" >
<ImageView
android:id="#+id/logoBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="25dp"
android:background="#ffffd0cd"
android:contentDescription="#string/content_description_useless"
android:minHeight="140dp"
android:minWidth="140dp" />
<TextView
android:id="#+id/u_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/logoBackground"
android:layout_alignLeft="#+id/logoBackground"
android:layout_alignRight="#+id/logoBackground"
android:layout_alignStart="#+id/logoBackground"
android:layout_alignTop="#+id/logoBackground"
android:text="#string/capital_u"
android:textSize="80sp"
android:gravity="center" />
</RelativeLayout>
Your issue is that you're not centering a view INSIDE of something else, you're just trying to draw on top.
One possible solution would be to set the width and height of the TextView to be the same as the dimensions of the ImageView. Then, add this to your TextView's XML:
android:gravity="center"
But overall, this is NOT the right way to do this sort of stuff. You'd be much better off just changing the background of your TextView with something like this:
android:background="#ffffd0cd"
You can also change the background to be a drawable:
android:background="#drawable/my_logo_background_image"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
tools:context="com.spencer.app.ProfileActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="140dp"
android:minWidth="140dp"
android:id="#+id/logoBackground"
android:layout_marginStart="25dp"
android:background="#ffffd0cd"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="U"
android:id="#+id/u_textView"
android:layout_marginTop="30dp"
android:layout_marginLeft="45dp"
android:textSize="80sp"
android:layout_alignTop="#+id/logoBackground"
android:layout_alignLeft="#+id/logoBackground"
android:layout_alignStart="#+id/logoBackground" />
I didnot changed much but Its work. Replace your code with this it will work for you. :)

Centering my linear layout in android

I'm trying to achieve this positioning:
Where I have two rows of EditText and TextView next to each other.
The version textview is anchored to the bottom of the screen, but takes up the entire width and the text is centered.
My current layout file is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:orientation="vertical"
android:gravity="center_vertical"
tools:context="com.example.mp.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edittext1"
android:layout_width="250dip"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEW"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edittext2"
android:layout_width="250dip"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEW"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:gravity="bottom">
<TextView
android:id="#+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Version 1.0"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</RelativeLayout>
</LinearLayout>
But the two rows of EditText and TextView are positioned past the middle of the screen and the Version textview is immediately below the two rows of EditText and TextView instead of anchored at the bottom.
I would recommend using a RelativeLayout for the root layout and use the property android:layout_alignParentBottom="true" for the "version". Then wrap the other Views in a LinearLayout. Something like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText
android:id="#+id/edittext1"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="NEW"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/edittext2"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview2"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="NEW"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Version 1.0"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I haven't had a chance to really test it but this should get you pretty close. I wrapped the two sets of Views in the center in a LinearLayout then gave each their own LinearLayout so they can make use of weight (which you can adjust to your needs). The parent LinearLayout is centered vertical so that those 4 Views will be centered the way I believe you want.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">//hope this will work for you
<LinearLayout
android:id="#+id/alltextfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"`enter code here`
android:orientation="horizontal" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="EditText" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New" />
</LinearLayout>
<LinearLayout
android:id="#+id/alltextfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="EditText" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New" />
</LinearLayout>

Unable To Center Objects Within Two Relative Layouts

I'm attempting to split the UI for my horizontal layout into two halves each with it's own realative layout so I am able to center text and graphics within the two left and right halves of the screen. The problem is when I attmept to do so - the go button still ends up in the middle of the screen instead of centered on the right hand of the screen.
(If I can figure out how to center correctly I'm sure I'll be able to apply the same technique to the other elements to achieve the result I desire.
SOURCE:
<?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:layout_marginBottom="8sp"
android:layout_marginLeft="8sp"
android:layout_marginRight="8sp"
android:layout_marginTop="8sp"
android:background="#drawable/background"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="24dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="#drawable/apn_app_logo" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/start2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/go_button"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/start_text2"
android:textColor="#000000"
android:textSize="18sp" />
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/apn_app_go_button" />
</RelativeLayout>
</RelativeLayout>
EDIT: (in response to Kuba's answer)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="#drawable/apn_app_logo" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/apn_app_go_button"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/start_text2"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
Instead of Relative layout, I would recommend using LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#00dd00"
android:gravity="center"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#dd0000"
android:gravity="center_horizontal"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
In the inner LinearLayout you can either use android:gravity="center" to have its children centered vertically and horizontally. Or use use android:gravity="center_horizontal" to center in only horizontal way.
EDIT: Added the textview, which was discussed in the comments.
To have the textView appear underneath the button, you must change the orientation of the LinearLayout from horizontal to vertical. Please see the developer site .
The inner LinearLayout thus looks like this.
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="#dd0000"
android:gravity="center"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text here"
/>
</LinearLayout>
How can I use android:layout_below="#+id/go_button" in a linear layout?
You can't. These relationships only work in relative layout where you place views in relation to each other or the parent view. In linear layout items are stacked either vertically or horizontally.
Set android:gravity="center" in
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/apn_app_go_button" />
Maybe its work.

Categories