How do I get a button to open another activity? - java

I've added a button to my activity XML file and I can't get it to open my other activity. Can some please tell me step by step on how to do this?

A. Make sure your other activity is declared in manifest:
<activity
android:name="MyOtherActivity"
android:label="#string/app_name">
</activity>
All activities must be declared in manifest, even if they do not have an intent filter assigned to them.
B. In your MainActivity do something like this:
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
}
});

Using an OnClickListener
Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity.
Button yourButton = (Button) findViewById(R.id.your_buttons_id);
yourButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
});
This is probably most developers preferred method. However, there is a common alternative.
Using onClick in XML
Alternatively you can use the android:onClick="yourMethodName" to declare the method name in your Activity which is called when you click your Button, and then declare your method like so;
public void yourMethodName(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
Also, don't forget to declare your new Activity in your manifest.xml. I hope this helps.
References;
Starting Another Activity (Official API Guide)

If you declared your button in the xml file similar to this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next activity"
android:onClick="goToActivity2"
/>
then you can use it to change the activity by putting this at the java file:
public void goToActivity2 (View view){
Intent intent = new Intent (this, Main2Activity.class);
startActivity(intent);
}
Note that my second activity is called "Main2Activity"

Button T=(Button)findViewById(R.id.button_timer);
T.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),YOURACTIVITY.class);
startActivity(i);
}
});

Write code on xml file.
<Button android:width="wrap_content"
android:height="wrap_content"
android:id="#+id/button"
android:text="Click"/>
Write Code in your java file
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Secondclass.class));
/* if you want to finish the first activity then just call
finish(); */
}
});

use the following code to have a button, in android studio, open an already existing activity.
Button StartButton = (Button) findViewById(R.id.YOUR BUTTONS ID GOES HERE);
StartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, YOUR ACTIVITY'S ID GOES HERE.class));
}
});

I did the same that user9876226 answered.
The only differemce is, that I don't usually use the onClickListener. Instead I write following in the xml-file: android:onClick="open"
open is the function, that is bound to the button.
Then just create the function open() in your activity class. When you click on the button, this function will be called :)
Also, I think this way is more confortable than using the listener.

Apply the following steps:
insert new layout xml in folder layout
rename window2
add new button and add this line: android:onClick="window2"
mainactivity.java
public void openWindow2(View v) {
//call window2
setContentView(R.layout.window2);
}
}

Use following steps to add the new activity (Manifest file will be automatically update)
File > New > Activity > Empty Activity
In your MainActivity.java file add the following code inside protected void onCreate(Bundle savedInstanceState).
Make sure you call finish(); function at the end. So when you tap the Back button, it will not go back to the previous activity.
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
finish();
}
});

Related

Causing an exception when setting 2 onClickListener() on buttons located on 2 different layout

I have a problem and I can't seem to set 2 onClickListener for 2 separate buttons located on 2 different layout, when running the program, it cause an exception to occur.
btnClickToSecondPage button is located in activity_main.xml layout and btnObjClickToGoToFirstPage button is located at second_activity.xml layout.
The java code for my program is located below here
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button btnObjClickToGoToSecondPage = (Button) findViewById(R.id.btnClickToSecondPage);
Button btnObjClickToGoToFirstPage = (Button) findViewById(R.id.btnChangetoFirstPage);
btnObjClickToGoToFirstPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.activity_main);
}
}
);
} }
Please help me rectified the problem thanks.
Please implement the View.Onclick listener not Button.onclick listener
btnObjClickToGoToFirstPage.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
It is not a proper way switching pages in Android. Use two activities for switching pages with intents.
Intent newPage = new Intent (this, YourActivityNameForNewPage.class);
startActivity(newPage);
Put above code in your button's onClick().
If you want to show a new page you either start a new activity or start a new fragment.
Changing the contentView is not the correct way to approach this and should not be done.
You refer to the documentation on activities here.
Assuming you have another Activity called SecondActivity here is how you would start it:
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
);
And then you define the layout in the XML of the new activity, that is second_activity.xml
If they all have similar layouts using a fragment is also a good option.
Basically you start a new activity or fragment to show anything new or change the data dynamically say on your button's onClick().
This question may further clear your doubts:
What is setContentView(R.layout.main)?

Have to click a button twice for it to work in Android Studio

So I am currently creating an app and one of the small things that have been bothering me is the fact that I have to click a button twice for it to work.
This is my code and I can't see anything wrong with it:
public void signUpButtonClickAction(View v){
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
}
xml code for my button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signUps"
android:id="#+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="signUpButtonClickAction"/>
It is probably a small fix but even I can't spot this bug
Solution
Remove the line android:onClick="signUpButtonClickAction" and add
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
to the onCreate method of your activity or the onCreateView method of your fragment.
Alternative Solution
Alternatively, change the code to this
public void signUpButtonClickAction(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
Explanation
The line android:onClick="signUpButtonClickAction" in the xml is causing an internal call to signUpButtonClick.setOnClickListener(), so you don't have to set up an onClickListener in the signUpButtonClickAction again.
Initializing multiple buttons
private void initializeButtons() {
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
Button anotherButton = (Button) findViewById(R.id.anotherButton);
anotherButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG", "Clicked on another button!");
}
});
}
Now simply call initializeButtons() from the onCreate method of your activity.
The problem is that you are setting two times a onClick action. In your xml code you have just asign an onClick() to your button, you don't need to setOnClickListener() inside the signUpButtonClickAction(View v). You have two options:
Leave the xml file like it is and inside signUpButtonClickAction(View v) do :
public void signUpButtonClickAction(View v){
startActivity(new Intent(MainActivity.this, Signup.class));
}
OR
Remove the onClick of your xml file:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signUps"
android:id="#+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
And do this in your Activity:
Button yourButton = (Button) findViewById(R.id.signUpButton);
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
The cause of the problem is : onclick() and onClickListener are literally the same! And you are implementing both, the end result is you'll need to press the button twice to start the Activity!
See this question for more info
FIX:
The solution to your problem is :
1:
public void signUpButtonClickAction(View v)
{
startActivity(new Intent(MainActivity.this, Signup.class));
}
2:
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
as mcwise said
android:onClick="signUpButtonClickAction"
and
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
does the same thing. so you have to go with one of them. Having the two is causing the problem
For whom it may concern:
I had the same issue but none of the solutions above solved it.
For some reason I cannot understand, I had in my button this line of code:
android:textIsSelectable="true"
Deleting this attribute from the button makes it work.
This obviously made the first click to select the text, and the second click triggered the onClick button.

Android click on the button to go to another page?

I need to link the button with the page (not the main page) like when I click on the button to go to the page for example (location page)?
private void setupLocationPageButton() {
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener()
If your goal is to open another activity this is what you are going to want to do.
In your xml file you are going to want something like this
...
<Button
android:id="#+id/activity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="#string/text_of_button" />
...
Then in your activity you want
public void onButtonClicked(View view) {
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
}
what i think you trying to do: is when you click on button it's change the MainActivity
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener({
public void onClick(View _view) {
Intent i = new Intent(MainActivity.this,TheActivityTheclassNameYouWannGoTo.class);
startActivity(i);
}
}));
but first you have to creat activity and class inherit Activity like MainActivity
initialize the class in AndroidMainfest.xml
i hope this help you
You can use fragment, each fragment contains a specific page, see this for Android fragment.

Defining destination Activity for a Button

I found this code very usefull to get from one activity to the other but the problem is, that I don't see where the destination is mentioned. I would really appreciate it if some one pointed out how to change the destination.
Here is the code:
Button getTaxi = (Button) findViewById(R.id.GetTaxi);
getTaxi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
final Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
The respective part in the xml:
<Button
android:id="#+id/GetTaxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="GetTaxi" >
</Button>
Thank you very much in advance!
Actually, the code that executes on the Button's click states that your Activity (which is a sub-activity here by the way) has done its job and now finishes with the result code RESULT_OK. It means that there was another Activity that has started this actual Activity for some kind of result. So, when you'll click the Button your Activity will finish. To start some other Activity on the Button's click you should create an Intent, specifying explicitly the Activity you want to start, or just the action you want to perform over some data, letting the Android resolve the final Activity for you. Then you should call startActivity(), passing in the Intent you've created. Hope this helps.
You can use something like this:
Button getTaxi = (Button) findViewById(R.id.GetTaxi);
getTaxi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
Intent intent = new Intent();
intent.setClass(this, GetTaxiActivity.class);
startActivity(intent);
//call finish() if the current activity is something like a loading page
}
});
The code piece above which you mentioned is a sub-activity which is called using
StartActivityForResult();

Creating a menu but one of the two buttons does not properly work

public class SuperActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button registerButton = (Button) findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(SuperActivity.this, Register.class);
startActivity(myIntent);
}
});
Button loginButton = (Button) findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(SuperActivity.this, Login.class);
startActivity(myIntent);
}
});
}
}
My register buttons works but not the login button. Is something wrong with my code?
The code posted looks fine. What exactly doesn't work? Do you get an error?
Since you mention it works for 'register' but not for 'login', you may want to double check if you didn't forget to add Login to your manifest.
Is button with id login_button declared in main layout? If I had to guess this button is not declared in main layout is declared somewhere else.
double check if button with id login_button declared in main layout
Since you are not posting your logcat view,it's very difficult to see where the problem this.I'm suggesting you few things which you should make sure that are inplace as said by me.If not then please correct them and let me know if that solved your problem or not.
Firstly,you should make sure that you have declared a button with same id in your main.xml file.It will look something like this:
<button android:id="#+id/login_button" >
</button>
Secondly you should make sure that you have declared your Login.class in your AndroidManifest.xml file.It will look something like this:
<application>
<activity android:name=".Login"></activity>
</application>
Check if these things are in place or not.
Have you declared all the activities? ;)

Categories