简体   繁体   中英

Alert Dialog Text color change issue

I want to change text color in my Alert Dialog.I use text from array.xml.I want this orange color shape in my pic , textcolor change into White color . here is my array.xml file code :-

<resources>
<array name="bug_type">
    <item>
        {"id":\"1\", "type":\"Wrong Question\"}
    </item>
    <item>
        {"id":\"2\", "type":\"Wrong Answer\"}
    </item>
</array>

Here is my Activity data :-

AlertDialog.Builder(this, R.style.popuptheme)
            .setTitle("Select bug")
            .setPositiveButton("Ok") { dialog, whichButton ->
                if (bugTypeDialog.selectReportBugType.checkedRadioButtonId > 0) {
                    postBugReport(bugTypeDialog.selectReportBugType.checkedRadioButtonId.toString(), que_id)
                }
                Toast.makeText(this, "Bug Request has been send ..", Toast.LENGTH_LONG).show()
                dialog.dismiss()
            }
            .setNegativeButton("Cancel") { dialog, whichButton ->
                dialog.dismiss()
            }
            .setView(bugTypeDialog)
            .create()
            .show()
}

I want all textview in white color like orange color shape in this pic 单击此处显示错误图像

You need to create a theme for the alert dialog. Check this out:

http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

Create a textColorSelector in your drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#0f0"/>
    <item android:state_checked="true" android:color="#fff"/>
    <item android:color="#00f"/>
</selector>

Apply this selector to your RadioButton.

<RadioButton
      ...
      android:textColor="@drawable/textColorSelector"
      />

You have two ways to do this

  1. Override default dialog. and apply theme on dialog

     //1. create a dialog object 'dialog' MyCustomDialog builder = new MyCustomDialog(getActivity(), "Exit", errorMessage); AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ... } }).create(); //2. now setup to change color of the button dialog.setOnShowListener( new OnShowListener() { @Override public void onShow(DialogInterface arg0) { dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("#f34235")); } } dialog.show() 

and the style file should be something like:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorAccent">#0000FF</item>
</style>
  1. Create your own custom dialog

     // create instance of dialog AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // get inflater and inflate layour for dialogue LayoutInflater inflater = this.getLayoutInflater(); View dialogView = inflater.inflate(R.layout.alert_label_editor, null); // now set layout to dialog dialogBuilder.setView(dialogView); // create instance like this OR directly mentioned in layout Button button= (Button) dialogView.findViewById(R.id.label_field); button.setText("test label"); AlertDialog alertDialog = dialogBuilder.create(); // show dialog alertDialog.show(); 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM