Android - Listview items change background when selected (checkable) - android

How can I make a ListView item changed its background when pressed, like it was checked but no checkbox, I know it has a checkListener or something but i don't know how to apply. Remain its color background after pressed and changed back to its first background when pressed again.

Your listItem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
</LinearLayout>
Your selector: listview_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/list_selected" android:state_activated="true"/>
<item android:drawable="#drawable/list_default"/>
</selector>
Your Layout with listivew:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewMainFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/listview_selector.xml"
android:choiceMode="singleChoice">
</ListView>
</LinearLayout>

1) Create boolean field in your Model-Class.
2) Define root Layout of item (create id and find view in ListView Adapter's getView() method).
3) Add OnClickListener for root Layout, if clicked new boolean = true.
4) If new boolean of your Model-Class item is true, set root Layout bg_color_1, else set bg_color_2.

Try this
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(Color.GREEN);
}
});

Related

Avoid showing selected text in Spinner

The default behavior of the android spinner is to initially display the first item in the top of the dropdown portion of the spinner. As the user selects other items/text in the dropdown, it will update the text at the top with the selected item.
I don't want any text to show up at all in the top part of the spinner. Without having an empty sting as the first item, because that leaves a gap. Does anyone know how to do that? This is what I have so far
styles.xml
<resources>
<style parent="#android:style/Widget.TextView.PopupMenu" name="SpinnerAsEditText">
<item name="android:background">#drawable/filter_arrow</item>
</style>
</resources>
this is my layout: main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:layout_height="match_parent">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/list"
style="#style/SpinnerAsEditText"/>
</RelativeLayout>
This is a visual representation of my problem - if you click on this image/gif, it should animate my problem.
You can use popUpWindow to show dropDownList.
<ImageView
android:id="+#id/spinnerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showSpinner" />
in java
findViewById(R.id.spinnerImage).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSortPopupWindow = popupWindow(spinnerItemList);
mSortPopupWindow.showAsDropDown(v, -5, 0); // This is impotent to where to show drop down
}
});
public PopupWindow popupWindow(String[] stringArray) {
PopupWindow popupWindow = new PopupWindow(mContext);
ListView listView = new ListView(mContext);
listView.setAdapter(dropDownAdapter(stringArray));
listView.setOnItemClickListener( AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, "Selected" + stringArray[position], Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.setFocusable(true);
popupWindow.setWidth(250);//Or you can set wrap_content
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(listView);
return popupWindow;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/list"
style="#style/SpinnerAsEditText"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="list">
<item></item>
<item>second</item>
<item>third</item>
<item>fourth</item>
<item>fifth</item>
</array>
</resources>
You could use an image which when clicked on shows the spinner. Initially the spinner would be hidden.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:layout_height="match_parent">
<ImageView
android:id="+#id/spinnerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showSpinner" />
<Spinner
android:id="+#id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/list"
style="#style/SpinnerAsEditText"
android:visibility="hidden"/>
and in Java:
void showSpinner() {
findViewById(R.id.spinnerImage).setVisibility(View.GONE);
findViewById(R.id.spinner).setVisibility(View.VISIBLE);
}

Highlight selected item on a gridview

I am trying to highlight a selected item on a gridview (dinamically populated with an adapter), but it is not working.
I did research and i even tried to copy exactly the selector of other people and even the way that they put it on the gridview but i am not being able to put it working.
It just doesn't do anything. The background of each item is white (like i wanted), but when i press it (it it is on top of a textview or a imageview (part of the gridview item) it doesn't do anything. If i press out of the imageView or textview, it will do what i want.
EDIT : I have listeners for the images and the textviews, so it might be interfering with this selector ? How could i solve this problem?
Here is the code of the activity where i create the gridview :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highway_appselection_activity);
gridView= (GridView) findViewById(R.id.HighwayGridView);
gridView.setSelector(new ColorDrawable(Color.BLACK));
Here is the xml of each item of this gridview : (where i define the background as the selector)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/HighwayGridViewItem"
android:orientation="vertical"
android:background="#drawable/highway_appselection_selector"
android:padding="5dp">
<cm.aptoide.lite.HighwayCustomImageView
android:layout_width="72dp"
android:layout_height="72dp"
android:id="#+id/highwayGridViewItemIcon"
android:background="#FFFFFF"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:padding="5dp"
android:clickable="true"/>
<!-- does this need to be my custom image view anymore? CHeck on that-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/highwayGridViewItemName"
android:textColor="#000000"
android:text="texto de teste"
android:textSize="10sp"
android:focusable="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:layout_weight="2"
android:textStyle="bold"
android:paddingRight="5dp"
android:layout_marginLeft="5dp"
android:clickable="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/info_icon"
android:padding="5dp"
android:clickable="true"
android:id="#+id/highwayGridViewItemInfoButton"/>
And here is my selector :
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#color/green_main_color" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#color/green_main_color" />
<item android:state_enabled="true" android:state_selected="true" android:drawable="#color/green_main_color" />
<item android:drawable="#android:color/white" />
I might be missing something, I am new to Android, sorry if there is any rookie mistake.
Create file selector.xml as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/green_main_color" android:state_pressed="true"/>
<item android:drawable="#color/green_main_color" android:state_selected="true"/>
<item android:drawable="#color/white"/>
</selector>
Put your selector file in drawable folder as drawable/selector.xml and then in your gridView:
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:listSelector="#drawable/list_selector"
android:scrollbars="none" />
try this :
int nPrevSelGridItem = -1;
gridview.setOnItemClickListener(new OnItemClickListener() {
View viewPrev;
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
try {
if (nPrevSelGridItem != -1) {
viewPrev = (View) gridview.getChildAt(nPrevSelGridItem);
viewPrev.setBackgroundColor(Color.WHITE);
}
nPrevSelGridItem = position;
if (nPrevSelGridItem == position) {
//View viewPrev = (View) gridview.getChildAt(nPrevSelGridItem);
view.setBackgroundColor(getResources().getColor(R.color.orange));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});

Andrioid Listview Hightlight when clicking item is not working

I want to highlight item when it is clicked in listview android.
Listview.xml
<it.sephiroth.android.library.widget.HListView
android:id="#+id/submenu_button"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"/>
This is Item design
ListItem_text.xml
<TextView
android:id="#+id/submenu_layout_button"
android:layout_width="130dp"
android:layout_height="70dp"
android:background="#drawable/listview_selector"
android:text="Soupsdfsddfsdf"
android:textColor="#fff"
android:textSize="18dp"
android:layout_margin="20dp"
android:padding="10dp"
android:gravity="center_horizontal|center_vertical"/>
listview_selector.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/item_click_button1"/>
<item
android:drawable="#drawable/submenu_bg" />
</selector>
Fragment.class
HListView sb_list = (HListView) view.findViewById(R.id.submenu_button);
sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
put adapter.notifydatasetchanged();
after the view.setSelected(true); this line
Try this.
Use listview.setOnClickListener() in your activity class that will work fine

How to change TextView text color inside a ListView when list item is clicked?

I am trying to change text color of a TextView inside a listview item.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
ViewGroup selectedGroup = (ViewGroup)view;
((TextView)selectedGroup.getChildAt(4)).setTextColor(Color.parseColor("#336699"));
String mID =String.valueOf(((TextView) selectedGroup.getChildAt(4)).getText());
}
});
But nothing happens, I can get text value of the textview, though. What might be wrong in this code?
This is, by the way, my listView layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="4dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#336699"
android:id="#+id/questionTitle"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#444444"
android:id="#+id/questionDate"
android:textSize="12sp"/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/questionContent" android:value=""/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/questionSenderContact" android:value=""/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/messageID" android:value=""/>
</LinearLayout>
EDIT: I was getting wrong textview. Index of it was not 4, but 0. Sorry for a question caused by lack of attention.
try this code:
public View row;
your_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v,
int position, long id) {
if (row != null) {
row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};
Create a new StateListDrawable like you did before but with black as default color and white when pressed.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/black" />
<item android:state_focused="true" android:color="#color/black" />
<item android:state_pressed="true" android:color="#color/black" />
<item android:color="#color/white" />
</selector>
Now for in the TextView change the text color to the new drawable:
android:textColor="#color/list_item_text"
More on StateListDrawables: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
you will have to set textview as tag to the clickable button as:
...
view.setOnClickListener(this);
view.setTag(textView);
...
public void onclick(View v){
...
TextView textView = (TextView)v.getTag();
textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
...
}

How to set ListView selected item alternet text color in android

I have a custom listview with a imageview and an textview. I want when user select a item the textview color should change and all the other textview should remain default.
here are my xmls
ListView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewBell"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
ListViewItems.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="#+id/txt_bell_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/rihanna_love_the_way_lie"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<ImageView
android:id="#+id/list_bell_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/bronze_bell" />
</RelativeLayout>
Create following button_text.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
Change your text view's textColor to:
<TextView
android:id="#+id/txt_bell_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/rihanna_love_the_way_lie"
android:textColor="#color/button_text" //point to that xml
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
You can read about color state list here
Like others have said, just change the color in your onItemClick method. Additionally:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
public void setItemSelected(View view){
View rowView = view;
TextView tv = (TextView)rowView.findViewById(android.R.id.text1);
tv.setTextColor(Color.BLUE);
}
public void setItemNormal()
{
for (int i=0; i< mDrawerList.getChildCount(); i++)
{
View v = mDrawerList.getChildAt(i);
//TextView txtview = ((TextView) v.findViewById(R.id.menurow_title));
TextView txtview = ((TextView)v.findViewById(android.R.id.text1));
txtview.setTextColor(Color.BLACK);
}
}
by doing this we set all items to normal before we change it on each item press.
For that you've to set OnClickListener for that particular ImageView and then change TextView's color on click of ImageView in Adapter Class.
Alternatively,
mList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
TextView tv= view.findViewById(resID);
tv.setTextColor(color)
}
});
Implement setOnItemClickListener for the listView
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long arg3) {
RelativeLayout relativeLayout = (RelativeLayout) view.getParent();
TextView textView = (TextView) relativeLayout.getChildAt(0);
ImageView imaegView = (ImageView) relativeLayout.getChildAt(1);
textView.setTextColor(Color.RED);
// Perform whatever action for your textView and imageView
}
});
after many attempts set color in navigation view, I found out the simplest way to set text color in listview, maybe it will help to someone
res/color/listview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This is used when the navigation item is selected -->
<item android:state_enabled="true"
android:state_activated="true" android:color="#color/colorAccent" />
<!-- This is the default text color -->
<item
android:color="#android:color/black" />
</selector>
and set in TextView as android:textColor:
<TextView
android:id="#+id/tvItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_vertical_extra_margin"
android:textColor="#color/listview_selector"
android:textSize="#dimen/text_size_normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Row item text" />
the trouble was in the right state, use android:state_activated in navigation view

Categories

Resources