Adding a Line Separator between two TextViews in Android XML - android

Recently I started practicing Android Development and I am stuck at a very basic problem. Since I am a beginner in this field, I am asking for your help.
<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"
tools:context=".MainActivity">
<TextView
android:text="Name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="18sp"
/>
<EditText
android:id="#+id/player_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="#integer/abc_max_action_buttons"
/>
<TextView
android:text="Name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="18sp"
/>
<EditText
android:id="#+id/player_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="#integer/abc_max_action_buttons"
/>
</LinearLayout>
In this XML scheme, I am trying to put a text followed by text-space and repeating it again.
e.g. Name1 ___________________ //Next Line//
Name2 ___________________
But the problem is that Name2 is not going into the next line and staying in the same line .
Please tell me how to fix it.
Thanks in advance

In these type of scenarios I create a layout within a layout.
So each line of yours will be a layout and hence. You can only those elements in that line.
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="57dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/lblName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Name"
android:textSize="20dp" />
<EditText
android:id="#+id/txtName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ems="10"
android:gravity="left" >
<requestFocus />
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="57dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/lblName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Name"
android:textSize="20dp" />
<EditText
android:id="#+id/txtName1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ems="10"
android:gravity="left" >
</EditText>
</RelativeLayout>

You need to add orientation = vertical and I've changed your widths to match parent.
<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"
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=".MainActivity">
<TextView
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Name1" android:textColor="#000000" android:textSize="18sp"/>
<EditText
android:id="#+id/player_name1" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="#integer/abc_max_action_buttons"/>
<TextView
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Name2" android:textColor="#000000" android:textSize="18sp"/>
<EditText
android:id="#+id/player_name2" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="#integer/abc_max_action_buttons"/>
</LinearLayout>
Not sure exactly what height space you want, but have used your dimensions.

Related

Android XML ScrollView not scrolling

I am trying to implement a scrollview in one of my XML files but it doesnt seem to be working. It is cutting out the bottom 3 spinners and the last button in the XML. I guessed this is because I needed to scroll down to see them but I am unable to scroll?
Can anyone seem the issue?
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="#+id/title"
android:text="New Recipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
<EditText
android:id="#+id/recipeName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:hint="Enter Recipe Name"/>
<ListView
android:id="#+id/ingredientsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/recipeName"
android:textColor="#000000"
android:layout_above="#+id/showIngredientDialog"
android:minHeight="150px">
</ListView>
<Button
android:id="#+id/showIngredientDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Ingredient"
android:layout_above="#+id/showDirectionDialog"
android:layout_alignParentStart="true"
android:layout_marginBottom="145dp" />
<ListView
android:id="#+id/directionsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_above="#+id/showDirectionDialog"
android:layout_alignParentStart="true"
android:layout_marginBottom="37dp">
</ListView>
<Button
android:id="#+id/showDirectionDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Direction"
android:layout_marginBottom="79dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/showIngredientDialog" />
<Spinner
android:id="#+id/timeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/showDirectionDialog"
android:entries="#array/time"/>
<Spinner
android:id="#+id/dietarySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/dietary"
android:layout_below="#+id/showDirectionDialog"
android:layout_toRightOf="#+id/timeSpinner" />
<Spinner
android:id="#+id/heatSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/heat"
android:layout_below="#+id/showDirectionDialog"
android:layout_toRightOf="#+id/dietarySpinner" />
<Button
android:id="#+id/saveRecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Recipe"
android:layout_below="#+id/timeSpinner"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/showDirectionDialog"
android:layout_marginBottom="25dp" />
</RelativeLayout>
</ScrollView>
Your facing this issue because you are using ScrollView in which having ListView. Both having default scroll property which conflicting the issue. Check this SO post for resolving the issue.
Set the relativeLayout height to "wrap_content".
<?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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<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">
<TextView
android:id="#+id/title"
android:text="New Recipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
<EditText
android:id="#+id/recipeName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:hint="Enter Recipe Name"/>
<ListView
android:id="#+id/ingredientsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/recipeName"
android:textColor="#000000"
android:layout_above="#+id/showIngredientDialog"
android:minHeight="150px">
</ListView>
<Button
android:id="#+id/showIngredientDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Ingredient"
android:layout_above="#+id/showDirectionDialog"
android:layout_alignParentStart="true"
android:layout_marginBottom="145dp" />
<ListView
android:id="#+id/directionsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_above="#+id/showDirectionDialog"
android:layout_alignParentStart="true"
android:layout_marginBottom="37dp">
</ListView>
<Button
android:id="#+id/showDirectionDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Direction"
android:layout_marginBottom="79dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/showIngredientDialog" />
<Spinner
android:id="#+id/timeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/showDirectionDialog"
android:entries="#array/time"/>
<Spinner
android:id="#+id/dietarySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/dietary"
android:layout_below="#+id/showDirectionDialog"
android:layout_toRightOf="#+id/timeSpinner" />
<Spinner
android:id="#+id/heatSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/heat"
android:layout_below="#+id/showDirectionDialog"
android:layout_toRightOf="#+id/dietarySpinner" />
<Button
android:id="#+id/saveRecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Recipe"
android:layout_below="#+id/timeSpinner"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/showDirectionDialog"
android:layout_marginBottom="25dp" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
you can't put two scrolling widgets inside one another the will cause major issues even with normal scrolling
if there is no way to change this layout design please force a height to each listview but still the scroll will be buggy and you have to set extra margin on the side of list for the user to be able to scroll the parent scroll view
or try to break down the design if you can maybe tabs one for normal data and a tab for each list
Because views of total heigt not bigger than screen height.
You can see with coding custom height. I hope below code help you.
I just change listview height.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="#+id/title"
android:text="New Recipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
<EditText
android:id="#+id/recipeName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:hint="Enter Recipe Name"/>
<ListView
android:id="#+id/ingredientsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/recipeName"
android:textColor="#000000"
android:layout_above="#+id/showIngredientDialog"
android:minHeight="150px">
</ListView>
<Button
android:id="#+id/showIngredientDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Ingredient"
android:layout_above="#+id/showDirectionDialog"
android:layout_alignParentStart="true"
android:layout_marginBottom="145dp" />
<ListView
android:id="#+id/directionsList"
android:layout_width="fill_parent"
android:layout_height="1000dp"
android:textColor="#000000"
android:layout_above="#+id/showDirectionDialog"
android:layout_alignParentStart="true"
android:layout_marginBottom="37dp">
</ListView>
<Button
android:id="#+id/showDirectionDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Direction"
android:layout_marginBottom="79dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/showIngredientDialog" />
<Spinner
android:id="#+id/timeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/showDirectionDialog"
/>
<Spinner
android:id="#+id/dietarySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/showDirectionDialog"
android:layout_toRightOf="#+id/timeSpinner" />
<Spinner
android:id="#+id/heatSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/showDirectionDialog"
android:layout_toRightOf="#+id/dietarySpinner" />
<Button
android:id="#+id/saveRecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Recipe"
android:layout_below="#+id/timeSpinner"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/showDirectionDialog"
android:layout_marginBottom="25dp" />
</RelativeLayout>

Android: Layout keeps moving up and down when putting in Text in EditText

I'm new to android development, and I think I messed up somewhere with the XML. Attached is the relevant code. What happens is when I try to enter in text into the EditText, the editText keeps shifting up and down as I try to type into it. It's lagging my phone, and I suspect I made a mistake with the layout.
I tried to use LinearLayout setup vertically and horizontally for a cleaner format than relative, and I think it the problem is with the weight, but I've been fiddling with the values for weight and I can't seem to figure this out.
<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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/summoner1"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/champSpinner1"
android:entries="#array/champions"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:entries="#array/positions"
android:layout_gravity="right" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/summoner2"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/champSpinner2"
android:entries="#array/champions"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner2"
android:entries="#array/positions"
android:layout_gravity="right" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/summoner3"
android:layout_gravity="center_horizontal"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/champSpinner3"
android:entries="#array/champions"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner3"
android:entries="#array/positions"
android:layout_gravity="right" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/summoner4"
android:layout_gravity="center_horizontal"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Spinner
android:layout_width= "wrap_content"
android:layout_height="wrap_content"
android:id="#+id/champSpinner4"
android:entries="#array/champions"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner4"
android:entries="#array/positions"
android:layout_gravity="right" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dodge?"
android:id="#+id/checkButton"
android:onClick="onClickDodge"
android:layout_gravity="center_horizontal" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

Align LinearLayout inside a RelativeLayout issue

How can i align the "LoginBox" LinearLayout to its parent (that is a RelativeLayout) both at left/right and bottom? I tried to insert:
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
but it doesn't do the trick! Infact the linearlayout is not properly aligned to the margin of the screen! :(
This is my XML 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:background="#drawable/sfondologin"
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=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/stemmatrasparente" />
<LinearLayout
android:id="#+id/LoginBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/usernametxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/sfondobuttonedittext"
android:drawableLeft="#drawable/usericon"
android:ems="10"
android:gravity="center_horizontal|center"
android:paddingLeft="15dp"
android:imeOptions="actionNext"
android:singleLine="true"
android:textSize="18dp"
android:typeface="sans" >
<requestFocus />
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<EditText
android:id="#+id/passwordtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/sfondobuttonedittext"
android:drawableLeft="#drawable/lockicon"
android:ems="10"
android:gravity="center_vertical|center"
android:inputType="textPassword"
android:paddingLeft="15dp"
android:imeOptions="actionDone"
android:textSize="18dp"
android:typeface="sans" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.29" >
<TextView
android:id="#+id/forgotpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Dimenticato la password?"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="#dimen/value12dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/loginbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/sfondobuttonlogin"
android:text="Login"
android:textColor="#color/white" />
</RelativeLayout>
<Button
android:id="#+id/registerbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center|end"
android:background="#drawable/sfondobuttonrb2"
android:text="Registrati"
android:textColor="#color/white" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_weight="0.01"
android:background="#00000000">
</RelativeLayout>
</RelativeLayout>
Thank you in advance!
EDIT:
The first screen is what i have now! While the second, edited with photoshop (sorry), is what i want to get!
Remove
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
these lines form your outermost ReletiveLayout.
Thats it!
Try this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<Button
android:id="#+id/registerbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center|end"
android:background="#drawable/sfondobuttonrb2"
android:text="Registrati"
android:textColor="#color/white" />
</LinearLayout>
I put a LinearLayout before the button, and added android:gravity="bottom" to the LinearLayout. This should do it.
Remove your top layout's right, left and bottom padding:
<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:background="#drawable/sfondologin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >

Display one TextView in bottom android

In my application, i want to display one TextView in bottom of the Screen with copyright note.
I used following xml code.
In my code TextView couldn't display in bottom of the screen.
I suppose I'm not doing right but I know why.
I would greatly appreciate your help.
Thanks in advance.
<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:background="#bfceee"
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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="#string/name"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="#+id/rt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btnSend"
android:layout_alignRight="#+id/btnSend"
android:layout_gravity="center_horizontal"
android:layout_marginTop="140dp"
android:inputType=""
android:textColor="#000"
android:textSize="20sp" >
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="210dp"
android:background="#EAEAAE"
android:text="#string/send"
android:textColor="#5C3317"
android:textSize="20sp"
android:textStyle="bold"
android:width="150sp" />
<TextView
android:id="#+id/Copyright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:textStyle="bold" >
</TextView>
</RelativeLayout>
Use this android:layout_below="#+id/btnSend".
<TextView
android:id="#+id/Copyright"
android:layout_below="#+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Hi"
android:layout_marginTop="15dp"
android:textStyle="bold" >
</TextView>
If you want to bottom of the screen then use
android:layout_alignParentBottom="true"
Use this in textView instead of gravity-
android:layout_alignParentBottom="true"
Don't use gravity, use:
android:layout_alignParentBottom="true"
See RelativeLayout.LayoutParams for more information
Try this way,hope this will help you to solve your problem.
Here i have used LinearLayout instead of RelativeLayout becz i have seen top margin which not proper work on various devices so i have implement weight in LinearLayout.
<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:background="#bfceee"
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=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<EditText
android:id="#+id/rt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<Button
android:id="#+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EAEAAE"
android:text="#string/send"
android:textColor="#5C3317"
android:textSize="20sp"
android:textStyle="bold"
android:width="150sp" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/Copyright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textStyle="bold" >
</TextView>
</LinearLayout>

XML Android align TextView

This is my first try to create an android application.
I am trying to create a layout right now and am stuck allready.
Here is what I am trying to accomplish:
Jaarverbruik
2013 - 2014
Afrekendatum Peildatum
So, the word Afrekendatum should be aligned to the left, the word Peildatum should be aligned to the right of the screen.
This is my current code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<TextView
android:id="#+id/periode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:text="#string/periode" />
<TextView
android:id="#+id/afrekendatum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/periode"
android:layout_alignParentLeft="true"
android:text="#string/afrekendatum" />
<TextView
android:id="#+id/peildatum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/periode"
android:layout_toRightOf="#id/afrekendatum"
android:layout_alignParentRight="true"
android:text="#string/peildatum" />
</RelativeLayout>
Sadly, the words are displayed directly behind each other.
What is the best way to fix this?
You don't need android:layout_toRightOf="#id/afrekendatum" because you have declared android:layout_alignParentRight="true". Remove it and it will work fine.
You should use something like this.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Try this...
You have to set TextView's gravity.
<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:background="#2b579a"
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=".MainActivity1" >
<TextView
android:id="#+id/periode"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/periode"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/afrekendatum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="start"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_below="#+id/periode"
android:text="#string/afrekendatum" />
<TextView
android:id="#+id/peildatum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/periode"
android:gravity="end"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_toRightOf="#id/afrekendatum"
android:text="#string/peildatum" />
I also attached screenshot here Genius!

Categories

Resources