Using Dialog Fragments in Honeycomb
Honeycomb (Android 3.x) introduced the Fragments to better utilize the UI areas and logic across the multiple activities of the in an application.
The showDialog/dismissDialog methods in Activity are being deprecated to promote the DialogFragments
This post shows a simple DialogFragment example.
The example layout file for the dialog is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_name"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:orientation="vertical" >
<TextView
android:id="@+id/lbl_your_name" android:text="Your name"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText
android:id="@+id/txt_your_name"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:inputType=”text”
android:imeOptions="actionDone" />
</LinearLayout>
Create the Dialog Class
The dialog class extends the DialogFragment (to support the DialogFragment on other devices I included the DialogFragment from version 4)
import android.support.v4.app.DialogFragment;
// ...
public class SampleDialog extends DialogFragment {
private EditText mEditText;
public SampleDialog() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_name, container);
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
getDialog().setTitle("Hello!CreativeAndroidApps");
return view;
}
}
Showing the Dialog Box in a Fragment Activity
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
// ...
public class FragmentDialogDemo extends FragmentActivity implements SampleDialogListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showSampleDialog();
}
private void showSampleDialog() {
FragmentManager fm = getSupportFragmentManager();
SampleDialog sampleDialog = new SampleDialog();
sampleDialog.show(fm, "fragment_edit_name");
}
@Override
public void onFinishEditDialog(String inputText) {
Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
}
Please provide your most valuable comments to improve this post.
No comments:
Post a Comment