Skip to content Skip to sidebar Skip to footer

Customizing Android Dialogfragment

iam a beginner level programmer in Android.Now iam after a small app development and i have a dialogFragment.Everything is perfectly working and its displaying Dialog box also.But

Solution 1:

Since you are using the .setTitle() method it is only setting the title with the defualt settings, such as the white background. If you want to customize the title background color you will need to have an xml to do that. Also, for DialogFragments, from my knowledge and experience, you should use public Dialog onCreateDialog instead of public View onCreateView. That way you return a Dialog as opposed to just a View that you can then just call .show() on and it will display your dialog. Here is an example:

@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builderbuilder=newAlertDialog.Builder(getActivity());
    LayoutInflaterinflater= getActivity().getLayoutInflater();

    Bundleargs= getArguments();
    currentName = args.getString(ARG_CURRENT_NAME);

    builder.setView(inflater.inflate(R.layout.name_dialog, null));
    builder.setTitle("Rename Rapper Program");
    builder.setMessage("Enter a new name for " + currentName + ":"); 
    builder.setPositiveButton("Rename", newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog, int which) {
            newName = (EditText) getDialog().findViewById(R.id.new_name);
            newProgName = newName.getText().toString();
            mRename.renameProgram(currentName, newProgName);
        }
    });
    builder.setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog, int which) {
            dismiss();
        }
    });

    return builder.create();
}

Here is an example dialog xml, though it is not the xml that is being inflated in the above DialogFragment:

<?xml version="1.0" encoding="UTF-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:drawableLeft="@drawable/login"android:layout_width="match_parent"android:layout_height="64dp"android:background="#FCD116"android:text="@string/login"android:textSize="36sp"/><EditTextandroid:id="@+id/username"android:inputType="textEmailAddress"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:layout_marginLeft="4dp"android:layout_marginRight="4dp"android:layout_marginBottom="4dp"android:hint="@string/un"/><EditTextandroid:id="@+id/password"android:inputType="textPassword"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="4dp"android:layout_marginLeft="4dp"android:layout_marginRight="4dp"android:layout_marginBottom="16dp"android:fontFamily="sans-serif"android:hint="@string/pw"/></LinearLayout>

The LinearLayout is setting up the rest of the child items to be placed accordingly. The first TextView acts as my "title" bar and then the EditTexts are the "body" of the dialog. I have no buttons in the xml because I set those programmatically within the onCreateDialog like in the other snippet of code above.

Solution 2:

The example of the above (TronicZomB) could work if you disable the default windows title:

// Remove the titlegetDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

Try it!

Post a Comment for "Customizing Android Dialogfragment"