Button location in listview android - android

I have a button on the listview when click on that will show popup. I have implemented that successfully. But the issue is that i want to display popup on the clicked button position.
View layout = inflater.inflate(R.layout.popup_layout,null);
pwindo = new PopupWindow(layout, 300, 250, true);
pwindo.showAtLocation(layout, Gravity.NO_GRAVITY, 30, 40);
pwindo.setOutsideTouchable(true);
pwindo.setTouchable(true);
pwindo.setBackgroundDrawable(new BitmapDrawable());
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
pwindo.dismiss();
return false;
}
});
Button code
vi = inflater.inflate(R.layout.list_row, null);
vi.findViewById(R.id.statusImage).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showPopup();//to display popup thats given above
}
});

You need to set buttons tag as its position, and onClick you need to get it:
button.setTag(position);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int position = (Integer) v.getTag();
// Do whatever you like with position
}
});

when the button is clicked you can get the position of the item in the list. Use
list.getFirstVisiblePosition();
to get the buttin postion. substract the FirstVisiblePosition from the position. you will get the click postion. and you can show the pop up in that postion.

You can get position for button click in listview by following code.
(vi.findViewById(R.id.statusImage)).setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(Classname.this, "POSITION"+ position,Toast.LENGTH_SHORT).show();
showPopup();
}
});

Use QuickAction. Examples :
http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/
http://www.edumobile.org/android/android-apps/quick-action-demo/
https://github.com/alhneiti/Android-QuickAction

Related

i want to get position in a list data how to get the position of click of button in android

how to get the position from list on click of data.
i want access button in android.on the click of button of the adapter class it should give the position.
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
you can set the position as the tag
holder.beginDate.setTag(position); // set every time getting the View
and retrieve it using
// put this code where creating a new instance of holder
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int pos = (Integer) view.getTag();
// do anything using the position
}
});
After initialization of your button in getView() method write:
holder.beginDate.setTag(position);
and in your onClick() use:
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int position=Integer.parseInt(((Button)v).getTag().toString());
}
});

Show hidden views of List item on click of button outside the list

I am developing an Android Application. In this app I am making a list of views using code given below. In each item of list there is delete button with visibility "Gone". Now there is another button Edit outside the list, on click of edit button I have to show delete button on each item of list. but using this code delete button shows only in the last item. Please help me to solve the problem. Thanks.
dynamicView();
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// lay.removeAllViews();
addnew.setVisibility(View.INVISIBLE);
btn_red.setVisibility(View.VISIBLE);
edit.setText("Done");
}
});
public void dynamicView() {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < templates.length; i++) {
final View customView = linflater.inflate(R.layout.order_template_item,
null);
btn_red=(ImageView)customView.findViewById(R.id.btn_negative);
btn_drag=(ImageView)customView.findViewById(R.id.button_drag);
btn_delete=(ImageView)customView.findViewById(R.id.button_delete);
final ImageView image = (ImageView)customView.findViewById(R.id.arrow);
final TextView text = (TextView)customView.findViewById(R.id.date);
final TextView sku = (TextView)customView.findViewById(R.id.time);
final TextView price = (TextView)customView.findViewById(R.id.last);
final TextView names =(TextView)customView.findViewById(R.id.name);
image.setId(i);
text.setId(i);
sku.setId(i);
price.setId(i);
names.setId(i);
btn_red.setId(i);
btn_red.setTag(i);
btn_delete.setId(i);
btn_drag.setId(i);
names.setText(templates[i]);
btn_red.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_delete.setVisibility(View.VISIBLE);
TranslateAnimation anim = new TranslateAnimation(100,0 , 0, 0);
anim.setInterpolator(new BounceInterpolator());
anim.setDuration(1000);
btn_delete.setAnimation(anim);
}
});
btn_delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lay.removeView(customView);
}
});
lay.addView(customView);
}
You're holding reference only to the last one btn_red.
You can do something like this.
List<Button> buttons = new LinkedList<Button> ();
Then in your loop after findViewById on btn_red
buttons.add(btn_red);
And finally in your onClickListener
for (Button button: buttons) {
button.setVisibility(View.VISIBLE);
}
Do somethink like below to make visible all the button added to list items-
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addnew.setVisibility(View.INVISIBLE);
for (int i = 0; i < templates.length; i++) {
int id = btn_red.getId(i);
id.setVisibility(View.VISIBLE);
}
edit.setText("Done");
}
});

Delete a LinearLayout on long press event

I am creating a LinearLayout dynamically. I need to delete the LinearLayout on LongPress event.
My code :
public void addTileView(View v) {
_parentLayout = (LinearLayout) findViewById(R.id.gridCont);
View child = getLayoutInflater().inflate(R.layout.customtileview,null);
((TextView)child.findViewById(R.id.tileText)).setText("Tile View :"+_tileViewCount++);
_parentLayout.addView(child);
_parentLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
return true;
}
});
}
How to do this ?
try this,
_parentLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
parentLayout.removeAllViews();
return true;
}
});
Its better to remove the view which you added in the parent .
Say Parent layout is the main layout and child layout is the one which you want to remove.
You should try
parent_layout.removeView(child_layout);
removeAllViews() - will remove all the views inside the view , but not the main view .
Please refer to ViewGroup
ViewGroup vg = (ViewGroup)(myView.getParent());
vg.removeView(myView);
Alternatively , You can make the visibility of your view to Visible.GONE , and then make it visible when required.
This works for me.
public void addTileView(View v) {
_parentLayout = (LinearLayout) findViewById(R.id.gridCont);
child = getLayoutInflater().inflate(R.layout.customtileview,null);
((TextView)child.findViewById(R.id.tileText)).setText("Tile View :"+_tileViewCount++);
_parentLayout.addView(child);
child.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
_parentLayout.removeView(v);
return true;
}
});
}
Thanks

Click on button in listItem does not respond on first click, but response on second click, how to set to response on first click?

I have ListView, and everubody listItem have a button.
I cant get event on first click on imageView, but after first when I click second time I get Event. Why I cant get response on first click on imageView?
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
ImageView addLarge = (ImageView) view.findViewById(R.id.addLargeImage);
ImageView addSmall = (ImageView) view.findViewById(R.id.addSmallImage);
addLarge.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addProductToCart = true;
addToOrderListClick = true;
OrderProduct order = new OrderProduct(listProductId.get(position), listProductName.get(position),
listProductNameEn.get(position), listProductImageUrl.get(position), "large", listProductPriceLarge.get(position));
orderListProduct.add(order);
animCopyListItemAddToCart(view, position, mainRelativeLaout);
Toast.makeText(getBaseContext(), "AddLarge", Toast.LENGTH_SHORT).show();
}
});
addSmall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addProductToCart = true;
addToOrderListClick = true;
OrderProduct order = new OrderProduct(listProductId.get(position), listProductName.get(position),
listProductNameEn.get(position), listProductImageUrl.get(position), "small", listProductPriceSmall.get(position));
orderListProduct.add(order);
animCopyListItemAddToCart(view, position, mainRelativeLaout);
Toast.makeText(getBaseContext(), "addSmall", Toast.LENGTH_SHORT).show();
}
});
}
}
Because you are setting your onClickListener() for your ImageViews within your ListView onItemClickListener().
So the user selects an item in your list, and only then does an onClickListener for your ImageViews get assigned. So the second time, the onclick listener of the ImageView gets successfully fired.
To navigate around this issue, assign the individual ImageView onClickListeners within a custom adapter so they are assigned when they come into view as opposed to when a user clicks an item in list.

Android: runtime OnClickListener does not work

I'm developing an app for Android devices using Eclipse and Android SDK;
I would like to add some ImageButtons (at runtime) with the same OnClickListener. The problem is that the OnClickListener works ONLY for the first added button.
For all the next buttons the onClick event simply doesn't fire.
Have someone already encountered (and solved) this problem?
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
ImageButton myButton= new ImageButton(this);
myButton.setMaxHeight(140);
myButton.setMaxWidth(140);
myButton.setPadding(0, 0, 0, 0);
myButton.setAdjustViewBounds(true);
myButton.setImageResource(resId);
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
myRelativeLayout.addView(myButton, 0);
}
You're always adding the button on the 0th position.
myRelativeLayout.addView(myButton, 0);
So you just create new buttons and add them as the "first button", which is probably why you only see that working. The buttons you created previously get lost.
after you add the view, set the click listener like this
final int count = myRelativeLayout.getChildCount();
for (int i = 0; i < count; i++) {
View child = myRelativeLayout.getChildAt(i);
child.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

Categories

Resources