How to create a Custom Dialog box in android

0 votes

I want to create a custom dialog box like shown in the image below:-

enter image description here

I have tried creating a subclass of AlertDialog.Builder and used a custom Title and Custom Content View and used that but the result was not as expected. Another attempt was to subclass DialogFragment and customize the dialog inside onCreateDialog but the result was not as expected. Then I tried using a plain Dialog class. The result was not as expected. In all three cases, the problem is when I overlook the title view the size of the dialog is not as expected and when I use Title view the result is there is a thick border around the content view (which really looks bad). Now I have two questions in my mind...

  1. How can I achieve that? As I have already tried so many things, a direct answer will be more appreciated.

  2. What is the best way to show an error or alert dialog in an android app?

Feb 18, 2022 in Others by Aditya
• 7,660 points
240 views

1 answer to this question.

0 votes

Here I have created a simple Dialog, like:

Dialog Box

Custom_dialog.xml
 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" 
android:layout_height="80dp" 
android:background="#3E80B4" 
android:orientation="vertical" > 

<TextView 
          android:id="@+id/txt_dia" 
          android:layout_width="wrap_content"  
          android:layout_height="wrap_content" 
          android:layout_gravity="center" 
          android:layout_margin="10dp" 
          android:text="Do you realy want to exit ?" 
          android:textColor="@android:color/white" 
          android:textSize="15dp" 
          android:textStyle="bold"/> 

<LinearLayout 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:layout_gravity="center" 
              android:background="#3E80B4" 
              android:orientation="horizontal" > 

<Button 
            android:id="@+id/btn_yes" 
            android:layout_width="100dp" 
            android:layout_height="30dp" 
            android:background="@android:color/white" 
            android:clickable="true" 
            android:text="Yes" 
            android:textColor="#5DBCD2" 
            android:textStyle="bold" /> 

<Button 
        android:id="@+id/btn_no" 
        android:layout_width="100dp" 
        android:layout_height="30dp" 
        android:layout_marginLeft="5dp" 
        android:background="@android:color/white" 
        android:clickable="true" 
        android:text="No" 
        android:textColor="#5DBCD2" 
        android:textStyle="bold" /> 
</LinearLayout> 


</LinearLayout>

You have to extends Dialog and implements OnClickListener
 

public class CustomDialogClass extends Dialog implements 
        android.view.View.OnClickListener { 

public Activity c; 
public Dialog d; 
public Button yes, no; 

public CustomDialogClass(Activity a) { 
      super(a); 
      // TODO Auto-generated constructor stub 
      this.c = a; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE);  setContentView(R.layout.custom_dialog); 
yes = (Button) findViewById(R.id.btn_yes); 
no = (Button) findViewById(R.id.btn_no); 
yes.setOnClickListener(this); 
no.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.btn_yes: 
        c.finish(); 
        break; 
    case R.id.btn_no: 
          dismiss(); 
          break; 
          default: 
          break; 
} 
dismiss(); 
  } 

}

How to Call Dialog ?
 

R.id.TXT_Exit: CustomDialogClass cdd=new CustomDialogClass(Values.this); cdd.show();

answered Feb 18, 2022 by Rahul
• 9,670 points

Related Questions In Others

0 votes
1 answer

How to create a drop-down in excel with custom values

You can accomplish that using code rather ...READ MORE

answered Sep 25, 2022 in Others by narikkadan
• 51,240 points
136 views
0 votes
0 answers

How to create a batch file in windows?

What is a batch file and How ...READ MORE

Jul 4, 2019 in Others by sindhu
337 views
0 votes
0 answers

How to create a Wordlist generator in Golang?

I want to create a Wordlist Generator ...READ MORE

Jul 14, 2019 in Others by Raj
• 180 points
461 views
0 votes
1 answer

How to create a Project in Flutter?

Hi@akhtar, Flutter has an inbuilt command to create ...READ MORE

answered Jul 20, 2020 in Others by MD
• 95,420 points
465 views
0 votes
1 answer

How do I display an alert dialog on Android?

You could use an AlertDialog for this ...READ MORE

answered Feb 22, 2022 in Others by Aditya
• 7,660 points
651 views
0 votes
1 answer

Running docker on Android

According to the documentation, the Android kernel is ...READ MORE

answered Aug 1, 2018 in Docker by Kalgi
• 52,350 points
3,069 views
0 votes
1 answer

Task Canceled Exception while invoking AWS Lambda

I'm guessing either the TaskCanceledException instance is ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,100 points
1,730 views
0 votes
1 answer

Is there a way to run Python on Android?

YES! An example via Matt Cutts via SL4A -- "here’s ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,100 points
516 views
0 votes
1 answer

How to open the Google Play Store directly from my Android application?

By using developer.andriod.com, one can solve this ...READ MORE

answered Feb 8, 2022 in Others by Rahul
• 9,670 points
120 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP