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?
Related
I want to develop the Messaging application like native Application. The Problem is when i add buttons after adding two or three buttons depending on name size will become like this
My Code is as below
RelativeLayout tr = (RelativeLayout) findViewById(R.id.contacts_div);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button b = new Button(MainActivity.this);
b.setText(name);
b.setId(id);
tr.addView(b,params);
if(prev_id!=0)
{
params.addRule(RelativeLayout.RIGHT_OF,prev_id);
}
I want if the enough space is not available the it will add on Next Line.
i try many posts in stack overflow but fail to solve this problem
Thanks in advance
You should use a gridview for this.
try setting your button position through yout code once the text is set then you can see if the total width of your buttons is too big. Then you'll know when to place a button on the next line.
And as #Snicolas said use a GridView, the placement will be easier.
I hope this will help you.
You should use FlowLayout. It's not standart android component, so you should find implementation you like. E.g. this one is nice.
I want to make a floating layout appears on all screens of any apps on the phone, and I can make actions on this layout besides other app running beside this layout can receive it's own actions and events
And here is an app which do what I want https://play.google.com/store/apps/details?id=com.ninja.sms
Here is the approach which I have worked on:
I used WindowManager to draw the layout on and a service to manage this layout.
I used the following library https://github.com/t0mm13b/TouchSoftly but it has some problems
1. The layout doesn't receive actions or touch events, the actions goes to the views under the one which drawn by this library.
2. The layout disappear when the activity which launched the service killed.
So, I have made some customization on it in the following snippet
_layOutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
_layOutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
_layOutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
_layOutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
if (_layOutParams != null) {
Log.d(TAG, "onCreate() - Got _layOutParams!");
_layOutParams.gravity = Gravity.RIGHT | Gravity.TOP;
} else
Log.d(TAG, "onCreate() - _layOutParams is null! :(");
_hudPageView = _layOutInflater.inflate(R.layout.service_hudpageview, null);
The result is that : the layout already drawn and can get touch event but the Home, Back and Recent apps buttons doesn't work or have any effect on any app while the layout appear on the screen.
So still doesn't achieve what I want.
And Here is the source code and repository which I am working on, so u can review my source code
https://github.com/mmelsabry/FloatingLayout
I know there is another question here Floating widget / Overlay on Android launcher
but it doesn't help
Try using TYPE_SYSTEM_OVERLAY instead of TYPE_SYSTEM_ALERT.
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;
I have created a custom title bar as shown in this example
http://staticallytyped.wordpress.com/2011/03/18/android-dynamic-and-custom-title-bars/
"A custom title bar" - half way down.
On some activities I would like to place a button on the right hand side of the titlebar (same as facebook app). I have attempted to add a button to the view as follows, but it doesn't appear.
Custom title bar is displayed as follows
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.maintabhost);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.headerbar_include);
Attempting to add button as follows. The button will eventually be an ImageButton and aligned to right of custom titlebar-if I get it working. (just realised I've too many layoutparams now, but this isnt affecting the button display)
LinearLayout layout = (LinearLayout) findViewById(R.id.headerbar);
Button searchButton = new Button(this);
searchButton.setText("info");
LayoutParams layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
searchButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(searchButton, layoutParams);
layout.invalidate();
I could just create another custom titlebar with the button already embedded, but a dynamic solution would be better.
Cheers
First of all, thanks for the link to my blog. Second, let me see if I can't answer that for you. One of the reasons you're having trouble adding another button when you want to add a button is that in that example I left you with no way of retrieving the Title Bar View through the usual channels. Hence, let's fix it (and potentially let me write another blog post this coming weekend.)
Starting with the xml file, add an id attribute:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:id="#+id/title_complex">
<!-- Stuff -->
</LinearLayout>
and here's code to show you how to get that button in there within your Activity (you'll have to add all the flair later):
LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.title_complex);
layout.addView(new Button(this));
and if you take a look, there's a non-descript button in the Title Bar (like I said, you'll have to add your own flair):
Due note, however, that I can't guarantee that the button will remain or won't remain on subsequent Activities. I haven't investigated it yet but this should get you started. And if you have any more questions, feel free to ask them here (more eyeballs) or on my blog.
Here's one approach to make sure the button(s) remain in the title bar:
How to Create Custom Window Title in Android
Essentially, wrap the android Activity class, and then extend from that new class.
I'm sure I'm missing the point here so I'm hoping someone can explain.
I want to create a popup when a user touches an ImageView. I had a look at AlertDialog and the docs say...
If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it:
...with the following code...
FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
So as a test I tried the following in my onCLick() method...
TextView tv = new TextView(this);
tv.setText("Hello World");
FrameLayout customFrameLayout = (FrameLayout) findViewById(android.R.id.custom);
customFrameLayout.addView(tv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
The last line of the above where I'm calling addView throws a NullPointerException which makes me think there's a problem with android.R.id.custom.
So the question is, what is wrong with the above and also is there a better way of creating a custom popup (perhaps by using the Dialog class or extending it)?
NOTE: I'm only using TextView in this example as a test, I want to add something more complex for my actual popup.
One option is to create an Activity and style it using the dialog theme:
<activity android:theme="#android:style/Theme.Dialog">
See applying themes for more information.
Checkout Mossila's AlertDialog customization examples. I found them more helpful than Google's examples.
I cut-and-pasted Mossila's code directly into my project and it just worked:-) Then I made a few tweaks to meet my needs.
http://mossila.wordpress.com/2011/05/10/android-dialog-single-choice-items/
I think your problem is because you dont "inflate" the layout. With a FrameLayout you need to use the LayoutInflater
use the following code:
LayoutInflater.from(context).inflate(android.R.id.custom, this, true)
This should work with FrameLayout. Read up more about this at the Android Layout tricks page
Also check out LayoutInflater
edit: i have noticed aswell that there is an identical article to this problem here too: How to implement a custom AlertDialog View