Question

In any application the add/edit will be comparatively having lesser inputs. I have seen that the application, esp., calendar, are using clever strategy to show these as simple dialog, so that the user may not notice that there is empty space in the designed form

As shown below side-by-side

My question is, how to make it happen?

Was it helpful?

Solution

What I'm doing is I extend DialogFragment:

public class AboutFragment extends DialogFragment { ... }

I also have an activity that contains that fragment. And when the dialog/activity needs to be called, this method decides how to display it:

public static void showAbout(Activity parent) {
    if (isTablet) {
        AboutFragment aboutFragment = AboutFragment.newInstance();
        aboutFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogTheme);
        DialogUtils.showDialog(parent, aboutFragment);
    } else {
        Intent aboutIntent = new Intent(parent, AboutActivity.class);
        parent.startActivity(aboutIntent);
    }
}

How to decide whether it is a tablet, is totally up to you.

This technique is explained in the documentation.

OTHER TIPS

In my opinion the best approach here is to use

<!-- Theme for a window without an action bar that will be displayed either full-screen
on smaller screens (small, normal) or as a dialog on larger screens
(large, xlarge). -->
"android:Theme.Holo.Light.DialogWhenLarge.NoActionBar"

The best/easiest solution I've found is to always use an Activity, and based on screensize (and version), change your Theme parent.

in res/values/themes.xml

    <style name="Detail.Theme" parent="@android:style/Theme.Holo.Light" >
        ...
    </style>

and in res/values-large/themes.xml

    <style name="Detail.Theme" parent="@android:style/Theme.Holo.Light.DialogWhenLarge" >
        ...
    </style>

use Context.setTheme method to set them programmetically. As the doc says

this should be called before any views are instantiated in the Context (for example before calling.

So, to switch between themes need to call setTheme before onCreate

public void onCreate(Bundle savedInstanceState) {
    // check screen size
    setTheme(dialogTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
}

As @StinePike answered, setting a dialog theme programatically doesn't do any use (to me), as it shows a wierd black screen behind the dialog, rather than a dimmed background (as shown in the question). This is obviously a bug.

Instead of trying to set it programatically, or in style.xml, and pretty much everywhere except for AndroidManifest.xml, I did the reverse, which has worked for me. (the solution which I took from the marvelous solution of the above issue)

The simplest solution (that works) as follows:

1. Make the activity a dialog by default through AndroidManifest.xml:

e.g., in the AndroidManifest.xml:

<activity
        android:name="com.example.MyActivity"
        android:label="@string/title_activity_mine"
        android:theme="@android:style/Theme.DeviceDefault.Dialog">
    ...
</activity>

2. On starting the activity, set the theme to default if device is not a tablet.

if (!(isTablet(this)) {
    setTheme(defaultTheme);
}
super.onCreate(savedInstanceState);
...

Note:

  1. solution will work with custom styles defined in style.xml.

Ref:

  1. How to detect device is Android phone or Android tablet?
  2. Dialog with transparent background in Android
  3. Issue 4394 - android - setTheme() does not work as expected

PS: final app on tablet and phone is as follows: enter image description here

Use a DailogFragment and then control how its shown with setShowsDialog()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top