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;
Related
I am developing a lockscreen application, and so far have achieved everything needed for the app to be working.
But I can't disable the home/menu buttons available as virtual as well soft in Android devices. I have gone through every possible answer on SO and other sites but can't achieve it.
Is there any tested and working workaround?
Thanks in advance.
One way is to display a dialog where the LayoutParams type is set to TYPE_SYSTEM_ERROR and set the owner of this Dialog to your "lockscreen" Activity to block the home button.
Here is an example on how this can be done:
Update: looks like this only works with pre Android 4.+
https://github.com/Joisar/LockScreenApp/blob/master/LockScreenApp/src/com/mehuljoisar/lockscreen/utils/LockscreenUtils.java
Another way is to add your contentView directly to the WindowManager where the LayoutParams type is set to TYPE_SYSTEM_ERROR
...
onCreate(){
//setContentView(R.layout.main_content);
//instead add a View directly to the WindowManager
View contentView = View.inflate(this, R.layout.main_content, null);
LayoutParams lockLayoutParams = new LayoutParams();
lockLayoutParams.width = LayoutParams.MATCH_PARENT;
lockLayoutParams.height = LayoutParams.MATCH_PARENT;
lockLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
//LOCK
getWindowManager().addView(contentView, lockLayoutParams);
...
//UNLOCK
getWindowManager().removeView(contentView);
The downside I had with this approach is that it looks to not support more complex views. I got a lot of flicker with ListViews in Fragments etc.
Some example projects where this is used:
https://github.com/Chin-Z/ArcherKeyguard/blob/master/src/com/lovewuchin/app/archerkeyguard/util/LockLayer.java
https://github.com/radames/ScreenLock-Android/blob/master/src/com/eva/me/mysquarescreenlock/lockutil/LockLayer.java
More answers to similiar question here: How to disable Home and other system buttons in Android?
So I need the view of my settings to display over my main page.where there is a "setting"button in Main page which opens this settings view. But I want My main page to be visible beneath my settings view which only covers a half or less of the main view.
I tried adding
<org.example.myCustomView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="#android:style/Theme.Dialog" />
Which I found from stackoverflaw itself. But I cannot do it instead the application corrupts at button click.
I am not sure I got this too correct, Or is there any other clear way to do it?
I replaced myCustomView with my relevent class and created the manifest also but it did not work.
If there is any other alternative way to do this mention to me.
I am not talking about how to place a TextView, Button, EditText on a view
I am talking about completely two layouts.
Below image is only an example to express my question.
I think you need to utilize layoutinflater. Here is a simple example how to use it
setContentView(R.layout.yourMainView);
LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
View overView = mInflater.inflate(R.layout.yourSmallView, null);
addContentView(overView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
Use FrameLayout.FrameLayout is like container.In it place first your bluelayout and then your settings layout.It will show as if settings layout is placed on top of your blue layout.
and then hide and show your settings layout on the onclick when required.
eg:
You could use a Dialog Fragment which would be much more simpler and show more complicated stuff on UI with better responsiveness. Have a look at Dialog fragment:
http://developer.android.com/reference/android/app/DialogFragment.html
Is there any possibility in Android to align a dialog Activity (activity with a dialog theme) at the bottom? By default, it is displayed in the middle of the screen. I haven't found any information on that... Thanks.
This is not exactly what you are doing, but I had a similar problem where I needed to display a dialog from an activity - at the bottom of the screen. The trick was to use WindowManager.LayoutParams. Here's what my onPrepareDialog looks like:
public void onPrepareDialog(int dialog, Dialog dlg) {
Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
switch (dialog) {
case DIALOGUE_LOADING_PLEASE_WAIT:
wlp.gravity = Gravity.BOTTOM;
window.setAttributes(wlp);
break;
default:
break;
}
}
I didn't tried but searching on google found this...does this help you getWindow().setAttributes() , I hope this will help you.
content of the link (if it doesn't work) :-
You can call getWindow().getAttributes() to retrieve the
WindowManager.LayoutParams for the window. This has the following
fields involving window placement:
http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#width
http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#height
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#gravity
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#x
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#y
After making your desired changes, use getWindow().setAttributes() to
install the new values.
Note that, though you can force a specific size through the width and
height fields, in general the correct way to do this is let the window
do its normal layout and determine the window size automatically that
way. If you have a single view in the dialog that wants a fixed size
such as 300x200, implement View.onMeasure() to call
setMeasuredDimension(300, 200). Then when the view hierarchy layout
happens, the dialog window will be given a size that ensures your view
is that dimension (probably making the actual window larger to take
into account the dialog frame and decoration).
http://code.google.com/android/reference/android/view/View.html#onMeasure(int,%20int)
I want to add a new view to my scene that will contain content that will change programatically throughout the course of my application. When it does change, it needs to pop up on the screen for 3 seconds (or a click) then disappear.
This view will change in size according to its content WRAP_CONTENT, but ideally I'd like it centered horizontally and vertically on the screen.
I'm stuck on three parts:
1) what type of view should I use for this...I was thinking Relative, but all of my playing with it has yielded no good results for what I'm trying to do
2) with respect to #1 (trying relative view), I could not get it centered properly (tried using param.leftMargin and param.topMargin with varying values but could not get it to work on different devices with different resolutions
3) also with respect to #1, I couldn't make this float over everything else on my screen (need something like a z-index or the like).
any ideas, code examples would be wonderful.
TIA
Use a custom dialog, i.e. a LinearLayout with android:theme="#android:style/Theme.Dialog" and for the class, it would be something like
public class YourCustomDialog extends Dialog implements DialogInterface
where you can implement you custom logic of what to display. Such dialog is floating and modal on top of all other views then and you can also optionally set the background to blurry, etc.
This is a typical constructor of my custom dialog - the layout would be defined in an xml layout file, which in my case is my_custom_dialog.xml:
public MyCustomDialog(Context context) {
super(context, android.R.style.Theme);
Window window = getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.empty));
setContentView(R.layout.my_custom_dialog);
// actually not necessary as it's already the default value:
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
...
}
I need to create a GUI (layout+views) in my .java activity class (I know it's far more flexible and easier to use .xml layout file, but I don't want to use it for now).
I can't find any setGravity() (but a "Gravity" object I can't figure how to use) or any set setMargin() method for the "View" object.
What is the easiest way to do it ?
Thanx.
For setting the margin on component. The following leaves the existing margins as previously set and sets the left margin as zero.
TextView title = ((TextView)findViewById(R.id.default_panel_title))
final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)title.getLayoutParams();
lpt.setMargins(0,lpt.topMargin,lpt.rightMargin,lpt.bottomMargin);
title.setLayoutParams(lpt);
You can add gravity to the "layouts" not to the "controls". Try to set gravity to any of your Linear/Relative or Frame layouts using setGravity(); .
Eg:
LinearLayout lll = (LinearLayout) findViewById(R.id.layoutname);
lll.setGravity(Gravity.CENTER);