Add Linear Layout view dynamically in eclipse android - android

How can I add view (with linear layout id/linearlayout_tambahkeluhan can add automatically when I click button id/tambah_keluhan) in case that...the content in linearlayout_tambahkeluhan is id/layout_keluhan, id/layout_status, id/layout_tindakan...How the code in Java? anyone can help me? Thx before
<LinearLayout
android:id="#+id/linearlayout_tambahkeluhan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layout_keluhan" >
<TextView
android:id="#+id/textView11"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="KELUHAN"
android:textColor="#color/blue" />
<TextView
android:id="#+id/textView12"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text=":"
android:textColor="#color/blue" />
<Spinner
android:id="#+id/spinner_keluhan"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layout_status" >
<TextView
android:id="#+id/textView13"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="STATUS"
android:textColor="#color/blue" />
<TextView
android:id="#+id/textView14"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text=":"
android:textColor="#color/blue" />
<Spinner
android:id="#+id/spinner_status"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layout_tindakan" >
<TextView
android:id="#+id/textView15"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="TINDAKAN"
android:textColor="#color/blue" />
<TextView
android:id="#+id/textView16"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text=":"
android:textColor="#color/blue" />
<EditText
android:id="#+id/editText_tindakan"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:ems="10"
android:textColor="#color/black" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button_tambahkeluhan"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/transparent"
android:onClick="onClick"
android:text="tambah keluhan"
android:textColor="#color/blue"
android:textStyle="italic" />

You try addView(View view) function) with view can be controls or xml layout.
Example: ((LinearLayout)findViewById(R.id.linearlayout_tambahkeluhan)).addView(btnNew);

Add to your linearLayout visibility 'gone' like this:
<LinearLayout
android:id="#+id/linearlayout_tambahkeluhan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
Then in onCreate function in your Activity do this:
LinearLayout yourLinearLayout=(LinearLayout)findViewById(R.id.linearlayout_tambahkeluhan);
Button yourButton = (Button)findViewById(R.id.button_tambahkeluhan);
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
yourLinearLayout.setVisibility(View.Visible);
}
}

Related

Button inside ListView is not working

I am using ListView for making this layout(ScreenShot). Here when I click Button 1st, it is not worked. But if I click ListItem 1st then I click Button it works.
Even I used android:descendantFocusability="blocksDescendants" in the layout.
android:focusable="false" & android:focusableInTouchMode="false" inside of Button. But nothing worked.
Please help me to fix it.
My Codes:
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardlist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/curved_shape"
android:orientation="vertical"
card_view:cardElevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="14dp"
android:layout_marginRight="14dp"
android:src="#drawable/image_4" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="14dp"
android:orientation="vertical">
<TextView
android:id="#+id/cartProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Fusce veh"
android:textColor="#9F9F9F" />
<TextView
android:id="#+id/cartQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="100 g"
android:textColor="#9F9F9F" />
<LinearLayout
android:layout_width="225dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Rs "
android:textColor="#9F9F9F" />
<TextView
android:id="#+id/cartPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:layout_weight="0.48"
android:text="19.99"
android:textColor="#9F9F9F" />
<TextView
android:id="#+id/cartPriceDum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:layout_weight="0.48"
android:text="19.99"
android:textColor="#9F9F9F"
android:visibility="gone" />
<ImageView
android:id="#+id/ivDecrease"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.1"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_action_expand" />
<TextView
android:id="#+id/cartCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.2"
android:gravity="center"
android:text="1"
android:textSize="15dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/ivIncrease"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_action_collapse" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F8F8F8"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/addTowish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#F8F8F8"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="ADD TO WISHLIST"
android:textColor="#787878" />
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<Button
android:id="#+id/remove"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#F8F8F8"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="REMOVE"
android:textColor="#787878" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
You have to add clicklistener to your button inside adapter class
public DataObjectHolder(View itemView) {
super(itemView);
CardView cardClick = (CardView) itemView.findViewById(R.id.xxx);
btn = (Button) itemView.findViewById(R.id.xxx);
Log.i(LOG_TAG, "Adding Listener");
cardClick.setOnClickListener(this);
btn.setOnClickListener(this);
}
Please remove android:focusable="false" and android:focusableInTouchMode="false" from both your buttons.
Since you have disabled focusability it doesn't react on click the first time. When you click on the listview the row gains focus and hence its working.
Also remove android:descendantFocusability="blocksDescendants" from the linear layout that holds the buttons.

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

Adding layout dynamically between an xml layout

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.

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);

Categories

Resources