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>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.