Start new activity on orientation change - android

I want to load a new activity when the orientation changes from potrait to landscape for which I am using:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
startActivity(new Intent(this, ABCDActivity.class));
}
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
super.onConfigurationChanged(newConfig);
finish();
}
}
But it doesn't change my activity with the change in orientation. Have I missed something?

Try this,
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
startActivity(new Intent(this, ABCDActivity.class));
}
}

The onConfigurationChanged() method will not be called unless you declared the respective attributes under the in AndroidManifest.xml:
android:configChanges="orientation"

In your Manifest Activity tag for each activity you wish to configChanges for, you must place this line:
android:configChanges="orientation|screenSize"
As an app design note... Starting a new activity on orientation change is not advised...

Why would you want to start a new Activity when your orientation changed?
It's better to just provide different resources for different configurations! Or use Fragments inside 1 Activity.
More information can be found here:
Supporting Multiple Screens
Designing for Multiple Screens
EDIT:
You can just create one activity that recreates itself on orientation change. So remove any configChanges="screenSize..." you have in your AndroidManifest.xml.
In the xml files for the activity, you can just link to another Fragment.
And again, more information about this can be found in the above links.
MyActivity.java
public class MyActivity extends Activity
{
#Override
public void onCreate(Bundle cycle)
{
super.onCreate(cycle);
this.setContentView(R.layout.myactivity);
}
}
res/layout/myactivity.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="com.example.myapp.fragments.TimeFrameGraphFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
And res/layout-land/myactivity.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="com.example.myapp.fragments.AllDataGraphFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

Related

Android Button that stays always above the all activities for standby

I am trying to make a Button which will always be on standby like Floating Action button despite whatever context it may be in but shall sustain all above my activities, How could I achieve this please refer me a link or give me an idea.
One solution is to create a single activity with the button and then use fragments instead of the current activities.
class ActivityOne extends BaseActivity{
#Override
protected View childView(){
return getLayoutInflator().inflate(R.layout.activity_one, null);
}
}
class ActivityTwo extends BaseActivity{
#Override
protected View childView(){
return getLayoutInflator().inflate(R.layout.activity_two, null);
}
}
public abstract class BaseActivity extends Activity{
protected abstract View childView();
#Override
protected void onCreate(SavedInstanceState savedInstanceState){
super.onCreate(savedInstanceState);
RelativeLayout baseLayout;
ViewStub stub;
baseLayout = (RelativeLayout)
this.getLayoutInflater().inflate(R.layout.layout_base, null);
stub = (ViewStub) baseLayout.findViewById(R.id.base_content_stub);
// Replace viewstub with content.
baseLayout.removeView(stub);
baseLayout.addView(childView(), stub.getLayoutParams());
super.setContentView(baseLayout);
}
}
layout_base.xml
<RelativeLayout
....
>
<ViewStub
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:id="#+id/base_content_stub"/>
<Button
.... // <---- common to all activities
/>
</RelativeLayout>
Your best bet would probably be to make an abstract activity that contains that button and adds it to the layouts of all activity that inherit from it. That way you don't have to duplicate the code in every activity.
It's not possible to use the same button for every activity. You could use the same layout, but you'd have to create the button anew every time you created a new activity. You might be able to avoid that if you did some fancy stuff with the action bar that is frankly not worth it. I suggest you look at using fragments instead of activities, in which case you can have your layout file for you activity look something like this:
<LinearLayout android:id="#+id/fragment_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="coop.nisc.intern2016.ui.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/button_text" />
<RelativeLayout
android:id="fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Then, you can use a FragmentManager to put the fragment into the fragment_container. The code could look something like this:
private void showCustomFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
customFragment = new customFragment();
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, customFragment, customFragment.TAG)
.commit();
}
Create a BaseActivity class as a usual activity and a corresponding xml layout file with any layout you want. Add your button inside this layout, and a FrameLayout below the button. Code your button inside the onCreate() of the above activity. Your button's done.
Now, make all your other activities extend the BaseActivity, the super.onCreate() will take care of the button. Instead of setContentView() use:
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your base activity xml
getLayoutInflater().inflate(R.layout.the_layout_you_want_to_load, contentFrameLayout);
'the_layout_you_want_to_load'
is the layout xml of your current activity.

Get height of child layout (including child elements) in onCreateView of fragment

The size of a layout i'm using in a fragment is always 0.
I have a fragment that includes a child-layout that looks like this:
<RelativeLayout android:id="#+id/animationLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:background="#color/colorPrimary"
android:elevation="4dp">
This layout has a child-layout that is defined like this:
<TableLayout android:id="#+id/searchForm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
android:background="#color/colorPrimary"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
[Some Child elements]
</TableLayout>
In the onCreateView of the parent fragment, the height of the layout 'searchForm' is always 0.
Even if i measure the size in
searchForm.post(new Runnable() {
#Override
public void run() {
searchFormHeight = searchForm.getHeight(); //always 0
}
}
the size is 0.
How can i get the size of this layout in onCreateView?
You need to use a ViewTreeListener for a callback when the entire view is inflated. If I recall android waits for the entire view to be inflated at once, so even if a view is instaniated, it may not be inflated returning a height of 0.
searchForm.getViewTreeObserver()
.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw () {
searchFormHeight = searchForm.getHeight();
return true;
});
Ok, i don't know what i've done wrong before, but finally following worked for me:
searchForm.post(new Runnable() {
#Override
public void run() {
searchFormHeight = searchForm.getHeight();
}
});
Actually onCreateView() process the creation of view that's why it return 0
Try following to get height, override function into fragment class
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
searchFormHeight = searchForm.getHeight();
}

Reload a View on Orientation Change

In my Activity I have a View that "depends" from screen orientation. If it's in landscape mode, i use a layout under layout-large-hand, but if it's in portrait mode, I use the layouts under the layouts folder.
The Activity shows also a Map with some markers and informations.
In my AndroidManifest i have
android:configChanges="orientation|screenSize"
But the activity shows only the layout on portrait mode.
How can I fix this?
EDIT 3/12:
I have a layout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<include
android:id="#+id/my_view"
layout="#layout/my_view"
android:visibility="gone" />
</RelativeLayout>
and i want that my_view changes the layout, but map remains with all markers, zoom level, position, ecc ecc...
my_view is visible when i click on a Button created dinamically in the activity.
EDIT 2:
Like SweetWisher ツ says, i was trying to setup a custom behaviour for the views. But when i rotate the device, the map disappear. This is part of my code in the activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initUI();
}
private void initUI() {
setContentView(R.layout.my_layout);
if (mp == null) {
mp = MapFragment.newInstance();
}
getFragmentManager().beginTransaction().replace(R.id.placeholder, mp).commit();
initUIStuff()
}
private void initUIStuff(){
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
initUI();
}
#Override
protected void onStart() {
super.onStart();
initGMap();
}
#Override
protected void onResume() {
super.onResume();
initGMap();
}
private void initGMap() {
if (mp != null {
//Initialize Map
}
}
If you use android:configChanges="orientation" in your Manifest you're preventing Android from doing default reset of view hierarchy on screen orientation change. You have to manually change your views in onConfigurationChanged method and by that I mean inflate your desired layout and replace your old layout with it. If you want to take advantage of Android automatic view hierarchy reset don't use android:configChanges="orientation".
Edit:
Use following code to add map fragment through XML:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
You also don't need now 'onConfigurationChange' method so delete it.

Share layout between fragment and activity

I have a main layout that contains a GridView and Buttons.
NOTE BEFORE YOU READ: The reason I am using fragments is because I am displaying lots of images in a GridView at the same time, so I've set up Fragment classes to handle them efficiently without it effecting performance.
I would like the fragments that handle the GridViews behavior to be separate from the activity that handles the buttons behavior(my main fragment and activity both share the same layout).
When I try to do this, the app load up, loads all the images and the button is present.
When I click the button(I've set up a TAG to it) no TAG message shows in console that I have pressed the button.
Then, when I press the back button on my phone the GridView disappears and the button is only present, then once I click it, the TAG message shows the message in the console.
How would I go about fixing this issue, also, what I am doing here, is it a good or a bad idea to do? Thanks in advance.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="#+id/gridView"
style="#style/PhotoGridLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="#dimen/image_thumbnail_size"
android:horizontalSpacing="#dimen/image_thumbnail_spacing"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="#dimen/image_thumbnail_spacing" >
</GridView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
Main Activity:
public class MainActivity extends Activity {
public static String TAG = "Test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_grid_fragment);
startActivity(new Intent(this, ImageGridActivity.class));
Button SearchListButton = (Button) findViewById(R.id.button1);
SearchListButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "clicked");
}
});
}
}
Main Fragment:
public class ImageGridActivity extends FragmentActivity {
private static final String TAG = "ImageGridActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
if (BuildConfig.DEBUG) {
Utils.enableStrictMode();
}
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(android.R.id.content, new ImageGridFragment(), TAG);
ft.commit();
}
}
}
You are starting ImageGridActivity in MainActivity's onCreate. Which means, as soon as the MainACtivity is started, it launches ImageGridActivity.
The SearchListButton is in the MainActivity but ImageGridActivity is on top of the activities stack. This is the reason why the button click is not happening. MainActivity has lost focus and moved to the next position in the activity stack.
When you press back, ImageGridActivity is destroyed and MainActivity is in focus. Now SeachListButton click works.
What is suggest is,
you can start ImageGridActivity on some events like button click. Why do you need two activities? Why not just ImageGrdActivity?

Setting landscape mode only to Android Google Map Fragment

I have a MapFragment class, which will take up the full screen upon calling it.
From this question, I understand that forcing a horizontal layout will require changes in the Android Manifest. However, how can I achieve this if it's for a MapFragment class?
This is my xml for my map display, if it helps:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/fragment_map"
android:name=".com.fragments.MapFragment"
class="com.fragments.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tag_fragment_map" />
</FrameLayout>
You can extend the MapFragment class. When onAttach() is invoked store the current orientation of an activity ( by calling getActivity().getRequestedOrientation() ) to an integer then set the parent's orientation to landscape. This is temporary. If your MapFragment is no longer used it will set the parent's old orientation.
public class DummyMapFragment extends MapFragment
{
private int activityOrientation;
#Override
public void onAttach(Activity arg0) {
super.onAttach(arg0);
activityOrientation = arg0.getRequestedOrientation();
arg0.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
#Override
public void onStop() {
Activity parent = getActivity();
if(parent != null)
getActivity().setRequestedOrientation(activityOrientation);
super.onStop();
}
}
Try this:
getActivity().setRequestedScreenOrientation(SCREEN_ORIENTATION_LANDSCAPE)
Also check this link.
define activity in manifest file like this
<activity
android:name=".yourMapActivity"
android:label="#string/title_activity_edit_profile"
android:screenOrientation="landscape" >

Categories

Resources