1

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: Select home picker

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.

frey
  • 431
  • 1
  • 5
  • 21
  • you mean, After start the `ChooserActivity` you call either `TabletMainActivity` or `MobileMainActivity` ? – Zahidul Islam Jul 05 '16 at 05:28
  • Yes, **ChooserActivtity.java** determines what activity to start then finishes itself. If it is a tablet device ChooserActivity opens TabletMainActivity else if phone it starts **PhoneMainActivity**. – frey Jul 05 '16 at 05:33

1 Answers1

0

See this Answer Here

Determine if the device is a smartphone or tablet?

http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali

If you read the entire topic, they explain how to set a Boolean value in a specific value file (as res/values-sw600dp/):

<resources>
    <bool name="isTablet">true</bool>
</resources>

Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:

<resources>
    <bool name="isTablet">true</bool>
</resources>

Then, in the "standard" value file (as res/values/), you set the boolean to false:

<resources>
    <bool name="isTablet">false</bool>
</resources>

Then in you activity, you can get this value and check if you are running in a tablet size device:

check on You choose Activity when Intent

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {


Intent intent = new Intent(ChooserActivity.this, TabletMainActivity.class);


    startActivity(intent);

} else {
        Intent intent = new Intent(ChooserActivity.this, PhoneMainActivity.class);


    startActivity(intent);


}

The Upper Method is working proper for Me

I am Also put an Other method for you...

Import

     import android.content.Context;
     import android.content.res.Configuration;
     import android.util.DisplayMetrics;

Function

    public static int FunctionCall(Context context) {

    if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {

             Intent intent=new Intent(this,PhoneMainActivity.class);

            startActivity(intent);
    } 

    else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
        Intent intent=new Intent(this,PhoneMainActivity.class);

                startActivity(intent);

    } 
    else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {

       Intent intent=new Intent(this,TabletMainActivity.class);

                startActivity(intent);


    } else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
       Intent intent=new Intent(this,TabletMainActivity.class);

                startActivity(intent);

    }


    return value;
    }

Or Do according to you

Community
  • 1
  • 1
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
  • The checking of the device whether it is tablet or phone is okay. The problem is the manifest setting for the `TabletMainActivity.java` for launcher and the `PhoneMainActivity.java`. – frey Jul 05 '16 at 05:53
  • Thanks but that is not the answer to the question/problem. – frey Jul 08 '16 at 03:00
  • I think Er. Arjun has the right answer, but he forgot of specify that you need another activity, where the code mentioned include, for example StartActivity ... In it you put the code and then launch your SmartphoneActivity or TabletActivity – Christian Cruz Jul 11 '16 at 18:42
  • Hi Christian if you are talking about the Activity that checks and launches PhoneMainActivity or TabletMainActivity if the device either a phone or a tablet the code is already there and working though my approach is a bit different to Er. Arjun. I updated the description to show you how I did it. – frey Jul 12 '16 at 03:21
  • What the problem now..... This Is working For me In Each Device...... You need to set permission also... AccessNetworkstate – Arjun saini Jul 12 '16 at 04:21
  • The AccessNetworkState is not needed for me, I have no operation in my code that needs access to network. This is the behaviour I want to get: When I click _home_ on phones, I **should not see** the app as one of the choices. When I click _home_ on tablet then I **should see** the app as one of the choices. – frey Jul 12 '16 at 05:45
  • this permission used for change state from one activity to other hilfritz in your code you used to change states from one activity to other – Arjun saini Jul 12 '16 at 05:50
  • I tried adding the permission, I still see the app as one of the choices in "Select a home app" in both tablet and phone. – frey Jul 12 '16 at 06:00
  • Try to Update answer also/// – Arjun saini Jul 12 '16 at 06:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117042/discussion-between-hilfritz-and-er-arjun-saini). – frey Jul 12 '16 at 06:13