Reputation: 91
I am new in android development and have problem with this(screenshot below).
Yes the No and Yes button bar's background color is dark gray. I've done everything what I know about android development but didn't figure out the way to do this. ** Can anyone tell me how should I customize it i.e. background color, horizontal align etc.**
Here is my code.
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:textColor="#ffffff"
android:textSize="45sp"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:paddingRight="10dp"
android:gravity="center_horizontal"/>
<RelativeLayout
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:paddingRight="10dp"
android:layout_below="@+id/title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="You are leaving from the app."
android:textColor="#000000"
android:textSize="25sp"
android:gravity="center_horizontal"/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_below="@+id/text1"
android:text="Are you sure."
android:textColor="#000000"
android:textSize="25sp"
android:gravity="center_horizontal"/>
</RelativeLayout>
</RelativeLayout>
homeactivity.java
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
HomeActivity.this);
View view = LayoutInflater.from(HomeActivity.this).inflate(R.layout.custom_dialog,null);
TextView title = (TextView) view.findViewById(R.id.title);
title.setText("Leaving?");
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
HomeActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setView(view);
alertDialog.show();
}
Help me anyone. Thanks in advance.
Upvotes: -1
Views: 3325
Reputation: 233
Create exact xml as given in image including title and buttons than add alert dialog as:
final Dialog dialog = new Dialog(MainActivity.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog1.setContentView(R.layout.custom_dialog);
dialog1.setCancelable(false);
dialog1.show();
btn_yes = (Button)dialog.findViewById(R.id.btn_yes);
btn_no = (Button)dialog.findViewById(R.id.btn_no);
btn_yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog1.dismiss();
finish();
}
});
btn_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog1.dismiss();
finish();
}
});
dialog1.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return true;
}
});
}
Upvotes: 1
Reputation: 1589
I. Declare custom drawable.xml for dialog background.
<?xml version="1.0" encoding="utf-8"?>
<!-- From: support/v7/appcompat/res/drawable/abc_dialog_material_background_light.xml -->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="@color/indigo" />
</shape>
</inset>
II. Declare custom styles in your styles.xml file.
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<!--buttons color-->
<item name="colorAccent">@color/pink</item>
<!--title and message color-->
<item name="android:textColorPrimary">@android:color/white</item>
<!--dialog background-->
<item name="android:windowBackground">@drawable/drawable</item>
</style>
III. Create your dialog and use style as parameter in AlertDialog.Builder.
AlertDialog.Builder builder =
new AlertDialog.Builder(this, R.style.MyDialogTheme);
...
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
Upvotes: 1