Issue
I've implemented a long click listener to delete an entry from my recyclerview and I'd like to give the user one last chance to change their mind. I've done something similar in a 'standard' view using an Alert Dialog and i've made a few drawables for the buttons in the alert view that I would like to use again (to keep a constant interface among all my views)
The drawable I'm using is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/minus_button"
android:state_pressed="false"
android:state_selected="false"/>
<item android:drawable="@drawable/add_button"
android:state_pressed="true"/>
</selector>
The alert I'm trying to use is :
public void deleteRequestCheck(final int tempIDval){
AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
builder.setMessage("Are you sure you want to reset all your climbs back to zero?");
builder.setCancelable(false);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
deleteEntry(tempIDval);
}
});
AlertDialog alert = builder.create();
alert.show();
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
nbutton.setTextColor(Color.WHITE);
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
pbutton.setTextColor(Color.WHITE);
}
and I'm calling this from inside my Adapter class (where the longclick action is handled).
The issue I'm having is that
nbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
pbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
are giving me an error that it Cannot resolve method 'getDrawable(int)'. This has me stumped as this is identical to how I've created and called this alert dialog in my previous activities.
Solution
There are actually 2 ways you can do this:
1) Programmatically like:
nbutton.setBackground(ContextCompat.getDrawable(this.context, R.drawable.mypositiveButtonDrawable));
But what if you want same button color for other dialogs? Why to duplicate the effort?
2) through styles.xml
Create a theme in your styles.xml like:
<style name="DiscardChangesDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/DialogButtonNegative</item>
<item name="buttonBarPositiveButtonStyle">@style/DialogButtonPositive</item>
</style>
<style name="DialogButtonNegative" parent="Widget.AppCompat.Button.Borderless">
<item name="android:textColor">@color/dialog_button_negative</item>
</style>
<style name="DialogButtonPositive" parent="Widget.AppCompat.Button.Borderless">
<item name="android:textColor">@color/dialog_button_positive</item>
</style>
and then use it like:
AlertDialog.Builder(this, R.style.DiscardChangesDialog)
and then you do not need to worry about adapter or anything.
Hope it helps!!!
Answered By - matrix
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.