Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a second activity on click of button in android app

I am learning to build android applications and I need some specific help. I can't seem to get my head around which bits of template code I am required to change, and which bits are static.

In the LAYOUT folder I have my ACTIVITY_MAIN.XML which reads

 <?xml version="1.0" encoding="utf-8"?>  <LinearLayout 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:orientation="horizontal">   <Button     android:id="@+id/button1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="@string/main_buttons_photos" />   </LinearLayout> 

Next, I have my second activity ACTIVITY_SEND_PHOTOS.XML which is

 <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" >   <TextView     android:id="@+id/textView2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_centerHorizontal="true"     android:layout_centerVertical="true"     android:text="@string/hello_world"     tools:context=".SendPhotos" />   <TextView     android:id="@+id/textView1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_alignParentTop="true"     android:text="@string/title_activity_send_photos"     android:textAppearance="?android:attr/textAppearanceLarge" />   </RelativeLayout> 

I then have my MainActivity.java (is this the .class?) this reads package com.example.assent.bc;

 import android.os.Bundle;  import android.app.Activity;  import android.view.Menu;  import android.view.View;   public class MainActivity extends Activity {   @Override  public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);  }   @Override  public boolean onCreateOptionsMenu(Menu menu) {      getMenuInflater().inflate(R.menu.activity_main, menu);      return true;  }  /** Called when the user clicks the Send button */  public void sendMessage(View view) {      // Do something in response to button  }  } 

and then my SendPhotos.java file which is;

 package com.example.assent.bc;   import android.os.Bundle;  import android.app.Activity;  import android.view.Menu;  import android.view.MenuItem;  import android.support.v4.app.NavUtils;   public class SendPhotos extends Activity {   @Override  public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_send_photos);     getActionBar().setDisplayHomeAsUpEnabled(true);  }   @Override  public boolean onCreateOptionsMenu(Menu menu) {     getMenuInflater().inflate(R.menu.activity_send_photos, menu);     return true;  }    @Override  public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {         case android.R.id.home:             NavUtils.navigateUpFromSameTask(this);             return true;     }     return super.onOptionsItemSelected(item);  }   } 

I would like the button in my main activity to link through to my sendphotos activity, simply opening up that activity, nothing fancy, not sending any data or anything.

I know that somewhere I need my

 Intent i = new Intent(FromActivity.this, ToActivity.class);  startActivity(i); 

but I have no idea what to replace ToActivity.class with or what else I need where.

like image 403
Henry Aspden Avatar asked Nov 02 '12 11:11

Henry Aspden


People also ask

How do I intent to start another activity?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.


1 Answers

You can move to desired activity on button click. just add this line.

android:onClick="sendMessage"

xml:

 <Button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:onClick="sendMessage"         android:text="@string/button" /> 

In your main activity just add this method:

public void sendMessage(View view) {     Intent intent = new Intent(FromActivity.this, ToActivity.class);     startActivity(intent); } 

And the most important thing: don't forget to define your activity in manifest.xml

 <activity>       android:name=".ToActivity"       android:label="@string/app_name">            </activity> 
like image 151
Juned Avatar answered Oct 04 '22 19:10

Juned