Android: Stop Recreating the activity on orientation change - android

I have a listview with two buttons in my main.xml layout. On click of one button i'am creating a textview dynamically and adding it at the bottom of the screen to confirm the user interaction. When the user clicks 2nd button (Confirm button), i need to add that text to listview. To support landscape mode, i have the same layout file in layout-land folder. When i click on 1st button it is creating a textview with some text and adding at bottom of the screen. Now if a change the device orientation then it is loading the landscape main.xml and activity is recreating again. So my textview is getting collapsed. How can i prevent that the recreation of activity on orientation change. (But it should pick up the other layout file).

Just edit the Activity Tag in androidmanifest.xml.
<activity
android:configChanges="keyboardHidden|orientation"
android:name=".testActivity"
android:label="#string/app_name"></activity>

You should add screenSize
if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
Added in API level 13.
then it should be like this
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name=".testActivity"
android:label="#string/app_name"></activity>
http://developer.android.com/guide/topics/manifest/activity-element.html

Related

Edittext hide under keyboard when edittext create on RunTime

I have a layout with scroll view as root layout with one linear layout as child with vertical orientation. I add a list of edit text dynamically in linear layout onCreate of activity. Problem is when i focus on an edit text the layout doesn't scroll up on soft keyboard up. But works perfectly if edit text are not added dynamically.
I have already set adjust pan in manifest.
In your Manifest file add the following code for this particular activity
<activity android:windowSoftInputMode="adjustPan">
or
Activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
<activity
android:name=".ActivityName"
android:screenOrientation="nosensor"
android:windowSoftInputMode="adjustPan|stateAlwaysHidden" />
Use nosensor if you want to use the app only for portrait modes. Use stateAlwaysHidden in the if you don't want android to pop that keyboard up everytime your activity opens.
Check out this link the official documentation and check other stackoverflow answers.

On Orientation change cannot update the fragment view in android

I have a MainActivity.java which has 2 fragments:
1) Fragment1.java -> Has a RecyclerView of items.
2) Fragment2.java -> Displays the details of the item when it is clicked.
In Portrait orientation only Fragment1.java or if item clicked then Fragment2.java is visible.
In Landscape mode both the fragments are visible side by side.
This works fine in both Portrait and Landscape orientations if I start the app in that orientation.
However if I change the orientation in between when the app is still running, then I get the error IllegalStateException : Cannot perform this action after onSaveInstanceState whenever another list item is clicked. The action on which it throws the error is transaction.commit().
Any idea how I can fix this?
I would suggest seeing if you have set the Orientation mode in the activity tag in the manifest and if you did remove it.Android handles this automatically.
such as :
<activity
android:name=".MyActivity"
android:screenOrientation="portrait" />
Regarding the different looks of the fragments when the app is in Landscape or Portrait , this has to do with the Layout you choose in the xml file.
Relative , Linear etc. Please checkout the Android official documentation and use that meets your needs.

Android rotate only status bar and buttons

I decided to develop my own camera activity since I need to force the user to take a 1:1 ratio picture; just like Instagram. I created my activity based on this activity from an open-source project:
https://github.com/pocorall/scaloid-apidemos/blob/master/src/main/java/com/example/android/apis/graphics/CameraPreview.java
Now I'm trying to handle the screen rotation to avoid the layout from being rotated. In other words, I want to keep the layout from rotating and rotate only the buttons and the status bar depending on the device rotation.
I already declared my activity to listen for config changes in the manifest file.
<activity
android:name=".activity.CameraActivity"
android:configChanges="orientation|screenSize"
android:theme="#style/Theme.Eyes.NoActionBar"
android:windowSoftInputMode="adjustNothing|stateHidden">
</activity>
Stop rotation of layout
Add android:screenOrientation="portrait" into manifest
Detect rotation for individual buttons
Call Display.getRotation() to check how much the screen has roated and adjust buttons accordingly

How to preserve orientation during a popup?

I am using menu in an activity to display a popup window.
The problem is that when the orientation is changed, the popup window disappears and the activity screen is shown again.
(It happens because when the orientation is changed the Activity is started again)
i want that the activity doesnt start again and after orientation change the popup window doesnt vannish.
please chenge activity in manifest file
put in activity below code
android:configChanges="orientation"
Please make the following changes to manifest where you defined your activity
add this lines:
android:screenOrientation="landscape" or
android:screenOrientation="portrait"

How to change the Orientation for fragments in Android?

I have managed to change the Orientation for my fragment in my activity.
The Orientation is changed when my device is rotated.
Now I want to same functionality when I click on a button in the fragment.
i.e. I want to change from portrait mode to LandScape mode when I click on a button on the portrait layout and visa vera.
I used
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
and
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
respectively.
But this made the auto rotate functionality stop working. How can I fix this issue?
When you need auto rotating use getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
Please try this
<activity
android:name="<class name with package>"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait">

Categories

Resources