I have a relative layout to which child views are added and removed dynamically(any number can be added or removed)
My question is how to know which view was clicked so that i can add different onclicklisteners depending on the type of child views
Adding and retrieving the tag while click event can help. Here is the code.
For adding tags:
customView1.setTag(someTag);
customView1.setOnClickListener(myClickListner);
For retrieiving:
OnClickListener myClickListener = new onClickListener(){
#Override
public void onClick(View v) {
if(v.getTag() == someTag){
//do stuff
}else if(v.getTag() == otherTag){
//do something else
}
}
in your adapter class you need to write like this am sharing the sample code snippet
public static class ChatListItemsViewHolder extends
RecyclerView.ViewHolder {
public ChatListItemsViewHolder(View v) {
super(v);
// TODO Auto-generated constructor stub
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// write your code here
}
});
}
let me know if you need more clarity.
Related
I saw some different ways of implementing onItemClickListener for RecyclerView. My favourite is to use interface callback:
Interface:
public interface OnClickListener {
void onCardClick(View v);
void onFavouriteButtonClick(View v);
void onRemoveClick(View v);
//Other clickable areas
}
In adapter:
currentView.favouriteButton.setTag(currentItem.getId());
currentView.favouriteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (OnClickListener != null){
OnClickListener.onFavouriteButtonClick(v);
}
}
});
In main activity/fragment
adapter = new CardAdapter( .... new OnClickListener() {
#Override
public void onFavouriteButtonClick(View v) {
int id = Integer.parse(v.getTag().toString());
//Do your stuff
}
....
});
So, I have some questions:
is it good way of handling things?
will performance be slow if number of items will grow?
any reasons why I shouldn't use this approach?
I think you could improve upon this by not passing the view outside of the adapter.
// Create an object that encapulates the information for each card
public class CardInfo {
// Whatever you want to show in your card
}
// Create a listener for items instead of views
public interface OnCardAction {
void onCardSelected(CardInfo cardInfo);
void onFavoriteSelected(CardInfo cardInfo);
void onCardRemoved(CardInfo cardInfo);
}
// In your adapter
favoriteButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// You many need to check if onCardActionLisetner is null
CardInfo cardInfo = getItemAt(viewHolder.getAdapterPosition());
onCardActionListener.onFavoriteSelected(cardInfo);
}
});
NOTE: If you are passing the listener in the constructor can either make it required (throw error if it is null) or not even create View.OnClickListner to reduce the number of null checks.
Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews, one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextViews should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected.
here is the screenshot of the my listview
https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706
You can do it in two ways:
1. Either by directly using onClickListener like this:
textView.setOnClickListener(new View.OnClickListener(
public void onClick(View arg0) {
// Do anything here.
}
});
OR
2. In XML file, in declaration of <TextView /> add one more attribute as:
android:onClick="onClickTextView"
and in yout activity, add this function:
public void onClickTextView(View view) {
// Do anything here.
}
UPDATE:
Use following code to get click event on TextView:
// Click event for single list row
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) (findViewById(R.id.title));
if (tv != null) {
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CLICKED",
Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(MainActivity.this, "TV not found",
Toast.LENGTH_LONG).show();
}
}
});
Try this :
When you select one textview the other three will be unclickable
final TextView texta = (TextView) findViewById(R.id.text_a);
final TextView textb = (TextView) findViewById(R.id.text_b);
final TextView textc = (TextView) findViewById(R.id.text_c);
final TextView textd = (TextView) findViewById(R.id.text_d);
texta.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
textb.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textb.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textc.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textd.setClickable(false);
}
});
textd.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textc.setClickable(false);
}
});
Assume you extend BaseAdapter to set the listview content ->
Open a TextView listener and settag of the current holder position , and perform your operation in the onclick method.
It's the default behavior of a ListView. Only one could be clickable: either the list row or the items inside the row.
Eg: if the row item is a textView(as in your case) the list row will be clickable but if the row item is a button then the the list row will not be clickable. Same is the case if you make TextView as clickable.
For your requirement the better approach would be to use RadioGroup (instead of multiple text view and disabling and enabling them).
You should use a custom layout for you list item with a TextView for question and a RadioGroup for options.
Layout could be something like this :
Follow these links for reference:
for listView
for RadioGroup
I hope this will help
Thanks for Shrikant and adam for there help Sorry and i appologize for a very late response.
either use this in adapter class as by Shrikant:
textViewa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do anything here.
}
});
textViewb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Do anything here.
}
});
//and so on...
// or better to use ViewHolder holder; for these type of listviews;
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do what you want to do.
// for my i have to call a method in my parent activity. so in constructor of adapter, I passed the activity and then typecasted it like
ParentActivity parent = (ParentActivity) activity;
parent.chosenAnswer(view.getId());
// then in chosenAnswer(int id) in parentActivity use a switch case for the same logic as in Adam's answer.
// OR
//you can write like this too..
switch (view.getId()) {
case R.id.textViewa:
break;
case R.id.textViewb:
break;
case R.id.textViewc:
break;
case R.id.textViewd:
break;
}
}
};
I have a parent view LinearLayout i have added a child view TextView, But whenever i click on childview why parentview is also clicked. What i want is to differentiate whether only parent has been clicked and only childview is clicked.
LinearLayout rlmain = (LinearLayout)findViewById(R.id.linearLayout1);
rlmain.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
TextView tv = (TextView )findViewById(R.id.TextViewLayout1);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
then try to implement View.OnClickListener in yout Activity first, then try to override
#Override
public void onClick(View v){
if(v.getId().equals(R.id.linearLayout1) {
//do ur stuffs
} else {
//do your stuffs
}
Hope it will help you. :)
In android, ViewGroups (i.e. all layouts by inheritance) use the property (and corresponding xml attribute ) : addStatesFromChildren
If set, the parent will just be set in the very same state as one of his children view.
try this code...
LinearLayout rlmain = (LinearLayout)findViewById(R.id.linearLayout1);
rlmain.setOnClickListener(this);
TextView tv = (TextView )findViewById(R.id.TextViewLayout1);
tv.setOnClickListener(this)
#Override
public void onClick(View v){
switch(v.getId()) {
case R.id.TextViewLayout1:
//do ur stuffs
break;
case R.id.linearLayout1:
//do ur stuffs
break;
}
}
Please help.
As I have stated in the title I am trying to make that individual elements of a row of a List adapter launch different actions depending on what the user click.
It "kind of" works but it takes LONG for it to react to user clicks. What is it that I am doing wrong?
Thanks in advance,
So I tried the following code in
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Cursor c = (Cursor) this.getListAdapter().getItem(position);
// c.moveToNext();
prescription_id = c.getString(0);
TextView pName = (TextView) v.findViewById(R.id.text2);
TextView paName = (TextView) v.findViewById(R.id.text3);
TextView rDateLabel = (TextView) v.findViewById(R.id.textView1);
TextView rDate = (TextView) v.findViewById(R.id.text4);
TextView rLeftLabel = (TextView) v.findViewById(R.id.text5);
TextView rLeft = (TextView) v.findViewById(R.id.text6);
ImageView callPhone = (ImageView) v.findViewById(R.id.Call_Pharmacy);
pName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
pa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDateLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeftLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeft.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
callPhone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Some Code
}
});
}
All those onClick listeners (those on single sub-views of one ListView element) probably shouldn't be here in the onListItemClick method, but in the getView method of your Adapter instead (with proper use of the convertView argument).
The way you do it seems quite wrong, maybe your onListItemClick method isn't even needed if you correctly implement the various onClick listeners at the right place.
Using an xml based layout for your list item is key here. Set each individually clickable View with two attributes android:clickable="true" and android:onClick="<your click handler>" the method will need to be implemented with this signature: public void <your click handler> (View v) {...} in your Activity. A side note is that you'll have to make a design decision to implement a click handler to overlap handling (one click hanlder for more than one View) or a single view handler per View, the former is best for when click are substantially similar in function and the latter is when they are different.
The next step is to implement the click handler, the key here is to use ListView.getPositionForView(View v) so you can associate the row, the data, and the View clicked.
Don't forget to implement ListActivity.onListItemClick() as a catch-all for clicking on the root layout of the list item and as a catch-all for Views that don't have their own onClick handler set.
The above technique will have good performance and makes use of several Android API's to speed your development.
If you decide to implement the listeners in code, please study getView() closely (as darma mentioned) and for the sake of performance (if you have several items in your list) reuse the click listeners with the above discussion about how to associate the data and row.
i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.