Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Listview highlighted before clicking dialog box – Android

Posted on November 11, 2022 By

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 onItemClickcallback.

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

READ  [FIXED] java - How to remove from firebase?
Powered by Inline Related Posts

enter image description here

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

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

READ  [FIXED] java - Get all the documents in a collection along with the path of the document
Powered by Inline Related Posts

Comment posted by MackyRV

Yes exactly, I’m stuck with this module until now 🙁 @Zain

Android Tags:android, android-alertdialog

Post navigation

Previous Post: [FIXED] React Native floating animation
Next Post: [FIXED] java – maintain single database between customer and seller of android app

Related Posts

[FIXED] android – List of parcelable objects to parcelable Kotlin Android
[FIXED] firebase – Facing SHA-1 Related problem in latest Andoroid Studio Android
[FIXED] android – Attempt to invoke virtual method: has an error java.lang.NullPointerException Android
[FIXED] linux – Android i2c permission denied Android
[FIXED] android studio – Type mismatch: inferred type is GroupsFragment but Context! was expected Android
[FIXED] android – How do I add a drop shadow to image view? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme