In this post I will show you the way I found to have multiple screens in your Android Application. To do this I used Eclipse as my IDE. And I used Android API 15 as my development environment.
When you create a new Android project in Eclipse, you can find that it will auto generate a main.xml file and the particular .java file for handling that. Lets see a screen shot of that.
When you insert some code and run it, it will show a single screen like this,
So this way you can develop your single screen android applications. Until you need single screen to develop a application it will be not a problem. But when you want to have several screens in your app and you need to navigate though them. You are in trouble. So I found out a way to do this. So I thought to share it with you guys.
To have multiple screen, first of all you have to create XML files for those screens. So in your projects res/layout folder create a new android XML file. then it will generate a new screen. Here is the way I created, Lets see a screen shot :
So how we use this xml files java files to achieve our task of switch between screens? As you know each and every screen in Android is a activity. So first of all we have to introduce our new screen as a activity to the Android app. To do that you have to edit the AndroidManifest.xml.
To add a new activity, you have to add this element as child element of application,
<?xml version="1.0" encoding="utf-8"?>
<activity
android:name=".You Java Files Name"
android:label="Any name you want show as the title of the activity window." >
</activity>
Here is my Example,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="travelceylon.client"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="Travel Ceylon" >
<activity
android:name=".Travel_CeylonActivity"
android:label="Travel Ceylon" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SelectionActivity"
android:label="Travel Ceylon Selection" >
</activity>
<activity
android:name=".Important_place_addActivity"
android:label="Add Importatn place to Travel Ceylon" >
</activity>
</application>
</manifest>
So after adding this you can see the R.java file in you application is generated. With your new elements in your new screens. So next thing is to add some code to those java files to navigate through screens. Here is the code you should add to your onCreate() method. It will show the new screen when you press a button.
Button b = (Button) findViewById(R.id.button1); // Here the R.id.button1 is the button form you design
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Current Java Files Name.this, The name of the next java files which will be executed on the button click.class);
startActivity(i);
}
});
Here is my example. It will guide user from Travel_CeylonActivity to SelectionActivity when user push the button Enter Travel Ceylon.
package travelceylon.client;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Travel_CeylonActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Travel_CeylonActivity.this, SelectionActivity.class);
startActivity(i);
}
});
}
}
After doing this I have edited the SelectionActivity.java like this, It also have two buttons and I have code it for go to another screen when I push a button.
package travelceylon.client;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SelectionActivity extends Activity {
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.selection_screen);
Button b = (Button) findViewById(R.id.button3);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(SelectionActivity.this, Important_place_addActivity.class);
startActivity(i);
}
});
}
}
So after that you can run your application. You can see that when you press the button it will take you from screen 1 to screen 2. So like wise customize this approach to your app. I thought this will help you. Please leave your comments.
Nice tutorial machan...keep it up.
ReplyDeletegreat job, very helpful :)
ReplyDeleteOK, your description seems clearer than other 2 I found. Then he proved, it is now time to sleep (2 am). Thanks (from Argentina) per share.
ReplyDeleteEdited my Androidmanifest.xml file according to your example but there is no new R.java file created..able to help?
ReplyDeleteSometimes Eclipse+Android acts strang.If you sure your Manifest File is syntactically correct. Please restart or reload your project. That will work. Or manually do clean+build.
Deletehow to create .java file?
ReplyDeleteI don't understand the question? .java file means?
Delete@dhwani kapadia make right click on the package of your app and select new--> click on class and name your .java file as you want
ReplyDelete.this is amazing tutorial, man!
ReplyDeletekeep it up...I will wait for other tutorials by you.
Special Request: please can you CONVERT THE BUTTON action into swiping action to switch view from one screen to another?
I will be waiting for your response and update...
please email me the code as well, my email address is: brojins@gmail.com
thank you so much.
I do think that this tutorial is useful. Does anyone know how to start the new activity screen on other devices/tablets?
ReplyDeletethanks dude. Amazingly difficult to find help on this topic. It seems like google wants you to use the same screen and just have the views disappear or something.
ReplyDeletehi there, thank you very much. it really helped me, but i have a question, what if i want to go back to my first screen what code must i add?
ReplyDeletehow to design with screen 4', 7', 10"
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI used it in a login page but the problem I have to click it twice to make it work. Any idea??? here is my code:
ReplyDelete// MainActivity.java
// Hosts the QuizFragment on a phone and both the
// QuizFragment and SettingsFragment on a tablet
package com.deitel.flagquiz;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
//private boolean phoneDevice = true; // used to force portrait mode
// private boolean preferencesChanged = true; // did preferences change?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view)
{
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin"))
{ // start if
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
// to make it go to the question page
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener
(
new View.OnClickListener()
{
public void onClick(View arg0)
{
Intent i= new Intent(MainActivity.this, Quiz.class);
startActivity(i);
}
}
);
} //end if
else if(username.getText().toString().equals("user") &&
password.getText().toString().equals("user"))
{ // start if
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
// to make it go to the question page
Button c = (Button) findViewById(R.id.button1);
c.setOnClickListener
(
new View.OnClickListener()
{
public void onClick(View arg0)
{
Intent i = new Intent(MainActivity.this, Quiz.class);
startActivity(i);
}
}
);
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This comment has been removed by the author.
ReplyDeleteI am try to create multiple page in elicpse tools but I don't know how to create application in multiple page.can you please anyone help for create multi page. i need source code for create multi page
ReplyDelete