Click is not working on the Listitem Listview android - java

I implemented the android listview with the ListActivity. Here I have the problem that when i click on the list item no action is performed when the flash color is also not coming that is the orange color. So do you have any idea about this kindly answer to my question.
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT)
.show();
}
I put this code also into the Main ListActivity.

The first thing what you have to note here is, whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. And so your ListView won't get the chance to accept the click event.
What you simply have to do is, set the focusable attribute to false for the Button or ImageButton you have in your ListView. But still they will work without any problem and also your ListView's onListItemClick will also work.
Try this,
<Button android:id="#+id/textsize_increaser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/back_button"
android:focusable="false"
android:text=" A + "/>
Here I have added this android:focusable="false" and it works fine. try it.

Have you set the choice mode of ListView to SINGLE :
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
And if you have any clickable imageview or textview or button in the list item, then make them not focusable (in your Adapter class):
yourButton.setFocusable(false);
yourButton.setFocusableInTouchMode(false);

Are you using custom Adapter? and inflating layout with button or any view that eats away the list list view focus as child, then it won't work obviously. make sure to set
android:focusable="false"
to such view in xml file. hope this works for you.

Set this in your listactivity java file
listview1.setFocusable(false);

Actually there is a parameter meant for that to prevent children views from getting focus, just add the following in the parent layout:
android:descendantFocusability="blocksDescendants"
As the documentation explains:
The ViewGroup will block its descendants from receiving focus.

Eclipse suggested me to add textIsSelectable="true" to my TextViews in the layout xml which was used for list view.
Well, if you want to click the items in the list then you should not add those tags.

make sure that you are
Not using Scroll View with List View
Not using Scroll View in your row item layout for List View
If Scroll View is present at any of above place remove it

refer to this post for a solution:
Click is not working on the Listitem Listview android
View v = parent.getChildAt(position);
parent.requestChildFocus(v,view);
v.setBackground(res.getDrawable(R.drawable."Some drawable for clicked row"));
int count = parent.getChildCount();
for(int i=0; i<count; i++)
{
if(i!=position)
{
v = parent.getChildAt(i);
v.setBackground(res.getDrawable(R.drawable."some drawable for not clicked row));
}
}

listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
Toast.makeText(v.getContext(), exm.get(pos).getDefinition(),
Toast.LENGTH_SHORT).show();
}
});

listItemButton.setFocusable(false);
listItemButton.setFocusableInTouchMode(false);
Set the above in your adapter. It's not working in XML

Related

Programmatic changes to a view aren't applied

I made a list that's loaded with Contact friends and the user can select them by tapping on them. If a person is selected, the listitem's backgorund changes colour, if deselected, the bg colouring goes away.
Problem is, when I call my method on an OnClickListener, it's fine.
When I however call it in a loop to colour already selected friends (e.g. when revisiting the list), it doesn't do the colouring.
The loop that goes through the elements to call colorize if needed:
for (int i = 0; i < adapter.getCount();i++){
ContactFriend cf = (ContactFriend) adapter.getItem(i);
View v = getViewByPosition(i,listView);
colorizeFriendBg(v, cf);
adapter.notifyDataSetChanged();
}
note I do the exact same in the listener and it works fine there.
And the colorizer:
private void colorizeFriendBg(View v, ContactFriend friend){
if(friend.isSelected()){
v.setBackgroundColor(0x993399ff);
}else{
v.setBackgroundColor(0x00000000);
}
v.invalidate();
}
This issue is quite strange and I have no idea what to do in order to make it right. The whole bunch is called from onActivityCreated, if that matters.
Edit:
I debugged it of course and the code runs and should change the colour, not running isn't the issue.
Edit again:
here's the listener implementation:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ContactFriend fr = (ContactFriend) adapter.getItem(position);
addToSelected(fr);
//TODO: make it switch some BG colour when clicked. use getViewByPosition.
View v = getViewByPosition(position,listView);
colorizeFriendBg(v,fr);
adapter.notifyDataSetChanged();
}
});
what type of item View are you getting from the Adapter?
that View could / should implement colorize() and color itself;
for example: v.colorize(contact.isSelected()) to switch colors.
or with Android Data-Binding XML (where the viewModel is an instance of Contact):
<data class="com.acme.databinding.ContactViewHolderBinding">
<variable name="viewModel" type="com.acme.model.Contact"/>
</data>
...
android:backgroundColor="#{viewModel.isSelected ? R.color.MAGENTA : R.color.BLACK}"
class Contact just would require a getter and a setter for property isSelected.
one actually can also bind event handlers, which would be an alternate approach.
You need to call invalidate() on your view to make the color changes visible.
invalidate() forces a redraw with the new colors.

How to give color for listview in android

i am having a list view in which i would like to give a background color to a row when it is pressed.When i select an item its changing the color but the problem is when i scroll down other rows too are got colored.I don't want that.
Here is my code:
Thanks in advance
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
view=parent.getChildAt(position);
view.setBackgroundColor(Color.GREEN);
}
});
}
I think you should handle that in your adapter. A listview re-uses listelements and in your case the one with green background will be reused when you scroll. Instead you should tell your adapter which item was clicked
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
((YourAdpater)listView.getAdapter()).setSelected(position);
}
});
And in your Adapter's getView() you can then set a background if the current position equals the selected one or not...
Or look at the listview's choice mode http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:choiceMode
I'm not really familiar with it, but it might be helpful...
The nicest way to handle this is to set listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);, which allows you to select multiple list items.
To highlight the selected items, all you have to do is create a custom background selector, which defines how the items will look like in which state. For your example this would be something like this:
selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white" android:state_activated="false"/>
<item android:drawable="#android:color/green" android:state_activated="true"/>
</selector>
Put this file in your drawable folder and give your list items this background like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector">
...
</LinearLayout>
This way you don't have to handle clicks on the items.
You can retrieve the selected items via listview.getCheckedItemPositions()
To highlight a ListView you dont have to manually colour them, Android already has a function for it.
m_ResultsArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_activated_1,m_ResultsArray)
Instead of using a simple_list_item_1 use a simple_list_item_activated_1. This will allow ur list to be highlighted and you can even check which one is checked by using
this.m_ResultsListView.getCheckedItemPosition();
Hopefully this answers your question :>

Android gridview change background by position

I have Gridview with images. above the image I have text(hello) and I can to show images(without text) to my gridview .now I want if I click gridview's 5th element I want to hide 5th image's text.meybe problem solution is gridview.setOnItemClickListener but I do not know how I can do this
I wrote some code witch can to show toast message by position, but I do not know how I can to hide text by position
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(), "Cliked" + position,
Toast.LENGTH_SHORT).show();
}
});
You could try setVisibility along with an array to sort through the GUI identifiers:
int textId = this.getResources().getIdentifier(position, "id", this.getPackageName());
TextView picText = (TextView) findViewById (R.id.TextID);
picText.setVisibility(TextView.INVISIBLE);
Or something like that. INVISIBLE makes it invisible, but take up the same space - GONE will make the space disappear too - so depending on what you want.
That should at least get you in the right direction.
For more: http://developer.android.com/reference/android/view/View.html#setVisibility(int)
First Find the textView that you want INVISIBLE.
Then set it INVISIBLE.
By below Code
((TextView) v).setVisibility(TextView.INVISIBLE);
if above Line dont work use below
TextView textview=(TextView)v.findViewById(R.id.id_of_textview);
textview.setVisibility(TextView.INVISIBLE);

Change the View's background of a ListView initially

I'm now implementing an application that contains an Activity includes ListView, when the user selects any item in the list view, the background & text color of this view are changed, So, i placed the code of this changes in the onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) method ..but,this is not the problem.
The problem is, when i open the activity, i need to make an initial selection "before the user selects any thing"..so i made listView.setSelection(index);, but, unfortunately, this code doesn't invoke the onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)..So, the view doesn't changed "background and text color".
Any solution for that..?!!!
Thanks,
try onItemSelected for selection
selecting a iem in list view does not fires onItemClick
Create a method in yout Adapter to set the selection (or do it in the constructor):
public myAdapter (COntext context, int initialSelectedPos){
setSelectedPos(initialSelectedPos);
}
public void setSelectedPos(int pos){
mSelectedPos = pos;
}
Then check in your getView is given pos is the same than mSelectedPos.
#Override
public View getView(..., int pos){
/*convertView stuff*/
if (pos == mSelectedPos){
//Put the background as it is selected
}else{
//...
}
return view;
}
In your OnItemClick method from your OnItemClickListener call the setSelectedPos method of your Adapter.
You will solve the issue you commented and also when your selected view is no longer visible on screen and comes back to screen, will still be marked as selected ( I am pretty sure it was appearing with the original background).
after setting selection
listView.setSelection(index);
and then after call the
listView.getAdapter().notifyDatasetChanged();
i think this will solve your problem

Android: Cannot get a clickable ListView header/footer

I'm trying to set a header and footer in my list view that are clickable buttons. The problem is that the OnClickListener isn't responding to anything and I can't figure out what I'm doing wrong.
$ View header = getLayoutInflater().inflate(R.layout.header_layout, null, true);
getListView().addHeaderView(header);
myAdapter = new myAdapter(this);
header.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do what I want when i click it
}
});
Update
The best solution I ultimately came up with was adding a separate button to the header layout, and then doing it like this:
View header = getLayoutInflater().inflate(R.layout.header_layout, null);
Button headerButton = (Button)header.findViewById(R.id.header_button);
getListView().addHeaderView(header);
headerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// My Click Stuff
}
});
You need to handle the click in the ListView's onItemClick. Simply check if that's the first or last item in the adapter and handle the clicks that way. You need to treat it as an item in the ListView.
I see a few issues:
when inflating the header, use getListView() as the second parameter (root, where you have null now)l
should the header be a View or a ViewGroup? I've ended up using ViewGroup in these situations.
finally -- perhaps you should be setting the click listener on the button in the header instead of the header itself?
There is a way more easier solution:
Just set a "OnClickListener" to the applied View:
View view = inflater.inflate(R.layout.xxx, null);
view.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//do something
}
});
Very easy thing which solved it!
Example with a footer:
mYourListView.addFooterView(footer, null, true);
Then in the OnItemClickListener you can check:
#Override
public void onItemClick(AdapterView<?> parent,
View view, final int position, final long id) {
if (id != -1) {
// do whatever you do with list items
} else {
// do what you need after the footer been clicked
}
(If you need to handle and the header and the footer click, check position - 0 for the header and [adapter.getCount() - 1] for the footer)
This approach will provide the same visual effect while footer click as if the list item been clicked. (But if you do not need that effect just add OnClickListener to the footer and it will intercept all footer clicks)
i think the listview and buttons are fighting for focus and your listview is winning.
you'll have to use a onTouchListener for the buttons.
There is one more alternative and its to set the following on the footer. There is another method overload created but it did not come up in the docs, i had to check online documentation:
mylistView.addFooterView(footerView, null, false);
where false tells the footer its not selectable. I tested this myself and the buttons inside the footer respond to touches now. I hope this an acceptable answer.
What worked for me:
When inflating the header view, before adding it to the listview, I then used that view to get the subview and add a click listener on it
myHeaderView.findViewById(R.id.myButton).setOnClickListener(new OnClickListener() { ... } );
also, when I added it to the listView, I used the overloaded constructor with the third variable set to true
mListView.addHeaderView(myHeaderView, null, true);

Categories

Resources