Rotate whole android layout - java

Is it possoble to rotate the whole android userinterface on an eventlistener ?
For example an app which runs in the background and after putting the headphones in the whole interface turn 180° until you put it out again.

Yes it is possible to change layout programmatically (Although not sure about 180 degrees?).
See this link: Change Screen Orientation programmatically using a Button
The first answer he has explained how to change layout programmatically instead of setOnClickListner you can use your eventListener.
EDIT: Look at this:
https://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG
Its the broadcast which takes place in the event a headset was plugged in.

Related

Preview live wallpaper preview in main activity background

I am quite new to android and java, and I would like to have a bit of help from you to figure things out.
I have been working on a live wallpaper app lately, here is what I achieved and my blocking points:
I use the WallpaperService to create my custom LWP, I change its colors and shapes based on the user preference (preference fragment screen) with preferenceChangeListener.
My main activity just handles the preference fragments and UI. Is is my main menu where I can set the WP and change the preferences.
Here is my problem: I would like to show the live wallpaper preview as background of my main activity.
I haven't figured out a way to do this so far.
The only solution I found is to show the wallpaper through the activity by changing my Theme to a transparent one.
But this requires to set the wallpaper first. Once the WP is set, my app works exactly the way I want. But before that, my main activity can't show the WP preview because the service isn't running yet.
I don't think that there's a way to extract the WP preview from the WP service. I need to change something.
Also, I don't want to show only a sample img of the WP, I really want to run my code and show the results to the user as a preview.
Could you tell me how to change my code in order to get the correct behavior? I might have to use another class that I could run in parallel or something. It is a bit obscure to me...
Thank you very much!

how to use fragments to create an overlay effect for an add contact function when a button is pressed

I'm trying to create an overlay that is triggered when a button is pressed. This overlay is supposed to allow the user to add their contact and I was wondering how can I use fragments to get this effect like you can see in this mockup.
I am in a dilemna over using fragments is the right choice. My reasoning being that I only need to have it do one task that is adding contacts, and thus I do not need a whole activity.
This is what I have on the main activity right now
I would really appreciate any help on understanding how to make this work.
You can use a DialogFragment.
It behaves like a normal Fragment for the most part. Here is a guide for a basic implementation https://guides.codepath.com/android/using-dialogfragment
They appear automatically in the center of the screen. To get it a bit lower like in your mockup you can change where it is in the window. Here is an answer showing such Position of DialogFragment in Android

Place notification beneath the divider android java

I am busy with notifications, and I was wondering how I could archieve such thing as in the image below. I tried setting it as ongoing, but it didn't quite archieve the functionality I would like it to be.
How can I place my notification beneath that divider? (So above or under USB for charging)
Set your notification to PRIORITY_MIN:
builder.setPriority(NotificationCompat.PRIORITY_MIN);

Change app background

I'm currently making an android app in which I'd like the user to be able to change the background image.
I've got 3 images and a screen where I can choose a picture and a button for applying.
The issue:
I can allow the user to see all images in the way I want, but I don't know how to set the selected image as app background.
What I want to do:
I want the user to click a button, which exports the selected image to "bakgrund.png" in "/res/drawable-mdpi" and replaces the current one. This would allow me to easily integrate the background switcher. Renaming of current files also works.
PS: My current background images are located in /res/drawable-mdpi named 1.png 2.png and 3.png.
Easiest way would be to call the setBackgroundResource(image_id) method on the root layout. Like if you have a LinearLayout which has android:id="#+linear" as the root layout in the layout xml, then this code will help:-
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.linear);
linear.setBackgroundResource(R.drawable.1);//call this in the OnClickListener's OnClick Method
Firstly, you need different themes which has different backgrounds. So you may use this.setTheme method in your Activity.
Indeed I suggest you, two different layout (with different backgrounds but have same components) and using setContentView during onClick.
I hope it solves your issue.

how to show a "window/subactivity/dialog" on top of an Activity but keeping the focus in the Activity

My activity A is a game and it does some background operations. When I press a button in the contextual menu, I want to pop up a "small window/dialog/subactivity" (lets call it B) that appears on top of activity A and displays some data about those background operations. But I need to keep the focus on the activity A in order to continue interacting with it (playing the game).
In essence, I want to be able to see the data display by B while playing the game.
I'm not really sure how to implement this. After reading the documentation I have the next conclusions:
I know that I can't use Dialogs because the have the focus. Is it possible to avoid this?
Using a subactivity with a Dialog theme it's another option that looks tempting...but I believe that the subactivity has the focus. Ditto.
My last option is to try to add a LinearLayout with my data to the main Layout, "sharing/splitting" the screen. It's not pretty, but at least I know that this is possible. What I don't like about this approach is that I use the width and height of the screen.
Any suggestions? Solutions?
PS: I found some this thread here that are very related to my question:
Android ==> Sub Activity?
Create an Activity with style Theme.Dialog. This is a normal activity which looks like a dialog, while being modeless and accepting events.
Additional catch is in setting WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL and resetting WindowManager.LayoutParams.FLAG_DIM_BEHIND.
See this answer for complete example: timed modeless dialog
Why not use a FrameLayout that is apart of your Activity? Just ensure that this View has a higher z index (make sure you declare it last in your XML layout or create it at runtime). That way you never leave your Activity.

Categories

Resources