Android UI equal spaces between Textviews [duplicate] - android

This question already has answers here:
How to make 3 textView control to be on the same size
(6 answers)
Closed 7 years ago.
I have 3 textviews: textItemRef, textViewName and textViewPrice, I want equal spaces between these 3 textviews but they are overlapping each other. Itried setting weight but it didn't help. where is the problem? Here's my Ui
<?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="horizontal" >
<RelativeLayout
android:id="#+id/linearFirst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<LinearLayout
android:id="#+id/textItemRef1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
>
<TextView
android:id="#+id/textItemRef"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/textViewName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/textItemRef1">
<TextView
android:id="#+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/textViewName1" >
<TextView
android:id="#+id/textViewPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/linear_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" />
<View
android:layout_width="match_parent"
android:layout_height="1dp" />
</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="horizontal">
<LinearLayout
android:id="#+id/linearFirst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:id="#+id/textItemRef1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_weight="1">
<TextView
android:id="#+id/textItemRef"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView1"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:id="#+id/textViewName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/textViewName"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView2"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/textViewPrice"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linear_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" />
<View
android:layout_width="match_parent"
android:layout_height="1dp" />

You dont need to have nested layout like that instead you can simply have one layout like 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="horizontal" >
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Hello"/>
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="From"/>
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Jay"/>
</LinearLayout>
Hope this works :)

1) Change : android:layout_height="wrap_content"
with
android:layout_height="match_parent"
2) set the layout_gravity wherever you want.
3) Your inside LinearLayout has layout_weight="1" and layout_height="0dp"
4) Your TextView has layout_weight=0"
Hope it helps.

Related

layout_weight does not work when the layout inside the relative layout

layout_weight does not work when the Linearlayout inside the relative layout, only the problem is in layout_weight, that is showing properly in design view but while compile its getting error and stop compile
my code is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:id="#+id/ll_bookmarkslistad"
android:weightSum="100"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="30" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="70"
android:layout_marginRight="0.0dp" />
</LinearLayout>
</RelativeLayout>
sorry for late reply. it will help you. :)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/darker_gray"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" hello world"
android:layout_weight="3"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="7"></Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/darker_gray"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1"
android:layout_weight="3"
/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn2"
android:layout_weight="4"
/>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn3"
android:layout_weight="3"
/>
</LinearLayout>
</RelativeLayout>

Display two text field in vertically(one below the other in list view)

I want to display text field Duration below the text field Item.I am having the list view which is populated in different for even and odd rows. But the thing is I want the two text field in parallel to image I want to display vertically, but when I put the two text field in the Liner Vertical layout it shows error. How to display the two text field in vertical manner. Sorry if any mistake in my english. Thanks in advance.
Below is my code:
<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" >
<ImageView
android:id="#+id/gender_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="39.5"
android:contentDescription="#string/app_name"
android:src="#drawable/male" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:gravity="center"
android:textColor="#android:color/holo_green_dark" />
<TextView
android:id="#+id/name1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:gravity="center"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>
<?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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/gender_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="39.5"
android:contentDescription="#string/app_name"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:gravity="center"
android:textColor="#android:color/holo_green_dark" />
<TextView
android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:gravity="center"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>
Try this!!!
It will give the view, what you are looking till.
Please check below my code here i manage your both different item view in single layout so you have just show/hide respective layout based on item :
<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">
<LinearLayout
android:id="#+id/male_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/gender_image_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name"
android:src="#drawable/male" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/name_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/holo_green_dark" />
<TextView
android:id="#+id/name1_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/female_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/gender_image_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name"
android:src="#drawable/female" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/name_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/holo_green_dark" />
<TextView
android:id="#+id/name1_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Note : android:layout_weight is used take rest of area.
use 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:orientation="horizontal" >
<ImageView
android:id="#+id/gender_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="39.5"
android:contentDescription="#string/app_name"
android:src="#drawable/male" />
<LinearLayout
android:layout_weight="60.5"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:id="#+id/name"
android:text="asdasdas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:gravity="center"
android:textColor="#android:color/holo_green_dark" />
<TextView
android:id="#+id/name1"
android:text="asdasd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:gravity="center"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>
</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" >
<ImageView
android:id="#+id/gender_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="39.5"
android:contentDescription="#string/app_name"
android:src="#drawable/male" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="60"
android:gravity="center"
android:textColor="#android:color/holo_green_dark" />
<TextView
android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="60"
android:gravity="center"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>
</LinearLayout>
PS: put the weights and width accordingly... :)

can not change layout_gravity from left

I have a simple LinerLayout with 3 child linear layouts , each have a text view
when Im trying to align the text view to the center or to the right , nothing happens. its always aligned to the left, why ?
<?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">
<LinearLayout
android:layout_weight="1"
android:id="#+id/alert_type"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="TYPE"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_below="#id/alert_type"
android:id="#+id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ALERT"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_below="#id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alert_details"
android:text="DETAILS" />
</LinearLayout>
</LinearLayout>
you can use RelativeLayout instead
<?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">
<RelativeLayout
android:layout_weight="1"
android:id="#+id/alert_type"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TYPE"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="1"
android:layout_below="#id/alert_type"
android:id="#+id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="ALERT"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="1"
android:layout_below="#id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alert_details"
android:text="DETAILS" />
</RelativeLayout>
</LinearLayout>
or you can use gravity to the linearlayout not textview..like
<LinearLayout
android:layout_weight="1"
android:id="#+id/alert_type"
android:gravity="right"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TYPE"/>
</LinearLayout>
Change android:layout_width="wrap_content" of TextView to match_parent
Set gravity like
android:gravity="center"
You need to set
android:gravity="center"
for each TextView's and LinearLayout.
change layout_width="wrap_content" and layout_gravity="centre"
<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:id="#+id/alert_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="TYPE" />
</LinearLayout>
<LinearLayout
android:id="#+id/alert_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/alert_type"
android:layout_gravity="center"
android:layout_weight="1" >
<TextView
android:id="#+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ALERT" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/alert_layout"
android:layout_gravity="center"
android:layout_weight="1" >
<TextView
android:id="#+id/alert_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DETAILS" />
</LinearLayout>
Use below XML code for your layout, in which I set all textview at center position
<?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:id="#+id/alert_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TYPE" />
</LinearLayout>
<LinearLayout
android:id="#+id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ALERT" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/alert_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DETAILS" />
</LinearLayout>
</LinearLayout>
You just need to add android:gravity="center" in your linear layout. You can also provide any alignment like right, center_vertical etc.

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>

Android layout help customizing according to image

I am trying to make a layout in android according to the image however whatever i try is just wrong.
Here's my code 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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="8dp" >
<TextView
android:id="#+id/bottomright"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#E5E5E5"
android:layout_weight="1"
android:gravity="bottom|right"
android:textSize="15sp"
android:text="#string/bottomright" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="8dp" >
<TextView
android:id="#+id/left"
android:layout_width="154dp"
android:layout_height="102dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:gravity="center|left"
android:layout_weight="1"
android:textSize="30sp"
android:adjustViewBounds="true"
android:maxLines="7"
android:text="" />
<TextView
android:id="#+id/topright"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="6dp"
android:gravity="top|right"
android:textSize="90sp"
android:text="topright" />
</LinearLayout>
</LinearLayout>
And here what i want to get. Maybe it is wrong to use LinearLayout? Better user Relative?
Can someone help me with 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:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="4" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top" />
</LinearLayout>
</LinearLayout>
Creating static xml layout's are very easy. I guess you haven't tried googling yourself.
anyways, with respect to the image, here is the code :-
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="350dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_toRightOf="#+id/textView1"
android:text="TextView3" />
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_toRightOf="#+id/textView1"
android:layout_below="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="TextView3" />
</RelativeLayout>

Categories

Resources