Adding layout dynamically between an xml layout - android

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.

Related

how to set two textview equal size horizontally in relative layout

<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#b4d3d3">
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:text="First" />
<TextView
android:id="#+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/hello"
android:padding="20dp"
android:text="Second" />
</RelativeLayout>
this is my xml i want set two textview inside relative layout equal part horzontally but using this xml left textview is coming only few part while right one take much space please suggest me what i am doing wrong .
Try this You can achive that using LinearLayout just set same Weight to Your both TextView like below code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#5d737e"
android:layout_weight="1"
android:padding="20dp"
android:text="First" />
<TextView
android:id="#+id/world"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Second" />
</LinearLayout>
if You want to use RelativeLayout than try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center">
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:text="First" />
<TextView
android:id="#+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/hello"
android:padding="20dp"
android:text="Second" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#b4d3d3">
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:text="First" />
<TextView
android:id="#+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/hello"
android:padding="20dp"
android:text="Second" />
You can set programmatically width child view like below code and manage different device size easily:
private TextView hello, world;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hello = findViewById(R.id.hello);
world = findViewById(R.id.world);
RelativeLayout.LayoutParams relativeParam = (RelativeLayout.LayoutParams) hello.getLayoutParams();
relativeParam.width = getDeviceWidth(MainActivity.this) * 50 / 100;
hello.setLayoutParams(relativeParam);
RelativeLayout.LayoutParams relativeParamWorld = (RelativeLayout.LayoutParams) world.getLayoutParams();
relativeParamWorld.width = getDeviceWidth(MainActivity.this) * 50 / 100;
world.setLayoutParams(relativeParamWorld);
}
public int getDeviceWidth(Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return metrics.widthPixels;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#b4d3d3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:layout_weight="1"
android:text="First" />
<TextView
android:id="#+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/hello"
android:padding="20dp"
android:layout_weight="1"
android:text="Second" />
</LinearLayout>
</RelativeLayout>
try below code
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#b4d3d3"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_weight="1"
android:id="#+id/hello"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#5d737e"
android:padding="20dp"
android:text="First" />
<TextView
android:layout_weight="1"
android:id="#+id/world"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/hello"
android:padding="20dp"
android:text="Second" />
</LinearLayout>
</RelativeLayout>
Use Perfect weight Concept
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/hello"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#5d737e"
android:layout_weight="1"
android:padding="20dp"
android:text="First" />
<TextView
android:id="#+id/world"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Second" />
</LinearLayout>

Why first click on the ImageView is not working?

I have read through other posts. I can't able to find the solution still.
I tried putting
android:focusableInTouchMode="false"
android:focusable="false"
not worked.
I have included
android:clickable="true"
not worked.
In Java code, I have included
imgLeft.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
v.performClick();
}
}
});
aside with the onClickListener().
But still it didn't worked.
Here is my rough layout structure and position of the image view.
<RelativeLayout>
<FrameLayout>
<LinearLayout>
<Relativelayout>
<LineatLayout/>
<LinearLayout>
<LinearLayout>
<ImageView <--- This is the I needed to click.
android:id="#+id/imgLeft"
android:layout_width="wrap_content"
android:layout_height="#dimen/title_img_width"
android:src="#drawable/left"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</Relativelayout>
</LinearLayout>
</FrameLayout>
</Relativelayout>
I am using setOnClickListener for handling the click event.
imgLeft.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some function here <-- **This function triggers only after the second click**
}
});
NOTE:
I thoroughly checked the other stackoverflow posts. But none of them solved the problem.. is there any solution for the above
UPDATE: Original code:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_image" >
<FrameLayout
android:id="#+id/layoutConfiguration"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/bg_image"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/menu_header_height" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<LinearLayout
android:id="#+id/ll_menu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="#dimen/menu_top_icon"
android:layout_height="match_parent"
android:layout_gravity="center"
android:padding="5dp"
android:src="#drawable/menu" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.68"
android:fontFamily="Oswald-Regular.ttf"
android:gravity="center"
android:text="Connect Bluetooths"
android:textColor="#fff"
android:textSize="#dimen/menu_tital_textsize" />
<!-- android:padding="#dimen/menu_tital_padding" -->
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:gravity="center"
android:padding="15dp"
android:text=""
android:textColor="#fff"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll_sole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/tital_margin_top"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="1" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_leftBattery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:fontFamily="Raleway-Regular.ttf"
android:gravity="center"
android:text="100%"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/holo_red_dark"
android:textSize="16sp"
android:visibility="gone" />
<ImageView
android:id="#+id/imgLeftBluetooth"
android:layout_width="wrap_content"
android:layout_height="#dimen/title_img_width"
android:src="#drawable/left" />
<TextView
android:id="#+id/tv_leftBluetooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:fontFamily="Raleway-Regular.ttf"
android:gravity="center"
android:text="Not Connected"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_rightBattery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:fontFamily="Raleway-Regular.ttf"
android:gravity="center"
android:text="100%"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/holo_red_dark"
android:textSize="16sp"
android:visibility="gone" />
<ImageView
android:id="#+id/imgRightBluetooth"
android:layout_width="wrap_content"
android:layout_height="#dimen/title_img_width"
android:layout_marginTop="5dp"
android:src="#drawable/right" />
<TextView
android:id="#+id/tv_rightBluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:fontFamily="Raleway-Regular.ttf"
android:gravity="center"
android:text="Not Connected"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingTop="#dimen/menu_tital_padding" >
<ToggleButton
android:id="#+id/toggleButton_connectBluetooth"
android:layout_width="#dimen/title_img_height"
android:layout_height="#dimen/title_text_height"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/toggle_selector"
android:checked="false"
android:padding="5dp"
android:text=""
android:textOff=""
android:textOn="" />
<TextView
android:id="#+id/tv_toggle_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="7dp"
android:text="Connect Left Bluetooths Only"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/fl_balloon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="-15dp"
android:gravity="center"
android:visibility="gone" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="#dimen/balloon_width"
android:layout_height="#dimen/balloon_height"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/balloon" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="7dp"
android:fontFamily="Oswald-Regular.ttf"
android:gravity="center"
android:text="Pair"
android:textColor="#android:color/white" />
</FrameLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:fontFamily="Oswald-Regular.ttf"
android:text="Configure Bluetooths"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="#dimen/menu_tital_textsize"
android:visibility="gone" />
<Button
android:id="#+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="#dimen/menu_top_icon"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:background="#drawable/button_box"
android:padding="3dp"
android:text="Continue"
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
The id of the clicks that were performed are
imgLeftBluetooth and imgRightBluetooth. These two imageviews were triggering only after 2nd click.
Its simple .You can try this
Just add android:onClick="clicking" in your ImageView (XML)
Then
public void clicking(View v)
{
Toast.makeText(v.getContext(),"Clicking On Image",Toast.LENGTH_LONG).show();
}
Edited
YOUR_IMAGE_VIEW_OBJ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do your Staff
}
});
Android ImageView's onClickListener does not work

Android - Not able to to perform onItemClick at a Listview item with ImageButton

I've made the next xml layout for listview item -
<?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="70dp"
android:background="#drawable/back_item"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView_bot"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/tv_ico" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_saying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:ellipsize="end"
android:singleLine="true"
android:text="blah blah"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/textView_nar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="sport"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="#+id/textView_clicks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textColor="#696969"
android:text="Clicks"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<Button
android:id="#+id/button_add_item"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:background="#drawable/add_state"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
Well, when I'm using this xml layout and pressing on a listview item all is fine - the code gets into the onItemClick.
But when I adding to this layout an ImageButton I'm not able to get into the onItemClick function.
Here's the full layout code with the ImageButton -
<?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="70dp"
android:background="#drawable/back_item"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView_bot"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/tv_ico" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_saying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:ellipsize="end"
android:singleLine="true"
android:text="blah blah"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/textView_nar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="sport"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="#+id/textView_clicks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textColor="#696969"
android:text="Clicks"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<Button
android:id="#+id/button_add_item"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:background="#drawable/add_state"
android:focusable="false" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/clip_ico"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
As you can see I've used android:focusable="false" but still not able to get into the onItemClick.
Any ideas why it happens?
Thanks for any kind of help
add android:descendantFocusability="blocksDescendants" in the root layout
enter code here
if you are suing custom Adpter for Listview
then
do it in you customadapter - getView method
ImageView YOURIMAGE=(ImageView) InflatedVIEW.findViewById(R.id.imageview1);
YOURIMAGE.setOnClickListener(new OnClickListener() {
#Override`
public void onClick(View v) {
}

Calling of another XML by View and Layout

Every body I want to call another XML in my activity , i called my main xml as given below
setContentView(R.layout.main);
and my xml file is as follow-
<?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:background="#drawable/appbg"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/ll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- listview -->
<ImageView
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:contentDescription="#drawable/logo12"
android:src="#drawable/logo12" />
<LinearLayout
android:id="#+id/homeLin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="right" >
<Button
android:id="#+id/btnCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/cart_icon" />
<Button
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/home" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/searchLin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/etSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search"
android:textColor="#color/Black"
android:textSize="15sp" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/cross1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="5dp"
android:contentDescription="#drawable/search"
android:src="#drawable/search" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linSortby"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvsortby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sortby"
android:textColor="#color/Blue"
android:textSize="13sp" />
<TextView
android:id="#+id/tvPriceLH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/priceLH"
android:textColor="#color/Blue"
android:textSize="13sp" />
<TextView
android:id="#+id/tvPriceHL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="#string/priceHL"
android:textColor="#color/Blue"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linPagingupper"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/linPaging"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:contentDescription="#drawable/prev"
android:src="#drawable/prev" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/buttonbackground"
android:text="#string/n1"
android:textColor="#color/Blue"
android:textSize="12sp" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonbackground"
android:text="#string/n2"
android:textColor="#color/Blue"
android:textSize="12sp" />
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonbackground"
android:text="#string/n3"
android:textColor="#color/Blue"
android:textSize="12sp" />
<TextView
android:id="#+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonbackground"
android:text="#string/n4"
android:textColor="#color/Blue"
android:textSize="12sp" />
<TextView
android:id="#+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonbackground"
android:text="#string/n5"
android:textColor="#color/Blue"
android:textSize="12sp" />
<!-- button -->
<ImageView
android:id="#+id/imgNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:contentDescription="#drawable/next"
android:src="#drawable/next" />
</LinearLayout>
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/n1"
android:textColor="#color/white"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/listViewResult"
android:layout_width="fill_parent"
android:layout_height="320dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linBottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bluegrad"
android:gravity="bottom"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:contentDescription="#drawable/contact"
android:src="#drawable/contact" />
<LinearLayout
android:id="#+id/bottomLin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<ImageView
android:id="#+id/imgHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:contentDescription="#drawable/home"
android:src="#drawable/home" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/add_other_xml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
and i want to call another activity by following code-
LinearLayout lin_lay_add_paynow =
(LinearLayout)findViewById(R.id.add_other_xml);
View pay_now_view = getLayoutInflater().inflate(R.layout.pay_now_btn, null);
lin_lay_add_paynow.addView(pay_now_view);
TextView btn_pay_now = (TextView) pay_now_view.findViewById(R.id.btn_paynow);
btn_pay_now.setText("mlsdmlsm");
and my pay_now_btn.xml is as follow
<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:orientation="vertical" >
<TextView
android:id="#+id/btn_paynow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
And problem is that i am doing every thing in correct manner . Then why my textview is not appearing . No error no exception then i am not able to see my textview of pay_now_btn.xml
Thanks in advance to all.
You have setContentView(R.layout.main).
And you are inflating a view. But the inflated view is not added to the layout.
Have a another LinearLayout in your main.xml. Place this linear layout to the required position or you can use Relativelayout.
Initialize it in onCreate
LinearLayout ll = LinearLayout findViewById(R.id.ll);
Now add the inflated view to linearlayout
ll.addView(pay_now_view);
Edit:
You can add the infalted view as a footer to listview as
list.addFooterView(pay_now_view );
You have not added the view into a layout in your main.xml
Create a layout in your main.xml
In your code
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(pay_now_view);
Hope this works. Let me know if you face any problem
CODE :
Add this at bottom of main.xml before closing the last tag
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" >
</LinearLayout>
In your Activity after inflating and setText:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(pay_now_view);

RelativeLayout.BELOW not working

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.

Categories

Resources