Java android listView After clicking, the selection disappears - java

I want to do this : I have a list whose colors of every two items are different and want to select the item that was clicked. The item that is selected is highlighted but the old color returns after it is released
I did this :
artists_list_background_alternate.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#color/sign_out_color" />
<item android:state_pressed="true"
android:drawable="#color/survey_toplist_item" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#color/survey_toplist_item" />
</selector>
artists_list_backgroundcolor.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/grey" android:state_pressed="false" android:state_selected="false" />
<item android:drawable="#color/survey_toplist_item" android:state_pressed="true" />
<item android:drawable="#color/survey_toplist_item" android:state_pressed="false" android:state_selected="true" />
<item android:drawable="#color/survey_toplist_item" android:state_pressed="false" android:state_selected="true" />
</selector>
Java File Code ::
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.listitem, parent, false);
}
if (position % 2 == 0) {
view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
} else {
view.setBackgroundResource(R.drawable.artists_list_background_alternate);
}
((TextView) view.findViewById(R.id.heading)).setText(data.get(position));
return view;
}
ListView lvMain = (ListView) findViewById(R.id.list);
lvMain.setAdapter(adapter);
Layout xml ::
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:dividerHeight="1dp" />
And I don't know what I did wrong

In order to make an item highlighted, you need to provide choiceMode for your ListView. For example:
android:choiceMode="singleChoice"
Also, add a color for state android:state_activated="true" in your selector.

Related

MaterialCalendarView decorator overrides selected day color issue

I am having a problem with MaterialCalendarView library.
Everything is fine until I select a day with a decorator because the decorator has the same color of the selection color.
Here is the normal click:
and the issue:
the decorator code:
public class EventDecoratorMonth implements DayViewDecorator {
private CalendarDay date;
private Context context;
public EventDecoratorMonth(CalendarDay date, Context context) {
this.date = date;
this.context = context;
}
#Override
public boolean shouldDecorate(CalendarDay day) {
return day.equals(date);
}
#Override
public void decorate(DayViewFacade view) {
if (context != null)
view.addSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.orange)));
}
}
and through xml the selection color: app:mcv_selectionColor="#color/orange"
the selected day text color is white, I used mcv_dateTextAppearance with a selector to change it to white when android:state_checked="true" and grey in every other case.
The problem is when a day with a decorator is selected that the mcv_dateTextAppearance is not applied.
Edit:
my selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_checked="true" />
<item android:color="#color/white" android:state_pressed="true" />
<item android:color="#color/textGrey" android:state_enabled="false" />
<item android:color="#color/textGrey" android:state_checked="false" />
<item android:color="#color/textGrey" />
</selector>
How to fix this?
EDIT 2:
I fixed using a decorator with ForegroundColorSpan color set to white, and on changeSelected date I remove old one and set new one
Try using this selector for your text color
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_checked="true" />
<item android:color="#color/white" android:state_pressed="true" />
<item android:color="#color/grey" android:state_enabled="false" />
<item android:color="#color/grey" />
</selector>

How to Change the background color of Spinner selection in Android

In the above picture GAIN 3 is selected but its not visible properly , so how can i change that color to darker color.
basically i want to change the selected text background in darker color.
I'm using com.jaredrummler.materialspinner.MaterialSpinner Spinner.
Here's the java implementation.
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
#Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
text = spinner.getText().toString();
Log.e("Spinner Listener",text);
if(text.contains("GAIN 0")){
sendToDevice("F");
} else if(text.contains("GAIN 1")){
sendToDevice("G");
} else if(text.contains("GAIN 2")){
sendToDevice("H");
} else if(text.contains("GAIN 3")){
sendToDevice("I");
}
}
});
And the layout item looks like the following.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinner"
app:ms_dropdown_max_height="350dp"
app:ms_dropdown_height="wrap_content"
android:textColorHighlight="#000000"
android:layout_width="130dp"
style="#style/spinner_style"
android:popupTheme="#android:style/ThemeOverlay.Material"
android:textColor="#color/blue"
android:layout_below="#+id/testmodetitle"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toEndOf="#+id/button1"
android:layout_marginStart="30dp" />
To change background color and other color this library has provided some attributes. To change background color of selected item use below code.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_background_selector="#drawable/selector_gray_white_spinner"
app:ms_dropdown_height="wrap_content"
app:ms_dropdown_max_height="350dp" />
create one selector in drawable having name selector_gray_white_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/darkGray"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/darkGray"/>
<item android:state_focused="true" android:drawable="#android:color/white"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="#android:color/white"/>
</selector>
Add dark color in your color.xml file
<color name="darkGray">#acacac</color>
Use this way it will help you:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list) {
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = null;
v = super.getDropDownView(position, null, parent);
// If this is the selected item position
if (position == selectedItem) {
v.setBackgroundColor(Color.BLUE);
}
else {
// for other views
v.setBackgroundColor(Color.WHITE);
}
return v;
}
};
There are some attributes available along with the implementation of that specific library. Please have a look in the readme.md section where the attributes are listed.
I think you might consider using ms_background_selector attribute in your layout where you have declared the spinner.
So the layout declaration will look like this.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinner"
app:ms_background_selector="#drawable/your_darker_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Add a file named your_darker_selector.xml and put the following code inside the file.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#android:color/darker_gray"/>
<item android:state_checked="false" android:drawable="#android:color/white" />
</selector>
Modify the color from the selector file as per your necessity.
Give html colour code for the first item of spinner.
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText),
TextView.BufferType.SPANNABLE);

Menu - Activity empty

I try to do a tuto about Menu.
My JAVA code :
public class MenuActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.myfirstmenu, menu);
return true;
}
}
My XML "R.layout.activity_menu" code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- nothing because I just want display a menu -->
</LinearLayout>
My XML "R.menu.myfirstmenu" code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:title="Item 1"></item>
<item android:id="#+id/item2" android:titleCondensed="Item 2" android:title="Item 2 mais avec un nom assez long quand même">
<menu>
<item android:id="#+id/item3" android:title="Item 2.1" android:checkable="true"/>
<item android:id="#+id/item4" android:title="Item 2.2"/>
</menu>
</item>
<item android:id="#+id/item5" android:title="Item 3" android:checkable="true"/>
<item android:id="#+id/item6" android:title="Item 4">
<group android:id="#+id/group1" android:checkableBehavior="all">
<item android:id="#+id/item7" android:title="Item 4.1"></item>
<item android:id="#+id/item8" android:title="Item 4.2"></item>
</group>
</item>
<group android:id="#+id/group2" android:enabled="false">
<item android:id="#+id/item9" android:title="Item 5.1"></item>
<item android:id="#+id/item10" android:title="Item 5.2"></item>
</group>
</menu>
My problem is that when I run my app, nothing appear !
What I must do in order to display the three vertical dots and the app name ?
Thank for your answers ;)
Max

How to change Listview item color and textcolor in android

In my app I have created one Listview. When I click on the row, I want to change the Listview row color as Lightgray color and textcolor as a Blue color.
For this I have tried the code below but only row background color is changing not the textcolor.
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int j = 0; j < parent.getChildCount(); j++) {
parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
view.setBackgroundColor(Color.LTGRAY);
rowText = (TextView) findViewById(R.id.rowitem);
rowText.setTextColor(Color.BLUE);
}
}
});
}
first enable the android:ListSelector attribute in your ListView
ListView android:id="#+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Tablayoutdesign"
android:cacheColorHint="#000000"
android:dividerHeight="1dip"
android:layout_marginTop="63dip"
android:layout_marginBottom="40dip"
/>
Thank create a selector (listselector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#drawable/focused"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#drawable/selected" />
</selector>
Than go for colors.xml
<resources>
<drawable name="focused">#ff5500</drawable>
<drawable name="selected">#FF00FF</drawable>
</resources>
and with some Java magic:
listview.setSelector(R.drawable.listselector)
Thanks to #sankar-anesh
List State Pressed
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#4285f4" />
<corners android:radius="2dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape android:shape="rectangle">
<solid android:color="#4285f4" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
List view background when not pressed
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFF" />
<corners android:radius="2dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape android:shape="rectangle">
<solid android:color="#FFF" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
List view Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/card_state_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/card_background" />
</selector>
First create a selector drawable like this :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/red"
android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
Then
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.color.red));
selector.addState(new int[] {}, getResources().getDrawable(R.color.white));
view.setBackgroundDrawable(selector);
Change the
rowText = (TextView) findViewById(R.id.rowitem);
to
rowText = (TextView)view.findViewById(R.id.rowitem);
since you should find the textview in the inflated view, not independently.

cannot catch a click event on custom android view

I have this xml:
<com.me.view.button.SwipeableButton
android:id="#+id/mainMenuSwipeableButton"
android:layout_width="250dp"
android:layout_height="62dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
app:button="#drawable/main_menu_button"
android:visibility="visible" />
and:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/icon_menu_press" /> <!-- pressed -->
<item android:drawable="#drawable/icon_menu" /> <!-- default -->
</selector>
why isn't my click caught when i have this code?
setCloseToolTipClickListener(getMainLayout().findViewById(R.id.mainMenuSwipeableButton), tooltipView);
private void setCloseToolTipClickListener(View view, final LinearLayout tooltip) {
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tooltip.setVisibility(View.GONE);
}
});
}

Categories