Solution 1 :
Instead of using a selector
You can solve this by setting the color programmatically.
So, first of all remove the selector xml file, and remove its relevant layout attribute.
Add the unread color to the dialog setCancelClickListener1()
and the read color to the setConfirmClickListener
using:
view.setBackgroundColor(getResources().getColor(R.color.light_orange));
The view
is the root of the clicked ListView row; and you can get it from the arguments of the onItemClick
callback.
So, change the signature of the alert_dialog()
method to accept a View:
text_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alert_dialog(position, view); // <<<<<<<<<< here is the change
}
});
And add adjusting the color in dialog clicks
public void alert_dialog(final int position, final View view){
final SweetAlertDialog pDialog = new SweetAlertDialog(
text_message.this, SweetAlertDialog.WARNING_TYPE);
pDialog.setTitleText("Highlight data?");
pDialog.setContentText("Please choose the corresponding details");
pDialog.setCancelText("Unread!");
pDialog.setConfirmText("Read!");
pDialog.showCancelButton(true);
pDialog.show();
pDialog.setCancelable(false);
pDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sDialog) {
sDialog.setTitleText("Successfully save!")
.setContentText("Item mark as Unread")
.setConfirmText("OK")
.showCancelButton(false)
.setCancelClickListener(null)
.setConfirmClickListener(null)
.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
sDialog.setCanceledOnTouchOutside(false);
isSelected[position]=false;
// text_listview.setItemChecked(position,false); // No need to this line
view.setBackgroundColor(getResources().getColor(android.R.color.transparent)); // Setting a transparent color for the unread item
}
})
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sDialog) {
sDialog.setTitleText("Successfully save!")
.setContentText("Item mark as Read")
.setConfirmText("OK")
.showCancelButton(false)
.setCancelClickListener(null)
.setConfirmClickListener(null)
.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
sDialog.setCanceledOnTouchOutside(false);
isSelected[position]=true;
// text_listview.setItemChecked(position,true); // No need to this
view.setBackgroundColor(getResources().getColor(R.color.light_orange)); // create light_orange color resource in colors.xml
}
})
.show();
updateBooleanArray();
}
public void updateBooleanArray() {
arrayAdapter = new ArrayAdapter(this,R.layout.list_item, list_items);
text_listview.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
boolean[] tempSelected=new boolean[arrayAdapter.getCount()];
for(int i=0;i<isSelected.length;i++)
{
tempSelected[i]=isSelected[i];
if(tempSelected[i])
{
text_listview.setItemChecked(i,true);
}
}
isSelected = tempSelected;
}
Problem :
I want to highlight a clicked
ListView
item after clicking the alert dialog box Read button; the code below works perfect, but the only issue is whenever I click the item, it’s highlighted with the color before the dialog shows.
I had doubt there is missing on my code, please check this out. Thank you
MainActivity.java
private boolean[] isSelected; //global variable
text_listview = findViewById(R.id.textlistview);
arrayAdapter = new ArrayAdapter(this,R.layout.list_item, list_items);
text_listview.setAdapter(arrayAdapter);
isSelected=new boolean[arrayAdapter.getCount()];
text_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alert_dialog(position);
}
});
}
public void alert_dialog(final int position){
final SweetAlertDialog pDialog = new SweetAlertDialog(
text_message.this, SweetAlertDialog.WARNING_TYPE);
pDialog.setTitleText("Highlight data?");
pDialog.setContentText("Please choose the corresponding details");
pDialog.setCancelText("Unread!");
pDialog.setConfirmText("Read!");
pDialog.showCancelButton(true);
pDialog.show();
pDialog.setCancelable(false);
pDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sDialog) {
sDialog.setTitleText("Successfully save!")
.setContentText("Item mark as Unread")
.setConfirmText("OK")
.showCancelButton(false)
.setCancelClickListener(null)
.setConfirmClickListener(null)
.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
sDialog.setCanceledOnTouchOutside(false);
isSelected[position]=false;
text_listview.setItemChecked(position,false);
}
})
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sDialog) {
sDialog.setTitleText("Successfully save!")
.setContentText("Item mark as Read")
.setConfirmText("OK")
.showCancelButton(false)
.setCancelClickListener(null)
.setConfirmClickListener(null)
.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
sDialog.setCanceledOnTouchOutside(false);
isSelected[position]=true;
text_listview.setItemChecked(position,true);
}
})
.show();
}
layout activitymain.xml
<ListView
android_id="@+id/textlistview"
android_layout_width="match_parent"
android_choiceMode="multipleChoice"
android_layout_height="match_parent" />
drawable myselecter
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns_android="http://schemas.android.com/apk/res/android">
<item
android_drawable="@color/lightOrange" android_state_activated="true"
/></selector>
Comments
Comment posted by Quick learner
use a custom adapter and keep the reference of selected items in list and use it in getview to set it
Comment posted by i.stack.imgur.com/kqjV0.png
Comment posted by stackoverflow.com/questions/62107231/…
Thank you for sharing your idea, your code works well and I already tried this but it conflicts related to my post (
Comment posted by Zain
Hello MackyRV.. Can you tell.. Only the new highlighted item loses the color or all the items?
Comment posted by MackyRV
When I use your code above when I get new message all of the highlighted item gone but my code above works all of the item remain when I receive messages but the only conflicts is it highlighed when I click item before the dialog box show, I dunno what’s going on please help
Comment posted by Zain
So the problem appears when you receive a new message, even after you call
Comment posted by MackyRV
Yes exactly, I’m stuck with this module until now 🙁 @Zain