How to open the second action when a button is pressed in an Android application

I am learning to create Android applications and I need some help. It seems I can’t understand which bits of the template code I need to change and which bits are static.

In the LAYOUT folder, I have ACTIVITY_MAIN.XML that 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> 

Then I have a second activity ACTIVITY_SEND_PHOTOS.XML , which

  <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> 

Then I have MainActivity.java (is this .class?) That reads the 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 the file SendPhotos.java , which:

  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 action to communicate with my sendphotos activity, simply opening this activity, nothing unusual, not sending any data or anything else.

I know that somewhere I need mine

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

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

+82
android android-intent android-activity
Nov 02 '12 at 11:12
source share
13 answers

You can go to the desired activity by pressing a button. just add android:onClick="sendMessage" this line.

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 most importantly: do not forget to determine your activity in manifest.xml

  <activity android:name=".ToActivity" android:label="@string/app_name"> </activity> 
+135
Nov 02 '12 at 11:29
source share

try it

  Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),SendPhotos.class); startActivity(i); } }); } 
+28
Nov 02 '12 at 11:17
source share

From action: where are you now?

At the event: where do you want to go?

Intent i = new Intent( MainActivity.this, SendPhotos.class); startActivity(i);

Both actions must be included in the manifest file, otherwise it will not find the class file and will not start Force close.

Change your Mainactivity.java

 crate button object; 

Now write the code for the click event.

  Button btn = (button)findViewById(R.id.button1); btn.LoginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //put your intent code here } }); 

Hope this works for you.

+10
Nov 02 '12 at 11:16
source share
 Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent myIntent = new Intent(MainActivity.this, MainActivity2.class); MainActivity.this.startActivity(myIntent); } }); 

The answer for a complete noob from a complete noob: MainActivity is the name of the first action. MainActivity2 is the name of the second action. button1 is the button identifier in xml for MainActivity Activity.

+9
Mar 21 '13 at 1:56
source share

just follow this step (I don’t write only Bcoz code, you can copy and paste and can not learn).

  • first you need to declare the button that you have in the layout

  • Give a link to this button by finding its identifier (using findviewById) in oncreate

  • setlistener for the button (e.g. setonclick listener)

  • last click event handler (means launching a new action using intent, as you already know)

  • Remember to add activity to the manifest file

BTW, it's simple, I would suggest you to start with simple online tutorials will be better for you ..

Good luck for Android

+4
Nov 02 '12 at 11:21
source share

Replace the line code below:

 import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener{ Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button1); button.setOnClickListener(this); } public void onClick(View v) { if(v.getId==R.id.button1){ Intent i = new Intent(FromActivity.this, ToActivity.class); startActivity(i); } } } 

Add the following lines to the manifest file:

  <activity android:name="com.packagename.FromActivity"></activity> <activity android:name="com.packagename.ToActivity"></activity> 
+2
Nov 02 '12 at 11:16
source share

Replace your MainActivity.class with this code

 public class MainActivity extends Activity { Button b1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button1); b1.setOnClickListener(new View.onClickListener() { public void onClick(View v) { Intent i=new Intent(getApplicationContext(),SendPhotos.class); startActivity(i); } } ) } 

Add this code to AndroidManifest.xml after </activity> and before </application>

 <activity android:name=".SendPhotos"></activity> 
+2
Nov 02 '12 at 11:37
source share

This always works, or should be fine:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick (View v) { startActivity(new Intent("com.tobidae.Activity1")); } //** OR you can just use the one down here instead, both work either way @Override public void onClick (View v){ Intent i = new Intent(getApplicationContext(), ChemistryActivity.class); startActivity(i); } } } 
+1
Oct. 11 '13 at 10:27
source share

add below code to activity_main.xml file:

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

and just add the method below to the MainActivity.java file:

 public void buttonClick(View view){ Intent i = new Intent(getApplicationContext()SendPhotos.class); startActivity(i); } 
+1
Oct 22 '17 at 10:32 on
source share

If you have two buttons and the same identifier on your button, click the following events:

 Button btn1; Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1= (Button)findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,target.class); startActivity(intent); } }); btn2=(Button) findViewById(R.id.button1);//Have same id call previous button---> button1 btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); 

When you press button1 , button2 will work, and you will not be able to open your second action.

0
Aug 14 '15 at 0:03
source share
  <Button android:id="@+id/btnSignIn" android:layout_width="250dp" android:layout_height="40dp" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="16dp" android:background="@drawable/circal" android:text="Sign in" android:textColor="@color/white" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/etPasswordLogin" /> 

In java code

 Button signIn= (Button) findViewById(R.id.btnSignIn); signIn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(SignInPage.this,MainActivity.class)); } }); 

}

0
Jul 08 '17 at 10:04 on
source share

You can go to the desired activity at the touch of a button. just add
android: onClick = "timerApp" of this line.

 xml: <Button android:id="@+id/timer_app" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="timerApp" android:text="Click To run Timer Activity" /> In your main activity just add this method: public void timerApp(View view){ Intent intent= new Intent(MainActivity.this,TimerActivity.class); startActivity(intent); } 

OR in the onCreate () method add the code below

 Button btn =findViewById(R.id.timer_app);//Don't need to type casting in android studio 3 btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, TimerActivity.class); startActivity(intent); } }); 
0
Mar 26 '18 at 8:38
source share

Using intention, you can achieve this ...

Intent intent = new Intent (FirstActivity.this, SecondActivity.class);

startActivity (intent);

You want more information. Click on this link ...

https://theandroidcodings.blogspot.com/2019/04/android-move-to-activity.html

0
Apr 29 '19 at 5:30
source share



All Articles