how can I inject different text into "included" layout items? - java

I have this xml:
<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: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"
tools:context=".MainActivity$PlaceholderFragment"
android:weightSum="1"
android:padding="0.5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#000000"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="#android:drawable/alert_dark_frame"
android:id="#+id/textView1"
android:gravity="center"
android:background="#FF6600"
android:layout_weight="0.20"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="New Text"
android:textColor="#FFFFFF"
android:id="#+id/textView2"
android:background="#B24700"
android:gravity="center"
android:layout_weight="0.30" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="--"
android:textSize="30dp"
android:id="#+id/textView3"
android:gravity="center"
android:background="#FFFFFF"
android:layout_weight="0.60" />
</LinearLayout>
which i include in here:
how can i inject the "new text" to be different in each item.

As I know, this cannnot be done with <include/> itself, the closest thing is to create your own widget class, in the class you inflate from the xml and read a custom attributes and set the value, e.g. text in TextView programmatically.

Related

I'm making a mistake somewhere while making a simple design

no matter what I do in my design, I want to center what I wrote with the photographs, but I cannot do the design, I wonder where I am going wrong I'm learning new and tried to make a design like in the photo
I'll create the rest using the recycler view and card view
Enough explanation, I hope I don't want to confuse you
<androidx.cardview.widget.CardView 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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView6"
android:layout_width="60dp"
android:layout_height="60dp"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Date" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView7"
android:layout_width="60dp"
android:layout_height="60dp"" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Splitted the center part to two linear layouts. Also added android:gravity="center|bottom" to the TextView which contains the date.
<androidx.cardview.widget.CardView 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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView6"
android:layout_width="60dp"
android:layout_height="60dp"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="text" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="text" />
</LinearLayout>
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|bottom"
android:text="Date" />
</LinearLayout>
<ImageView
android:id="#+id/imageView7"
android:layout_width="60dp"
android:layout_height="60dp" />
</LinearLayout> </androidx.cardview.widget.CardView>
Here's how it looks like. I added some colors so it's easier to see where layouts begin and end. The colors are not part of the xml.

How to make Dynamic one row 7 columns that has horizontal Scroll

So i have been working on my android project and the layout has gotten me and I cannot figure out what is the best to do something like this.
Currently my XML looks like this.
What i am trying to do is have 7 columns with one row that is filled with textviews. I was looking at Grid views but it seems like the entire point of the grid is to scroll with more than one row. How do I achieve this layout?
<android.support.constraint.ConstraintLayout
android:id="#+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/foodmenuexample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="123456789123456789123456789123456789123456789123456789123456789" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
The answer is... it depends. For simplicity, you can use a GridLayout, which if you have all your data within the view, this would work well.
However if you want the data to load from a database, or if there is an indeterminate amount of columns, then a gridview is your best bet inside a horizontal scrollview. You can just set the number of rows to 1, and the number of columns programmatically.
Example of that:
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setNumColumns(numOfColumns);
Gridviews are more complicated because you will have to use an adapter, which you can learn about here:
https://developer.android.com/guide/topics/ui/layout/gridview.html
However, if you are looking for static simplicity, here is what you could do:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:rowCount="1"
android:columnCount="7">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Lots of text on this one" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Some text on this one" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Textview3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Textview4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Textview5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Textview6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Textview7" />
</GridLayout>
</HorizontalScrollView>
</android.support.constraint.ConstraintLayout>

Why is android studio reporting URL is not registered?

enter image description here https://docs.google.com/document/d/1_ag-aMsJn408Yef_V0TbVC0GvRIbbuRRPd0uRgsNoM4/edit?usp=sharing
It's in red. I tried to solve it but I still can't resolve the error. :( I already found some similar questions around here but nothing helped me to solve this issue. I hope someone has an idea... ^.^
A valid XML file has only one root node.
Now It included LinearLayout and RelativeLayout.
You must put RelativeLayout and LinearLayout in a reasonable place.
You can do like this,but you need to change the location.
<?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:gravity="center_horizontal"
android:orientation="vertical"
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="sg.edu.tp.uxidcomt.LoginActivity">
<!-- Login progress -->
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone"/>
<ScrollView
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/email_sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/action_sign_in"
android:textStyle="bold"/>
<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.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="#+id/layoutDots"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="200dp"
android:gravity="center"
android:orientation="horizontal"></LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/layoutDots"
android:alpha=".5"
android:background="#android:color/white"/>
<Button
android:id="#+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#null"
android:next="NEXT"
android:textColor="#android:color/white"/>
<Button
android:id="#+id/btn_skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#null"
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:text="SKIP"
android:textColor="#android:color/white"
tools:context=".LoginActivity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>

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

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