2

In my Android app i want to show a view on top of any Activity and still have the focus on the activity. I actually figured this out using a Dialog with the code bellow:

Dialog progressDialog = new Dialog(getThisContext());
progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
progressDialog.setContentView(R.layout.hint_view_layout);

TextView theText = (TextView) progressDialog.findViewById(R.id.hintText);
theText.setText("(Some Text)");

Window window = progressDialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.BOTTOM);
progressDialog.show();

but the problem is that my view is showing as the following:

Showing up as

But the preview of my xml for the Dialog is like:

xml preview

So, with the code above i was able to not dim the background and to keep the focus with the main activity, but how can i make the Dialog appear in the app just like in the xml preview? Because besides the Dialog background, its not exactly in the bottom of the view. Also, how would i add a onTouchListener to the dialog so the user could could just click to dismiss it? As a bonus, how would i animate the Dialog to appear from the bottom up?

Thanks, Newton

Edit #1:

Using the flag FLAG_NOT_TOUCH_MODAL may cause some problems when showing the Dialog in a ListView, so change it to FLAG_NOT_FOCUSABLE instead.

newton_guima
  • 1,569
  • 2
  • 18
  • 26

1 Answers1

5

That's a lot of questions, I will answer the first one:

Dialog progressDialog = new Dialog(getThisContext());
progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); //You have to put your background window transparent. 
progressDialog.setContentView(R.layout.hint_view_layout);

second one, gravity to the bottom:

WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;

third one (cancel on touch outside) :

progressDialog.setCanceledOnTouchOutside(true);

Also, if you set your flags and then you clear your flags they are not going to be set.

forth question (animation from the bottom):

take a look at this answer, I can see that your are an iOS developer, so this works like the iOS action sheet sliding from bottom to the center.

Community
  • 1
  • 1
Dyna
  • 2,304
  • 1
  • 14
  • 25
  • I was able to clear the background with your code and also put it to the bottom, thanks, but the setCanceledOnTouchOutside did not work.. maybe because of the FLAG_NOT_TOUCH_MODAL. Also, the flags im clearing is the FLAG_DIM_BEHIND, not the one i added before. Yes, im a iOS developer digging into Android.. :P. Will take a look into the animation! – newton_guima Oct 24 '13 at 14:47
  • you have to take tose flags Newton, sorry for not writing it all. you have to search this things on google Bro. – Dyna Oct 24 '13 at 15:02
  • Hey, the animation worked just awesome, Thanks! And i also tried removing the FLAG_NOT_TOUCH_MODAL flag and now it cancels when i touch anywhere in the window, but then i loose focus in the main activity.. the dialog should be dismissed when clicked in it.. Nice answer anyways ;) – newton_guima Oct 24 '13 at 15:06
  • Just figured it out.. i simply added an id to the main RelativeLayout and then i set a onTouchListener so it would cancel the dialog..!! – newton_guima Oct 24 '13 at 15:24
  • lembra-te de protegeres a opção do Dialog não se encontrar à vista `if(progressDialog.isShowing)` ;) – Dyna Oct 24 '13 at 15:27
  • Como advinhou que eu falava portugues? Acho ate que so podemos falar ingles aqui.. yes, im doing that. – newton_guima Oct 24 '13 at 15:36
  • BR, I'm portuguese ;) – Dyna Oct 24 '13 at 16:05
  • Hello @Dyna, i implemented your suggestion to animate the Dialog but then i implemented the "Android Action Bar Style Generator" files and the Theme that it generated, and now the Style used to create the Dialog "hintDialog = new Dialog(c, R.style.DialogSlideAnim);" is showing the dialog with a transparent background.. any ideas on what I should fix? thanks :) – newton_guima Dec 07 '13 at 13:49
  • @newton_guima in your custom style DialogSlideAnim you have to put this line: ` @android:color/white`. You can replace this with the color you want: `@android:color/white – Dyna Dec 09 '13 at 10:52
  • It worked with @android:color/darker_gray but only the background, not the sounded shape.. i was using a xml with the background data in a before i changed the app theme in the Manifest.. now this xml with the Dialog background doesn't work :S – newton_guima Dec 09 '13 at 17:28
  • it goes without saying but did you removed this line: progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); – Dyna Dec 09 '13 at 17:31
  • oh no.. i didn't .. will try that :S – newton_guima Dec 09 '13 at 17:33
  • ok, removing this line brought the dialog back to its normal style… it's ignoring the android:background="@drawable/hint_view_bg" in the dialog xml.. – newton_guima Dec 09 '13 at 17:35
  • newton, I believe I am not understanding your problems, maybe you should open a new question and/or show me the images of what is happening and what do you want to happen. – Dyna Dec 09 '13 at 17:38
  • i posted a new question: http://stackoverflow.com/questions/20477412/custom-dialog-ignoring-the-background-in-xml – newton_guima Dec 09 '13 at 18:00