How do you make one apk be a launcher app on tablets but just a normal app on phones? How do you configure the manifest file for this?
By launcher app I mean it will show up as one of the option when the apk is run on tablets:
If the same apk is run on phones, it will not show the "Select a home app" picker.
In simple terms, this is the behaviour the apk needs to have:
Goal:
- When I click home on phones, I should not see the app as one of the launcher choices.
- When I click home on tablet then I should see the app as one of the choices.
My approach is I have a ChooserActivity.java that determines what kind of device is being run. If it is a tablet it will start TabletMainActivity.java, if it is phone it will start PhoneMainActivity.java. (The code for finding out if it is a tablet or a phone is okay.)
ChooserActivity.java
public class ChooserActivity extends AppCompatActivity {
ImageView tabIndicator;
private static final String TAG = "Chooser";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chooser);
Log.d(TAG, "onCreate2() Chooser");
finish();
if (findViewById(R.id.tabIndicator)==null){
//PHONE
startActivity(new Intent(this, PhoneMainActivity.class));
}else{
//TABLET
startActivity(new Intent(this, TabletMainActivity.class));
}
}
}
res/layout/activity_chooser.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sample.tabletchecker.Chooser">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="PHONE"
/>
</RelativeLayout>
res/layout-sw600dp/activity_chooser.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sample.tabletchecker.Chooser">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TABLET"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/tabIndicator"
/>
</RelativeLayout>
TabletMainActivity.java has the following launcher manifest settings:
<activity android:name=".TabletMainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- launcher setting -->
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
PhoneMainActivity.java is just a normal activity, has nothing on the manifest. This is on purpose because I want this activity to be a normal activity on phones.
<activity android:name=".PhoneActivity">
</activity>
My ChooserActivity.java is the first activity that gets started when the app gets clicked/opened, it then chooses between the 2. ChooserActivity manifest entry
<activity android:name=".ChooserActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I tried different variations for the intent-filter for the TabletMainActivity.java but never able to get the correct behaviour. I always see the launcher picker being shown.