2

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?

6 Answers 6

5

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.

3
  • 1
    why should i maintain two set of codes? I need the solution with the existing code. The fragment, as I understand, is to have from calling the showing and dismissing methods directly (android-developers.blogspot.in/2012/05/…). Which is not my intension here.
    – Avinash R
    Jan 3, 2014 at 4:41
  • @AvinashR What do you mean by 'two set of codes'? There's only one fragment with all the logic and one containing activity with nothing but that fragment inside. Jan 3, 2014 at 18:32
  • looks like the theme way does have some performance issue (told by fellow developers).
    – Avinash R
    Jun 4, 2014 at 12:21
4

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"
4

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>
4
  • yes, i'm doing the same in my application, but setting the theme using setTheme() is the problem here. (I have found the solution)
    – Avinash R
    Jan 3, 2014 at 4:54
  • 1
    Don't get why set the theme in code, if the android resources system could take care of it... More cleanly and with less code.
    – luciofm
    Jan 3, 2014 at 5:18
  • @luciofm interesting. Possible to provide a little more code showing how those styles fit with the dialog/full screen? I am still a little confused. Mar 27, 2015 at 2:57
  • Thanks @luciofm, When I change theme at runtime for the activity as Dialog, background is not transparent,Now it is OK with separate theme. Nov 2, 2016 at 4:18
1

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);
}
1
0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.