how to change Button background in Custom adapter on Its onClick? - android

I am trying to change UI of button in custom adapter click, I am able to do functional thing on particular button click event. But when I tried to change UI of any Button, it reflects to last added Button.
I tried with setTag() option too.

use V.setBackgroundResource(R.drawable.drawableName);
it worked for me, where the drawable name is the background resource in your drawable folder.

you can set the background of button inside onClick event like this :
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundDrawable(drawable);
}
});

I found answer by myself.
When any Inflator and Holder used in Custom adapter, you need to create new Object for every single row (data), so you can access any item belongs to their family.

Related

custom listview save selected item position

I create a custom listview.
I want to select any item using another button click or something views click.
Already I tried via these code for custom selection.
And I also want to get previous selected item on resume.
listView.setItemChecked(2,true);
listView.setSelection(2);
listView.requestFocus();
But everytime , I failed.
I used this method to create custom listview.
Android ListView with Custom Adapter Example Tutorial
Advance Thanks .
For setItemChecked to work, your list items need to be Checkable; you can, for example use CheckedTextView (at the top level of the item view, no further layout around it).
For setSelection to work, you need to use it inside a Runnable, like so:
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(2);
}
});
In both cases, don't ask why.
For a selection to become visible, you can put a statelist in the background attribute of your list item.

Handling multiple clickable areas in listview

I'm using a ListView to show some images and names in my application. I can make the ListView tiles clickable and start new activities as well . But now i want to do different actions for the different items that are on one listview tile. If user clicks on image, image opens and the textView will show some toast or something. The ImageView and TextView are on the same ListView Tile. How can i achieve this? Any Help?
Define OnClickListener events in your adapter for the button and textview as required and you are done. They will precede the click event for the list item by default.
if the contained views in your custom row layout does not have a click event it will fall back to the click event of the list view item.
You can implement click event in adapter, but you also implement it in activity.
You can find solution in :http://www.migapro.com/click-events-listview-gridview/
hope it can help you.
Some note: should not use view holder in gridview like above tuturial, it can make some bug, can implement direct like this:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
Another way to implement this elegantly is to add secondary views called 'accessories' to the item views associated with your ListView. This is explaned quite well in the link below by Cyril Mottier: A Google Developer Expert on the Android platform:
http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/

Which one is better, setting OnClickListener on a Button or View?

I want to set OnClickListener on a Button. Should I cast it to the Button or let it in View? You can see from the code below, the former cast the View into Button while the later let the widget in a form of View. Is the later better because it doesn't need to do the casting or is it just do the same?
It's not that I don't understand how to set click listener on a button. I just want to know it in term of performance and best practice point of view.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(listener);
or
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
View button = findViewById(R.id.button);
button.setOnClickListener(listener);
The Same, You only save the cast (Performance).
if you dont need the View object I would prefer
findViewById(R.id.button).setOnClickListener(listener);
I believe there's no difference in performance. What changes is your code readability so, if you're setting a clickListener for a button and you also use the button instance later in the code you might want to use "button".setOnClickListener.
Since, most of the times you don't need to keep a reference of the clicked view, i'd suggest to use something like ButterKnife
Performance wise there is almost no difference. For readabillity I would suggest using Button and not View.
I am pretty sure the difference in performance is minimal or simply there is no difference.
However, there is a difference between View and Button.
To shorten the answer and be as specific as possible ,I will tell you that if you want the button to be as "Standard" (Android standard at least) as possible, you should use the Button ,but if you want to sort of create a Custom Button, than you should go ahead and use the View.
To make it a bit more clear ,the Buttom is actually a View ,so when you use a Button, you actually use a View that is customized for the purpose of being a button and acting like one.
If you need more details, ask up,I will try to help or link you some documentation.

Select from dynamically created imageviews

I have generated dynamically ImageView's in LinearLayout - let's say 6, with backgrounds and images, so they are looking like an icons.
Now I would like to select one and based on this which icon is selected proceed with other things.
I know that I can set onClickListener for dynamically create ImageView's.
but question is, how to select one? with jQUery I would add some class after tapping on an icon, in Android ? I really do know, tried something with setTag() but, well, did not happened.
Assuming that somehow I know which icon was tapped, then how can I loop through all dynamic generated ImageView's to get selected one?
Lets say in a loop you are creating the ImageView's and adding that to the LinearLayout. Assign a onClickListener to all the ImageViews. Just like the following code.
yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView selectedImageView = (ImageView) v;
// selectedImageView is the imageView which you have selected
}
});
So when you tap on a imageView, its onClick function will be called. Parameter passed to the onClick function will be the imageView which you have selected. Just typeCast the View 'v' to ImageView and use in your application.

Is it possbile to call android spinner from a click of ImageButton?

I have an image button which says Select city I want to call an android spinner if anyone clicks on that image. Is it possible??
I would recommend you check out the App/Alert Dialogs section of the API Demos application. I think what you are looking for is a dialog with a single choice list.
Or take a look at this:
http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList
This is entirely possible in several ways. One way is to have a spinner whose visibility is GONE. Clicking on the image makes the spinners visibility VISIBLE.
Another choice is to create a dialog that has the spinner and use the selection to do whatever it is you want to do.
There are other options, but that should get you started.
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.performClick();
}
});

Categories

Resources