Click here to Skip to main content
11,645,889 members (77,627 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android Eclipse
I want to open a new activity page/window while click on a button.
e.g. when click on a login button the profile page open.

How I do it?

I have searched a code for it. but I dnt know how do I give the path of new page ? The code I have searched is:

public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
         setContentView(R.layout.content_layout_id);
 
         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // ????? here I want to give the path of the page to be opened...
             }
         });
     }
 }

how do I give the path of new page?
Thanks in advance!
Posted 17-Aug-12 22:50pm
Misbah1536
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Put this code inside your onClick(){}...It should work..If not,let me know.
if(v.getId()==R.id.button_id)
{
    Intent firstpage=new Intent(this,yourPageName.class);
    startActivity(firstpage);
}
  Permalink  
Comments
Misbah1 at 23-Aug-12 5:45am
   
Thanks...

I tried the codes which are mentioned in the solutions.. no error but No desired output.. on button's click event next page does not open .. dont know where am I missing the step..

I made a new class named SecondPage.java and new activity where i want to jump named activity_main.xml.

I put following code in manifest file within application tag...

< activity android:name=".SecondPage.class" />

In java file I put this piece of code

protected void onCreate(Bundle icicle) {
super.onCreate(icicle);

setContentView(R.layout.new1);

final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {

if(v.getId()==R.id.button1)
{
Intent intent = new Intent(this, SecondPage.class);
startActivity(intent);
}
}
});
}

In SecondPage.java I put following code

public class SecondPage extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub

}
}

....................
Please guide me where i am wrong???
ridoy at 23-Aug-12 6:23am
   
while coding i use like this..use it..
< activity android:name=".SecondPage" />
if not working,then let me know...then i will concentrate in your code,then tell me your 1st & 2nd xml page name.
Misbah1 at 23-Aug-12 6:29am
   
done :) :) :)
Thanks a lot ...... it is moving on next page on button's click event...
Thank you so much :)
ridoy at 23-Aug-12 6:30am
   
glad to help you..happy coding..:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
         setContentView(R.layout.content_layout_id);
 
         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 Intent intent = new Intent(MyActivity.this, YourActivity.class);
        startActivity(intent);
             }
         });
     }
 }

And Add that activity in manifeast file
  Permalink  
v2
Comments
Harsh K Shah at 6-May-13 15:35pm
   
am realy sorry am not able to solve my problem even after coping your code and it keep on giving me some errors

Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2180
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2230
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 141
ActivityThread$H.handleMessage(Message) line: 1234
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 5041
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 793
ZygoteInit.main(String[]) line: 560
NativeStart.main(String[]) line: not available [native method]


plese contact me on shahkharsh@gmail.com
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Put Intent as
Intent firstpage=new Intent(getApplicationContext(),yourPageName.class);
startActivity(firstpage);

don't forget to add activity in manifest
<activity android:name=".yourPageName.class"/>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Ok Android uses Intent short for intention
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Click here for more info about intent
http://developer.android.com/reference/android/content/Intent.html[^]
Now about your question you could use this code snippet
    	Intent intent = new Intent(this, YourActivity.class);
        startActivity(intent);
  Permalink  
Comments
Member 8476247 at 18-Aug-12 6:51am
   

I wrote this piece of code in my project but there is an error:

The constructor Intent(new View.OnClickListener(){}, Class) is undefined.
Suggestion to fix this error is:
Remove arguments...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

don't forget to add activity in manifest
<activity android:name=".yourPageName.class" xmlns:android="#unknown" />


Put Intent as
Intent firstpage=new Intent(getApplicationContext(),yourPageName.class);
    startActivity(firstpage);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi,

I am beginner to the Java programming and android ...I am trying to create an app convertor which has 4-5 conversion money convertor, kgs to pound convertor..So, when I click on one conversion button it has to take me to the page pertaining to calculating the values....

Actually before i had made an app which converts rupees to dollars..I am try use the same xml and java files here...I read tutorial and watched youtube and tried tht in my sdk but its not opening up anything so I do not which part is wrong or I made any mistakes in coding.. If any one out there can help it would be great.

My code goes like this....MainActivity.java

package com.example.converts;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
 
import android.view.View;
import android.widget.Button;
 

public class MainActivity extends Activity implements View.OnClickListener
{
 Button button2;
@Override
public void onCreate(Bundle savedInstanceState) {
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
           
public void onClick(View v){
            
if(v.getId()==R.id.button2)
 
{
startActivity(new Intent("com.example.converts.Main1"));
}}
            });
            
}       
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}}
  


My Main1 which I created has

package com.example.converts;
 

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
 
public class Main1 extends Activity implements OnClickListener {
TextView dollars;
TextView euros;
    RadioButton dtoe;
    RadioButton etod;
Button calculate;
      
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub

 

dollars = (TextView)this.findViewById(R.id.dollars);
    euros = (TextView)this.findViewById(R.id.euros);
    
    dtoe = (RadioButton)this.findViewById(R.id.dtoe);
    
    etod = (RadioButton)this.findViewById(R.id.etod);
    
    calculate = (Button)this.findViewById(R.id.calculate);
    calculate.setOnClickListener(this);
}
 
public void onClick(View v) {
if (dtoe.isChecked()) {
convertDollarsToEuros();
}
if (etod.isChecked()) {
convertEurosToDollars();
}
}
 
protected void convertDollarsToEuros() {
double val = Double .parseDouble(dollars.getText().toString());
// in a real app, we'd get this off the 'net
euros.setText(Double .toString(val*0.67));
} 
 
protected void convertEurosToDollars() {
double val = Double .parseDouble(euros.getText().toString());
// in a real app, we'd get this off the 'net
dollars.setText(Double.toString(val/0.67));
 

}
 
}


My activity_mail.xml file has code


android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:orientation="vertical" >


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<button>
android:id="@+id/button2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="28dp"
android:text="POUNDS TO KGS" />

<button>
android:id="@+id/button3"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button4"
android:layout_alignLeft="@+id/button2"
android:layout_marginBottom="21dp"
android:text="CELCIUS TO FARENHEIT" />

<button>
android:id="@+id/button4"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button5"
android:layout_alignLeft="@+id/button3"
android:layout_marginBottom="30dp"
android:text="GALLONS TO LITRES" />

<button>
android:id="@+id/button5"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_alignParentBottom="true"
android:layout_marginBottom="29dp"
android:text="MILES TO KMS" />

<button>
android:id="@+id/button1"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:text="RUPEE TO DOLLAR" />

<button>
android:id="@+id/button6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="25dp"
android:background="#0099cc" />

<button>
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button6"
android:layout_alignBottom="@+id/button6"
android:layout_alignLeft="@+id/button1"
android:layout_marginLeft="20dp"

android:text=" CONVERSION APP " />
android:typeface="sans"
android:textStyle="bold"

</button></button></button></button></button></button></button>






activity1.XML has the following code



RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

android:id="@+id/widget32"
android:layout_width="200dp"
android:layout_height="300dp"
android:orientation="vertical"
android:layout_x="2dp"
android:layout_y="15dp">


android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOLLARS"
android:typeface="serif"
android:textStyle="bold"
android:gravity="center" />

android:id="@+id/dollars"
android:layout_width="80dp"
android:layout_height="50dp"
android:textSize="18sp" />

android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EUROS"
android:typeface="sans"
android:textStyle="bold"
android:gravity="center" />

android:id="@+id/euros"
android:layout_width="80dp"
android:layout_height="50dp"
android:textSize="18sp" />

android:id="@+id/dtoe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dollars to Euros"
android:typeface="sans"
android:textStyle="bold"
android:gravity="center" />

android:id="@+id/etod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Euros to Dollars"
android:typeface="sans"
android:textStyle="bold"
android:gravity="center" />


<button>
android:id="@+id/calculate"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="Calculate"
android:typeface="sans"
android:textStyle="bold"
android:gravity="center"
android:layout_x="165dp"
android:layout_y="319dp" />
</button>








So, please let me know what I am doing wrong..or what corrections I need to make to get this...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 628
1 jyo.net 514
2 Sergey Alexandrovich Kryukov 478
3 Afzaal Ahmad Zeeshan 378
4 DamithSL 260
0 OriginalGriff 1,143
1 jyo.net 994
2 Sergey Alexandrovich Kryukov 808
3 Afzaal Ahmad Zeeshan 702
4 DamithSL 691


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 25 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100