I try to make view under other view progmaly my problem is that its dont seems to work.
The views not going below they view in my code.
The firstText and secondText arent going under the ID's i put.
Thank for helping :)
public void setHelper() {
linear1 = (LinearLayout) findViewById(R.id.checkHelp1);
linear2 = (LinearLayout) findViewById(R.id.layout2);
firstText = (TextView) findViewById(R.id.textView4);
secondText = (TextView) findViewById(R.id.timer);
linear1.setVisibility(View.GONE);
linear2.setVisibility(View.GONE);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, R.id.checkNeedHelp);
firstText.setLayoutParams(lp2);
lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, R.id.addNewExercise);
lp2.setMargins(0, 25, 0, 0);
secondText.setLayoutParams(lp2);
}
XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<EditText
android:id="#+id/editRoutine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/routines"
android:ems="10"
android:hint="add new routine"
android:textColorHint="#color/black" >
</EditText>
<TextView
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/layout2"
android:text="Routine exercsies" />
<Spinner
android:id="#+id/routineExercises"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/timer"
android:layout_toLeftOf="#+id/deleteExerciseFromRoutine" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/routineExercises"
android:text="Choose exercise to add to selected routine" />
<Spinner
android:id="#+id/exerciseForRoutine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_toLeftOf="#+id/addExercisesToRoutine" />
<ImageButton
android:id="#+id/addExercisesToRoutine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/exerciseForRoutine"
android:layout_alignParentRight="true"
android:src="#android:drawable/ic_input_add" />
<ImageButton
android:id="#+id/addNewRoutine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/routines"
android:src="#android:drawable/ic_input_add" />
<ImageButton
android:id="#+id/deleteExerciseFromRoutine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignParentRight="true"
android:src="#drawable/close" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/deleteExerciseFromRoutine"
android:layout_alignParentRight="true" />
<ImageButton
android:id="#+id/removeRoutine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/routines"
android:layout_toRightOf="#+id/routineExercises"
android:src="#drawable/close" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Edit Routines"
android:textSize="30sp" />
<LinearLayout
android:id="#+id/checkHelp1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkNeedHelp"
android:background="#color/gray"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manage routines"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add new routines or delete one from the list" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editRoutine"
android:layout_marginTop="25sp"
android:background="#color/gray"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Routine exercies"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add exercies to selected routine or delete one" />
</LinearLayout>
<LinearLayout
android:id="#+id/swipeLayOut2"
android:layout_width="110sp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/Black"
android:orientation="vertical"
android:visibility="gone" >
<Button
android:id="#+id/goHomePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="home page"
android:textSize="15sp" />
<Button
android:id="#+id/goCalendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calendar"
android:textSize="15sp" />
<Button
android:id="#+id/goLive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="live workout"
android:textSize="15sp" />
<Button
android:id="#+id/goToday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Today log"
android:textSize="15sp" />
<Button
android:id="#+id/goProgram"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Program plan"
android:textSize="15sp" />
<Button
android:id="#+id/goPersonal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pesonal detials"
android:textSize="15sp" />
<Button
android:id="#+id/goWorkout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set workout"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkHelp1"
android:text="Routines" />
<Spinner
android:id="#+id/routines"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView4"
android:layout_toLeftOf="#+id/removeRoutine" />
<CheckBox
android:id="#+id/checkNeedHelp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="25sp"
android:text="Need help?"
android:textSize="13sp" />
<com.example.workoutlog.VerticalTextView
android:id="#+id/swipeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/swipeLayOut2"
android:background="#color/red"
android:text="swipe right"
android:textSize="18sp" />
</RelativeLayout>
Layouts process their children in a top down fashion so any layout_above, layout_below etc must refer to a view physically above it in the layout file.
So before using layout_below put the xml of relevant view.
An example is below:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/contribution_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:textSize="72sp"
android:text="77"
tools:text="77"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/contribution_amount"
android:paddingTop="12sp"
android:textSize="28sp"
android:text="$"
tools:text="$"/>
</RelativeLayout>
Notice that the TextView with layout_toStartOf references a view that has been defined before itself. If I had put the TextView with the layout_toStartOf element before the contribution_amount TextView the layout would not work.
Related
I have an app where the scrolView (relative layout inside it) take up the whole screen (fill_parent). But instead of ending at the end of the phone i would like to have a fragment at the bottom and have the ScrollView layout to stop at the top of the fragment. I think it probably has to do with the height but can't really figure out the code. Please look at my imgur image to get my idea. Imgur_Image, press here
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"><![CDATA[
tools:context="c.timno.smsgsm.MainActivity">
]]>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="status?"
android:id="#+id/skicka"
android:hint="Status?"
android:layout_marginTop="38dp"
android:layout_below="#+id/buttonanvandare"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="på"
android:id="#+id/skickaaon"
android:layout_marginEnd="36dp"
android:layout_below="#+id/textViewlarm"
android:layout_alignEnd="#+id/skicka" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Av"
android:id="#+id/skickaaoff"
android:layout_below="#+id/textViewlarm"
android:layout_toEndOf="#+id/skickarela1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relä 1"
android:id="#+id/skickarela1"
android:layout_alignTop="#+id/skickarela2"
android:layout_alignStart="#+id/skickaaon" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relä 2"
android:id="#+id/skickarela2"
android:layout_below="#+id/textView"
android:layout_toEndOf="#+id/skickarela1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aktivera"
android:gravity="center"
android:textSize="29dp"
android:id="#+id/textView"
android:layout_below="#+id/skickaaon"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Larm"
android:textSize="29dp"
android:id="#+id/textViewlarm"
android:layout_below="#+id/skicka"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Avaktivera"
android:textSize="29dp"
android:id="#+id/textView3"
android:autoText="false"
android:layout_below="#+id/textView"
android:layout_alignEnd="#+id/buttoninstallator"
android:layout_marginTop="46dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relä 1"
android:id="#+id/skickatrela1off"
android:layout_below="#+id/textView3"
android:layout_toStartOf="#+id/skickatrela2off" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relä 2"
android:id="#+id/skickatrela2off"
android:layout_alignTop="#+id/skickatrela1off"
android:layout_alignStart="#+id/skickarela2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Användare"
android:id="#+id/buttonanvandare"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/skickaaoff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Installation"
android:id="#+id/buttoninstallator"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/buttonanvandare" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="status?"
android:id="#+id/statustxt"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="aon"
android:id="#+id/aon"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="aoff"
android:id="#+id/aoff"
android:layout_alignParentStart="true"
android:layout_below="#+id/statustxt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="on*1*"
android:id="#+id/rela1txxt"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="On*2*"
android:id="#+id/rela2onn"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="off*2*"
android:id="#+id/rela2offtxt"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="off*1*"
android:id="#+id/offrela1txt"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Värmestyrning"
android:textSize="33dp"
android:id="#+id/textView8"
android:layout_below="#+id/skickatrela1off"
android:layout_marginTop="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Komfortvärme"
android:textSize="29dp"
android:id="#+id/textView9"
android:layout_below="#+id/textView8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heaton"
android:id="#+id/btnHeaton"
android:layout_below="#+id/textView9"
android:layout_alignEnd="#+id/skickatrela1off" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heatoff"
android:id="#+id/btnHeatoff"
android:layout_below="#+id/textView9"
android:layout_alignStart="#+id/skickatrela2off" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Underhållsvärme"
android:textSize="29dp"
android:id="#+id/textView10"
android:layout_below="#+id/btnHeaton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Termon"
android:id="#+id/btnTermon"
android:layout_below="#+id/textView10"
android:layout_toStartOf="#+id/btnHeatoff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Termoff"
android:id="#+id/btnTermoff"
android:layout_alignBottom="#+id/btnTermon"
android:layout_toEndOf="#+id/btnTermon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Heaton"
android:id="#+id/txtHeaton"
android:layout_alignTop="#+id/statustxt"
android:layout_alignEnd="#+id/statustxt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Heatoff"
android:id="#+id/txtHeatoff"
android:layout_alignBottom="#+id/statustxt"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Termon"
android:id="#+id/txtTermon"
android:layout_alignBaseline="#+id/aoff"
android:layout_alignBottom="#+id/aoff"
android:layout_alignEnd="#+id/statustxt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Termoff"
android:id="#+id/txtTermoff"
android:layout_alignTop="#+id/statustxt"
android:layout_alignEnd="#+id/txtHeatoff" />
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="c.timno.smsgsm20.SecondFragment"
android:id="#+id/fragmentsms"
android:layout_below="#+id/textView8"
android:layout_alignParentStart="true"
tools:layout="#layout/second_layout"
android:layout_above="#+id/textView10" />
</RelativeLayout>
</ScrollView>
I would suggest putting the ScrollView and the Fragment inside a new top-level element and then using the respective method within that element to adjust the size and position of each.
For instance, using a RelativeLayout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
...>
<ScrollView
android:id="#+id/scrollview"
android:layout_above="#+id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
...>
<RelativeLayout
android:paddingBottom="50dp"
...>
...
</RelativeLayout>
...
</ScrollView>
<Fragment
android:id="#+id/frag"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp"
...>
...
</Fragment>
</RelativeLayout>
(Note that the padding on the bottom of the RelativeLayout inside the ScrollView matches the height of the Fragment so that contents of the ScrollView don't get hidden behind the Fragment)
You could also do this using LinearLayout as the parent element with an attribute of android:orientation="vertical", and the ScrollView and Fragment each getting an appropriate layout_weight attribute, but that's not as clean imho, so I'll leave it to you if you'd prefer to do that.
<ScrollView>
<RelativeLayout>
//main content
</RelativeLayout>
<FrameLayout android:alignParentBottom="true"/> load fragment here dynamically(or not)
</ScrollView>
This worked for me. Check this question out aswell
I need to set a part of my layout dynamically the number of times i receive a value of data.
For example if receive data=3 I want that my same layout part appears three times but rest of the layout remains same.
I tried inflating it but then it overlaps on my rest of the layout.
This is the layout I want to add recursively.
link.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/offers"
android:layout_below="#id/visits_remaining"
android:layout_height="wrap_content"
android:layout_width="match_parent" >
<TextView
android:id="#+id/tier_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Outlet Offer"/>
<TextView
android:id="#+id/tier_visits"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1 - 5 visits"/>
<TextView
android:id="#+id/tv1"
android:layout_below="#id/tier_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Hello"/>
<ImageView
android:id="#+id/v1"
android:layout_below="#id/tier_name"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:src="#drawable/arrow" />
<TextView
android:id="#+id/tv2"
android:layout_below="#id/tv1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="World"
android:visibility="gone"/>
<TextView
android:id="#+id/tv3"
android:layout_below="#id/tv2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="World"
android:visibility="gone"/>
<TextView
android:id="#+id/tv4"
android:layout_below="#id/tv3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="World"
android:visibility="gone"/>
<TextView
android:id="#+id/tv5"
android:layout_below="#id/tv4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="World"
android:visibility="gone"/>
</RelativeLayout>
This is the layout in which I want to add it above last two buttons.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/outlet_names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="14dp"
android:textSize="25sp"
android:text="Outlet Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:id="#+id/rule"
android:layout_below="#id/outlet_names"
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:layout_height="0.02mm"
android:background="#ffe6e1d4" />
<TextView
android:id="#+id/address"
android:layout_marginTop="10dp"
android:layout_below="#id/rule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:hint="address" />
<TextView
android:id="#+id/landline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/address"
android:textSize="15sp"
android:autoLink="phone"
android:hint="landline" />
<TextView
android:id="#+id/mobile1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/address"
android:layout_toRightOf="#id/landline"
android:layout_marginLeft="5dp"
android:textSize="15sp"
android:autoLink="phone"
android:hint="mobile"/>
<ImageView
android:id="#+id/outlets"
android:layout_below="#id/mobile1"
android:layout_height="100dp"
android:layout_marginTop="7dp"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/prog_name"
android:layout_below="#id/outlets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:text="Program Name" />
<TextView
android:id="#+id/validity"
android:layout_below="#id/prog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:text="Program ends :"/>
<TextView
android:id="#+id/end"
android:layout_below="#id/prog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:text="Aug 5,2014"
android:textSize="15sp" />
<ProgressBar
android:id="#+id/progress"
android:layout_below="#id/validity"
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="10dp"/>
<TextView
android:id="#+id/visits_remaining"
android:layout_below="#id/progress"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2 visits to go for the next reward"/>
<Button
android:id="#+id/more"
android:layout_below="#+id/offers2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="See more" />
<Button
android:id="#+id/terms"
android:layout_below="#id/more"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Terms and Conditions" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
place a ViewGroup in where you want in xml and add your view to it dynamically.
onclick a button or... below function call
final View addedView = getLayoutInflater().inflate(R.layout.someId,
yourViewGroup, false);
Button btn= (Button) addedView.findViewById(R.id.someId);
ImageButton imgBtn= (ImageButton) addedView.findViewById(R.id.someId);
addedView .setTag("a unique id");
btn.setText("click me to do something");
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int thisViewId = (Integer) addedView.getTag();//use this for indicate which view is click
//do some thing
}
});
imgBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
yourViewGroup.removeView(addedView);//delete just this addedView
}
});
yourViewGroup.addView(addedView);
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" />
<!--some tags here-->
<HorizontalScrollView
android:id="#+id/doc_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#cccccc" >
<LinearLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<!--views will add here-->
</LinearLayout>
</HorizontalScrollView>
<!--some tags here-->
</RelativeLayout>
addedView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical"
android:background="#eee" >
<Button
android:id="#+id/chapter_page"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="" />
<ImageButton
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_delete"
android:background="#00000000" />
</LinearLayout>
if use above code you will see your Horizontal view get child on every time run function.
Take linear layout on above button
<?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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/outlet_names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="Outlet Name"
android:textSize="25sp" />
<View
android:id="#+id/rule"
android:layout_width="fill_parent"
android:layout_height="0.02mm"
android:layout_marginTop="10dp"
android:background="#ffe6e1d4" />
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="address"
android:textSize="15sp" />
<TextView
android:id="#+id/mobile1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:hint="mobile"
android:textSize="15sp" />
<ImageView
android:id="#+id/outlets"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="7dp" />
<TextView
android:id="#+id/prog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Program Name"
android:textSize="15sp" />
<TextView
android:id="#+id/validity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Program ends :"
android:textSize="15sp" />
<TextView
android:id="#+id/end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Aug 5,2014"
android:textSize="15sp" />
<ProgressBar
android:id="#+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/visits_remaining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="2 visits to go for the next reward" />
<LinearLayout
android:id="#+id/linDynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/more"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="See more" />
<Button
android:id="#+id/terms"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Terms and Conditions" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
write in your main activty
linDynamic = (LinearLayout) mView.findViewById(R.id.linDynamic);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < 4; i++)
{
TextView txtDemo = new TextView(getActivity());
txtDemo .setTextSize(16);
txtDemo .setLayoutParams(lp);
txtDemo .setId(i);
lp.setMargins(0, 10, 0, 0);
txtDemo .setPadding(20, 10, 10, 10);
txtDemo .setText("Text View"+ i);
linDynamic .addView(txtDemo );
}
}
Add the ScrollView below where you want (in your case below the two buttons)
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/add_dynamic_layout_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
</ScrollView>
Then add the layout dynamically through code
//add dynamic layout below to the clicked layout
final RelativeLayout newView = (RelativeLayout)getLayoutInflater().inflate(R.layout.dynamic_layout, null);
newView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
dynamicLayoutContainer.addView(newView);
ImageView removeView = (ImageView)dynamicLayoutContainer.findViewById(R.id.dynamic_image_remove);
removeView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dynamicLayoutContainer.removeView(newView);
}
});
I hope this will help you.
I am going through a peculiar problem. I have multiple EditText inside a RelativeLayout which is ultimately inside a ScrollView. My problem is that whenever I edit the second(or third and so on) EditText, it loses focus to the first EditText inside the ScrollView and this first EditText gets the keyboard input.
Here goes the xml layout:
<?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" >
<RelativeLayout
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/particularsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:id="#+id/particularsTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PARTICULARS"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
/>
<View
android:id="#+id/bar1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:layout_below="#+id/particularsTextView"
/>
<RelativeLayout
android:id="#+id/particularsParentRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bar1"
android:layout_marginTop="10dp"
>
<EditText
android:id="#+id/nameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_alignLeft="#+id/confirmPasswordEditText"
/>
<TextView
android:id="#+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NAME"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
android:layout_alignBottom="#+id/nameEditText"
android:layout_marginBottom="15dp"
android:textSize="12sp"
/>
<TextView
android:id="#+id/passwordTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASSWORD"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
android:layout_alignBottom="#+id/passwordEditText"
android:layout_marginBottom="15dp"
android:textSize="12sp"
/>
<EditText
android:id="#+id/passwordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/nameEditText"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/passwordTextView"
android:layout_alignParentRight="true"
android:nextFocusUp="#+id/passwordEditText"
android:layout_alignLeft="#+id/confirmPasswordEditText"
/>
<TextView
android:id="#+id/confirmPasswordTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONFIRM PASSWORD"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
android:layout_alignBottom="#+id/confirmPasswordEditText"
android:layout_marginBottom="15dp"
android:textSize="12sp"
/>
<EditText
android:id="#+id/confirmPasswordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/passwordEditText"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/confirmPasswordTextView"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/birthdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIRTHDAY"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
android:layout_alignBottom="#+id/birthdayEditText"
android:layout_marginBottom="15dp"
android:textSize="12sp"
/>
<EditText
android:id="#+id/birthdayEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/confirmPasswordEditText"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/birthdayTextView"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/confirmPasswordEditText"
/>
<TextView
android:id="#+id/genderTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GENDER"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
android:layout_marginTop="18dp"
android:layout_below="#+id/birthdayTextView"
android:textSize="12sp"
/>
<TextView
android:id="#+id/contactDetailsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/genderTextView"
android:text="CONTACT DETAILS"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
android:layout_marginTop="50dp"
/>
<View
android:id="#+id/bar2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:layout_below="#+id/contactDetailsTextView"
/>
<TextView
android:id="#+id/contactNoTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/contactNoEditText"
android:layout_marginBottom="15dp"
android:text="CONTACT NO."
android:textSize="12sp"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
/>
<EditText
android:id="#+id/contactNoEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bar2"
android:layout_marginTop="15dp"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/confirmPasswordEditText"
/>
<TextView
android:id="#+id/emailAddressTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMAIL AD."
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
android:layout_alignBottom="#+id/emailAddressEditText"
android:layout_marginBottom="15dp"
android:textSize="12sp"
/>
<EditText
android:id="#+id/emailAddressEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/contactNoEditText"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/confirmPasswordEditText"
/>
<EditText
android:id="#+id/emailAddressEditText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/emailAddressTextView"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/confirmPasswordEditText"
/>
<TextView
android:id="#+id/countryTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COUNTRY"
android:textColor="#android:color/darker_gray"
android:textStyle="bold"
android:layout_below="#+id/emailAddressEditText2"
android:layout_marginTop="18dp"
android:textSize="12sp"
/>
<CheckBox
android:id="#+id/termsCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/countryTextView" />
<TextView
android:id="#+id/termsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/termsCheckbox"
android:layout_alignBottom="#+id/termsCheckbox"
android:text="I AGREE TO THE TERMS AND CONDITIONS STATED"
android:textColor="#android:color/darker_gray"
android:layout_alignBaseline="#+id/termsCheckbox"
/>
<Button
android:id="#+id/signupButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/termsTextView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/signup"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
How to resolve this issue ?
EDIT:
I have attached a screenshot of my problem. I have entered "h" in the passwordEditText but nameEditText gets the value.
mTabHost.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
#Override
public void onViewDetachedFromWindow(View v) {}
#Override
public void onViewAttachedToWindow(View v) {
mTabHost.getViewTreeObserver().removeOnTouchModeChangeListener(mTabHost);
}
});
Add this code in MainActivity.Java #setTabs() function after
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
this line.
Best of luck.
You can set focus to desired edit text after you make the change: ex.
myEditText3.setFocusableInTouchMode(true);
myEditText3.requestFocus();
It's Working in my device.
I don't know why it's not working in yours, I think in java code, there may be some error.
But you can try,
<RelativeLayout
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:focusableInTouchMode="true"
android:layout_height="match_parent" >
add 3rd line to your "parentLayout"
I want to inflate Relative Layout in a Relative Layout .But it is inflating it only one time after that it is not inflating please help me what is the problem in my code.
Activity Code
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout main = (RelativeLayout)findViewById(R.id.passenger_details_layout);
for(int i=0;i<9;i++){
View view = getLayoutInflater().inflate(R.layout.passenger_details_layout, main,false);
main.addView(view);
}
After #Ritaban Suggestion and Help this what i have done
Activity Code:
LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_itineary_page);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*
* LayoutInflater inflater = (LayoutInflater) getBaseContext()
* .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
*/
LinearLayout main = (LinearLayout) findViewById(R.id.main_passenger_details_layout);
for (int i = 0; i < 2; i++) {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(
R.layout.passenger_details_layout, main, false);
main.addView(layout, i);
}
RelativeLayout lay = (RelativeLayout) findViewById(R.id.thingstoshowbelow);
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) lay
.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.main_passenger_details_layout);
lay.setLayoutParams(params);
lay.setVisibility(View.VISIBLE);
}
Main.XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"
android:padding="3dip"
android:src="#drawable/logo_demo" />
<ScrollView
android:id="#+id/main_scroll_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imgLogo" >
<RelativeLayout
android:id="#+id/main_ScrollView_Container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/trip_details_main"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="#drawable/flight_detail"
android:gravity="center_vertical" >
<TextView
android:id="#+id/trip_locations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:text="Delhi to Hydrabad"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/depart_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/trip_locations"
android:layout_marginLeft="5dp"
android:text="18 Sep 2013"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<ImageView
android:id="#+id/imgRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:src="#drawable/cal" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/travel_itin_header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="#+id/trip_details_main"
android:layout_marginTop="2dp"
android:background="#drawable/gray"
android:gravity="center_vertical" >
<TextView
android:id="#+id/itinerary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="5dp"
android:text="Itinerary Details"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<LinearLayout
android:id="#+id/main_passenger_details_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_itin_header"
android:layout_marginTop="2dp"
android:orientation="vertical" >
</LinearLayout>
<RelativeLayout
android:id="#+id/thingstoshowbelow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/passenger_details_layout"
android:visibility="gone" >
<RelativeLayout
android:id="#+id/depart_flight_details_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:background="#drawable/small_search" >
<ImageView
android:id="#+id/flight_depart_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="3dip"
android:src="#drawable/dep" />
<TextView
android:id="#+id/depart_return_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/flight_depart_image"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/arrow_image"
android:layout_toRightOf="#+id/flight_depart_image"
android:text="Delhi to Hydrabad"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/one_way_date_and_duration_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/depart_return_location"
android:layout_marginLeft="12dp"
android:layout_toRightOf="#+id/flight_depart_image"
android:text="18 Sep 2013 09:45 | Duration 2:45"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<ImageView
android:id="#+id/arrow_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/depart_return_location"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
android:layout_marginTop="10dp"
android:src="#drawable/arrow2" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/travel_agency_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/depart_flight_details_duration"
android:layout_marginTop="2dp"
android:background="#drawable/travelagency_bg" >
<TextView
android:id="#+id/travel_agency_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Travel Agency"
android:textColor="#676767"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tarvel_agency_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_agency_header"
android:layout_marginTop="2dp" >
<ImageView
android:id="#+id/agent_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:src="#drawable/uniglobe_logo" />
<ImageView
android:id="#+id/mobile_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/agent_logo"
android:src="#drawable/mobile" />
<ImageView
android:id="#+id/at_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/mobile_logo"
android:src="#drawable/at" />
<ImageView
android:id="#+id/email_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/at_logo"
android:src="#drawable/at" />
<TextView
android:id="#+id/travel_agency_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/agent_logo"
android:layout_marginLeft="5dp"
android:text="XXXXXXXXXXXXXXXXXXXX"
android:textColor="#676767" />
<TextView
android:id="#+id/travel_agency_fax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_agency_address"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="Fax : 2345678"
android:textColor="#676767" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/genral_remarks_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tarvel_agency_details"
android:layout_marginTop="2dp"
android:background="#drawable/general_remar" >
<TextView
android:id="#+id/genral_remark_header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Genral Remarks"
android:textColor="#676767"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/genral_remark_text_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/genral_remarks_header"
android:layout_marginTop="2dp" >
<TextView
android:id="#+id/genral_remark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="HAVE A NICE FLIGHT"
android:textColor="#676767" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</ScrollView>
Use Linear layout as the parent View and give android:orientation = "vertical". Relative Layout is a a Layout where the positions of the children are described in relation to each other or to the parent.If you want to use relative layout then you have to use the addrule property or else all the child will be drawn over the previous child.
For ex :
<Linearlayout
android:id="#+id/passenger_details_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_itin_header"
android:layout_marginTop="2dp"
android:gravity="center_vertical"
android:orientation = "vertical" >
</Linearlayout>
And in activity:
LayoutInflater inflater = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout main =(LinearLayout)findViewById(R.id.passenger_details_layout);
for(int i=0;i<9;i++){
View view = inflater.inflate(R.layout.passenger_details_layout, null);
main.addView(view);
}
LayoutInflater is working and nine time layout is adding. But you cannot see the all views since all Layouts are having adding to another RelativeLayout so they are adding one over the other.
So inorder to overcome that use LinearLayout with orientation vertical instead of Relativelayout in parent view
Every time you add view to that Relative Layout it is being added at the same position..like stack of views..u can't see the below view..better try LinearLayout .. views will be added horizontally or vertically..so u can see that nine views..
// Replace this line
View view = getLayoutInflater().inflate(R.layout.passenger_details_layout, main,false);
to
View view = getLayoutInflater().inflate(R.layout.passenger_details_layout, null);
Okay this is your solution copy and paste it in your main xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"
android:padding="3dip"
android:src="#drawable/ic_launcher" />
<ScrollView
android:id="#+id/main_scroll_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imgLogo" >
<RelativeLayout
android:id="#+id/main_ScrollView_Container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/trip_details_main"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="#android:color/holo_blue_dark"
android:gravity="center_vertical" >
<TextView
android:id="#+id/trip_locations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:text="Delhi to Hydrabad"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/depart_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/trip_locations"
android:layout_marginLeft="5dp"
android:text="18 Sep 2013"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<ImageView
android:id="#+id/imgRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:src="#android:color/holo_blue_bright" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/travel_itin_header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="#+id/trip_details_main"
android:layout_marginTop="2dp"
android:background="#android:color/holo_blue_light"
android:gravity="center_vertical" >
<TextView
android:id="#+id/itinerary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="5dp"
android:text="Itinerary Details"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Relative1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_itin_header"
android:layout_marginTop="2dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/main_passenger_details_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/depart_flight_details_duration"
android:layout_width="wrap_content"
android:layout_below="#+id/Relative1"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:background="#android:color/holo_blue_bright" >
<ImageView
android:id="#+id/flight_depart_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="3dip"
android:src="#android:color/holo_blue_bright" />
<TextView
android:id="#+id/depart_return_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/flight_depart_image"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/arrow_image"
android:layout_toRightOf="#+id/flight_depart_image"
android:text="Delhi to Hydrabad"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/one_way_date_and_duration_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/depart_return_location"
android:layout_marginLeft="12dp"
android:layout_toRightOf="#+id/flight_depart_image"
android:text="18 Sep 2013 09:45 | Duration 2:45"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<ImageView
android:id="#+id/arrow_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/depart_return_location"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
android:layout_marginTop="10dp"
android:src="#android:color/holo_blue_bright" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/travel_agency_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/depart_flight_details_duration"
android:layout_marginTop="2dp"
android:background="#android:color/holo_blue_bright" >
<TextView
android:id="#+id/travel_agency_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Travel Agency"
android:textColor="#676767"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tarvel_agency_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_agency_header"
android:layout_marginTop="2dp" >
<ImageView
android:id="#+id/agent_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:src="#android:color/holo_blue_bright" />
<ImageView
android:id="#+id/mobile_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/agent_logo"
android:src="#android:color/holo_blue_bright" />
<ImageView
android:id="#+id/at_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/mobile_logo"
android:src="#android:color/holo_blue_bright" />
<ImageView
android:id="#+id/email_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/at_logo"
android:src="#android:color/holo_blue_bright" />
<TextView
android:id="#+id/travel_agency_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/agent_logo"
android:layout_marginLeft="5dp"
android:text="XXXXXXXXXXXXXXXXXXXX"
android:textColor="#676767" />
<TextView
android:id="#+id/travel_agency_fax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_agency_address"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="Fax : 2345678"
android:textColor="#676767" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/genral_remarks_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tarvel_agency_details"
android:layout_marginTop="2dp"
android:background="#android:color/holo_blue_bright" >
<TextView
android:id="#+id/genral_remark_header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Genral Remarks"
android:textColor="#676767"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/genral_remark_text_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/genral_remarks_header"
android:layout_marginTop="2dp" >
<TextView
android:id="#+id/genral_remark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="HAVE A NICE FLIGHT"
android:textColor="#676767" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
and for activity put this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout main = (LinearLayout) findViewById(R.id.main_passenger_details_layout);
for (int i = 0; i < 9; i++) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.activity_second, main, false);
main.addView(row);
}
}
I want to generate the relative layout dynamically So that i can repeat the data as much as many times i get data from the server .The layout id that i want to create dynamically are depart_flight_details_duration,passenger_details_layout.How could create them in a loop so that it will be repeated more than one .Please help me on this i am new in android.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"
android:padding="3dip"
android:src="#drawable/logo_demo" />
<ScrollView
android:id="#+id/main_scroll_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imgLogo" >
<RelativeLayout
android:id="#+id/main_ScrollView_Container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/trip_details_main"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="#drawable/flight_detail"
android:gravity="center_vertical" >
<TextView
android:id="#+id/trip_locations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:text="Delhi to Hydrabad"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/depart_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/trip_locations"
android:layout_marginLeft="5dp"
android:text="18 Sep 2013"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<ImageView
android:id="#+id/imgRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:src="#drawable/cal" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/travel_itin_header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="#+id/trip_details_main"
android:layout_marginTop="2dp"
android:background="#drawable/gray"
android:gravity="center_vertical" >
<TextView
android:id="#+id/itinerary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="5dp"
android:text="Itinerary Details"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/passenger_details_layout"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="#+id/travel_itin_header"
android:layout_marginTop="2dp"
android:background="#drawable/gray"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/pax_image"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:padding="3dip"
android:src="#drawable/black_man" />
<TextView
android:id="#+id/first_pax_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pax_image"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/pax_image"
android:text=" XXX XXX XXX"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imgRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/first_pax_name"
android:layout_alignParentRight="true"
android:src="#drawable/arrow2" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/depart_flight_details_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/passenger_details_layout"
android:layout_marginTop="2dp"
android:background="#drawable/small_search" >
<ImageView
android:id="#+id/flight_depart_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="3dip"
android:src="#drawable/dep" />
<TextView
android:id="#+id/depart_return_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/flight_depart_image"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/arrow_image"
android:layout_toRightOf="#+id/flight_depart_image"
android:text="Delhi to Hydrabad"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/one_way_date_and_duration_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/depart_return_location"
android:layout_marginLeft="12dp"
android:layout_toRightOf="#+id/flight_depart_image"
android:text="18 Sep 2013 09:45 | Duration 2:45"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<ImageView
android:id="#+id/arrow_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/depart_return_location"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:src="#drawable/arrow2" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/travel_agency_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/depart_flight_details_duration"
android:layout_marginTop="2dp"
android:background="#drawable/travelagency_bg" >
<TextView
android:id="#+id/travel_agency_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Travel Agency"
android:textColor="#676767"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tarvel_agency_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_agency_header"
android:layout_marginTop="2dp" >
<ImageView
android:id="#+id/agent_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/mobile_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/agent_logo"
android:src="#drawable/mobile" />
<ImageView
android:id="#+id/at_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/mobile_logo"
android:src="#drawable/at" />
<ImageView
android:id="#+id/email_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/at_logo"
android:src="#drawable/at" />
<TextView
android:id="#+id/travel_agency_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/agent_logo"
android:layout_marginLeft="5dp"
android:text="Address : XXXXXXXX"
android:textColor="#676767" />
<TextView
android:id="#+id/travel_agency_fax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/travel_agency_address"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="Fax : XXXXXXXX"
android:textColor="#676767" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/genral_remarks_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tarvel_agency_details"
android:layout_marginTop="2dp"
android:background="#drawable/general_remar" >
<TextView
android:id="#+id/genral_remark_header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Genral Remarks"
android:textColor="#676767"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/genral_remark_text_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/genral_remarks_header"
android:layout_marginTop="2dp" >
<TextView
android:id="#+id/genral_remark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="HAVE A NICE TRIP"
android:textColor="#676767" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
You have to use custom listview.
And for two listview in one layout see this link Link
A Simple Program for LayoutInflater from here
a-make this layout as your activity_main.xml-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
b-this is the hidden layout which we will add dynamically,save it as hidden_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hidden_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, this is the inflated text of hidden layout"/>
<EditText
android:id="#+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, this is your name"/>
</LineraLayout>
c- Now this is the code for main activity-
public class MainActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<numberOfRows;i++){
LinearLayout main = (LinearLayout)findViewById(R.id.main_layout);
View view = getLayoutInflater().inflate(R.layout.hidden_layout, main,false);
main.addView(view);
}
}
}
RelativeLayout mDisplayBlock = (RelativeLayout)
LayoutInflater.from(getActivity()).inflate(
depart_flight_details_duration, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mDisplayBlock.setLayoutParams(params);
you can add this layout to your main scroll view
main_scroll_view_layout.addView(mDisplayBlock);
just put this code at appropriate place in your code.