Getting listView child on itemClicked - android

listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
//Get childViews here and set their click listener from here not from adapter
// something like view.forward_icon.setOnClick(...)
}});
I simply like to get the ListView item childs here and set their text or put an event from here not from adapter. can someone guide me how to do this. I have done it before but forget to use the technique. here is my item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<RelativeLayout
android:id="#+id/items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/listview_bg"
android:padding="5dp" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Mote"
android:textColor="#android:color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:padding="5dp"
android:text="20 March 2015 17:00 to 21:00"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/forward_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="3dp"
android:src="#drawable/forward_icon" />
</RelativeLayout>
</RelativeLayout>
I want to get the forward_icon here and set an onclickListener from here. I can do it from adapter but due to some accessibility issues . I want to do it like this.

Try the following:
int childcount = listView.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
}
And then set the OnClickListener for each View, hope it helps

What about:
ImageView imageView = view.findViewById(R.id.forward_icon);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
EDIT: actually combine that with zozelfelfo's answer:
int childCount = listView.getChildCount();
for (int i=0; i < childcount; i++){
View view = listView.getChildAt(i);
ImageView imageView = view.findViewById(R.id.forward_icon);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}

I have remember the solution by seeing the answer of #zozelfelfo and the other answer too.
The correct way of achieving the functionality is
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
TextView tv = (TextView)view.findViewById(R.id.item_title);
// you can also get other listView item childs like this...
tv.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(context, "Title is Clicked", Toast.LENGTH_LONG).show();
}
});
}});

Related

Why is Image OnClickListener() in not responding on click

This is my first try to ask a question. I am a beginner in android.When I am using image OnClickListener() in a simple way it is working but when I use gridView which has one imageView and one TextView on Image Click I want to jump to a new activity.
What I do is just call gridView OnItemClickListener() and jump to another activity which is working as I show below
gridView = (GridView) findViewById(R.id.gridview1);
gridView.setAdapter(new Adapter1(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent sendID_intent = new Intent(Activtiy1.this, Activity2.class);
startActivity(sendID_intent);
}
});
wherein Adapter1 I am extends BaseAdapter where an image is load using Picasso library which is also working the only issue is with image click event which is not working properly.
public class Adapter1 extends BaseAdapter {
private Context mC1;
ArrayList<View> viewList = new ArrayList<View>();
public Adapter1(Context context) {
mC1 = context;
}
#Override
public int getCount() {
return normlSearchList.size();
}
#Override
public Object getItem(int position) {
return normlSearchList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view1=convertView;
if ((position + 1) > viewList.size()) {
LayoutInflater inflater = (LayoutInflater) mC1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view1 = inflater.inflate(R.layout.view_grid,null);
ImageView imageView = (ImageView) view1.findViewById(R.id.iv_galleryb);
TextView textView = (TextView) view1.findViewById(R.id.tv_name);
textView.setText(normlSearchList.get(position).getPost_title());
final String txtID;
String img_url = normlSearchList.get(position).getImage();
Picasso.with(mContext)
.load(img_url)
.placeholder(R.drawable.it_logo_td)
.error(R.drawable.it_logo_td)
.noFade().resize(150, 150)
.centerCrop()
.into(imageView);
viewList.add(view1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(mContext, Activity2.class);
mC1.startActivity(intent);
}
});
}
return view1;
}
}
My layout file simply contain relative layout which holds a linearLayout a TextView for display data not found message if no data is found and a gridView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp"
>
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
...................
</LinearLayout>
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:gravity="center_horizontal"
android:visibility="invisible"
/>
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/layout1"
android:numColumns="3"
android:columnWidth="200dp"
android:gravity="center"
android:stretchMode="columnWidth"
/>
My viewgrid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="80dp"
android:descendantFocusability="afterDescendants">
<ImageView
android:id="#+id/iv_galleryb"
android:layout_width="100dp"
android:layout_height="80dp"
android:clickable="true"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/tv_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/iv_galleryb"
/>
I hope I make my problem clear.
I'd avoid use clickable views inside of GridView, instead of, I'd use RecyclerView. Because GridView implements onItemClick, it can conflict with ImageView clickable.
Anyway try add android:focusable="true" in <ImageView />

How to trigger radioButton to checked if I pressed on Linear layout

How can I make the radio button get selected if I pressed on other place ( not the radio button ) but on image, text view and etc inside 1 Linear layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/card_balance"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="#color/dc_white" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp">
<RadioButton
android:id="#+id/radio_button_payment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>

</RelativeLayout>
And here is the adapater: PaymentAdapter.java
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View allView = inflater.inflate(R.layout.item_choose_payment, parent, false);
TextView txt1 = (TextView) allView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) allView.findViewById(R.id.card_balance);
final RadioButton r = (RadioButton) allView.findViewById(R.id.radio_button_payment);
LinearLayout layout_card = (LinearLayout) allView.findViewById(R.id.layout_1);
r.setChecked(position == selectedPosition);
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedPosition = (Integer) view.getTag();
notifyDataSetChanged();
}
});
}
Right now the condition is I can pressed only at radio button.
I've already tried to add onClickListener to linear layout, and yes I can pressed on the linear layout however the Radio button not select only 1 but all selected.
this code for linearLayout onClick
layout_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
r.setChecked(position == selectedPosition);
r.setTag(position);
if (r.isChecked()) {
r.setChecked(true);
r.setSelected(true);
} else if (!r.isChecked()) {
r.setChecked(false);
r.setSelected(false);
}
}
});
Sorry for my bad explanation, any help is very grateful for me.
Thank you
inside your getView(...), write
layout_card.setTag(position);
and change click listener as
layout_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition = Integer.parseInt(v.getTag().toString());
notifyDataSetChange();
}
});
remove below code, if not needed
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedPosition = (Integer) view.getTag();
notifyDataSetChanged();
}
});

How to add button to custom adapter?

I am using FunDapter for my project.When i try to add imagebutton to my layout other texts become unclickable.Does nothing when i click them.How can i add button to this adapter?
Below is list_layout where everything works fine
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Isimsiz"
android:id="#+id/Housename"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="2dp"
android:clickable="false"
android:textColor="#241d1d"
android:layout_row="0"
android:layout_column="0"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Aciklama Yok"
android:id="#+id/Desc"
android:layout_below="#+id/Housename"
android:clickable="false"
android:layout_alignStart="#+id/Housename"
android:textColor="#241d1d"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_alignEnd="#+id/Housename" />
</RelativeLayout>
When i add image button nothing is clickable except imagebutton
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_alignTop="#+id/Housename"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/Desc"
android:clickable="true"
android:onClick="berk"
/>
And this is part of the class where i create my adapter
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
BindDictionary<Product> dict = new BindDictionary<Product>();
dict.addStringField(R.id.Housename, new StringExtractor<Product>() {
#Override
public String getStringValue(Product product, int position) {
return product.HouseID;
}
});
dict.addStringField(R.id.Desc, new StringExtractor<Product>() {
#Override
public String getStringValue(Product product, int position) {
return product.Desc;
}
});
FunDapter<Product> adapter = new FunDapter<>(
ListActivity.this, productList, R.layout.layout_list, dict);
lvProduct = (ListView)findViewById(R.id.lvProduct);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do sth
}
public void berk(View v) {
//do sth
}
Note:Also when i give clickable attribute to texts in xml they stop working.Like When i play with layout XML everything stops working.
In Adapter Class
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vehicals_details_row, parent, false);
Button deleteImageView = (Button) row.findViewById(R.id.DeleteImageView);
deleteImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
}
But you can get an issue - listView row not clickable. Solution:
make ListView focusable android:focusable="true"
Button not focusable android:focusable="false"
First of all if u are using button or edit text in your adapter or row file Then every thing will get stop working .
You should use
1.Set descendant Focusablity="before Descendants" in the list view.
2.Set window Soft Input Mode="adjust Pan" in the manifest
3.Set list view.set Items Can Focus(true)
you have to give separate click event for all views.
Your ImageButton has android:clickable="true" which makes sub view clickable. But you other views are not clickable.
Solution
For click event in textview
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
similar for Imagebutton
remove
android:clickable="true"
hope it helps.

Catching onClick events in child view of a list item

How can I catch onClick events in multiple child views of a list item in a ListFragment with custom Adapter?
I want the onClick event to return the item id and the child view id.
My list item layout is.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/event_status" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="7dp"
android:nestedScrollingEnabled="false">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/event_members" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/event_date"/>
</LinearLayout>
</LinearLayout>
I want to catch clicks on event_status and event_members separately.
I personnaly use android left side menu, which manages different fragments by clicking on a custom adapter
https://developer.android.com/training/implementing-navigation/nav-drawer.html
I think the interesting part for you is
myList.setOnItemClickListener(new myItemClickListener());
then
private class myItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code
}
}
Hope this can help
You can do it like this
private class YourAdapter extends ArrayAdapter<...>
{
TextView event_date = (TextView) convertView.findViewById(R.id.event_date);
event_date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
TextView event_members = (TextView) convertView.findViewById(R.id.event_members );
event_date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
What might also work, but I think there is no benefit in doing it like this and seems not clean:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// the same two handler here
TextView event_members = (TextView) convertView.findViewById(R.id.event_members );
event_date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}

ListView selection problem with rating control?

friends,
i have created very simple custom listview adapter with ratingBar in it.
now i have noticed one thing i cannot rate those rating bars in listview because
when i click on listview that row particular row gets selected.
any one guide me how to select individual items in android listview?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/android:list"
/>
</LinearLayout>
and listview_item design
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="265dip"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:textSize="25dip"
android:text="This is text1"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text2"
android:text="This is text2"/>
<RatingBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:id="#+id/star"
android:numStars="10"
android:stepSize="0.1"
android:isIndicator="true"
/>
</LinearLayout>
</LinearLayout>
any help would be appreciated.
Dear UMAR
In order to implement the listeners to the each element to the listview(Single row) use Custom adapter and in the getview(....) method of the custom adapter ,implements the listeners which u want.. sample code `public class FindFriendsListAdapter extends BaseAdapter {
private ArrayList<SingleElementDetails> allElementDetails;
private LayoutInflater mInflater;
private Context context;
private String userid1;
private int usersno1;
private DBAdapter db;
public FindFriendsListAdapter(Context context, ArrayList<SingleElementDetails> results,String userid,int usersno) {
this.context=context;
this.userid1=userid;
this.usersno1=usersno;
allElementDetails = results;
mInflater = LayoutInflater.from(context);
db=new DBAdapter(context);
db.open();
}
public int getCount() {
return allElementDetails.size();
}
public Object getItem(int position) {
return allElementDetails.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.friendlisthelper, null);
ImageView imageview = (ImageView) convertView.findViewById(R.id.friendimageview);
TextView textview = (TextView) convertView.findViewById(R.id.friendtextview);
Button button=(Button)convertView.findViewById(R.id.friendbutton);
convertView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//do what u want
}
});
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// do what u want
}
});
return convertView;
}
}
`
android:isIndicator="false" in order to rate it. and listview selection will be gone automatically and control will be transferred to that rating control.
Try this
ListView lv_party;
lv_party.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position,long id)
{
// TODO Auto-generated method stub
view.findViewById(R.id.btn_del).setOnClickListener(new View.OnClickListener(){
//your code
}

Categories

Resources