1

Okay, I asked another question here trying to make my activities look like dialogs. I'm thinking maybe instead of asking about a specific method, I should ask about what I'd like to do, and perhaps there is a different way to go about it...

Here's what I have. My app allows shortcuts to be placed on the home screen. The code and logic for creating the shortcuts all works flawlessly, and the shortcuts then launch the proper activity which shows what it's supposed to... again, all works flawlessly.

What I'm wondering though, is is there a way to have the home screen shortcut launch my activity as a dialog (as opposed to simply trying to make my activity LOOK like a dialog)?

Community
  • 1
  • 1
eidylon
  • 7,068
  • 20
  • 75
  • 118
  • Well I am not a fun of tweaking android Layouts to behave like dialogs or otherwise. Whenever you feel like you need to do something like that, know that you need to improve your app architectural design. – Bukenya Charles Lusansa Apr 28 '17 at 08:35

1 Answers1

12

Add this in your manifest, in the activity you want to look like dialog, declaration:

<activity android:theme="@android:style/Theme.Dialog">

for more information and themes: http://developer.android.com/guide/topics/ui/themes.html

furthermore, to this proggramatically you can use the following code:

public class ShowDialogActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    //
    //Log.d("DEBUG", "showing dialog!");

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.select_dialog_singlechoice);
    dialog.setTitle("Your Widget Name");
    dialog.setCancelable(true);
    dialog.setCanceledOnTouchOutside(true);
    TextView text = (TextView) dialog.findViewById(R.id.text1);
    text.setText("Message");

    dialog.show();
    //
   dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

    public void onCancel(DialogInterface arg0) {
        finish();
    }

   });
}

}

You can choose whatever layout you wish for the dialog and design it as you want.

In addition you would need to set this activity declaration in the manifest for the following:

<activity android:name=".ShowDialogActivity"
          android:theme="@android:style/Theme.Translucent.NoTitleBar">
</activity>

Hope this is what you was looking for, Gal.

GalDude33
  • 7,071
  • 1
  • 28
  • 38
  • Thanks. This is what I did myself, the method I was taking in my other question - trying to make my activity LOOK like a dialog. This gets it half-way there... makes it "float" and gives it the outline or glow border as defined in the system's theme. It does not theme the activity's titlebar to match the title of dialogs. I could replicate how dialog titles look on my system... but then they would not match, say on a system running SenseUI, or TouchWiz. I want to call my activity using SDK dialog functions, so that it matches the user's phone theme, not whatever I have hard coded in it. – eidylon Jan 11 '12 at 02:47
  • I've edited my answer, for proggramatic dialog managing as you asked, hope this will help you. – GalDude33 Jan 11 '12 at 07:46
  • Thanks Gal, ... from what I can see, it looks like it might do what I'm hoping. I will have to try that tonight when I get home and get back. An activity that "dialogs" itself like this should still be able to respond to `intent-filters` without any issue I would expect, correct? – eidylon Jan 11 '12 at 17:29
  • Not one I can think of...It's exactly like a normal activity, so It's should have the same capabilities of a "non-dialog" activity. If something specific doesn't work let me know and I'll try to help. – GalDude33 Jan 11 '12 at 22:56
  • 1
    Well, I couldn't get this to work in my app, ... so broke it out into a simple test app for code isolation. This did not give me the themed titlebar, but I got that by using `AlertDialog` instead of `Dialog`. The TEST app now looks precisely as I intended, after I also used this question (http://stackoverflow.com/questions/5184065/android-activity-invisible-by-default) to hide the main activity behind the dialog. Now just to figure out how to port my logic into a dialog (SingleSelect methods methinks). Thanks for the direction and the help! – eidylon Jan 12 '12 at 07:08
  • Do we have any downsides in this approach? What if someone implements a wizard sort of a thing using this approach? Would there be any performance hits, or may be possible security issues or anything as such? – Romeo Sierra Jun 05 '17 at 09:33