Skip to content

Instantly share code, notes, and snippets.

@manishcm
Created August 8, 2014 07:42
Show Gist options
  • Save manishcm/63fdfa30bb74eca662d8 to your computer and use it in GitHub Desktop.
Save manishcm/63fdfa30bb74eca662d8 to your computer and use it in GitHub Desktop.
DialogFragment can be used as Dialog as well as embedded inside Acitivity's view hierarchy.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
tools:ignore="MergeRootFrame">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog"
android:onClick="launchDialog"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Embed"
android:onClick="embedDialog"
android:layout_below="@id/button1" />
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button2" />
</RelativeLayout>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
package com.example.test;
import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void launchDialog(View v) {
MyDialog dialog = new MyDialog();
dialog.show(this.getFragmentManager(), "mydialog");
}
public void embedDialog(View v) {
FragmentTransaction ft = this.getFragmentManager().beginTransaction();
MyDialog dialog = new MyDialog();
ft.add(R.id.fragmentContainer, dialog);
ft.commit();
}
public static class MyDialog extends DialogFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
if(getDialog() != null) // For embedded views this is null
getDialog().setTitle("Shown as Dialog");
TextView tv = (TextView) v.findViewById(R.id.textView);
tv.setText("This is a dialog");
return v;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment