Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add layouts to dialog

I have an idea in mind but im not sure about how to implement it

first i have a dialog

final Dialog dialog = new Dialog(mContext);

i also have a layout

<?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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textViewDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textViewWhen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

what i want is to add this layout in the dialog, i may also want to add more of the same layout right under it inside that dialog how can i do that?

for example how can i add two of this layout in one dialog? something like


  Dialog Title

   Large Text
   Small Text
   Medium Text


   Large Text
   Small Text
   Medium Text
like image 348
lamp ard Avatar asked Aug 27 '12 13:08

lamp ard


4 Answers

Something like this:

LayoutInflater li = LayoutInflater.from(SomeActivity.this);
someLayout = (LinearLayout)li.inflate(R.layout.some_layout, null);

alert = new AlertDialog.Builder(SettingsActivity.this);
alert.setView(someLayout);
like image 159
Dmytro Zarezenko Avatar answered Sep 18 '22 21:09

Dmytro Zarezenko


You can check this documentation page which explain how to add a custom layout on dialog

http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

The key is the setContentView method:

dialog.setContentView(R.layout.custom_dialog);
like image 24
Aerilys Avatar answered Sep 18 '22 21:09

Aerilys


Check out this one :

how to get customized alert dialog , like the one shown in image?

Refer the answer which I've given (Aamir Shah)

like image 25
Aamir Shah Avatar answered Sep 22 '22 21:09

Aamir Shah


This is an example from my application:

public class ConfirmDialog extends DialogFragment {

public static String TAG = "Confirm Dialog";

public interface ConfirmDialogCompliant {
    public void doOkConfirmClick();
    public void doCancelConfirmClick();
}

private ConfirmDialogCompliant caller;
private String message;

public ConfirmDialog(ConfirmDialogCompliant caller, String message){
    super();
    this.caller = caller;
    this.message = message;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.confirm_dialog, container, false);
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    ((TextView) view.findViewById(R.id.textview_confirm)).setText(message);
    ((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // When button is clicked, call up to owning activity.
            caller.doOkConfirmClick();
        }
    });
    ((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // When button is clicked, call up to owning activity.
            caller.doCancelConfirmClick();
        }
    });
    return view;
}

}

where the inflated layout is confirm_dialog.xml.
You inflate your layout in the onCreateView method.
In this case I used DialogFragment (which I suggest you to use...see the support library so that you don't have to worry about your target SDK) but the same applies to Dialog.
Hope it helps you!

like image 22
type-a1pha Avatar answered Sep 21 '22 21:09

type-a1pha