Why views are not equally spread - android

I have 4 TextViews. What I'm trying to do is to make all guests in the list take equal space (except the 'VIP List' TextView).
So I've set layout_height to 0dp and layout_weight to 1 but there is still some place not taken in the bottom.
What is wrong with my code?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray">
<TextView
android:text="VIP List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ACC1"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:gravity="center" />
<TextView
android:text="Bob"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#BF360C"
android:textSize="24sp"
android:textColor="#FFFFFF"/>
<TextView
android:text="Bill"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#4CAF50"
android:textSize="24sp"
android:textColor="#FFFFFF"/>
<TextView
android:text="Ben"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#2E7D32"
android:textSize="24sp"
android:textColor="#FFFFFF"/>
</LinearLayout>

Change your LinearLayout code with this;
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:background="#android:color/darker_gray">
...
</LinearLayout>

Put out "VIP List" TextView from content container LinearLayout because it is not part of "relative" TextView group, then add
android:weightSum="3" to LinearLayout content container because you have 3 TextViews with weight = 1. Finally wrap all view within a parent LinearLayout.
Consider something like below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ACC1"
android:gravity="center"
android:text="VIP List"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:weightSum="3"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#BF360C"
android:text="Bob"
android:textColor="#FFFFFF"
android:textSize="24sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#4CAF50"
android:text="Bill"
android:textColor="#FFFFFF"
android:textSize="24sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#2E7D32"
android:text="Ben"
android:textColor="#FFFFFF"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>

It will work.. Use a Linear layout under the parent layout and it android:weightSum="3" and just put the vip list outside of second linear layout Like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ACC1"
android:gravity="center"
android:text="VIP List"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:weightSum="3"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#BF360C"
android:text="Bob"
android:textColor="#FFFFFF"
android:textSize="24sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#4CAF50"
android:text="Bill"
android:textColor="#FFFFFF"
android:textSize="24sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#2E7D32"
android:text="Ben"
android:textColor="#FFFFFF"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>

Related

How to create layout_weight for vertical and horizontal at the same time in a RecyclerView item

I want a layout like below image for the item of a RecyclerView:
Below is my XML Item script:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:weightSum="1"
android:layout_height="wrap_content">
<TextView
android:id="#+id/t"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp"
/>
<TextView
android:id="#+id/d"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp"
/>
</LinearLayout>
What changes to be needed so that vertical and horizontal weight will be applied on layout at same time using Linear Layout?
If you want to use LinearLayout You can use something like this:
[LinearLayout ] [LinearLayout ]
[child 1] [child 1](takes full screen height)
[child 2]
Here is the XML for it:
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button"/>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
</LinearLayout>
It will look like this:
Put it in Linear Layout and provide weight to your each layouts
<LinearLayout
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:id="#+id/myText"
android:text="Hello"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:id="#+id/newText"
android:text="NEW Text"/>
</LinearLayout>
<TextView
android:layout_weight="0.5"
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_height="wrap_content"
android:text="Hello"
android:id="#+id/Newtext"/>
</LinearLayout>
Thank you so much for your helps!
And this is my exact answer from your helps:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:weightSum="1"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.4">
<TextView
android:id="#+id/t"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/d"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.6">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"/>
<Switch
android:id="#+id/switch1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.2"
android:checked="true" />
</LinearLayout>

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>

Android layout even spacing

Here's a comparison of what my editor and device is showing:
The problem is:
row containing TextView's should take 50% of the screen
row containing Icon should take remaining 50% of the screen
It works correctly in editor, but as you can see, layout behaves much differently on actual device.
Where's my problem and how to solve it?
Library used for icon is:
https://github.com/code-mc/material-icon-lib
It extends directly from ImageView.
Layout XML:
<TableLayout
android:id="#+id/menuLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*" >
<TableRow
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_weight="1"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/teenSelection"
android:textColor="#color/WHITE"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="TEEN" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/adultSelection"
android:textColor="#color/WHITE"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="ADULT"/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/seniorSelection"
android:textColor="#color/WHITE"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="SENIOR"/>
</LinearLayout>
</TableRow>
<TableRow
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<net.steamcrafted.materialiconlib.MaterialIconView
android:layout_weight="1"
android:id="#+id/confirmButton"
app:materialIcon="check"
app:materialIconColor="#fff"
android:scaleType="center"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</TableRow>
</TableLayout>
you have set your heights of the two TableRows to wrap_content, but they must be match_parent like this:
<TableRow
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent">
otherwise the weight for the height is ignored.
You should try PercentRelativeLayout
in app level build.gradle add this
compile 'com.android.support:percent:23.3.0'
and change your layout with this.
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layoutTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
app:layout_heightPercent="50%"
app:layout_widthPercent="100%">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/teenSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="TEEN"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/adultSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="ADULT"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/seniorSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="SENIOR"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="#+id/confirmButton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/layoutTop"
android:scaleType="center"
app:layout_heightPercent="50%"
app:layout_widthPercent="100%"
app:materialIcon="check"
app:materialIconColor="#fff" />
</android.support.percent.PercentRelativeLayout>
add your TableRow in LinearLayout like this.
i worked. i did test.
<TableLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:stretchColumns="*">
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/teenSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:background="#color/black_slow"
android:textStyle="bold"
android:textSize="30sp"
android:text="TEEN" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/adultSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:background="#color/black_slowest"
android:textStyle="bold"
android:textSize="30sp"
android:text="ADULT"/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/seniorSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#color/colorPrimaryDark"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="SENIOR"/>
</LinearLayout>
</TableRow>
<TableRow
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="#+id/confirmButton"
app:materialIcon="check"
app:materialIconColor="#fff"
android:scaleType="center"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</TableRow>
</LinearLayout>
</TableLayout>

vertical alignment of the text views of different linear layouts

I am trying to create a screen that shows the details of the students. The following is my xml code for that screen.
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".PatientDetails">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3"
android:background="#color/BackgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Details"
android:textColor="#color/white"
android:textSize="30dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="#color/white">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#color/BackgroundColor"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/PatientName"
android:text="Joseph"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="#color/BackgroundColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#color/BackgroundColor"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/age"
android:text="21"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="#color/BackgroundColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sex"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#color/BackgroundColor"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Sex"
android:text="Female"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="#color/BackgroundColor"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
The output of the code is as follows:
Can someone tell me how to modify the code so that the values on the right hand side of each row are aligned properly.
Row example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Student Name"
android:textColor="#color/BackgroundColor"
android:textSize="20dp" />
<TextView
android:id="#+id/PatientName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Joseph"
android:textColor="#color/BackgroundColor"
android:textSize="20dp" />
</LinearLayout>
Remove android:layout_weight="1" from row root. Change android:layout_width="wrap_content" to android:layout_width="0dp"
Sample
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Student Name"
android:textColor="#color/pika_bg"
android:textSize="20dp" />
<TextView
android:id="#+id/PatientName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="Joseph"
android:textColor="#color/pika_bg"
android:textSize="20dp" />
</LinearLayout>
Here I am added android:weightSum="2"to the parent LinearLayout. So that the child views can get same width. Then we can adjust padding/gravity attributes according to your needs.
I think you use linear layout but linear layout have also one property its called layout_weight i think you should use this.
<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:layout_weight="1"
android:text="Test" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Test" />
</LinearLayout>

How to write TextView over an ImageView at a specific position in android?

I am trying to combine TextView and ImageView and want to place the TextViews at the center of both the circles in the image but somehow I haven't been able to figure out a solution that will work across different kind of devices.
This is the image I'm trying to use:
This is what I've tried till now. I've put both the imageview and the textview in a framelayout and then tried to adjust the textview by using linearlayouts and layout weights.
`
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80">
<ImageView
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/circle_pic"
android:adjustViewBounds="true" />
<LinearLayout
android:id="#+id/home_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="60"
android:gravity="center">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="85"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#color/white"
android:text="#string/text1"/>
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#color/white"
android:text="#string/text2"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="15">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/text3"
android:orientation="horizontal"
android:layout_weight="40"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="#string/tv3"
android:textColor="#color/white"/>
<TextView
android:id="#+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="000"
android:textColor="#color/white"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>`
You can use RelativeLayout to achieve this as
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="<your_drawable>"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_alignRight="#id/imageView"
android:layout_alignTop="#id/imageView"
android:layout_alignBottom="#id/imageView"
android:gravity="center"/>
</RelativeLayout>

Categories

Resources