Android Dialog background


A dialog box which is created using the Dialog class has a white background. However, it is possible to change its background to any color or gradient style that you like. In this Android tip, I am going to show you how to achieve in easy steps.
Firstly, you need to define the background style of the dialog in an xml file stored in res/drawable folder. In the style file, you define a rectangle shape that its corners, gradient, stroke, and size as shown below.




dialog_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android= "http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  
   <gradient
android:angle="90"
android:centerColor="#440D1E"
android:endColor="#660114"
android:startColor="#555FDF"
android:type="linear"
/>
   <corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
<stroke
android:width="1dp"
android:color="#ffffffff" />
<size
   android:width="120dp"
   android:height="120dp"
   />  
</shape>



Next, in your code, you will apply the dialog background style to the dialog using setBackgroundDrawableResource() method as in the example code below. In this code, the dialog is displayed when you click the Show Dialog button. The button is defined in the activity_main.xml file.

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools= "http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.anddialogbackground.MainActivity" >

   <Button
     android:id="@+id/bt"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Show Dialog"
     android:onClick="showDialog"
     />

</RelativeLayout>



MainActivity. java

package com.example.anddialogbackground;
import android.support.v7.app.ActionBarActivity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
    
   }
  
   public void showDialog(View view){
   Dialog dialog = new Dialog(MainActivity.this);
   // Apply background style to dialog
       dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_background);
       // Set the dialog title
       dialog.setTitle("Dialog");
       // Set dialog message
       TextView mess=new TextView(this);
       mess.setPadding(10, 10, 10, 10);
       mess.setTextColor(Color.WHITE);
       mess.setText("Styling the dialog background");
       dialog.setContentView(mess);
       // Close the dialog when touching outside
       dialog.setCanceledOnTouchOutside(true);
       // Show the dialog
      
       dialog.show();
   }

}

Android-Dialog-background

Posted by: Dara | post date: 12-08-2014 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.