0

I need a custom dialog, very similar to the one showed for authorizing USB DEBUG. I'm on API LEVEL 15 and above. DEBUG AUTH DIALOG Basically I need to put a checkbox with a text in the custom view. I tried:

LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.my_dialog, (ViewGroup) getActivity().findViewById(R.id.root_of_my_dialog)));

And my custom view is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_of_my_dialog"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Here I'll have the checkbox -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textColor="@color/red" />

</LinearLayout>

This way my custom layout is not getting the right margin/padding (the word test is just on the extreme left)...it's just like not having any style. How can I inherit from the standard/default one?

Jumpa
  • 4,319
  • 11
  • 52
  • 100

2 Answers2

0

Creating a dialog has already been answered. Read the links below:

  1. create-a-custom-dialog-box-in-android

  2. create-modal-dialog-box-in-android

Hope this helps.

Community
  • 1
  • 1
  • I don't need a whole new custom dialog, I just need to add a body with a checkbox. I'd like to remain "standard". – Jumpa Aug 24 '15 at 17:25
  • Check the second link. It is the same as answered by dieter_h. – Saurabh Nailwal Aug 24 '15 at 17:30
  • I'm already using an alert dialog, I've the Title, the Message, Negative and Positive Buttons, the only thing I'm missing is a custom body with a checkbox. – Jumpa Aug 24 '15 at 17:33
0

Try this:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

   View v = inflater.inflate(R.layout.my_dialog, null);
    builder.setView(v)
    // Add action buttons
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // OK
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   //cancel
               }
           });


   Dialog dialog =  builder.create();
   dialog.show();

Add padding

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_of_my_dialog"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:orientation="vertical">

    <!-- Here I'll have the checkbox -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textColor="#ff0000" />

</LinearLayout>
dieter_h
  • 2,707
  • 1
  • 13
  • 19
  • Your hint is passing null as parent view? If so it doesn't work. :( – Jumpa Aug 24 '15 at 17:24
  • So the the text is correctly placed? Are you on Lollipop? – Jumpa Aug 24 '15 at 17:35
  • Ah uhm, so the solution is to add a fixed padding, no way to get the system style? I think that this is not very compatible across devices and OS versions. – Jumpa Aug 24 '15 at 17:44