0

I have a textview, when i click on it will open an alert multichoice items and allow us to select it which contain OK and Cancel buttons. Now if i select any items in alertdialog multichoice then my textview color needs to change. I am aware of selector but how to use it for when items selected change textview color.

MY textview XML:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="horizontal"
    android:id="@+id/linearLayout1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/genere"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/genere"
        android:layout_gravity="center"
        android:text=" Search Genre"
        android:textSize="20sp"
   />

</LinearLayout> 

Then

    Views mViews = new Views();
    mViews.genere.setOnClickListener(this);
      public void onClick(View v) {
    switch (v.getId()){
        case R.id.genere:
             ilist();
            break;

ilist:

   public ArrayList<String> ilist() {

    final String[] ratings = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    final boolean[] ratingschecked = {false, false, false, false, false, false, false, false, false, false};
    SharedPreferences sharedPreferences = this.getSharedPreferences("checkedrate_i", Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sharedPreferences.edit();

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Ratings");
    builder.setMultiChoiceItems(ratings, ratingschecked, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {

            if (isChecked) {

                   if(!ilist.contains(ratings[which])){
                       ilist.add(ratings[which]);

                    }

            } else if (ilist.remove(ratings[which])) {

                if(ilist.contains(ratings[which])){
                    ilist.remove(ratings[which]);
                }


            }
        }
    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

                for (int i = 0; i < ilist.size(); i++) {
                editor.putString("selectedratings" + i, String.valueOf(ilist.get(i)));
            }
            editor.putInt("size", ilist.size());

            editor.apply();

        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    AlertDialog dialog = builder.create();
    builder.show();
  return ilist;
}

Tried using the selector but it is not working.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff6ff57"/>
<item android:state_activated="true" android:color="#ffff1013"/>
<item android:color="#ff897fff" />
</selector>
user2269164
  • 1,095
  • 2
  • 15
  • 31

1 Answers1

0

create a global TextView object (outside oncreate method)

TextView txt_view;

then initialize inside onCreate method:

txt_view = findViewById(R.id.genere); 

then set onclick listener on txt_view (after initializing)

txt_view.chat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ilist();
                }
            });

then inside onclick listener for positive button of alert dialog add

txt_view.setColor(getResources().getColor(R.color.color_name));

You can still use getResources().getColor(R.color.color_name) but it is depricated now, refer here for the new method.

Community
  • 1
  • 1
Deepak John
  • 967
  • 1
  • 7
  • 19
  • Application crashed when i do change like above. – user2269164 Jan 04 '16 at 10:47
  • can you paste the log – Deepak John Jan 04 '16 at 11:08
  • have you created color with name color_id – Deepak John Jan 04 '16 at 11:09
  • java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference. I used txt_view.setTextColor(getResources().getColor(R.color.changetext)); – user2269164 Jan 04 '16 at 11:16
  • Note that "txt_view" is not a command or keyword, it's a variable. I think you didn't create a TextView variable named "txt_view" that's why you got NullPointerException. – hehe Jan 04 '16 at 11:26
  • Its working now as per suggestion by user3069305. But its not when reopen the app as the setbackgroundcolor is present in onclick method. Any suggestion. – user2269164 Jan 06 '16 at 05:25
  • So what you want to do is to retain the color of the text? such that when you open it again you will get the same color? – hehe Jan 07 '16 at 02:06