Screen to next screen with a button

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Screen to next screen with a button

Postby xklownx » Tue Mar 17, 2009 11:33 pm

I am still just starting out with this i been searching and searching to find out how to go from my main.xml to a new view xml file. i am using eclipse to make it. any help would be awesome thanks.
xklownx
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Mar 16, 2009 8:02 am

Top

Postby karthik narayan M » Wed Mar 18, 2009 5:57 am

if you want to go from one activity(1st screen) to another 2nd screen you should be using startActivity or an avtivity can have many views you can switch from to another. but i think you can't two different layout files for same activity
mkn
karthik narayan M
Freshman
Freshman
 
Posts: 4
Joined: Thu Jan 22, 2009 5:55 pm
Location: bangalore

Postby xklownx » Wed Mar 18, 2009 6:42 am

karthik narayan M wrote:if you want to go from one activity(1st screen) to another 2nd screen you should be using startActivity or an avtivity can have many views you can switch from to another. but i think you can't two different layout files for same activity


thats what i was trying to do is go from the first screen to the next screen with a button. i am not sure what to add to make the second screen either in the eclipse. thank you for the reply hope this somes it up a little.
xklownx
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Mar 16, 2009 8:02 am

Postby xklownx » Wed Mar 18, 2009 9:20 am

What i am trying to do is get the View01 to go to visible when Button01 is clicked. i am not sure what i need to add to it to make this happen any help would be awesome thanks
Attachments
Code.JPG
Code.JPG (24.51 KiB) Viewed 3533 times
xklownx
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Mar 16, 2009 8:02 am

Postby ashwinthegrim » Tue Apr 14, 2009 2:28 am

you need to define an onclick listener for your button.. i'll give you an idea..

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public void onCreate(Bundle savedInstanceState) {
  4.  
  5.                 super.onCreate(savedInstanceState);
  6.  
  7.                 first();
  8.  
  9. }
  10.  
  11.  
  12.  
  13. void first()
  14.  
  15. {
  16.  
  17.          //SHOW THE FIRST XML VIEW WHICH HAS THE BUTTON
  18.  
  19.         setContentView(R.layout.view1);                          
  20.  
  21.  
  22.  
  23.         //GET THE BUTTON INTO A VARIABLE (Here, my_button must be defined as type 'Button'
  24.  
  25.         my_button = (Button)findViewById(R.id.title_button1);  
  26.  
  27.    
  28.  
  29.         //DEFINE AN ONCLICK-LISTENER FOR THE BUTTON
  30.  
  31.         //(An onclick listener is a function which would be called everytime the button is clicked)    
  32.  
  33.         my_button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){my_func();}});
  34.  
  35. }
  36.  
  37.  
  38.  
  39. void my_func()
  40.  
  41. {
  42.  
  43.         //SHOW THE SECOND XML VIEW
  44.  
  45.         setContentView(R.layout.view2);
  46.  
  47. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I guess this made sense.. If it didn't, feel free to ask..
ashwinthegrim
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 26, 2008 4:26 am

Postby cesar09 » Thu Apr 23, 2009 4:51 pm

So far so good
but i want the class that set the new layout, to be in a new java file (for a better overview)
my problem is that i dont know how to say the main java file to go on in the new java file...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.   .....
  2.  
  3. my_button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){my_func();}});
  4.  
  5. }
  6.  
  7.  
  8.  
  9. void my_func()
  10.  
  11. {
  12.  
  13.         //link to javaFile2.java
  14.  
  15.         ?????
  16.  
  17. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
cesar09
Freshman
Freshman
 
Posts: 8
Joined: Thu Apr 23, 2009 4:34 pm
Location: Germany

Top

Postby iPaul Pro » Thu Apr 23, 2009 9:13 pm

The key you are missing are Intents.

try:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private Button my_button;
  2. my_button.setOnClickListener(buttonListner);
  3. private OnClickListener buttonListner = new OnClickListener() {
  4.         public void onClick(View v) {
  5.                 Intent i = new Intent(MainActivity.this, SecondActivity.class);
  6.                 startActivity(i);
  7.         }
  8. };
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


then in your manifest:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <activity android:name=”.SecondActivity”
  2.      android:label=”@string/app_name”>
  3. <intent-filter>
  4.      <action android:name=”android.intent.action.VIEW” />
  5.      <category android:name=”android.intent.category.DEFAULT” />
  6. </intent-filter>
  7. </activity>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
Last edited by iPaul Pro on Fri Apr 24, 2009 11:34 pm, edited 1 time in total.
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby ashwinthegrim » Fri Apr 24, 2009 8:11 pm

or, you could do it this way too.. just have a new java class in the same package, create an instance of the java class in your main class, and you can do whatever you want to do with the new class..
ashwinthegrim
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 26, 2008 4:26 am

Postby superjet » Fri May 15, 2009 4:10 am

I'm also trying this... and I'm getting the following error (with code below)::

Code: Select all
05-15 03:05:12.976: WARN/dalvikvm(721): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
05-15 03:05:12.976: ERROR/AndroidRuntime(721): Uncaught handler: thread main exiting due to uncaught exception
05-15 03:05:12.996: ERROR/AndroidRuntime(721): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MasterClass/com.example.MasterClass.SecondClass}: java.lang.NullPointerException
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.os.Looper.loop(Looper.java:123)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.app.ActivityThread.main(ActivityThread.java:3948)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at java.lang.reflect.Method.invoke(Method.java:521)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at dalvik.system.NativeStart.main(Native Method)
05-15 03:05:12.996: ERROR/AndroidRuntime(721): Caused by: java.lang.NullPointerException
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at com.example.MasterClass.SecondClass.onCreate(SecondClass.java:14)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
05-15 03:05:12.996: ERROR/AndroidRuntime(721):     ... 11 more


Code: Select all
package com.example.MasterClass;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class MasterClass extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        firstView();
    }
   
    void firstView() {
      setContentView(R.layout.main);     
        final ImageButton button = (ImageButton) findViewById(R.id.android_button);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(MasterClass.this, "Beep Bop", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(MasterClass.this, SecondClass.class);
                startActivity(i);
            }
        });
    }
}



And the second class...

Code: Select all
package com.example.MasterClass;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class SecondClass extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       WebView webview;
       webview = (WebView) findViewById(R.id.webview);
       webview.getSettings().setJavaScriptEnabled(true);
       webview.loadUrl("http://www.google.com");
    }   
}




Everything compiles, so I wont include the layout files, but if they're needed, lmk. Finally, heres my android manifest...



Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.      package="com.example.MasterClass"
  4.      android:versionCode="1"
  5.      android:versionName="1.0">
  6.     <uses-permission android:name="android.permission.INTERNET" />  
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  8.         <activity android:name=".MasterClass"
  9.                  android:label="@string/app_name">
  10.             <intent-filter>
  11.                 <action android:name="android.intent.action.MAIN" />
  12.                 <category android:name="android.intent.category.LAUNCHER" />
  13.             </intent-filter>
  14.         </activity>
  15.        
  16.         <activity android:name=".SecondClass"
  17.                 android:label="@string/app_name">
  18.                 <intent-filter>
  19.                         <action android:name="android.intent.action.VIEW" />
  20.                         <category android:name="android.intent.category.DEFAULT" />
  21.                 </intent-filter>
  22.                 </activity>
  23.        
  24.     </application>
  25.     <uses-sdk android:minSdkVersion="3" />
  26. </manifest>
  27.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4



This is my first post so thank for any help!

EDIT: I added the line setContentView(R.layout.second); just below the SUPER line in SecondClass and I get the "beep bop" as expected, but now webview DOES NOT comes up as I would expect. After the "beep bob" toast, there is only black space. Is there a refresh or something I need to call? here is my layout for the main and second xml files:



Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7.         <TextView  
  8.             android:layout_width="fill_parent"
  9.             android:layout_height="wrap_content"
  10.             android:text="@string/hello"
  11.             />
  12.            
  13.         <ImageButton
  14.             android:id="@+id/android_button"
  15.             android:layout_width="100dip"
  16.             android:layout_height="wrap_content"
  17.             android:src="@drawable/android" />
  18.      
  19. </LinearLayout>
  20.  
  21.  
  22.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:layout_width="wrap_content"
  4.    android:layout_height="wrap_content"
  5.    android:orientation="vertical">
  6.  
  7.     <WebView
  8.        android:id="@+id/webview"
  9.        android:layout_width="fill_parent"
  10.        android:layout_height="fill_parent"
  11.    />
  12.  
  13. </LinearLayout>
  14.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
superjet
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 25, 2009 5:26 pm

Postby superjet » Sun May 17, 2009 1:15 am

bump... anyone! people have to have a solution to this! :cry: Someone give me a hand! 8)
superjet
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 25, 2009 5:26 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests