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)?
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>
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>
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.
<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>
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.
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.
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>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
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.
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.
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)
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:
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"/>
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>
A screenshot of my activity preview
I'm new to android studio and have been following various tutorials online. Cant find the answer to this problem. if i change the app_name in the strings.xml, the name on the picture posted above also changes and if i generate the apk it also acts as the app name. I tried to add another string on the strings.xml but nothing changes. Is there a way to edit the title above without changing the final app name
strings.xml code
<resources>
<string name="app_name">Hello world</string>
That's because the same String is being used in both the layout where your image shows, and in your Manifest file.
You might recognize this code inside your AndroidManifest.xml file:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Note the part that says android:label="#string/app_name". That's the String that your app will show as the app name.
So if you want to use two different names, one for the app name and one for inside your layout, simply create two different Strings and assign one to the AndroidManifest.xml and the other to whichever layout you're image shows.
Use the app_name in the intent-filter of the launcher activity. and use a different string for android:label of your activity. Manifest would look like something this -
<activity
android:name=".MainActivity"
android:label="#string/title_main_activity"
android:icon="#drawable/icon">
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And create <string name="title_main_activity">Hello Main Activity</string>
I have android:label="Name app" in <application
and android:label="Activity name" in <activity
(all in androidmanifest.xml)
and if i install this app on real device, this app have name "Activity name"
why ? How can i change app name ?
edit1
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher2"
android:label="Dog"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".FirstActivity"
android:label="Cat"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".secondActivity">
</activity>
</application>
and app still have name Cat in my phone
in phone menu, under icon this app
That is not the label of the application. That is the label of the specific activity that has the MAIN/LAUNCHER <intent-filter> in your manifest. If you want to change this label, change the android:label on that <activity> element.
The android:label on <application> serves as the default label (for <activity> elements that lack one) and is used in places like the Settings app.
Im new to android, and when i read "Create New Activity" i was under the impression it creates a new "Window/Layout/Screen".
When i start making my own application i see
Main.java
is one application and
SecondActivity.java
is another app in my Emulators menu?
Why is that and how can i go about making one app with multiple windows as apposed to multiple applications essentially?
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ryan.bidorbuyapp" >
<application
android:allowBackup="true"
android:icon="#mipmap/bidorbuy_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SearchResults"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_search_results"
android:parentActivityName=".FullscreenActivity"
android:theme="#style/FullscreenTheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.ryan.bidorbuyapp.FullscreenActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
thanks
Look at your AndroidManifest.xml there is the problem. Your activities have Launcher between it's tags. Only one activity must have LAUNCHER tag
Only one activity must have
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You should understand what is an Activity before starting to developing an android application. You can think an activity as a page in web. There can be one or more and every activity includes their own contents.
When you create a new activity in your project, you should navigate by doing some operations in another activity. Intent helps you at this point to navigate one activity to the another.
Your problem is that, you should check your AndroidManifest.xml file. Please check the declaration of intent here. I think you will solve it after making a search about Intent.
So my original problem was that in my manifest my menu wasent loading ie
<activity
android:name=".MainMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.MAINMENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This was fixed by loading it at startup ie
<activity
android:name=".MainMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
So now my main menu works. However, the buttons inside MainMenu.xml will take you to another .xml file with more buttons. So now I have the same problem. I created another class called SubMenuChapter3 and put it in the manifest as such.
<activity
android:name=".SubMenuChapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.SUBMENUCHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Now this doesnt work I am assuming for the same reason as before with mainmenu. Doesnt crash or give me errors. It just wont open the submenuchapter3 class. I forced the submenuchapter3 class to open by putting
startActivity(new Intent("com.th3ramr0d.learnar670_1.SUBMENUCHAPTER3"));
directly into the MainMenu class outside of an onclick just to see if it was working. When I do that it opens the chapter_3.xml like it is supposed to and the button works. Thanks for the help.
You misunderstood <intent-filter> tag and the way you start activities.
Also maintain proper terminology - Menu and Activity are completely different things.
Everything you need to know about Activities can be found here: Activities | Android Developers
Example:
This entry in AndroidManifest.xml says "show the MainMenu Activity as icon in the launcher":
<activity
android:name=".MainMenu"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
EDIT: This allows Android to start MainActivity. It will also show in the installed app list.
The following Activity will not be displayed in launcher but can be opened from the app:
<activity android:name=".SubMenuChapter3"/>
EDIT: This allows Android to start SubMenuChapter3. It won't show in the installed app list.
These lines say "open the SubMenuChapter3 Activity":
Intent i = new Intent(this, SubMenuChapter3.class);
startActivity(i);
EDIT: You call this code from inside the onClick method inside MainMenu. It will launch SubMenuChapter3.