How to get position of listview on imageview click? - android

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();
...
}
});

Related

Android: Unable to setText in Custom ListView

I have created a custom ListView with 2 ImageView and a TextView.
In the Adapter, I'm doing setText in TextView, but it is not visible in the app.
Here is my List:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#289FA5"/>
<RelativeLayout
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Popular products"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="120dp">
<LinearLayout
android:id="#+id/pop_pro_men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Catagories"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<ListView
android:id="#+id/cata_men"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
Here is my Custom List:
<LinearLayout
android:id="#+id/custom_list"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#ffffff">
<ImageView
android:layout_gravity="left"
android:id="#+id/imageView2"
android:layout_height="130dp"
android:layout_marginLeft="7dp"
android:layout_width="120dp"
android:src="#drawable/clothing_men"/>
<TextView
android:id="#+id/cata_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clothing"
android:layout_marginTop="35dp"
android:textStyle="bold"
android:textSize="27sp"
android:padding="10dp"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward_arrow"
android:layout_marginTop="42dp"/>
And Here is the getView method of my Adapter:
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_subcatagory_list, null,true);
TextView sub_catagory= (TextView) rowView.findViewById(R.id.cata_name);
ImageView pic = (ImageView) rowView.findViewById(R.id.imageView2);
sub_catagory.setText(catagory[position]);
pic.setImageResource(image[position]);
Log.d("Strings========", sub_catagory.getText().toString());
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + catagory[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
Please Help as i am unable to find what is wrong in this.
Remove your layout_weight attribute from your TextView. If you remove sub_catagory.setText(catagory[position]); does "Clothing" appear when you run your app?
I got the answer now. I don't know how my textColor was set to White, because i didn't mentioned it. So, change the color to something else and it worked.And it wasted my whole day.
Sorry to disturb you all.

Change the height from a RelativeLayout inside costumeadapter

Hey guys I'm newbie at Android development, and I would like to build a sub item for each listview item.
Just like this:
ListView 1
sub item 1
ListView 1
sub item 1
I though that it would be esier to create a new relative layout for each listview item, so my XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrap_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/first_col"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bg_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/overlay_bg" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="9dp" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/current_event"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:letterSpacing="1.2"
android:shadowColor="#color/dark_grey"
android:shadowDx="-2"
android:shadowDy="2"
android:shadowRadius="0.01"
android:textColor="#color/white"
android:textSize="22sp" />
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/persons" />
<TextView
android:id="#+id/current_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_alignParentBottom="true"
android:text="TextView"
android:textColor="#color/grey"
android:textSize="13sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/second_col"
android:layout_width="fill_parent"
android:layout_marginTop="200dp"
android:layout_height="200dp" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
</RelativeLayout>
Each item is divided by two columns, the second column should be hidden column, thats why I changed the layout height to 200dp.
And to show the full layout, I'm doing that:
public View getView(final int position, View convertView, ViewGroup parent) {
// here you let SimpleAdapter built the view normally.
final View v = super.getView(position, convertView, parent);
final RelativeLayout lebg = (RelativeLayout) v.findViewById(R.id.wrap_layout);
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
}
});
return v;
}
I just need to change the height from 200 to 400 to show the full Listview item.
The issue:
Is not working that well because when I click at the listview it only changes the height when I scroll...
Is there any better way to do what I am trying?
Thanks.
When you change LayoutParams you have to call requestLayout to see changes:
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
requestLayout();
}
});

Android Layout height not updating on button click

In a layout I have a button which toggles view's visibility (hides/displays a linear layout). Inside this layout I also have a spinner. When I click the button it toggles the visibility but it does not change the layout height to wrap_content, but when I change the spinner value it updates the height.
This is the XML Layout code
<LinearLayout
android:id="#+id/layoutA1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvAlarmHeading"
android:layout_margin="5dip"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/transparent_border"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layoutA1C1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spnParameterA1C1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnAddA1C1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#drawable/button_pattern"
android:text="+"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutA1C2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spnConditionA1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnRemoveA1C2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#drawable/button_pattern"
android:text="-"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is the activity code where I toggle the views visibility
//Set the on item selected listener on the parameter spinner for alarm1 condition 1
spnParameterA1C1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id)
{
tvUnitA1C1.setText(unitList.get(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent){}
});
//Set onClickListener for add condition button for Alarm 1
btnAddA1C1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layoutA1C2.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
layoutA1C2.setVisibility(android.view.View.VISIBLE);
btnAddA1C1.setVisibility(android.view.View.INVISIBLE);
layoutA1.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}
});
//Set onClickListener for add condition button for Alarm 1
btnRemoveA1C2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layoutA1C2.getLayoutParams().height = 0;
btnAddA1C1.setVisibility(android.view.View.VISIBLE);
layoutA1C2.setVisibility(android.view.View.INVISIBLE);
layoutA1.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}
});
I have also tried layoutA1.refreshDrawableState();. Can anybody tell me which method should I call to refresh the layout length?
You need to set the layout params again in order to refresh the view.
try :
LayoutParams params = layoutA1C2.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
layoutA1C2.setLayoutParams(params); // this call is what you need to add

Android custom List row with buttons onclick get index

I created a list view with a custom list row. That list row contains a image view with four imagebuttons:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:padding="2dip"
android:id="#+id/list_image"
android:layout_width="match_parent"
android:layout_height="200dip"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/test"
android:scaleType="centerCrop"/>
<LinearLayout
android:paddingTop="5dip"
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/list_image"
android:layout_alignRight="#+id/list_image"
android:layout_below="#+id/list_image"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/copyLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/copy"
android:background="#color/white"
/>
<ImageButton
android:id="#+id/visitHP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/website"
android:background="#color/white" />
<ImageButton
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/delete"
android:background="#color/white" />
<ImageButton
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_social_share"
android:background="#color/white" />
</LinearLayout>
As a pic:
Now i would like to add to each of the buttons a onclicklistener where i can get the index it of the row or the object of the row.
I tried to implement the OnClickListener to my ArrayAdapter:
ImageButton btn = (ImageButton) convertView.findViewById(R.id.delete);
btn.setOnClickListener(this);
public void onclick(View v) {
int img = v.getId();
}
But with this i wont get the index of the row.
How should i approach this problem ? Thanks
You can use button.setTag() in getView()
And v.getTag() in onClick()
it works in this way:
public void onclick(View v) {
int id = v.getId();
switch(id) {
case R.id.share: .... break;
case R.id.delete:
...
}

Disabling click event for a view but not for its childs

I have a ListView that its items have some buttons as child/sub view (buttons like share, reply, like, etc). I want to disable clicks on items but not for childs. For example when clicking on a like button, its color changes and nothing happens to the list view item. (see nine gag app)
I read similar questions saying about overriding areAllItemsEnabled and isEnabled and I did that (returning false for both) but didn't get proper result (childs also are unabled)
Can you please help me out? And sorry for my bad English
in custom adapter:
public CustomCommentAdaptor(Context context){
ctx = context;
listData = new ArrayList();
}
#Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
listview in main layout xml file
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:groupIndicator="#null"
android:layout_above="#+id/leave_comment" />
and listview items layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!-- android:padding="10dip" -->
<RelativeLayout
android:id="#+id/comment_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:minHeight="40dp"
android:padding="5dp">
<ImageView
android:id="#+id/user_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#+drawable/brad" />
<TextView
android:id="#+id/comment_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="#+id/user_image"
android:textSize="15sp" />
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="#+id/user_image"
android:textSize="12sp" />
<RatingBar
style="#style/myRatingBar"
android:layout_height="12dp"
android:layout_width="wrap_content"
android:numStars="5"
android:rating="5"
android:isIndicator="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:stepSize="1"
android:layout_toLeftOf="#id/comment_title"
/>
</RelativeLayout>
<TextView
android:id="#+id/comment_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/comment_header"
android:textSize="8sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/comment_content"
android:orientation="horizontal"
>
<include
android:id="#+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions" />
<include
android:id="#+id/dislike"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
/>
<include
android:id="#+id/reply"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
/>
<include
android:id="#+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
/>
</LinearLayout>
</RelativeLayout>
Set a click listener only on your buttons. So, in your adapter:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
View likeView = convertView.findViewById(R.id.like);
likeView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Do something.
}
}
}
Don't set a click listener on your list. So, if you are doing this anywhere, take it out!
listView.setOnItemClickListener(new OnItemClickListener() ...);
Finally, to make the like button change colors when pressed, set a selector as the background of your comment_actions layout.

Categories

Resources