public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
Intent intent = new Intent(this, java1.class);
startActivity(intent);
break;
case R.id.button2:
Intent i = new Intent(this, java2.class);
startActivity(i);
break;
}
}
the buttons don't seem to work at all and the app crashes when button is pressed, i have 2 buttons that when clicked go to different activities fro the main activity.
Try declaring intent at Class Level and refer it in onCreate() ,then just use startActivity in onClick()
Intent intent,i; //Globally
onCreate(Bundle savedInstances)
{
...
i=new Intent(getApplicationContext(),java2.class);
intent=new Intent(getApplicationContext(),java1.class);
...
}
Then in onClick
public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
yourclassname.this.startActivity(intent);
break;
case R.id.button2:
yourclassname.this.startActivity(i);
break;
}
}
And make sure your java1 and java2 classes are of type Activity/ListActivity
Related
So, we're working with intents at school and I'm having trouble with the intents when I try to pass data from the "Activity2" to the "Activity1", when I do the setResult() and stuff. The problem is it won't go back to the first activity when I trigger the event the first time, but it will the second.
I've been working with Android studio only for about 12h so I really lack a lot of understanding.
Here is what I'm doing:
First I call this form the main activity.
public void CheckPassword(View view) {
password = PasswordManagement.getPassword(this);
TextView txtPassword = findViewById(R.id.txtPassword);
if (txtPassword.getText().toString().equals(password)) {
Intent intent;
intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("password", password);
startActivityForResult(intent, 1);
startActivity(intent);
} else {
Intent intent;
intent = new Intent(this, RestrictedActivity.class);
startActivityForResult(intent, 1);
startActivity(intent);
}
}
Then, when I'm done from the second activity I run this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restricted);
lblRestrictedArea = findViewById(R.id.lblRestrictedArea);
lblRestrictedArea.setOnLongClickListener(
new OnLongClickListener() {
public boolean onLongClick(View view) {
intent = new Intent();
intent.putExtra(EXTRA_RESPONSE, true);
setResult(RESULT_OK, intent);
finish();
return false;
}
});
}
And back to the main activity I overwrote this to act according to the response:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
if (data.getBooleanExtra(RestrictedActivity.EXTRA_RESPONSE,false)){
LinearLayoutPasswordActivity.setBackgroundColor(getResources().getColor(R.color.red));
}else{
LinearLayoutPasswordActivity.setBackgroundColor(getResources().getColor(R.color.white));
}
}
}
}
If anyone can help I would be very glad, meanwhile I'll try to solve it my own.
Thanks!
Your are calling startActivity twice. So there are two instance of the same Activity and then you have to finish twice.
Keep your startActivityForResult(...) and delete startActivity in CheckPassword(View view)
->
public void CheckPassword(View view) {
password = PasswordManagement.getPassword(this);
TextView txtPassword = findViewById(R.id.txtPassword);
if (txtPassword.getText().toString().equals(password)) {
Intent intent;
intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("password", password);
startActivityForResult(intent, 1);
// startActivity(intent);
} else {
Intent intent;
intent = new Intent(this, RestrictedActivity.class);
startActivityForResult(intent, 1);
//startActivity(intent);
}
}
Plus, note that you are using the same requestCode (1) for two different activities. The requestCode is very important for onActivityResult method.
Wondering how to implement a complete sign out of my Firebase-enabled Android application, which will return the user to LoginActivity.
My Sign Out button has been implemented into an Action bar that is visible only on the activity_main.xml.
The code for the for the other buttons in the Action Bar is located in the MainActivity as a switch statement.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.sign_out:
Toast.makeText(getApplicationContext(), "You have been signed out", Toast.LENGTH_SHORT).show();
//sign out
break;
case R.id.settings:
Intent settings = new Intent (this, SettingsActivity.class);
startActivity(settings);
//settings
break;
case R.id.emergency_contacts:
Intent emergency_contacts = new Intent(this, EmergencyContactsActivity.class);
startActivity(emergency_contacts);
//emergency contacts
break;
default:
//unknown error
}
return super.onOptionsItemSelected(item);
}
At present, I have a simple Toast message, just to show that the button works.
I know this code FirebaseAuth.getInstance().signOut();, will need to be implemented, but I'm just not sure how within a switch statement.
Any help on this would be much appreciated.
replace your toast with this
FirebaseAuth.getInstance().signOut();
and then fire an Intent to desired activity
Intent intent = new Intent(this,DesiredActivity.class);
startActivity(intent);
finish();
I have 9 buttons, and I need to make 9 pages. Every button needs his own page to navigate to when clicked. 1 of them needs to navigate to a website instead of his own activity. So that one is done, and that works already.
Now I need to make an activity for a button so that it navigates to that on click. Once i've done that, I know how to do it and I can repeat that for the other buttons. I have the code listed below for whay my main activity looks like. That is the code for the button that navigates to the website. If any one you can refer me where to place the new code, and how to place it, would be very much appreciated. I'm pretty new to this so it might be an easy question for the lot of you.
I know that you have to change some code in the Manifest as well, but I think i can sort that one out, just the Mainactivity is the problem..
package com.example.rodekruis;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
public class MainActivity extends Activity {
private static Button button_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_sbm = (Button)findViewById(R.id.button2);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("https://www.rkz.nl/nieuws_agenda_nieuws");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
);
}
}
You can create a generic function to be trigger when the user click the button and check the id of the button clicked.
Example:
public void navigate(View v) {
int id = view.getId();
Intent intent;
if (id == R.id.button1) {
Uri.parse("https://www.rkz.nl/nieuws_agenda_nieuws1");
intent = new Intent(Intent.ACTION_VIEW, uri);
}
else if(id == R.id.button2) {
intent = new Intent(MainActivity.this, ActivityButton2.class);
}
else if(id == R.id.button3) {
intent = new Intent(MainActivity.this, ActivityButton3.class);
}
//Repeat for every button
startActivity(intent);
}
And set onclick attribute in the button calling this function.
Example:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:onClick="navigate" />
Declare your new activities in AndroidManifest.xml
Example:
<activity
android:name=".ActivityButton2"
android:label="ActivityButton2" >
</activity>
<activity
android:name=".ActivityButton3"
android:label="ActivityButton3" >
</activity>
I think you should take some time out and read Basics First app, specially Starting Activity
to answer your question, you'll need to declare 8 new activities (if each of button have a specific task) or 1 new activity (if all button leads to similar content different format or something)
so lets assume its the second case, so you create a new activity i.e. ActivityB (in android you have to register all the activities you are using in Manifest <activity android:name=".ActivityB"/>). to start that activity following is the code
Intent intent = new Intent(MainActivity.this, ActivityB.class);
startActivity(intent);
so in terms of your code it should be something like below
public class MainActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.button1:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button2:
Uri uri = Uri.parse("https://www.rkz.nl/nieuws_agenda_nieuws");
intent = new Intent(Intent.ACTION_VIEW, uri);
break;
case R.id.button3:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button4:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button5:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button6:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button7:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button8:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button9:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
}
startActivity(intent);
}}
firstly you have to create new WebViewActivity and in this xml class put these code
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
and in your new activity class put this code.
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.rkz.nl/nieuws_agenda_nieuws");
}
now open this WebViewActivity On button click.
public void OnClickButtonListener() {
button_sbm = (Button)findViewById(R.id.button2);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
}
);
}
Im new on Java so its confusing me a little bit, i want to create an intent for Activity two but there seem to be a problem with the code that i have written.
#Override
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method
// Create an intent stating which Activity you would like to
// start
Intent activityTwo = new Intent(ActivityTwo.this.finish());
Intent intent = null;
// Launch the Activity using the intent
startActivity(activityTwo);
}
});
// Has previous state been saved?
if (savedInstanceState != null) {
// TODO:
// Restore value of counters from saved state
super.onRestoreInstanceState(savedInstanceState);
mCreate = savedInstanceState.getInt(CREATE_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mStart = savedInstanceState.getInt(START_KEY);
}
// Emit LogCat message
Log.i(TAG, "Entered the onCreate() method");
// TODO:
from First to Second:
Button next = (Button) findViewById(R.id.button2);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Second.class);
intent.putExtra("Tag", "Value");
startActivity(intent);
finish();
}});
Second to First:
Button previous= (Button) findViewById(R.id.button);
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),First.class);
startActivity(intent);
}});
First Understand the concept of Intent.
In your case, you want to call ActivityTwo from ActivityOne.
follow this steps
Create ActivityTwo.
Declare ActivityTwo in your AndroidManifest.xml
<activity android:name=".ActivityTwo" />
Write Intent code where in onclick() method.
Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);
startActivity(intent);
In this code Intent Constructor contains two parameters.
Current Context
ActivityTwo reference.
I'm have one activity with 10 buttons, and transfer info about button clicked to next activity.
View.OnLongClickListener olongBtn = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (v == btn1) {
Intent intent = new Intent(MainActivity.this, AddContact.class);
intent.putExtra("slot", "slot1");
startActivity(intent);
} else if (v == btn2) {
Intent intent = new Intent(MainActivity.this, AddContact.class);
intent.putExtra("slot", "slot2");
startActivity(intent);
} else if (v == btn3) {
Intent intent = new Intent(MainActivity.this, AddContact.class);
intent.putExtra("slot", "slot3");
startActivity(intent);
}
return false;
}
};
btn1.setOnLongClickListener(olongBtn);
btn2.setOnLongClickListener(olongBtn);
btn3.setOnLongClickListener(olongBtn);
}
I want to change it like this
String slot = toString(v);
Intent intent = new Intent(MainActivity.this, AddContact.class);
intent.putExtra("slot", slot);
startActivity(intent);
But when I do it, I get in next activity something like this:
android.widget.Button{6548fcd}VFED..CL...P....17.0-147.130 #7f08004f app:id/btnPayman}
But i'm expectation name of button object.
To get name of clicked button use ((Button)v).getText() method instead of v.toString() which return String representation of calling Object:
String slot = (Button)v).getText().toString();
Intent intent = new Intent(MainActivity.this, AddContact.class);
intent.putExtra("slot", slot);
startActivity(intent);
You can simply send button id.
View.OnLongClickListener olongBtn = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Intent intent = new Intent(MainActivity.this, AddContact.class);
intent.putExtra("slot", v.getId());
startActivity(intent);
return false;
}
};
On next Activity, you can use a switch in order to know what Button has been pressed:
int buttonId = getIntent().getIntExtra("slot", 0);
switch() {
case R.id.firstbutton:
//Do what you want
break;
case R.id.secondbutton:
//Do What you whant
}
Sending int is better than sending a String because you can use a switch on the receiver activity which is faster than anidated if/else
-----------------------EDIT----------------------
While you are using include tag in order to compose your layout, you should set a different id to every button (at the same time your are setting Button text). You can also use an Integer as a tag for every button, and send that tag to the next activity (instead of id).
Look about view tags here: View setTag() method
If you want to send the name of the button, try this:
String slot = ((Button) v).getText().toString();
Intent intent = new Intent(MainActivity.this, AddContact.class);
intent.putExtra("slot", slot);
startActivity(intent);
Thanks user pozuelog for solution.
I'm use separate xml files for layout and attach it by include to main_activity.
Result of it, that getId method don't work for me.
solution is set Tag for my buttons.
btn1.setTag(1);
btn2.setTag(2);
btn3.setTag(3);
View.OnLongClickListener olongBtn = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Intent intent = new Intent(MainActivity.this, AddContact.class);
intent.putExtra("slot", String.valueOf(v.getTag()));
startActivity(intent);
return false;
}
};
btn1.setOnLongClickListener(olongBtn);
btn2.setOnLongClickListener(olongBtn);
btn3.setOnLongClickListener(olongBtn);
I think that solution may be optimize, but I'm googlecoder and leave it as is for this time.