Android: TextViews next to each other on different screen sides - android

I'm struggling with TextViews. I want them next to each other but in the opposite screen sides. First should be on the left of the screen and the second on the right. This is my code:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/unit"
android:textSize="22sp"
android:text="Unit"/>
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/unitName"
android:text="Km"
android:textColor="#android:color/darker_gray"
android:textSize="22sp"/>
</LinearLayout>
I tried layout_gravity and gravity and it doesn't work. I was experimenting with wrap_content and match_parent but still my TextViews are just next to each other. I want them in opposite screen sides. What should I do?

Change your width to Wrap_content and Linear layout to RelativeLayout and then set alignParent attributes
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/unit"
android:layout_alignParentLeft="true"
android:textSize="22sp"
android:text="Unit"/>
<TextView
android:id="#+id/unitName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:layout_alignParentRight="true"
android:text="Km"
android:textColor="#android:color/darker_gray"
android:textSize="22sp" />
</RelativeLayout>

Just add on both TextViews this line
android:layout_weight="0.5"

I have made some changes in your layout please take a look
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="#+id/unit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".50"
android:text="Unit"
android:textSize="22sp" />
<TextView
android:id="#+id/unitName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".50"
android:gravity="right"
android:text="Km"
android:textColor="#android:color/darker_gray"
android:textSize="22sp" />
</LinearLayout>

Try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/unit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="Unit"
android:textSize="22sp"/>
<TextView
android:id="#+id/unitName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right"
android:text="Km"
android:textColor="#android:color/darker_gray"
android:textSize="22sp"/>
</LinearLayout>

try this use android:layout_weight="" and android:gravity=""
<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="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="left"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="left"
android:layout_weight="1"/>
</LinearLayout>

Related

Aligning Elements in a Linear Layout

I am new to android and learning. I am building form like UI and using linear layout. I have tried to put these in two vertical linear layouts inside a horizontal layout but couldn't manage to align label text elements with each input element. Below, I am using three linear layouts.
xml layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/linlaytop"
android:layout_marginLeft="#dimen/linlayleft"
android:layout_marginRight="#dimen/linlayleft"
android:layout_marginBottom="#dimen/linlaytop"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/linlaytop">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/traffic"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
<Spinner
android:id="#+id/trafficSpinnerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/linlayleft">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_marginTop="#dimen/linlaytop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/location"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
/>
<EditText
android:id="#+id/locationTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:layout_marginLeft="#dimen/linlayleft"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="#dimen/linlaytop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/date"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
<TextView
android:id="#+id/dateTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/datetime"
android:layout_marginLeft="#dimen/linlayleft"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
</LinearLayout>
</LinearLayout>
Design Layout:
I want to align these three elements:
Please advise.
Give Weightsum to your linearLayout, nd give layout_weight to your views ( Textviews and spinner ). Change the width of views from wrap_content to 0dp. Do the same with all of three layouts like this.
Hope this is what you looking for.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
android:layout_marginTop="#dimen/linlaytop">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:text="#string/traffic"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
<Spinner
android:id="#+id/trafficSpinnerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="70"
android:layout_marginLeft="#dimen/linlayleft">
</Spinner>
</LinearLayout>
Here you go
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_weight="10"
android:layout_height="wrap_content"
android:text="traffic"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
<Spinner
android:id="#+id/trafficSpinnerView"
android:layout_width="0dp"
android:layout_weight="40"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="10"
android:layout_height="wrap_content"
android:text="location"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
/>
<EditText
android:id="#+id/locationTxtView"
android:layout_width="0dp"
android:layout_weight="40"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="10"
android:layout_height="wrap_content"
android:text="date"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
<TextView
android:id="#+id/dateTxtView"
android:layout_width="0dp"
android:layout_weight="40"
android:layout_height="wrap_content"
android:hint="datetime"
android:layout_marginLeft="10dp"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
</LinearLayout>
</LinearLayout>

align textview below ImageView

I've one ImageView and 3 TextViews. I need to align the 3 text views below the imageview. the first on the left . the second in center. the 3rd on the right.
below code is working for normal and small screens. but for tablets and large screens, the first text view is not below the imageview. it's shifted too left. and the third TextView is shifted too Right.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Rel_wind2_img">
<ImageView
android:id="#+id/window2_image1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/window2_image1"/>
<TextView
android:id="#+id/TV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/window2_image1"
android:layout_alignParentLeft="true"
android:textAlignment="center"/>
<TextView
android:id="#+id/TV2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/window2_image1"
android:layout_centerInParent="true"
android:textAlignment="center"/>
<TextView
android:id="#+id/TV3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/window2_image1"
android:layout_alignParentRight="true"
android:text="#string/info2_txt_moves"
android:textAlignment="center"/>
</RelativeLayout>
I can solve this problem grammatically. but i need a simple way using XML.
Give layout weight inside a linear layout. It will set the width equally between all textviews. try this
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Rel_wind2_img"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/window2_image1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/window2_image1"
>
<TextView
android:id="#+id/TV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:textAlignment="center"/>
<TextView
android:id="#+id/TV2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:textAlignment="center"/>
<TextView
android:id="#+id/TV3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="info2_txt_moves"
android:layout_weight="1"
android:textAlignment="center"/>
</LinearLayout>
</RelativeLayout>
Best way to do this kind of UI design just divide the Layout into weights.
because if we divide the ui in weights it will automatically adjust according to divide size so there will be no problem on any device.
like:
<?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:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Rel_wind2_img">
<LinearLayout
android:id="#+id/tr"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/window2_image1"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#color/black"
/>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/tr"
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:id="#+id/TV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dhshshs"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:id="#+id/TV2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fjkhajhjfha"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:id="#+id/TV3"
android:text="aoifoijoajoifjoajofjafafg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/window2_image1"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Use this One :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Rel_wind2_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/window2_image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="dfgf"
android:id="#+id/TV1"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAlignment="center" />
<TextView
android:id="#+id/TV2"
android:text="dfgf"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center" />
<TextView
android:id="#+id/TV3"
android:text="dfgf"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
Hope this help you...if you need any help you can ask

Android Layout Help: 2 textview vertical Inline next to 1 button

I try to do this layout but i got only echec until now:
- 2 textview vertical Align sharing 50% width with 1 button.
- all must be vertical centered.
Here is the mockup showing what i want:
Mockup
Here is my actual code :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/PanelName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#color/colorBlack"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Parking Name"
android:textSize="14sp" />
<TextView
android:id="#+id/PanelAddress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#color/colorBlack"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Parking Address blablabla Paris"
android:textSize="14sp" />
</LinearLayout>
<android.support.v7.widget.AppCompatButton
android:id="#+id/btn_go"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:textColor="#color/colorAccent"
android:layout_marginTop="30dp"
android:layout_marginBottom="24dp"
android:layout_marginRight="24dp"
android:padding="12dp"
android:text="GO !"/>
</LinearLayout>
Thanks for help :)
Remove Nested Linear Layouts it's bad for performance. I have made some changes in your Layout. Make your AppCompatButton Layout inside Relative Layout and make it's width to android:layout_width="wrap_content" and also Remove First Linear Layout inside Root Layout.
Refer this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/image_area"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_go"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/PanelName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Parking Name"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="#+id/PanelAddress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Parking Address blablabla Paris Parking Address blablabla Paris Parking Address blablabla Paris"
android:textColor="#000"
android:textSize="14sp" />
</LinearLayout>
<android.support.v7.widget.AppCompatButton
android:id="#+id/btn_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="30dp"
android:layout_weight="2"
android:background="#000"
android:padding="12dp"
android:text="GO !"
android:textColor="#FFF" />
</RelativeLayout>
Here is Screen.
<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:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/txtOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView One" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/txtTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView Two" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/btnSomething"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Positioning of the elements - not in the same line

I have started learning android development and I have stumbled across a thing I don't quite understand why is it happening. Here is the code I am having trouble with:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/lbl_Title"
android:id="#+id/lbl_Title"
android:layout_marginTop="#dimen/activity_vertical_margin" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_weight="2"
android:layout_marginTop="#dimen/activity_vertical_margin" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/lbl_Title"
android:id="#+id/lbl_Date"
android:layout_marginTop="#dimen/activity_vertical_margin" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="-"
android:id="#+id/textView2"
android:layout_marginTop="#dimen/activity_vertical_margin" />/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which results in the following:
As you can see, even though my two containers are exactly the same, for unknown reason text under the second container is located slightly above the first element. Why is this so?
P.S. These containers are under the RelativeLayout panel.
The issue is the EditText. Since you are only using wrap_content on the second TestView on the right side, the height of that linear layout is smaller than the one on the left.
I have changed some portions. Now its working as your requirement
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/lbl_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/lbl_Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textAppearance="?android:attr/textAppearanceLarge" />/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Android how take full screen with more LinearLayout orientation horizontal

i'm trying to do a school day and i use multiple linearLayout with days and hours.
With the layout_weight i can set the space for every elements inside the single LinearLayout.
But my problem is how can i set the layout_weightfor the LinearLayout because they are set with orientation horizontal and they don't take the entire screen.
It's like a table but i use LinearLayout because i need to create only rows.
This is the xml of the Activity
<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.ddz.diarioscolastico.OrarioActivityProva">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Lun"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mar"
android:id="#+id/textView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mer"
android:id="#+id/textView3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Gio"
android:id="#+id/textView4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Ven"
android:id="#+id/textView5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sab"
android:id="#+id/textView6" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="1"
android:id="#+id/textView7" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="2"
android:id="#+id/textView8" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="3"
android:id="#+id/textView9" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="4"
android:id="#+id/textView10" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="5"
android:id="#+id/textView11" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout7">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="6"
android:id="#+id/textView12" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout8">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="7"
android:id="#+id/textView13" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="8"
android:id="#+id/textView14" />
</LinearLayout>
As you can see if i set the android:layout_height="match_parent" yes all LinearLayout take full screen but only the first is visible, i wanna divide the screen for all.
Using Layout Weight is a little bit tricky task.
Suppose you want to show three buttons horizontally which share equal width in a linear layout. Then the xml code should be 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:weightSum="3"
android:orientation="horizontal" >
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
</LinearLayout>
As you can see,In the parent LinearLayout i used weightSum as 3 and in each child button i set the width to 0dp and weight to 1 and there are three buttons so it will share equal width.
By the way the root parent (RelativeLayout) is not closed in your given layout.
EDIT:2
As you said here is the code divide each LinearLayout equally with layout_weight
<?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:weightSum="3"
android:orientation="vertical" >
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#00FF00"
>
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#FF0000"
>
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#0000FF"
>
</LinearLayout>
</LinearLayout>
OUTPUT :
Have you tried to replace your LinearLayout with a TableRow or use a GridLayout ? GridLayout requires API 14 but lower API can be use with the support library.

Categories

Resources