Python Tutorial

Saturday, January 15, 2011

Android multiple screen example

Let we want constract an application which have two different screen and we want to move one screen to another scrren.



At first we need to constract this two screen individually, both screen should extends Activity.

After build two screen you add your new screen to AndroidManifest.xml file (first screen which extends Activity, it's automatically added to AndroidManifest.xml, but all new screen should be added individually).



//main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="You are in the first Screen" /> <Button android:id ="@+id/btnClick" android:layout_width="150px" android:layout_height="50px" android:text="Open New Screen" /> </LinearLayout>

//AndroidMultipleScreenEasy.java

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;

public class AndroidMultipleScreenEasy extends Activity
{
 MyNewScreen obB=new MyNewScreen();
   public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.main);
      
      Button b = (Button) findViewById(R.id.btnClick);
      b.setOnClickListener(new View.OnClickListener() {
      
         public void onClick(View arg0) {
         // here i call new screen;
         Intent i = new Intent(AndroidMultipleScreenEasy.this, MyNewScreen.class);
         startActivity(i);
         } 
      });
   }
}

//my_new_screen.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="You are in the New Screen, press close for back to previous screen" /> <Button android:id="@+id/btnClick2" android:layout_width="100px" android:layout_height="50px" android:text="Close" /> </LinearLayout>

//MyNewScreen.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyNewScreen extends Activity
{
 AndroidMultipleScreenEasy ob;
   public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.my_new_screen);
      Button b = (Button) findViewById(R.id.btnClick2);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
   setResult(RESULT_OK);
         finish();
         } 
      });
   }
   
   public void setOb( AndroidMultipleScreenEasy obA){
    this.ob=obA;
   }
}



Add this line to your AndroidManifest.xml file.



//AndroidManifest.xml
<activity android:name=".MyNewScreen" android:label="MyNewScreenLabel"> </activity>


output:



Screen 1


Now you can goto new screen by clicking Open New Screen button.


Image not found


Screen 2


Now you can goto previous screen by clicking Close button.


Image not found

21 comments:

  1. In close button click event is there any need of calling the setob() method.I think calling finish() is enough to go back.

    Anyway thankx 4 this example.

    ReplyDelete
    Replies
    1. i am also suspicious about setOb(). It seems that you never call it.

      Delete
    2. No need for MyNewScreen obB=new MyNewScreen(); as well as far as I can see.

      Delete
  2. Hi there, if anyone could help me out please here. I am facing a problem and can not find the solution for this … My code is the following and i cant seem to find the errors but its just when the splash screen is exited towards the main page, just top button works, others don’t … but when tapped that button and return from there then all the buttons work. I want them all to work

    package net.xuting;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class xutingMenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // button 1
    Button btn_live_tv = (Button) findViewById(R.id.btnLiveTv);
    btn_live_tv.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    startActivity(new Intent(“net.xuting.LIVETV”));

    //button2

    Button btn_Favorites = (Button) findViewById(R.id.btnFavorites);
    btn_Favorites.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    startActivity(new Intent(“net.xuting.FAVORITES”));

    }
    });
    //button 3
    Button btn_Featured = (Button) findViewById(R.id.btnFeatured);
    btn_Featured.setOnClickListener (new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    startActivity(new Intent(“net.xuting.FEATURED”));

    }
    });
    //button 4
    Button btn_Video_On_Demand = (Button) findViewById(R.id.btnVideoOnDemand);
    btn_Video_On_Demand.setOnClickListener (new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    startActivity(new Intent(“net.xuting.VIDEO_ON_DEMAND”));
    }
    });

    //button 5
    Button btn_Search = (Button) findViewById(R.id.btnSearch);
    btn_Search.setOnClickListener (new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    startActivity(new Intent(“net.xuting.SEARCH”));
    }
    });

    //button 6
    Button btn_Settings = (Button) findViewById(R.id.btnSettings);
    btn_Settings.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    startActivity(new Intent(“net.xuting.SETTINGS”));
    }
    });
    }

    });
    }

    }

    ReplyDelete
  3. hi there can you show how to put splacescreen before screen1...
    thnxs

    ReplyDelete
    Replies
    1. You can use a splash screen ...
      in MainActivity.java - -use this thread

      package com.example.planet;

      import android.os.Bundle;
      import android.app.Activity;
      import android.content.Intent;
      //
      public class MainActivity extends Activity {

      @Override
      //on create is the main method of android
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.splash);// splash means the layout name

      Thread log = new Thread(){
      public void run(){

      try {
      int log=0;
      while(log<5000){
      sleep(100);
      log=log+100;

      }
      startActivity(new Intent("com.example.planet.CLEARSCREEN")) ; // act means your project name
      } catch (Exception e) {
      e.printStackTrace();
      }
      finally{
      finish();
      }
      }
      };
      log.start();
      }

      @Override
      //sub methods
      protected void onDestroy() {
      // TODO Auto-generated method stub
      super.onDestroy();//super means the parent class it is used because its protected
      }

      @Override
      protected void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      }

      @Override
      protected void onRestart() {
      // TODO Auto-generated method stub
      super.onRestart();
      }

      @Override
      protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      }

      @Override
      protected void onStart() {
      // TODO Auto-generated method stub
      super.onStart();
      }

      @Override
      protected void onStop() {
      // TODO Auto-generated method stub
      super.onStop();
      }

      }

      Delete
  4. thanks dude...ur tutorila was really helpful

    ReplyDelete
  5. What do i add to NewScreen to then go to another page?

    ReplyDelete
  6. I attempted to do so, and got a java.io.IOException: Broken pipe exception when I attempted to switch screen... any ideas on how to go about it?

    ReplyDelete
  7. In your case how to show my_new_screen.xml file inside the main.xml? Not in new window.I am working on this case.

    ReplyDelete
  8. Thanks, very helpful tutorial

    ReplyDelete
  9. Thank you,
    Thank you,
    and Thank you
    so much

    ReplyDelete
  10. Please do i have to create a new project for a new screen or all can be made in one project ? please help

    ReplyDelete
    Replies
    1. all in one project

      Delete
  11. This comment has been removed by the author.

    ReplyDelete
  12. I forgot to add the second activity to the manifest, but once I did it worked perfectly. Thank you for this. I have been bashing my head against the wall trying to figure out how to make a button go to another page.

    ReplyDelete
  13. how to code suport screen 3", 5", 7", 10"

    ReplyDelete
  14. i want to access multiple pages from a single page using button can any one help me.........

    ReplyDelete