I am trying to create an application that has two different interfaces for tablets and phones, and I use fragments to implement this. I created two separate xml files for each tablet and phone plan, and both of them are called activity_main.xml, with the phone layout placed in the res / layout folder and the tablet layout placed in the res / layout-sw600dp folder.
However, when I try to run my application on the Nexus 10 emulator (Android Studio), it will automatically switch to the default phone layout. The application does not crash or something else, but it just launches in the phoneβs user interface when it should work in the tabletβs user interface. I do not know what the source of this error is, and therefore any help would be appreciated.
In this update, I tried to rename the folder to res / layout-large-mdpi, but did not find any changes. Is it possible that this may have something to do with the emulator?
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidattack.www.sunshine" > <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.androidattack.www.sunshine.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.androidattack.www.sunshine.DetailActivity" android:label="@string/title_activity_detail" android:parentActivityName="com.androidattack.www.sunshine.MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.androidattack.www.sunshine.MainActivity" /> </activity> <activity android:name="com.androidattack.www.sunshine.SettingsActivity" android:label="@string/title_activity_settings" android:parentActivityName="com.androidattack.www.sunshine.MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.androidattack.www.sunshine.MainActivity" /> </activity> <provider android:authorities="com.androidattack.www.sunshine" android:name=".data.WeatherProvider" /> </application> </manifest>
sw600dp layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="?android:attr/dividerHorizontal" android:baselineAligned="false" tools:context="com.androidattack.www.sunshine.MainActivity"> <fragment android:layout_width="0dp" android:layout_height="fill_parent" android:name="com.androidattack.www.sunshine.ForecastFragment" android:id="@+id/fragment_forecast" android:layout_gravity="center_vertical" android:layout_weight="1" /> <FrameLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="2" android:id="@+id/weather_detail_container"> </FrameLayout> </LinearLayout>
normal layout:
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment_forecast" android:name="com.androidattack.www.sunshine.ForecastFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" tools:context="com.androidattack.www.sunshine.ForecastFragment" tools:ignore="MergeRootFrame" tools:layout="@android:layout/list_content" />
And finally, this is my MainActivity.java class:
package com.androidattack.www.sunshine; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity { private final String LOG_TAG = MainActivity.class.getSimpleName(); private boolean mTwoPane; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (findViewById(R.id.weather_detail_container) != null) {
java android android-layout android-fragments
Dhruv mehra
source share