When I click the button no message appears, but if I click on the listview item and then click the button then it shows the message.
what is want is to update my database by clicking on each listview items's button. I have used android:focusable="false" android:focusableInTouchMode="false" in list_items.xml button.
productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Button saleBtn = (Button) view.findViewById(R.id.sale_btn);
if (saleBtn != null) {
saleBtn .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "clicked",
Toast.LENGTH_SHORT).show();
}
});
}
}
});
The items layout for list_items.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/sale_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Sale"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/product_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-medium"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#2B3D4D"
android:textStyle="bold"
tools:text="Product" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-medium"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#2B3D4D"
android:textStyle="bold"
tools:text="Quantity" />
<TextView
android:id="#+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-medium"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#2B3D4D"
android:textStyle="bold"
tools:text="Price" />
</LinearLayout>
</LinearLayout>
have you tried
View button = productListView.getChildAt(position).findViewById(R.id.sale_btn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "clicked",
Toast.LENGTH_SHORT).show();
}
});
instead of finding the button using the view and the id which will only prompt toast when you press the first button in the list you have created instead of the individual button in the list using getChildAt() you can get the individual button in the list
Related
I need to detect the onclick event on a imageview, and get the position to get the row from a cursor, and I don't know how.
I'm trying to modify https://github.com/Odoo-mobile/framework to and shopping cart app.
The code of the layout view where I need to detect the click event is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:padding="10dp" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="7dip"
android:padding="2dip" >
<odoo.controls.BezelImageView
android:id="#+id/image_small"
android:layout_width="40dp"
android:layout_margin="#dimen/default_8dp"
app:maskDrawable="#drawable/circle_mask"
android:layout_height="40dp" />
</LinearLayout>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/thumbnail"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#color/body_text_1"/>
<LinearLayout
android:id="#+id/cart_plus_minus_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/name"
android:orientation="horizontal" >
<TextView
android:id="#+id/list_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="60dip"
android:layout_weight="0.23"
android:paddingRight="5dip"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#color/android_red" />
<ImageView
android:id="#+id/cart_minus_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_minuscircle" />
<TextView
android:id="#+id/cart_product_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:text="0"
android:textSize="18dip"
android:textStyle="bold" />
<ImageView
android:id="#+id/cart_plus_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_addcircle" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
the images I need to detect the the click are: #+id/cart_plus_img #+id/cart_minus_img
When the row is click the function that capture the event is:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ODataRow row = OCursorUtils.toDatarow((Cursor) mAdapter.getItem(position));
//AddToCart(row);
loadActivity(row);
}
What I need is to call loadActivity(row) when the user click the row but when click #+id/cart_plus_img #+id/cart_minus_img call another function but with the row parameter
Any help is thanksfull
Use the Tag property of the View
ImageView imgMinus = (ImageView)view.findViewById(R.id.cart_minus_img);
ImageView imgPlus = (ImageView)view.findViewById(R.id.cart_plus_img);
imgMinus.setTag(position);
imgPlus.setTag(position);
imgMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
...
}
});
imgPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
...
}
});
I have a custom list view in fragment , and on Item Click text color changes and marquee running, but on first time item click none of things is working , no marquee or no text color changes, and on second time click both working fine. Please Help!!
mainListViews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
textView = (TextView) view.findViewById(R.id.textView);
String text = textView.getText().toString();
song_namef.setText(text);
setItemNormal();
View rowView = view;
views=rowView;
setItemSelected(rowView);
}});
public void setItemSelected(View view){
View rowView = view;
TextView tv = (TextView)rowView.findViewById(R.id.textView);
tv.setTextColor(Color.WHITE);
tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv.setSingleLine(true);
tv.setHorizontallyScrolling(true);
tv.setSelected(true);
tv.requestFocus();
}
public void setItemNormal()
{
for (int i=0; i< mainListViews.getChildCount(); i++)
{
View v = mainListViews.getChildAt(i);
TextView txtview = ((TextView)v.findViewById(R.id.textView));
txtview.setSelected(false);
txtview.setSingleLine(true);
txtview.setTextColor(getResources().getColor(R.color.tabsScrollColor));
}}
Layout:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/card_view"
android:layout_height="80dp"
android:layout_width="match_parent"
android:layout_marginBottom="16dp"
app:cardCornerRadius="16dp"
app:cardElevation="16dp"
card_view:cardCornerRadius="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
card_view:cardBackgroundColor="#android:color/black"
android:divider="#B49238"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/cancel"
android:layout_marginLeft="1200dp"
android:layout_marginTop="18dp"
android:id="#+id/imageView19" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_toStartOf="#+id/imageView16">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:textSize="30dp"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginStart="100dp"
android:layout_marginTop="18dip"
android:textColor="#color/tabsScrollColor"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="#+id/textView11" />
</LinearLayout>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginEnd="10dp"
android:id="#+id/button"
android:src="#drawable/musicalbum"
android:padding="5dp"
android:focusable="false"
android:layout_marginStart="10dp"
android:layout_marginTop="7dip"
android:layout_alignParentRight="true"
/>
Add android:descendantFocusability="blocksDescendants" to your row item
It should work fine
Let me know if you still getting issue
I have a fragment with a ListView and two buttons above this ListView. The two buttons call the following methods which reload the ListView with a different adapter for the data source. The problem is with the second one. When calling the showWhenWhere(), the onItemClick() is never fired.
private void showAtoZ() {
lineupAdapter = new LineupAdapter(parentView.getContext());
listView.setAdapter(lineupAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
System.out.println("A to Z tapped");
int itemPosition = position;
if(parentActivity != null) {
parentActivity.changeFragment(new BandFragment());
}
}
});
loadData();
}
private void showWhenWhere() {
whenWhereAdapter = new LineupWhenWhereAdapter(parentView.getContext());
listView.setAdapter(whenWhereAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
System.out.println("When/Where tapped");
int itemPosition = position;
if(parentActivity != null) {
parentActivity.changeFragment(new BandFragment());
}
}
});
loadData();
}
UPDATE - I determined it's caused by the XML for the whenWhereAdapter, which I'm pasting below:
<?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="150dp" android:id="#+id/lineupWhenWhereView">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bandImageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:background="#android:color/darker_gray" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="#+id/textView"
android:background="#drawable/list_row_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Elijah & The Moon"
android:id="#+id/bandTitle"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="30dp"
android:textSize="23sp"
android:maxLines="2"
android:textColor="#android:color/white" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="25dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#android:color/black">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12:00 PM"
android:id="#+id/whenLabel"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:textColor="#android:color/holo_orange_light"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:id="#+id/divider"
android:layout_toRightOf="#+id/whenLabel"
android:layout_toEndOf="#+id/whenLabel"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="6dp"
android:layout_alignParentBottom="true"
android:textIsSelectable="true"
android:textSize="13sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="West Stage"
android:id="#+id/whereLabel"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/divider"
android:layout_toEndOf="#+id/divider"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textSize="12sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD TO SCHEDULE"
android:id="#+id/addButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/add_to_schedule"
android:drawablePadding="5dp"
android:textColor="#android:color/holo_orange_light"
android:layout_marginRight="10dp" />
</RelativeLayout>
</RelativeLayout>
Any idea why this would cause the OnItemClickListener to not work?
ListView only maintains one OnItemClickListener at time. When you set the second listener, only that listener will be called.
-------EDIT -------
After seeing your row layout, I think you should set
android:descendantFocusability="blocksDescendants"
on your very first RelativeLayout.
That happen because TextViews && Buttons inside layout are blocking click event.
In my android app i need to use alertdialog programmatically with two images in first row and another two images in second row. i seen some example like,
CharSequence colors[] = new CharSequence[] {"red", "green", "blue", "black"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on colors[which]
}
});
builder.show();
in the above example have only one option for every row like red in first row, green in next row, blue in third row like that. I need to implement 2 option in every row, pls guide me,,
Dialog mDialog;
mDialog=new Dialog(SplashScreen.this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.yourcustomlayout);
TextView ok,cancel;
ok=(TextView) mDialog.findViewById(R.id.dialogyes);
cancel=(TextView) mDialog.findViewById(R.id.dialogno);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mDialog.cancel();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
mDialog.cancel();
}
});
mDialog.show();
yourcustomlayout.xml -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white" >
<TextView
android:id="#+id/dialogtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Internet Connection"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textColor="#android:color/black" />
<View
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/dialogtitle"
android:layout_marginTop="25dp"
android:background="#C83437" />
<TextView
android:id="#+id/dialogMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/view1"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="You are not connected to internet.\nDo you want to on connection?"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textColor="#android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="150dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/dialogyes"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:layout_weight="0.5"
android:gravity="center"
android:text="YES"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/dialogno"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:layout_weight="0.5"
android:gravity="center"
android:text="NO"
android:textColor="#android:color/black" />
</LinearLayout>
</RelativeLayout>
in mDialog.setContentView() add your custom layout.
This is custom View layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="25dp"
android:background="#drawable/newbackrow"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_search_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toLeftOf="#+id/txt_search_sport"
android:layout_weight="1"
android:focusable="false"
android:inputType="textMultiLine"
android:maxLines="2"
android:singleLine="false"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_search_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:focusable="false"
android:layout_marginLeft="10dp"
android:background="#color/red"
android:padding="0dp"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="10dp"
android:textStyle="bold" />
</RelativeLayout>
// layour inflate in getview() methode
view perfectly dispay but its not clickable ,let me suggest where going to wrong or any other
** // autocompletetextview OnItemclicklistener**
search_text.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View myView, int position,
long arg3) {
Toast.makeText(getApplicationContext(), "Position = "+position, Toast.LENGTH_LONG).show();
}
}