Is this the correct way to place the TextView in left and right side of the view?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="3dp"
android:layout_below="#+id/l_section_login"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_forgot_password"
android:layout_gravity="left"
android:textStyle="normal"
android:text="Forgot Password?" />
<TextView
android:id="#+id/tv_sign_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:text="Sign up?"
/>
</LinearLayout>
No, you have hardcoded 180dp as a margin. This won't handle devices with different screen sizes gracefully.
I'd recommend starting with a ConstraintLayout and using app:layout_constraintEnd_toEndOf="parent" in the second TextView
<android.support.constraint.ConstraintLayout
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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
app:layout_constraintEnd_toEndOf="parent"
/>
</android.support.constraint.ConstraintLayout>
If you still want to use a linear layout you can fill the line with empty views and use weights,for example :
<?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_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Forgot Password?"
android:gravity="center_vertical" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center_vertical" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0.2"
android:text="Sign up?" />
</LinearLayout>
</LinearLayout>
Related
So, I'm trying to get something like this.
And I am having a problem lining up the + and - buttons in the same line.
Once I have the horizontal layout, and I drag the buttons. I end up with this. I don't know how to make it work, from code or design window. Please help.
Any solution?
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:orientation="horizontal">
<Button
android:id="#+id/suma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="171dp"
android:layout_weight="1"
android:onClick="incrementaContador"
android:text="+" />
<Button
android:id="#+id/resta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="169dp"
android:layout_weight="1"
android:onClick="restaContador"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<TextView
android:id="#+id/titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Cafe del dia "
android:textAlignment="center" />
<TextView
android:id="#+id/contadorA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="#+id/reinicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Reinicio"
android:onClick="reseteaContador" />
</RelativeLayout>
I think you need something like this. Paste this code in your code window.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:id="#+id/titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Cafe del dia " />
<TextView
android:id="#+id/contadorA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/titulo"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="4"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/contadorA"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:orientation="horizontal">
<Button
android:id="#+id/suma"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="16dp"
android:onClick="incrementaContador"
android:text="+"
android:textSize="30sp" />
<Button
android:id="#+id/resta"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="16dp"
android:onClick="restaContador"
android:text="-"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="#+id/reinicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:onClick="reseteaContador"
android:text="Reinicio" />
</RelativeLayout>
From my point of view, it is easier to get appropriate design by coding it. I am having something like this.
You've got
android:layout_alignParentTop="true"
android:layout_marginTop="171dp"
on one button and
android:layout_alignParentTop="true"
android:layout_marginBottom="169dp"
on the other.
Try 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<Button
android:id="#+id/suma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:onClick="incrementaContador"
android:text="+" />
<Button
android:id="#+id/resta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:onClick="restaContador"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<TextView
android:id="#+id/titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Cafe del dia "
android:textAlignment="center" />
<TextView
android:id="#+id/contadorA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="#+id/reinicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Reinicio"
android:onClick="reseteaContador"
/>
</RelativeLayout>
You have too many nested views. You could just use a vertical Linear Layout as parent viewgroup and a horizontal linear layout with layout weight 1 for the middle two buttons.
For example:
<?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">
<TextView
android:id="#+id/titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Cafe del dia "
android:textAlignment="center" />
<TextView
android:id="#+id/contadorA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="24sp"
android:text="24"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_weight="1">
<Button
android:id="#+id/suma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:onClick="incrementaContador"
android:text="+" />
<Button
android:id="#+id/resta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="restaContador"
android:text="-" />
</LinearLayout>
<Button
android:id="#+id/reinicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Reinicio"
android:onClick="reseteaContador" />
</LinearLayout>
Best if you use constraintLayout once you learn to use it.
You should instead use a ConstraintLayout to achieve what you want. It will be more efficient than using a RelativeLayout, especially nesting sub-layouts inside it. With the ConstraintLayout, you can use the GUI builder to drag the elements around the screen and get them pixel-perfect. The ConstraintLayout may require a bit of work but it is really powerful and you can get major performance gains when you have complex Viewgroup hierarchies consisting of many nested ViewGroups.
But as a solution to your problem here is the updated code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<Button
android:id="#+id/suma"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginRight="15dp"
android:onClick="incrementaContador"
android:textSize="35sp"
android:text="+" />
<Button
android:id="#+id/resta"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="15dp"
android:onClick="restaContador"
android:textSize="35sp"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<TextView
android:id="#+id/titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Contender"
android:textAlignment="center" />
<TextView
android:id="#+id/contadorA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="35sp"
android:text="4"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="#+id/reinicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
android:onClick="reseteaContador"
android:text="Reinicio"
/>
</RelativeLayout>
See on the picture the property that will put text box below the button.
Then you get the squigly line in red circle indicating the relation.
How to space it even further down the page, I'm still working on. (you could use padding, but I'm sure there is better solution)
click
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
I'm trying to get two linear layout side by side and put content inside of it.
I want the two linear layouts to be side by side, but the left one should take about 3/4 of the screen and the right one the rest of it.
This is my code: https://gitlab.com/snippets/1682040
But the two linear layouts always change the width and the height depending on the content.
How can I have two linear layouts side by side with fixed width and height?
A better solution with RelativeLayout is always welcome.
This is the result:
Use layout_weight property of LinearLayout and android:weightSum property for parent layout.
Do Something like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".75" // For First layout to 3/4
android:background="#color/colorPrimary"
android:gravity="left|center"
android:orientation="vertical">
//First Layout content Here
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".25" // For Second layout to rest of screen
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical">
//Second Layout Content Here
</LinearLayout>
</LinearLayout>
I know that you asked for a solution with the LinearLayout but I suggest you to solve this problem with the ConstraintLayout. You will get incredible performance improvements without nested view.
In this sample I added a vertical guideline at 75% of from the left of the screen. I have only to remove the nested LinearLayouts and add the constraints to your views!
Try it out!
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="628dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorPrimary"
android:gravity="left|center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/repository_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue"
android:textSize="20dp" />
<TextView
android:id="#+id/repository_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="AAAAA"
android:textColor="#color/gray"
android:textSize="15dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_fork_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_star_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="0dp"
android:layout_height="628dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline4"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue" />
<TextView
android:id="#+id/first_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mock Name"
android:textColor="#color/lightGray" />
</LinearLayout>
<android.support.constraint.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
Try this give Your first LinearLayout android:layout_weight=".75" so it can take 3/4 space in screen
than assign android:layout_weight="0.25" to Your second LinearLayout so it can take remaining space
<?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"
android:padding="15dp"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:id="#+id/repository_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/colorBlue"
android:textSize="20dp" />
<TextView
android:id="#+id/repository_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="AAAAA"
android:textColor="#color/colorGreen"
android:textSize="15dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_fork_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/colorPrimary"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_star_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/colorPrimary"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/colorBlue" />
<TextView
android:id="#+id/first_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mock Name"
android:textColor="#color/colorRed" />
</LinearLayout>
</LinearLayout>
Seems like both of the weights are set to "1". Have you tried ".5"?
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:layout_weight="0.50"
android:gravity="left|center">
Try this I have added 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:orientation="horizontal"
android:padding="15dp"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".75"
android:background="#color/colorPrimary"
android:gravity="left|center"
android:orientation="vertical">
<TextView
android:id="#+id/repository_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue"
android:textSize="20dp" />
<TextView
android:id="#+id/repository_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="AAAAA"
android:textColor="#color/gray"
android:textSize="15dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_fork_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_star_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".25"
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue" />
<TextView
android:id="#+id/first_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mock Name"
android:textColor="#color/lightGray" />
</LinearLayout>
</LinearLayout>
First remove weightSum attribute from parent layout.
If you want your first layout to be 3/4 of you should put weight 0.75 and 0.25 (1/4) for the other one like here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:layout_weight=".75"
android:layout_gravity="left"
android:gravity="left|center">
<TextView
android:id="#+id/repository_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue"
android:textSize="20dp"/>
<TextView
android:id="#+id/repository_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:text="AAAAA"
android:textColor="#color/gray"
android:textSize="15dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"/>
<TextView
android:id="#+id/repository_fork_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"
android:layout_marginLeft="15dp"/>
<TextView
android:id="#+id/repository_star_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorAccent"
android:layout_weight=".25"
android:gravity="center">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue"/>
<TextView
android:id="#+id/first_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mock Name"
android:textColor="#color/lightGray"/>
</LinearLayout>
try this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".75"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000"
android:textSize="16dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_txt"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".25"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000"
android:textSize="16dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_txt"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
Change the weight sum to 4 in the parent layout and add weight as 3 for child linear layout so it can take 3/4 space and 1 to child linear layout so that it will take the remaining space.
<?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"
android:padding="15dp"
android:weightSum="4">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#color/colorPrimary"
android:gravity="left|center"
android:orientation="vertical">
//Your contents
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical">
//Your contents
</LinearLayout>
</LinearLayout>
I am trying to achieve the below layout but I am not sure how I can split / have two layouts on one screen, with one being empty and one containing other elements. Like shown in the image below, I want to have layout 1 that contains other elements and layout that is empty. With a divider line between the two layouts.
Any help would be nice, I am not sure where to start. I have came accross having two layouts on 1 screen and splitting them to about 40:60 ratio.
Try to use
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.rushabh_pc.lin.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label2"
android:id="#+id/toggleButton"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:checked="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label 3"
android:id="#+id/lab3"
android:layout_below="#+id/toggleButton"
android:layout_toLeftOf="#+id/toggleButton"
android:layout_toStartOf="#+id/toggleButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label 4"
android:layout_alignBaseline="#+id/lab3"
android:layout_alignBottom="#+id/lab3"
android:layout_toRightOf="#+id/toggleButton"
android:layout_toEndOf="#+id/toggleButton"
android:id="#+id/l4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="last"
android:layout_below="#id/l4"/>
</RelativeLayout>
Code Below:
<?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:id="#+id/activity_main3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.staff.app.Main3Activity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_light">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"/>
<LinearLayout
android:id="#+id/linearLyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="editText1"
android:layout_weight="1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="editText2"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView"
android:textSize="40dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/linearLyout"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/black"></View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"></RelativeLayout>
</LinearLayout>
Use LinearLayout with layout_weight to specify the ratio for the space occupied by each child layout. For a 40:60 split, use the below
<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="0dp"
android:layout_weight="0.4"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_green_light"
android:text="Label"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_bright"
android:text="Label"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_weight="1"
android:background="#android:color/holo_green_light"
android:hint="Textbox1" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_weight="1"
android:background="#android:color/holo_green_light"
android:hint="Textbox2" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:text="Label" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"></LinearLayout>
</LinearLayout>
This is how the cart screen looks like. The layout changes as per the length of item name.
I want to keep a fixed ratio for all columns in the listview and at the same time, keep the size of button fixed.
How to achieve this ?
Below is the Screenshoot and layout code:
]1
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:background="#ffffff">
<TableRow>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/cartlist_layout"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t1"
android:textStyle="normal|bold"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/t2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t2"
android:padding="5dip"
android:textStyle="normal|bold"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/t2"
android:id="#+id/t3"
android:textStyle="normal|bold" />
<Button
android:layout_width="5dp"
android:layout_height="50dp"
android:id="#+id/button6"
android:background="#drawable/delete1" />
</TableRow>>
</TableLayout>
Edit1: The Listview layout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragment_mycart">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView1" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#color/colorPrimary"
app:backgroundTint="#android:color/holo_orange_light"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:srcCompat="#drawable/cart" />
</RelativeLayout>
You can use a LinearLayout as root view of the item instead of TableLayout. You can add weights to the views inside the LinearLayout.
<?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"
android:background="#ffffff">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:id="#+id/t1"
android:textStyle="normal|bold"
android:padding="5dip"
android:maxLines="1"
android:ellipsize="end" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/t2"
android:padding="5dip"
android:textStyle="normal|bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:id="#+id/t3"
android:textStyle="normal|bold" />
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button6"
android:background="#drawable/delete1" />
</LinearLayout>
</LinearLayout>
Note : You may want to adjust the weights a bit.
Use LinearLayout instead
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dip"
android:text="text1"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dip"
android:text="text2"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/t3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dip"
android:text="text3"
android:textStyle="normal|bold" />
<Button
android:id="#+id/button6"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#android:drawable/ic_input_add" />
</LinearLayout>