Created
April 17, 2012 12:35
-
-
Save daichan4649/2405705 to your computer and use it in GitHub Desktop.
DialogFragment sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="abeshi" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" > | |
<Button | |
android:id="@+id/success" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="showDialog(success)" /> | |
<Button | |
android:id="@+id/error" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="showDialog(error)" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.fragment; | |
import android.app.Activity; | |
import android.app.Fragment; | |
import android.app.FragmentTransaction; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
public class MainActivity extends Activity { | |
private static final String TAG = "dialog"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
// 成功パターン | |
findViewById(R.id.success).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
SuccessCaseDialogFragment.newInstance().show(getFragmentManager(), TAG); | |
} | |
}); | |
// 失敗パターン | |
findViewById(R.id.error).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
ErrorCaseDialogFragment.newInstance().show(getFragmentManager(), TAG); | |
} | |
}); | |
} | |
private static class ErrorCaseDialogFragment extends DialogFragment { | |
public static DialogFragment newInstance() { | |
return new ErrorCaseDialogFragment(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.fragment; | |
import android.app.DialogFragment; | |
public class SuccessCaseDialogFragment extends DialogFragment { | |
public static DialogFragment newInstance() { | |
return new SuccessCaseDialogFragment(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.fragment; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.app.Dialog; | |
import android.app.DialogFragment; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
public class TestDialogFragment extends DialogFragment { | |
public static DialogFragment newInstace() { | |
DialogFragment dialogFragment = new TestDialogFragment(); | |
return dialogFragment; | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
builder.setTitle("TestDialogFragment"); | |
builder.setView(getContentView()); | |
Dialog dialog = builder.create(); | |
return dialog; | |
} | |
private View getContentView() { | |
LayoutInflater inflater = getActivity().getLayoutInflater(); | |
return inflater.inflate(R.layout.dialog, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it possible to make an embeddable DialogFragment?