align reyclerview elements android - java

I have a recyclerview with a TextView and an EditText building the rows with the TextView on the left and the EditText right of it.
Currently view looks as follows:
Aaa: value1
Abbsbc: value2
Cskhkdhd: value3
HJhk: value4
I would like the view to look this way:
Aaa: value1
Abbsbc: value2
Cskhkdhd: value3
HJhk: value4
the layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/brand_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/brand_selector">
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider"/>
<TextView
android:id="#+id/object"
android:layout_width="wrap_content"
android:layout_height="#dimen/edit_box_height"
android:layout_below="#id/divider"
android:gravity="center_vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/noData"/>
<EditText
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="#dimen/edit_box_height"
android:layout_below="#id/divider"
android:layout_toRightOf="#id/object"
android:background="#android:color/transparent"
android:hint="Enter value"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black"
android:textColorHint="#color/divider"/>
</RelativeLayout>

Add this line in Textview:
android:textAlignment="viewEnd"
android:gravity="center_vertical|end"
Also I think this will still lead to uneven alignment if the length of textview text varies in rows, So its better if you use Linear Layout as parent layout. I have redone the xml accordingly:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/brand_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/brand_selector">
<TextView
android:id="#+id/object"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="#dimen/edit_box_height"
android:textAlignment="viewEnd"
android:gravity="center_vertical|end"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/noData"/>
<EditText
android:id="#+id/value"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="#dimen/edit_box_height"
android:layout_below="#id/divider"
android:layout_toRightOf="#id/object"
android:background="#android:color/transparent"
android:hint="Enter value"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black"
android:textColorHint="#color/divider"/>
</LinearLayout>

Try LinearLayout with weight:
<RelativeLayout
android:id="#+id/brand_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/brand_selector">
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/divider"
android:orientation="horizontal">
<TextView
android:id="#+id/object"
android:layout_width="0dp"
android:layout_height="#dimen/edit_box_height"
android:layout_weight="0.5"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/noData"
android:gravity="end"/>
<EditText
android:id="#+id/value"
android:layout_width="0dp"
android:layout_height="#dimen/edit_box_height"
android:layout_weight="0.5"
android:background="#android:color/transparent"
android:hint="Enter value"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black"
android:textColorHint="#color/divider"
android:paddingStart="10dp"/>
</LinearLayout>
</RelativeLayout>
EDIT: I added a gravity END to align on the right on the textView and a Startpadding on the EditText to leave space between label and value

Related

Android linearLayout children not expanding to fill parent

I have the following as a layout of mine:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
This makes my edittext and ratingbar be pushed up to the top and both of the heights are just 'wrap_content'. I have tried with RelativeLayouts as well and all that happens is the EditText takes up the entire screen and pushes the ratingbar off the screen.
Why are the layout weights not working as expected? If edittext weight is 3 and the ratingbar is 1 I would assume the edittext would take 75% of the screen, and so forth?
The background is a 9patch image so it should have no problem expanding either.
Thank you
I believe you want something like 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="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EditText"
android:ems="10"
android:id="#+id/editText"
android:layout_weight="0.53" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.53">
<RatingBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/ratingBar"/>
</RelativeLayout>
</LinearLayout>
use Relative Layout to center object:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
</RelativeLayout>
Change LinearLayout's
android:layout_height="wrap_content"
to
android:layout_height="match_parent".
Also, instead of setting "0dp" to EditText and RatingBar set it to match_parent.
[Update]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:numStars="5"
android:rating="4"
android:stepSize="1" />
</LinearLayout>
</LinearLayout>

Add TextView next to buttons? [duplicate]

This question already has answers here:
Add textview to layout?
(4 answers)
Closed 7 years ago.
I have a layout with 4 buttons in the center. I simply want to add a textview next to the four buttons (WITHOUT MOVING THE FOUR BUTTONS FROM THE CENTER), like illustrated here (TextView should be in the place of the red) :
[![enter image description here][1]][1]
Right now, I have the textview in its own linear layout and the four buttons in their own Relative Layout, all in one big relative layout. When ever I try to move the textview to the left of the buttons, the buttons move over to the right. How do I achieve what I illustrated? Here is my XML:
<?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.ruchir.circleswithmap.MainActivity"
android:id="#+id/layout"
android:gravity="center"
android:background="#010101">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="20:00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textColor="#fbfafa"
android:id="#+id/timetext" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Blue"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/bluesquare" />
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Green"
android:layout_below="#+id/Blue"
android:layout_alignLeft="#+id/Blue"
android:layout_alignStart="#+id/Blue"
android:background="#drawable/greensquare" />
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Red"
android:layout_alignTop="#+id/Blue"
android:layout_toRightOf="#+id/Blue"
android:layout_toEndOf="#+id/Blue"
android:background="#drawable/redsquare" />
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Purple"
android:layout_alignBottom="#+id/Green"
android:layout_alignLeft="#+id/Red"
android:layout_alignStart="#+id/Red"
android:background="#drawable/purplesquare" />
</RelativeLayout>
</RelativeLayout>
Put the textView in one Linearlayout and the four buttons in another LinearLayout and then trying Horizontal weights to align then in a single line might work out.
Code could be as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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.ruchir.circleswithmap.MainActivity"
android:id="#+id/layout"
android:gravity="center"
android:background="#010101"
android:weightSum="100">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_weight = "50"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="20:00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textColor="#fbfafa"
android:id="#+id/timetext" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="50">
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Blue"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Green"
android:layout_below="#+id/Blue"
android:layout_alignLeft="#+id/Blue"
android:layout_alignStart="#+id/Blue"
/>
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Red"
android:layout_alignTop="#+id/Blue"
android:layout_toRightOf="#+id/Blue"
android:layout_toEndOf="#+id/Blue"
/>
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Purple"
android:layout_alignBottom="#+id/Green"
android:layout_alignLeft="#+id/Red"
android:layout_alignStart="#+id/Red"
/>
</RelativeLayout>
</LinearLayout>

Android : buttons not visible in scrollview

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

Android layout_weight not working as it should be

I'm trying to set my ImageView and LinearLayout to have half of the screen each. This works fine on tablets, but on smaller screens the image takes up the entire screen (thus, not actually working).
The way I see it is, I have set a layout_weight="1" on both the ImageView and LinearLayout and the layout_width="0dp" on both also. This SHOULD work, as I have researched a lot into this.
My Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/button_text_white">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/image"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="20dp"
android:orientation="vertical"
android:gravity="bottom">
<TextView
android:id="#+id/text_view_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:gravity="center"
android:textSize="24dp"
android:textColor="#color/button_text_white"
android:visibility="invisible"/>
<TextView
android:id="#+id/text_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_up_in_username_text"
android:textColor="#color/color"/>
<EditText
android:id="#+id/edit_text_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:inputType="text"
android:textColor="#color/color"
android:hint="#string/sign_up_in_username_hint"/>
<TextView
android:id="#+id/text_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_up_in_email_text"
android:textColor="#color/color"/>
<EditText
android:id="#+id/edit_text_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:inputType="textEmailAddress"
android:textColor="#color/color"
android:hint="#string/sign_up_in_email_hint"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_up_in_password_text"
android:textColor="#color/color"/>
<EditText
android:id="#+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:inputType="textPassword"
android:textColor="#color/color"
android:hint="#string/sign_up_in_password_hint"/>
<TextView
android:id="#+id/text_view_forgotten_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="right"
android:text="#string/forgotten_password_text"
android:textColor="#color/color"/>
<Button
android:id="#+id/button_sign_up_in"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textColor="#color/color"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Thanks, appreciate any help!
I would say your problem is with your LinearLayout's height being set to match_parent inside of a ScrollView.
As you know, a ScrollView expands to fit its contents - now if you tell its contents to expand as big as its parent (match_parent), then you have a paradox.
Try setting the child of your ScrollView's height to wrap_content instead of match_parent.
There is no use for your ScrollView & it's parent RelativeLayout. Cut down your layout as below.
<LinearLayout>
<ImageView w="1"/>
<ScrollView w="1">
<LinearLayout>
....
</LinearLayout>
</ScrollView>
</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="vertical"
android:background="#color/button_text_white">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/image"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
android:gravity="bottom">
<TextView
android:id="#+id/text_view_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:gravity="center"
android:textSize="24dp"
android:textColor="#color/button_text_white"
android:visibility="invisible"/>
<TextView
android:id="#+id/text_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_up_in_username_text"
android:textColor="#color/color"/>
<EditText
android:id="#+id/edit_text_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:inputType="text"
android:textColor="#color/color"
android:hint="#string/sign_up_in_username_hint"/>
<TextView
android:id="#+id/text_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_up_in_email_text"
android:textColor="#color/color"/>
<EditText
android:id="#+id/edit_text_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:inputType="textEmailAddress"
android:textColor="#color/color"
android:hint="#string/sign_up_in_email_hint"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_up_in_password_text"
android:textColor="#color/color"/>
<EditText
android:id="#+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:inputType="textPassword"
android:textColor="#color/color"
android:hint="#string/sign_up_in_password_hint"/>
<TextView
android:id="#+id/text_view_forgotten_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="right"
android:text="#string/forgotten_password_text"
android:textColor="#color/color"/>
<Button
android:id="#+id/button_sign_up_in"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textColor="#color/color"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

Spreading text to match the parent width

Please have a look at the following code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/textView1"
android:src="#drawable/add_2" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</RelativeLayout>
This generates the following output
To be honest, this is not what I want. The text should "spread" to match the screen width, until it find the image view. I think improving the space between letters might be the solution.
But I would like to know whether there is any better solution, which we can do in Code or XML.
Hey i did some changes in your code. I use LinerLayout instead of Relative Layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3.0f"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:layout_marginLeft="21dp"
/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>
If I get you right, you want the EditText below "Sales Inquiry".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/add_2"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>
Note I replaced the ImageView with drawableRight. It is called compoundDrawable and it could be set to any location near the text of TextView.
There is no need to use RelativeLayout here. Vertical LinearLayout should do the trick.
layout_width="match_parent"
Tells that the View should be spread across the width of the layout.
But if you want EditText in the same line as "Sales Inquiry" then it's done line this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:drawableRight="#drawable/add_2"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>
Use horizontal LinearLayout, set layout_width of first TextView to wrap_content.
The EditText should use layout_weight="1" (only works in LinearLayout) that means it will take as much space available. When using layout_weight in horizontal LinearLayout, set the layout_width to 0dp. If it's vertical LinearLayout, layout_height should be 0dp.

Categories