AlertDialog button text and background color
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aac1a3"
>
<Button
android:id="@+id/btn_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:layout_gravity="top|center_horizontal"
/>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;
private CoordinatorLayout mCLayout;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the application context
mContext = getApplicationContext();
mActivity = MainActivity.this;
// Get the widget reference from XML layout
mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
mButton = (Button) findViewById(R.id.btn_alert);
// Set a click listener for the button widget
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Initializing a new alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
// Set a title for alert dialog
builder.setTitle("Say Hello!");
// Show a message on alert dialog
builder.setMessage("Are you want to do this?");
// Set the positive button
builder.setPositiveButton("Say",null);
// Set the negative button
builder.setNegativeButton("No", null);
// Set the neutral button
builder.setNeutralButton("Cancel", null);
// Create the alert dialog
AlertDialog dialog = builder.create();
// Finally, display the alert dialog
dialog.show();
// Get the alert dialog buttons reference
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
Button neutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
// Change the alert dialog buttons text and background color
positiveButton.setTextColor(Color.parseColor("#FF0B8B42"));
positiveButton.setBackgroundColor(Color.parseColor("#FFE1FCEA"));
negativeButton.setTextColor(Color.parseColor("#FFFF0400"));
negativeButton.setBackgroundColor(Color.parseColor("#FFFCB9B7"));
neutralButton.setTextColor(Color.parseColor("#FF1B5AAC"));
neutralButton.setBackgroundColor(Color.parseColor("#FFD9E9FF"));
}
});
}
}
No comments:
Post a Comment