How to design a simple form in android? - android

I need to create a simple form in which I give a description like "Name" and give a textbox below it to give the name. But, I am not able to achieve using this code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1.8"
android:text="Name *"
android:textSize="20dp" />
<EditText
android:id="#+id/name_edt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1.8"
android:text="Training Types *"
android:textSize="20dp" />
<EditText
android:id="#+id/trainingtypes_edt"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>

try below code :
<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">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field1"
/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field2"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field3"
/>
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

try this. when orientation is vertical u should give height to 0dp, and when orientation is horizontal u should give width as 0dp if u r specifying weight
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1.8"
android:text="Name *"
android:textSize="20dp" />
<EditText
android:id="#+id/name_edt"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1.8"
android:text="Training Types *"
android:textSize="20dp" />
<EditText
android:id="#+id/trainingtypes_edt"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name *" />
<EditText
android:id="#+id/name_edt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Training Types *" />
<EditText
android:id="#+id/trainingtypes_edt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>

Try this code it working properly
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Name *"
android:textSize="20sp" />
<EditText
android:id="#+id/name_edt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Training Types *"
android:textSize="20sp" />
<EditText
android:id="#+id/trainingtypes_edt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Hope it helps ... :)

Try this code. I did not change much. I just correct some codes. Try to find those and enjoy :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Name *"
android:textSize="20sp" />
<EditText
android:id="#+id/name_edt"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:hint="Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Training Types *"
android:textSize="20sp" />
<EditText
android:id="#+id/trainingtypes_edt"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="Training types"/>
</LinearLayout>
Hope this help you :)

<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.Afieldinc.registeruser.MainActivity$PlaceholderFragment" >
<EditText
android:id="#+id/UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="User Name"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/UserName"
android:layout_alignLeft="#+id/UserName"
android:layout_marginBottom="35dp"
android:text="Name" />
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/UserName"
android:layout_below="#+id/PassWord" android:layout_marginLeft="16dp">
<Button
android:id="#+id/SignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign In" />
<Button android:id="#+id/SignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"/>
</LinearLayout>
<EditText
android:id="#+id/PassWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/UserName"
android:layout_below="#+id/UserName"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/PassWord"
android:layout_below="#+id/UserName"
android:layout_marginTop="19dp"
android:text="Password" />
</RelativeLayout>
try this one out i hope it will help

Related

Android how to set scrollview

For my college I'm making an android app..
The xml code is:--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/back">
<LinearLayout
android:id="#+id/linear_login"
android:layout_width="320dp"
android:layout_height="430dp"
android:background="#drawable/oie_transparent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="10dp"
android:text="College name"
android:textColor="#color/red1"
android:textSize="20dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="14dp"
android:padding="15dp"
android:text="#string/desc"
android:textColor="#color/purple2"
android:textSize="20dp" />
<LinearLayout
android:layout_width="230dp"
android:layout_height="180dp"
android:layout_marginLeft="40dp"
android:padding="10dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:background="#drawable/edit_style"
android:ems="10"
android:hint="student name"
android:textColorHint="#7D0541"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edit_style"
android:layout_marginTop="6dp"
android:ems="10"
android:hint="student roll no"
android:textColorHint="#7D0541">
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Here"
android:layout_marginTop="8dp"
android:layout_gravity="center"
android:background="#drawable/button_state"
android:textColor="#0000A0"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_gravity="center"/>
</LinearLayout>
Here,the 2 edit boxes and one button is there,I want to add a scrollview in there..But When I am adding its showing error every time..Hey guys can anyone tell me where is the problem..
Paste your above line of code in between the scroll line of code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
//Paste your xml code here
</ScrollView>
try this,
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btnEdit"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvExtraCharge"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/sackscontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
Are you looking for something like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/linear_login"
android:layout_width="320dp"
android:layout_height="430dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="10dp"
android:text="College name"
android:textColor="#android:color/darker_gray"
android:textSize="20dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="14dp"
android:padding="15dp"
android:text="description"
android:textColor="#android:color/white"
android:textSize="20dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="230dp"
android:layout_height="180dp"
android:layout_marginLeft="40dp"
android:padding="10dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:ems="10"
android:hint="student name"
android:textColorHint="#7D0541"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:ems="10"
android:hint="student roll no"
android:textColorHint="#7D0541">
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Here"
android:layout_marginTop="8dp"
android:layout_gravity="center"
android:textColor="#0000A0"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_gravity="center"/>
</LinearLayout>
Use ScrollView as parent layout for linear_login and try to remove un-required inner LinearLayout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/back">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="#+id/linear_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/oie_transparent"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="College name"
android:textColor="#color/red1"
android:textSize="20dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:padding="15dp"
android:text="#string/desc"
android:textColor="#color/purple2"
android:textSize="20dp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/edit_style"
android:ems="10"
android:hint="student name"
android:textColorHint="#7D0541"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edit_style"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="student roll no"
android:textColorHint="#7D0541"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Here"
android:layout_marginTop="10dp"
android:background="#drawable/button_state"
android:textColor="#0000A0"/>
</LinearLayout>
</ScrollView>
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher"/>
</LinearLayout>

View not shown in ScrollView

I have the following xml layout:
<?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">
<TextView
android:id="#+id/textHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:textStyle="bold"
android:text="#string/form_header"
android:textSize="10pt" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_below="#+id/textHeader">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textComp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/red"
android:text="#string/form_comp"
android:textSize="7pt" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textComp" >
<TextView
android:id="#+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_title"
android:textSize="7pt" />
<Spinner
android:id="#+id/title_spinner"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/textTitle"
android:focusable="true"
android:padding="10dip" />
<TextView
android:id="#+id/textFname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textTitle"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_fname"
android:textSize="7pt" />
<EditText
android:id="#+id/editFname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_below="#+id/textTitle"
android:layout_toRightOf="#id/textFname"
android:ems="10"
android:maxLength="15" />
<TextView
android:id="#+id/textLname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textFname"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_lname"
android:textSize="7pt" />
<EditText
android:id="#+id/editLname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_below="#+id/textFname"
android:layout_toRightOf="#id/textLname"
android:ems="10"
android:maxLength="15" />
<TextView
android:id="#+id/textTel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textLname"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_tel"
android:textSize="7pt" />
<EditText
android:id="#+id/editTel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_below="#+id/textLname"
android:layout_toRightOf="#id/textTel"
android:ems="10"
android:maxLength="15" />
<TextView
android:id="#+id/textEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textTel"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_email"
android:textSize="7pt" />
<EditText
android:id="#+id/editEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_below="#+id/textTel"
android:layout_toRightOf="#id/textEmail"
android:ems="10"
android:maxLength="25" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout1" >
<TextView
android:id="#+id/textDep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_dep"
android:textSize="7pt" />
<EditText
android:id="#+id/editDep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_toRightOf="#id/textDep"
android:ems="10"
android:maxLength="25" />
<TextView
android:id="#+id/textDest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_dest"
android:layout_below="#+id/textDep"
android:textSize="7pt" />
<EditText
android:id="#+id/editDest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_toRightOf="#id/textDest"
android:layout_below="#+id/textDep"
android:ems="10"
android:maxLength="25" />
<TextView
android:id="#+id/textNpersons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_npersons"
android:layout_below="#+id/editDest"
android:textSize="7pt" />
<EditText
android:id="#+id/editNpersons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_toRightOf="#id/textNpersons"
android:layout_below="#+id/editDest"
android:ems="10"
android:maxLength="3" />
<TextView
android:id="#+id/textDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_date"
android:layout_below="#+id/textNpersons"
android:textSize="7pt" />
<Spinner
android:id="#+id/spYear"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textNpersons"
android:layout_toRightOf="#id/textDate"/>
<Spinner
android:id="#+id/spMonth"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textNpersons"
android:layout_toRightOf="#id/spYear"/>
<Spinner
android:id="#+id/spDay"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textNpersons"
android:layout_toRightOf="#id/spMonth"/>
<TextView
android:id="#+id/textTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_time"
android:layout_below="#+id/spDay"
android:textSize="7pt" />
<Spinner
android:id="#+id/spTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_toRightOf="#id/textTime"
android:layout_below="#+id/spDay"/>
<TextView
android:id="#+id/textComms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="#string/form_comments"
android:layout_below="#+id/spTime"
android:textSize="7pt" />
<EditText
android:id="#+id/editComms"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:inputType="textMultiLine"
android:lines="2"
android:minLines="2"
android:gravity="top|left"
android:maxLines="5"
android:layout_below="#+id/spTime"
android:layout_toRightOf="#id/textComms"
android:scrollbars="vertical"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editComms"
android:layout_centerHorizontal="true"
android:text="#string/form_snd_button" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The ScrollView scrolls fine; however, the last button (#+id/button1) is not shown.
I've tried several combinations with match_parent and wrap_content for the contained layouts but with no luck.
Any ideas?
Thanks in advance.
Edit: Actually I'm writing my app so as to work in both tablets and phones. The above layout that belongs to a fragment works fine for phones.
In the case of tablets, the specific button is not shown. The above layout is attached to the Framelayout:aboutusdet shown in the code below:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/UnitId"/>
<FrameLayout
android:id="#+id/mainfrg"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_below="#+id/adView"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
/>
<FrameLayout
android:id="#+id/aboutusdet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/mainfrg"
android:layout_below="#+id/adView"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/mainfrg" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>

how to align fields in android?

Hi
I am making a simple example in android but My problem is that
my textview and editfield is not align.It mean that
it should look like that
![Name editView
Rollnumber editView
Button on center][2]
It look like this
here is my code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/text_view_boat1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
/>
<EditText
android:id="#+id/entry"
android:hint="add name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/text_view_boat2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll Number"
/>
<EditText
android:id="#+id/entry2"
android:hint="add roll number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:id="#+id/clickme"
/>
</LinearLayout>
Use TableLayout instead :
<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" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:shrinkColumns="1"
android:stretchColumns="1" >
<TableRow
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name :" />
<EditText
android:id="#+id/companyIdEditText_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:enabled="false"/>
</TableRow>
<TableRow
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RollNumber :" />
<EditText
android:id="#+id/companyNameEditText_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</TableRow>
</TableLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ClickMe"/>
</LinearLayout>
You can use the attribute weight. Set the the width of the row's chrildren to 0dp
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/text_view_boat2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Roll Number"
/>
<EditText
android:id="#+id/entry2"
android:hint="add roll number"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
/>
</LinearLayout>
try this one...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text_view_boat1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="05dp"
android:text="Name" />
<EditText
android:id="#+id/entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/text_view_boat1"
android:layout_marginRight="05dp"
android:layout_toRightOf="#id/text_view_boat1"
android:hint="add name" />
<LinearLayout
android:id="#+id/lin_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/entry"
android:layout_marginTop="05dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/text_view_boat2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll Number" />
<EditText
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="add roll number" />
</LinearLayout>
<Button
android:id="#+id/clickme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lin_lay"
android:layout_marginTop="05dp"
android:text="Click Me" />

Android layout not as expected

So I am a beginner at layouts in Android and I am learning RelativeLayout. I am attempting to make the following:
However, all I get in my virtual device is the name field taking up 100% of the space (width and height).
Here is my XML:
<?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:orientation="vertical"
android:id="#+id/root">
<EditText android:id="#+id/name"
android:layout_alignParentLeft="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint = "Name"/>
<EditText android:id="#+id/phone"
android:layout_below="#id/name"
android:layout_alignParentRight="true"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:hint="Phone" />
<EditText android:id="#+id/email"
android:layout_below="#id/phone"
android:layout_alignParentRight="true"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:hint="Email" />
<EditText android:id="#+id/dob"
android:layout_below="#id/email"
android:layout_alignParentRight="true"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:hint="D.O.B." />
<EditText android:id="#+id/address"
android:layout_below="#id/phone"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/dob"
android:layout_weight="0.6"
android:layout_height="0dp"
android:layout_width="match_parent"
android:hint="Address" />
<Button android:id="#+id/submit"
android:layout_weight="1.0"
android:layout_below="#id/address"
android:layout_height="match_parent"
android:layout_width="0dp"
android:text="Submit"/>
</RelativeLayout>
What is going wrong?
Here is the xml for your output.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:hint="Name" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true
android:layout_weight="-10"
android:hint="Phone" />
<EditText
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/phone"
android:layout_weight="0.4"
android:hint="Email" />
<EditText
android:id="#+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/email"
android:layout_weight="0.4"
android:hint="D.O.B." />
<EditText
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/phone"
android:layout_toLeftOf="#id/dob"
android:layout_weight="0.6"
android:hint="Address" />
</RelativeLayout>
<Button
android:id="#+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
Change the layout_height for the name element to:
android:layout_height="wrap_content"
Change your Layout XML to the above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:hint="Name" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/name"
android:orientation="horizontal"
android:layout_marginBottom="60dp"
android:weightSum="1" >
<EditText
android:id="#+id/address"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:hint="Address" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.4"
android:weightSum="3" >
<EditText
android:id="#+id/phone"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Phone" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Email" />
<EditText
android:id="#+id/dob"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="D.O.B." />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/submit"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:text="Submit" />
</RelativeLayout>
Hope that helps:)

Layout alignment Query {Center Horizontal + Center Vertical}

Must be very simple to solve but I'm not able to solve this.
I want to align the "top image and log_in_box", so the whole thing appear in exact center (Horizontal+Vertical center) of the screen.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal">
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="#drawable/ic_launcher"
android:contentDescription="" />
<LinearLayout android:id="#+id/log_in_box" android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="User ID"
android:inputType="text" />
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"
android:hint="Password" android:inputType="textPassword" />
<CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true"
android:text="Remember Me" android:layout_marginTop="10dp" />
<Button android:id="#+id/log_in" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:text="Log In" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Current Look
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true" >
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="#drawable/ic_launcher"
android:contentDescription="" />
<LinearLayout android:id="#+id/log_in_box" android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="User ID"
android:inputType="text" />
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"
android:hint="Password" android:inputType="textPassword" />
<CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true"
android:text="Remember Me" android:layout_marginTop="10dp" />
<Button android:id="#+id/log_in" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:text="Log In" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="#drawable/ic_launcher"
android:contentDescription="" />
<LinearLayout android:id="#+id/log_in_box" android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="User ID"
android:inputType="text" />
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"
android:hint="Password" android:inputType="textPassword" />
<CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true"
android:text="Remember Me" android:layout_marginTop="10dp" />
<Button android:id="#+id/log_in" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:text="Log In" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription=""
android:src="#drawable/ic_launcher" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="User ID"
android:inputType="text" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Password"
android:inputType="textPassword" />
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="true"
android:text="Remember Me" />
<Button
android:id="#+id/log_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Log In" />

Categories

Resources