Preventing an activity from occupying the full width of the screen - android

I have a dialog-style android activity that gets displayed on top of the main activity in my android app. I don't want it to take up the full width of the screen. How can I specify that it leaves a margin of x dp on either side of it?

Two solutions:
you can set the margin manually as said by #Raghunandan there.
you could set a custom theme to that particular activity which by default doesn't take up the full screen space. for that particular activity just add
<activity android:theme="#android:style/Theme.Dialog">

Try this code
LayoutParams lp = this.getWindow().getAttributes();
lp.x=50;lp.y=20;lp.width=100;lp.height=200;lp.gravity=Gravity.TOP | Gravity.LEFT;
lp.dimAmount=0;
lp.flags=LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_NOT_TOUCH_MODAL;
this.setContentView(view, lp);

Related

Android CustomDialog minWidth

I have a CustomDialog (extending Dialog) which is currently set to wrap_content for both width and heigh. Problem is if I set the main text as something really short (say "hello") the dialog gets very small in width. I would like the dialog to always take all the space available, but still have the default margin from the edges of the screen.
I already tried various methods:
setting:
android:layout_width="match_parent"
on the dialog layout, doesn't have any effect.
doing:
int width = (int)(getContext().getResources().getDisplayMetrics().widthPixels*0.90);
getWindow().setLayout(width, LinearLayout.LayoutParams.WRAP_CONTENT);
causes the dialog to go at the start of the screen and doesn't change anything regarding the width
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.horizontalMargin = 10;
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
makes the dialog take the whole screen but without any horizonthal margin
putting
<item name="windowMinWidthMajor">100%</item>
<item name="windowMinWidthMinor">65%</item>
in the dialog style has no effect.
how do I accomplish that? Mind that the dialog is used all around the app, so putting any code outside of the dialog class itself is not an option.

Why are these buttons mis-aligned in a TableLayout?

I'm using a table layout to arrange some buttons. As long as I use the same font for all the labels they are properly aligned in each row.
For some buttons I'd like to use icons from a custom ttf font.
When I use such an icon, the button is placed slightly higher, like so:
(This image is scaled up to make the the problem more evident.)
I took measurements - the buttons appear to be of same height, regardless of the used font.
Why are the buttons not aligned properly?
Does anyone have a suggestion to get them aligned?
Thanks.
Following CommonsWare's advice (thanks for the quick replies!), I tried this:
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER_VERTICAL;
row.setLayoutParams(layoutParams);
This did not work. Will try the base alignment comment next.
Add the following attribute to your TableRow:
android:baselineAligned="false"
By default, the button labels' base lines are vertically aligned which causes the offset you experience.

Android Activity with Transparent Layout

I wish to create a activity that look like a popup window
That is with transparent background for activity als i wish to show it in custom position on scree that is on right corner of device screen
what i did was inside onCreate of pop over like activty
Display display = getWindow().getWindowManager().getDefaultDisplay();
WindowManager.LayoutParams params = getWindow().getAttributes();
// params.x = -20;
params.height = (display.getHeight()) / 2;
params.width = (display.getWidth()) / 2;
// params.y = -10;
params.gravity = Gravity.RIGHT;
getWindow().setAttributes(params);
in maifest
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light.NoActionBar"></activity>
This is what my launcher activity looks like
So when i click on search on action bar i'm sending intent to my new activity .I want to make it look like pop just below the search icon
This what i obtained is
You can see search of previous activity is clicked and new activity is loaded(blue portion).
As you can see the new activity moves to centre of screen . but i want it just below the action bar icon.
i tried params.gravity = Gravity.TOP | Gravity.RIGHT;. then i got this
I want to place it just below the action bar of previous activity . I tried many ways to achieve it but failed. so can anyone suggest a methode
In your manifest you wrote
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light.NoActionBar">
you should remove the NoActionBar part
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light">
I can think of two things that may help. First, you could try changing the Theme in your manifest to android:theme="#android:style/Theme.Dialog and this should give you the wanted look.
Edit
You can hide the title bar with
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
just make sure to call it before calling setContentView()
Also, if it doesn't need to be a separate Activity you could use PopupWindow. With this you can use isAboveAnchor and use the ActionBar as the anchor and place it below or use one of the other associated methods. I don't know if either of these will fulfill what you need but they may work for you.
#edwin What you can do is after making
params.gravity = Gravity.TOP | Gravity.RIGHT;
you just provide some margin from top, like this:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
yourCustomPopOveractivity.setLayoutParams(params);
and i think after doing this you can get your needed position and everything you just did is right.
Please try then tell me
Try Gravity to Center rather than Top Right..

Align an activity as dialog on top left

i know this question was asked for aligning a dialog on the left,
but here i'm using an activity with a
android:theme="#android:style/Theme.Dialog" and the problem is that it's display at the center of the screen and i want to display on the top left corner.
is there any way to do so ?
i have tried using the android:layout_gravity="top|left" on my concerned activity .xml but that doesn't work.
Thank you for your help
Try this on your onCreate of dialog activity
WindowManager.LayoutParams wmlp = getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
Hope it helps.

Windowmanager with Animation

First of all thanks everyone who tries to reply this topic.
I have an activity and I wanted to show a sort of menu at the top of the screen and I used windowmanager to handle this. it was about UI issues I encountered why I choise windowmanager to do such a menu. But for now I want this menu to animate but it seems animation takes no effect. Here is my code.
If anyone has any idea how to animate windowmanager I ll be appreciate.
Animation animShowTopLine;
animShowTopLine = AnimationUtils.loadAnimation(this, R.anim.translate);
animShowTopLine.reset();
LinearLayout top_line;
WindowManager wm;
WindowManager.LayoutParams wmParams;
LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
top_line = (LinearLayout) inflate.inflate(R.layout.line, null);
wm =(WindowManager) getApplicationContext().getSystemService("window");
wmParams =new WindowManager.LayoutParams();
wmParams.type=2002;
wmParams.format = 1;
wmParams.flags=40;
wmParams.width=WindowManager.LayoutParams.FILL_PARENT;
wmParams.height=WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.gravity = Gravity.TOP;
wm.addView(top_line, wmParams);
top_line.startAnimation(animShowTopLine);
Thanks in advance. Regards.
First, please for your own sanity don't hard-code a bunch of constants like that. The name of the window manager service is Context.WINDOW_SERVICE. The window type is WindowManager.LayoutParams.TYPE_PHONE. The flags you have set is... ummm... WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE (seriously, written in decimal as well??). The format is PixelFormat.RGBA_8888 (and I would strongly recommend using PixelFormat.TRANSLUCENT instead).
Now, first, what in the world are doing using TYPE_PHONE? You don't want to do that. If you want a window layered on top of your main window, you should be using TYPE_APPLICATION. In fact, I would urge you to just use Dialog for this and set its attached window to be what you want. It will take care of all the details of working with the window manager, and doesn't limit you in any way with how you can animate it.
I think the main problem you have here is that you are trying to animate the root view of the window. The root view is somewhat special -- it defines the top-most part of the window, exactly matches the window, and is always forced to be the size of the window. It is what drives the layout of its child windows. If you want to do a view animation, you should leave the root view alone (it is the anchor of the window) and animate the elements inside of it.
That said, since you are using an old-style animation, there is a better way to animate full windows, the way that the system animates dialogs and activities and such: set the animation in the window's layout params. Then the window manager will apply that animation to the entire window surface as you specify. This is more efficient than doing it inside of the window because each frame of the animation only requires re-compositing the screen with the new animation transformation, instead of re-drawing the window contents and then re-compositing it.
You do this by setting WindowManager.LayoutParams.windowAnimations. This is an integer field that takes the resource id of a style resource defining the various animations associated with the window. For example, the style used for the standard dialogs is:
<style name="Animation.Dialog">
<item name="windowEnterAnimation">#anim/dialog_enter</item>
<item name="windowExitAnimation">#anim/dialog_exit</item>
</style>
You set windowEnterAnimation to the animation resource to run when the window is being shown, and windowExitAnimation to the one to run when it is hidden. If not set, no animation is run.
So for your code here, you can just make a Dialog, set its content to your custom content, set its gravity, width and height to the values you have here, and set its windowAnimations field to your style defining the animations. You can also tweak the flags if you want some behavior different than a default dialog (not touch modal or such). The API on Dialog.getWindow() has everything you need to set the layout params.
mParams.windowAnimations = android.R.style.Animation_Toast;
mParams.windowAnimations = android.R.style.Animation_Toast;
Where android.R.style.Animation_Toast is a style resource defining the animations to use for this window. This must be a system resource, it can not be an application resource because the window manager does not have access to applications.
Others valid styles are:
mParams.windowAnimations = android.R.style.Animation_Translucent;
mParams.windowAnimations = android.R.style.Animation_Dialog;

Categories

Resources