Android Development start - android

when creating a new android application project..a by default activity should be created in my "src" folder ..
but when i created new project there is no any default activity... i have chose "blank activity " too.. what can i do for this ??
and its compulsory that a by default activity must be created according to this official site: http://developer.android.com/training/basics/firstapp/building-ui.html
please help

Create a new class that extends Activity.
In your manifest file add
<activity
android:name=".activities.MyActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Make sure the android:name field is correct for your application. For me MyActivity is the name of my activity and activities is a subpackage.
From here on you can start like normally.
If you still have problems, you can just look for an example and copy the file.

Related

How to run a certain activity in Android Studio?

For instance, I have a few activities within one app, and in order to see a certain activity's UI or whatever, I need to run a certain activity that is not the launcher of the app.
One stupid way is to build a "door" for that activity in the launcher and go inside the activity from the door. However, Is there any better way to run a certain activity alone?
Very easy. Start by exporting the activity you need to run:
Add android:exported="true" in the Activity declaration in the Manifest. This is because am is an external application, and you need to export Activities to allow external application to start them.
Go to "Edit Configurations..." in the "Run" menu.
In the left pane, select your application. In the right pane, in the "General" tab, in the "Launch Options" section, there is a "Launch:" dropdown.
Select "Specified Activity", and enter the name of your activity as it appears in your Manifest.
You can create as many Configurations as you like, and name them according however you like, for example to indicate which activity is being started.
I am using Android Studio stable version 2.1.2 and there is one shortcut to do so. Just open the activity class you wish to run and right click on coding area, There is options to run and debug the particular activity as shown in below screen shot.
For windows use shortcut ctrl+shift+F10 and for mac use ctrl+shift+R. I have tested this in emulator and its working fine, didn't test in actual device.Works only for activity class and don't forget to put cursor in coding area by clicking on it. Also I am not aware whether this option available in older Android studio versions less than 2.1.2.
As mentioned in this answer, you can easily achieve that by giving the activity an action name in the manifest.xml of the app:
<activity android:name="Activity3" ... >
<intent-filter>
<action android:name="com.company.package.FOO"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
then create the following intent from anywhere in order to run this activity specifically:
startActivity(new Intent("com.company.package.FOO"));
After your clarification that the activity has to be run firstly when running the app instead of the launcher, you can achieve that by not setting the content of the launcher activity and instead create an intent that runs the wanted activity:
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_layout); // remove this line
Intent intent = new Intent(ThisActivity.this, WantedActivity.class);
intent.putExtra("EXIT", false);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Add exported true Manifest declaration of that activity.
Go to that activity, right click anywhere, go will get certain option with a 'Run XYZ Activity' option too. just run it
<activity android:name=".phoneVideo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
First you need to have two or more activities in your app to start with. Let's say you want to go to a certain activity in your app to display first. May be for testing purposes or any other. Let's see how it can be done,
First you need to find the AndroidManifest.xml file. Its there under the manifests folder.
According to this first dispaly activity is MainActivity
Lets's say I want to make the home activity display first. So what I have to do is simply cut the intent-filter.../intent-filter and paste it within home activity. Like this
First Displaying Acivity is MainActivity according to this,
<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=".home">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When we want to make the home acivity display first simplay change it as this,
<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=".home">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
This should work. Hope this will help
For my example, the specific activity was called Activity2 and the project was called ScreenSizes
1- Open the Android Manifest: app>manifests>AndroidManifest.xml
2- Change the activity section for the specific activity to include android:exported="true" like this:
<activity android:name=".Activity2"
android:exported="true">
</activity>
3- Open the java class of the specific activity: app>java>com.example.(your app name)>(specific activity)
in my case it was: app>java>com.example.screensizes>Activity2
4- Right click anywhere in the blank/white area of the Java file and select the option Run '(activity name)'
in my case it was: Run 'Activity2'

How to registeractivity in manifest.xml

I am green hand in android studio.How can I register activities in Manifest.xml,why always get wrong in the name??
Register activity in manifest like this -
<activity
android:name="com.yourpackage.name.Activity_Register"
/>
Here - com.yourpackage.name is a package where your Activity file (class) Actvity_Register is place. Put this always inside Application tag of manifest.xml
Actually even though it displays in red.when you run it can run perfectly.
If you want to rectify it just add your package name before the activity name.Like
com.example.android.broadcastbestpractice.LoginActivity
Hope it helps!
For you:
< activity android:name="[your package of Activity_Register class].Activity_Register"></ activity>
First Clean your project or restart Android Studio then give the path to main Acitvity using alt+space
For launcher activity you can register your activity like this:
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
For simple other activity you can register your activity:
<activity
android:name=".Testactivity"
android:label="#string/title_activity_test" >
</activity>

Android Eclispe : 2 .java files in editor, how to set1 file as startup

In nutshell, i am trying to add multiple examples, by adding multiple .java files, in editor but all the time, mainactivity.java file is being executed. How could i make other file as start up file.
If your other file is also an Activity, which it must, you will have to change your AndroidManifest.xml. Add a new entry for your new Activity, and add the Main and Launcher Intent filter:
<activity
android:name="com.example.activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Also make sure to either remove the Intent Filter from the Manifest entry of the old Activity.
Yep you are gonna 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>
Change this*.Launcher* to Default for your MainActivity and Add .LAuncher in that activity you want to Launch First

How do I load an Activity from within a Library?

I've got a projected marked as "Is Library" - it has all the activities. I've got a new Android project that references this library. How do I mark one of my library's activities as the first one that starts up?
EDIT
My original manifest looks like this:
<activity android:name=".Welcome" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
All my activities are in the package com.rjs.animator.
As long as you AndroidManifest.xml contain those Activity tag and reference to the correct namespace/class within your Library, you should be good to go.
You have to add all activities and other components to the new Android project's manifest, with whatever <intent-filter> elements you want. Just put the standard MAIN/LAUNCHER <intent-filter> on whichever activity you want.

Android Eclipse Add New Activity

I am using Win 7.0, Eclipse and android SDK. I want to add new activity in AndroidManifest.xml Application tab as it is shown in this tutorial Android Development – Adding Screens & Button Handlers
I add an Activity name to my manifest but it does not automatically turn it into a link. e.g. I cannot click the "Name"(It is not a hyperlink as shown in the article), thus I cannot create my class.
Can You Help me? what is the problem ?
1.Go to the Androidmanifest.xml file and add the activity inside the tag
if your activity name is secondAct.
2.Create a class named secondAct.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Project1Activity"
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=".secondAct"></activity>
<activity android:name=".third"></activity>
</application>
3 . if you are using a button for going to next activity, use the following code in secondAct.java
Button fbtn=(Button)findViewById(R.id.sbtn);
fbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sec=new Intent(secondAct.this,com.asish.third.class);
startActivity(sec);
}
});
Go to the small tab underneath that says AndroidManifest.xml and shows you the XML code for it. It should look like this:
<application android:label="#string/app_name" android:icon="#drawable/icon">
<activity android:name=".ApplicationName"
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=".AnotherActivity"></activity>
</application>
Okay, click on ADD, then select the top box that says "Create a new element at the top level, in Application" and then you should get a box with linkable NAME*.
You need to create the class first, then point to that class in your manifest... just putting the class name in the manifest is not enough. It will not automatically create it for you.
Also, it is easier to create the class first because then Eclipse will autocomplete the class name/path for you.
EDIT: AH HAH! I see what link you are talking about...
Yeah, you need to actually create the class first for that to appear.

Categories

Resources