441

I have created the meat and guts of my application but I want to add a different activity that will be the starting point (sort of a log-in screen).

Couple questions:

  • 1 I have a fairly decent handle on how to switch between activities (based on this article: http://www.linux-mag.com/id/7498) but I'm not sure how to go about creating a new one (with eclipse).

  • 2 Once I have a new activity created, how can I set it as the default activity of my application? I presume I could just change the name of the classes...but is there a more elegant way to handle that (maybe within the AndroidManifest.xml)?

Brian
  • 14,610
  • 7
  • 35
  • 43
Kyle
  • 10,839
  • 17
  • 53
  • 63

13 Answers13

748

Yes, you use the AndroidManifest.xml file. You can actually even have more than one launcher activity specified in your application manifest. To make an activity seen on the launcher you add these attributes to your activity in the manifest:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
BSMP
  • 4,596
  • 8
  • 33
  • 44
Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • 57
    For anyone that doesn't know what the manifest this post is talking about, it is the AndroidManifest.xml in the root of your project. In Eclipse you can double click it and click the tab on the furthest right to edit the XML directly. – Austyn Mahoney Sep 03 '10 at 08:29
  • 3
    great thanks. So by including that intent-filter node with it's child nodes, that makes whatever activity it's in the first one to load when the application starts? – Kyle Sep 03 '10 at 14:55
  • 5
    @Kyle it will make that activity appear on the launcher page, you can have multiple activities with this intent-filter giving you multiple icons on the homescreen. – Nathan Schwermann May 18 '11 at 18:00
  • 3
    you mention this line "ou can actually even have more than one launcher activity specified in your application manifest".Which activity will first launch if we have more than one activity – Tofeeq Ahmad Nov 29 '11 at 05:06
  • 51
    This is correct answer, but I just noticed something odd: if you change the starting Activity to anything else, and the original launched activity still exists, the SDK won't be able to launch your application anymore. Somehow it has a reference to the activity on the launch configuration. So after you do the above change, you also need to go to "Run" > "Debug Configurations", find your project's build profile, tab "Android", and change the launched activity to either the default or the correct one under "Launch:". Not sure why the default was deselected for me, but changing it back worked. – zeh Feb 01 '12 at 01:32
  • 1
    zeh, your a star!! I've wondered what was going on with an app I had with that exact same issue, for 2 years :-) – andy_spoo Mar 24 '12 at 12:24
  • It is really odd to get in Eclipse and find out that you can't use 'Properties' context menu of a project. We just cannot easily set which will be the main activity. I am wandering if Android Studio allow us to set the main activity. – Junior Mayhé Mar 07 '14 at 14:47
  • is there any shortcut in android studio to make any activity to main activity, like "right click(on any activity file)" -> "make main activity" etc ? – MarsPeople Oct 18 '15 at 12:07
  • Simple solution. Solve fast – Faris Rayhan Apr 10 '16 at 13:12
  • what if I don't have a LAUNCHER category. My application is a first time installation application. And I don't know how can I run it on my phone. – elifekiz Oct 03 '16 at 14:20
  • In AndroidStudio it's necessary to manually change the starting activity since it ignores the manifest.xml file. In "Run" menu you'll find "Edit configurations..." option. – Stig Helmer Feb 13 '20 at 16:20
118

Go to AndroidManifest.xml in the root folder of your project and change the Activity name which you want to execute first.

Example:

<activity android:name=".put your started activity name here"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
neeraj t
  • 4,654
  • 2
  • 27
  • 30
  • 14
    if with this still doesn't work in Android Studio you have to click on the android icon(the one with the word app) and select "Edit configurations", then in the General tab, Activity section select "Launch default Activity" – Ed_ Aug 25 '14 at 17:23
58

If you are using Android Studio and you might have previously selected another Activity to launch.

Click on Run > Edit configuration and then make sure that Launch default Activity is selected.

Launch default Activity

Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37
Zeezer
  • 1,503
  • 2
  • 18
  • 33
  • if in AndroidManifest.xml there's no intent filter for launcher, this is the solution. – Amir Fazwan Nov 06 '15 at 14:45
  • 1
    This is applicable for Eclipse too. Just specifying the intent filters is not enough. The intent filters are used if the APK is executed directly by the user, but if the APK is executed by Eclipse, that installs it, tries to launch a specific activity. This specific activity is specified in "Project properties" (right click on the project) -> Run/Debug Settings. – Iker Jamardo Zugaza Jan 11 '16 at 23:12
  • This is amazing! Thanks for the tip – Rafael Oliveira Apr 07 '16 at 19:07
38
 <application
    android:icon="@drawable/YOUR_ICON"    <!-- THIS ICON(IMAGE) WILL BE SHOWN IN YOUR APPS -->
    android:label="MY APP NAME " >    <!-- HERE LABEL(APP NAME) -->
    <activity
        android:name=".application's starting activity"  <!-- (.)dot means current dir, if your activity is in another package then give full package name ex: com.xxx.Activity  -->
        android:label="LABEL FOR ACTIVITY "
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
LMK
  • 2,882
  • 5
  • 28
  • 52
23

Follow to below instructions:

1:) Open your AndroidManifest.xml file.

2:) Go to the activity code which you want to make your main activity like below.

such as i want to make SplashScreen as main activity

<activity
    android:name=".SplashScreen"
    android:screenOrientation="sensorPortrait"
    android:label="City Retails">
</activity>

3:) Now copy the below code in between activity tags same as:

<activity
    android:name=".SplashScreen"
    android:screenOrientation="sensorPortrait"
    android:label="City Retails">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

and also check that newly added lines are not attached with other activity tags.

Torbilicious
  • 467
  • 1
  • 4
  • 17
Bhunnu Baba
  • 1,742
  • 15
  • 22
18

This is easy to fix.

  • Changes to the Launcher activity are also stored in the Debug configuration.
  • Go to Run > Debug Configurations and edit the setting.
  • There is also a similar setting in Intellij under Run > Edit Configurations select Run default Activity and it will no longer save the setting in this fashion.
Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37
user3154790
  • 557
  • 5
  • 6
14

It's simple. Do this, in your Manifest file.

<activity
    android:name="Your app name"
    android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>
Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37
  • 1
    you should remove – Behrouz.M Oct 18 '14 at 04:34
  • 2
    ``, that is the very thing that do work here – 1111161171159459134 Aug 19 '15 at 09:07
  • 1
    `android.intent.category.HOME` is for use in a Kiosk-mode style application, where the default launcher is replaced. Using this without replacing the Launcher will result in a prompt during the start of SystemUI asking which launcher to use. Also see: http://stackoverflow.com/questions/22911156/android-open-source-setting-the-default-launcher – CJBS Oct 14 '15 at 16:31
7
 <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
5

Just go to your AndroidManifest.xml file and add like below

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

then save and run your android project.

Torbilicious
  • 467
  • 1
  • 4
  • 17
user3206168
  • 61
  • 1
  • 2
5

You add this you want to launch activity android:exported="true" in manifest file like

 <activity
      android:name=".activities.activity.MainActivity"
      android:windowSoftInputMode="adjustPan"
      android:exported="true"/>
  <activity

Open java file of this activity and right click then click on Run 'main Activity'

OR

Open java file of this activity and press Ctrl+Shift+F10.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Prabh deep
  • 1,024
  • 11
  • 15
3

In a recent project I changed the default activity in AndroidManifest.xml with:

<activity android:name=".MyAppRuntimePermissions">
</activity>

<activity android:name=".MyAppDisplay">
    <intent-filter>
        <action android:name="android.intent.activity.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

In Android Studio 3.6; this seems to broken. I've used this technique in example applications, but when I use it in this real-world application it falls flat. The IDE once again reports:

Error running app: Default activity not found.

The IDE still showed a configuration error in the "run app" space in the toolbar (yellow arrow in this screenshot)

Error in "run app" configuration

To correct this error I've tried several rebuilds of the project, and finally File >> "Invalidate Cache/Restart". This did not help. To run the application I had to "Edit Configurations" and point at the specific activity instead of the default activity:

Edit configuration dialog box

martshal
  • 43
  • 4
  • Hide that path... It is really sensitive information there... Someone could copy your activity path and sell it on play store – PhysiOS Mar 24 '21 at 05:12
  • @PhysiOS "sell your activity path on playStore" if I may ask, what do you mean by that Please. I have never heard of that... ever. Could you elaborate more please?? – Daniel Iroka Oct 21 '22 at 17:35
3

In AndroidManifest.xml

I changed here the first activity to be MainActivity4 instead of MainActivity:

Before:

    <activity android:name=".MainActivity" >
       <intent-filter>
           <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity2" />
    <activity android:name=".MainActivity3" />
    <activity android:name=".MainActivity4" />
    <activity android:name=".MainActivity5" />
    <activity android:name=".MainActivity6"/>

After:

    <activity android:name=".MainActivity" />
    <activity android:name=".MainActivity2" />
    <activity android:name=".MainActivity3" />
    <activity android:name=".MainActivity4" >
       <intent-filter>
           <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity5" />
    <activity android:name=".MainActivity6"/>
Shady
  • 95
  • 7
2

For anyone getting errors in their debug\AndroidManifest.xml file make sure that you include the tag! "<activity android:name=".MainActivity>"

For example:

<activity android:name=".MainActivity"><intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
</intent-filter></activity>
Alexis
  • 816
  • 2
  • 11
  • 28
Sammi Lin
  • 69
  • 4