How to set screen rotation in android for only one activity? - java

If i set rotation for the inner activity the whole screen rotates. I only want the inner activity to turn into landscape mode, not the outer activity.
This is a screenshot in which you can see 2 activities. One is the whole screen another is the camera that scans a QR code:

Add the below mentioned attribute in your Activity tag inside AndroidManifest.xml
android:screenOrientation="landscape"
Ex:
<activity android:name=".MainActivity" android:screenOrientation="landscape">

Related

Changing orientation of the device refreshes the fragments activities

I realized a problem in my app, I use BottomNavigation with FragmentActivities, the problem was that when clicking one of the 5 FragmentActivities. 5 activities are [Home, Search, Post, Notifications & Profile] if I click any of them being in portrait state and change the device orientation to landscape the activities refreshes and start to [Home] screen always.
How do I fix this?
maybe you can try add configuration in your main_activity in manifest
<activity
android:name=".your.main_activity"
android:configChanges="keyboardHidden|orientation|screenSize"/>

auto rotate stops music when app rotates

I added
android:screenOrientation="portrait"
To the activity in the manifest but the app still rotates if the device is turned. Cannot add all of code yet, on phone. But will add when i get back home.
You can set the orientation in two ways:
I. Programatically:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
II. In your AndroidManifest.xml
<activity android:name=".activity"
android:screenOrientation="portrait">
</activity>
It's hard to understand what you need from the few lines you wrote. It looks like you are creating a music player app and you are facing the problem that music stops playing when changing the orientation of the device. It also looks like your solution to this problem is to prevent screen orientation changes. If I got it right, you can prevent screen orientation by putting the following line in your Activity tag inside the manifest:
android:configChanges="orientation|screenSize"
The music stops playing because when screen orientation changes, the system destroys and recreates the Activity. If you cut away screenSize from the line above, the system will not destroy your Activity, thus the music will keep playing, but the user will still be able to rotate the screen. Moreover, if your application is actually a music player I would consider other ways of implementing it, such as background services.

Android Google Maps API, prevent map reloading/redrawing on screen orientation change

On my app, when you tilt the device in the mapView, it redraws all the markers. I want to not let this happen.
To avoid the reloading of the map on screen orientation change I've added in the manifest, in the activity tag this line:
android:configChanges="orientation"
I don't know if it's the best solution but it works
Try using;
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"

Android - How to start the exact same activity every time the app is opened up?

In a nutshell, to give you an example, I basically have an app with 3 activities:
Activity1
Activity2
StartActivity
StartActivity contains two buttons that correspond to the other two activities respectively, starting them up. If I exit the application from Activity1, when I later click on the app icon from the phone, Activity1 is restarted since Android keeps track of this. I need to have the app restart to bring me to the StartActivity, so that I can choose where to go by clicking the buttons, instead of having to click the back button to be able to end up at the StartActivity.
I'm assuming onResume and onRestart are involved, but where should they go?
Any help is greatly appreciated thank you.
You might look at the android:clearTaskOnLaunch activity attribute from the Manifest file : http://developer.android.com/guide/topics/manifest/activity-element.html
I think setting this attribute to "true" on your root activity does what you want.
I would think the solution to be destroying Activity1 and Activity2 onStop. This leaves the stack with only your StartActivity. You can call the finish method in Activity to terminate it programmatically at anytime.
Generally the way it works (from what I understand) is you use the BACK button to EXIT the activity, and the HOME button will essentially "minimize" the activity - bringing you back to whatever activity was left open.
While you should leave this functionality the same, you can override the home button to completely exit your application.
In your mainfest.xml file write like this.
<activity android:name=".StartActivity" android:label="#string/app_name"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:clearTaskOnLaunch="true" attribute automatically restart the activity.
Just add the below code into your manifest file into the activity whichever you want t open on start.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Android "Header" Text

Is there a way to modify an Android app's header programatically? Maybe even adding a custom view with an image in it? I'm talking about the gray bar at the top of an app, whose text can be modified via an app's Mainfest.xml with the label attribute of the activity:
<activity android:name=".ProgramTracks" android:label="#string/app_name">
</activity>
Is there some way to, for a lack of a better word, mess with that?
Already discussed here.
You can add a custom header inside your activitys onCreate().
Write an usual xml layout and call
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
where R.layout.window_title is your custom layout.

Categories