I'm trying to have a program go from one class to another by the press of a button. I know this needs to be done with intent, but I'm not sure exactly what action to use for intent. Also, i would rather not try to use OnClickListener since it seems to me to be a bit excessive for what I need to do. Though I could be wrong and I'm just over thinking it. Here's what I have for the code:
public void loginScreen(View view) {
// Should take user to the next view when button pressed
Intent intent = new Intent(this, LoginActivity.class);
Button login = (Button) findViewById(R.id.button1);
intent.getData(login);
startActivity(intent);
}
I want it to go to the next screen when only button1 is pressed so i don't know if just lines 3 and 6 are the only ones needed, but it won't work if it's just those two. I believe the problem is getData, i don't know what to put there, but i know getData's wrong. I'm probably also missing something else, but I don't know what.
Also, forgive me if this isn't easy to follow, first time trying to ask a question here.
In your XML file you can declare your on click:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="loginScreen" />
Then in your Activity:
public void loginScreen(View button) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
Here is the onClick View API
Name of the method in this View's context to invoke when the view is
clicked. This name must correspond to a public method that takes
exactly one parameter of type View. For instance, if you specify
android:onClick="sayHello", you must declare a public void
sayHello(View v) method of your context (typically, your Activity).
Note that if you add an onClick to a Fragment layout it will still need to be caught in the Activity.
Without explicitly setting an onClickListener:
<button
...
android:onClick="loginClicked" />
public void loginClicked(View v) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
Also setting an onClickListener explicitly is not overkill.
Your question isn't entirely clear, so let me know if you need more and I'll edit my answer.
Related
New to Android Studio. I'm creating an app project for practice and I am trying to create a Menu Activity. I want to test to see if I can mute sounds and hide the display of text (score) via a Menu UI. I get that I can use Intent to pass values back and forth between activities and that I can use those values to turn features on and off across the app.
I cannot figure out with a button and onClick how to get a variable to change so that I can pass it via Intent. I've only seen it done INSIDE the onClick. I'm trying to change the variable OUTSIDE the onClick.
Example Code:
public class Menu extends AppCompatActivity {
private boolean soundOn = true;
private Button isSoundOn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
isSoundOn = findViewById(R.id.isSoundOn_button);
isSoundOn.setOnClickListener(v -> {
soundOn = false;
});
Now when I attempt to access the soundOn parameter and pass it on via Intent to another activity the value remains true, it never changes after the button is clicked.
I did figure out one trick, I can use intent and pass the value to the same activity, like so:
soundOff.setOnClickListener(v -> {
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", false);
startActivity(intent);
This reloads the Activity after the button is clicked, it makes it appear as though a glitch happened as it is run, but I think that issue could be resolved via altering the transition animation...I think. However, this seems like a clumsy approach, especially in a Menu Activity that could have numerous settings to edit (sound, score, language, timer, color, background, etc.).
I get that I can have the onClick go back to the original Activity with the change, but I want to create a menu where I can have multiple selections made and then pass them all back to the original Activity.
I hope this makes sense, I know this is rather basic, but I'm new to this and my searching hasn't been able to yield a solution. Thanks.
If you are doing an intent to the same Activity you should retreive your intent on the onCreate method:
isSoundOn = intent.getBooleanExtra("soundOn", true) //true is the default parameter in case intent does not contain that key;
That way you are always checking your intent in case you need it.
You also need to use your variable in the intent; right now you are always setting it to false:
soundOff.setOnClickListener(v -> {
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", soundOn);
startActivity(intent);
}
There are other solutions, for example: you can use SharedPreferences for persisting your values and then call this.recreate to recreate Activity again and avoid using intents. Then you can retreive your SharedPreferencesvalues on the onCreate method to do whatever you want.
Now when I attempt to access the soundOn parameter and pass it on via
Intent to another activity the value remains true, it never changes
after the button is clicked.
Lets start with keeping track of soundOn
When Menu activity is first launched soundOn = true
When isSound button is clicked soundOn = false
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", soundOn); // false is the value that is extra.
startActivity(intent);
When MenuActivity is again launched due to the intent soundOn = true this is because of this line
private boolean soundOn = true; //<---
You are passing Extra in intent but you arent storing the intents extra value in soundOn
Thats why it is true always
to solve this use need to Get the Intent Extra that you have passed and we do it in onCreate method
private boolean soundOn = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Intent intent = getIntent();
if(intent != null) { // we need to get this cause when we first start our app we dont start it with any intents
soundOn = intent.getExtra("soundOn");
}
}
I'm a newbie and I'm working on a Unit Converter.
I would like to open the same UI regardless of which button I click(Weight or Length)
main_activity.xml UI
Below is the UI I want to open: activity_conversion.xml UI
And I would like each button in the main_activity to run on a different java class.
So, (minus the main_activity.java and it's .xml file)
I have 1 xml file(activity_conversion.xml) and 2 java files one for each button of the main_activity.xml
activity_main.xml Weight button
android:onClick="weightPage"
activity_main.xml Length button
android:onClick="lengthPage"
MainActivity.java
public void weightPage(View view){
Intent intent = new Intent(this, WeightActivity.class);
startActivity(intent);
}
public void lengthPage(View view) {
Intent intent2 = new Intent(this, LengthActivity.class);
startActivity(intent2);
}
Length_Activity.java code for Length button
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversion);
}
setContentView() method doesn't work for me:(
Thanks in advance!
I would like to open the same UI regardless of which button I click(Weight or Length)
You can do that by creating an activity and its layout XML file. And then start that activity via explicit intent like this:
//Place this code inside the onClick method
Intent intent = new Intent(SoucreActivity.this, DestinationActivity.class);
startActivity(intent);
And I would like each button in the main_activity to run on a different java class.
No, you cannot. All UI elements on a screen are always in the same activity; they cannot run on different java classes. (Unless you are using fragments of which you need not worry about as you are a newbie)
Apparently, you want the two buttons in your main activity to open the same activity. Which you can achieve using intents using the code snippet mentioned above.
I'm very new to Android Studio Development and I was wondering how to do this, when I click a button on MainActivity, it will direct me to secondActivity where the text become visible (Originally TextView will not be visible until the button from MainActivity is pressed)
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String status = "Success!";
intent2.putExtra("Status",status);
startActivity(intent);
}
});
I want to make an if-else statement for this (on SecondActivity page) where if user straight away go to SecondActivity, it will not display any text there. But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.
Thanks!
Basically, there are several approaches you can do that.
Use intents pass data
Sure you pass a boolean type or whatever you want into this intent, I think this is the approach you are trying to make here. So I can give you an example:
In your first activity you can do something like this,
button.setOnClickListener {
val intent = Intent(this#MainActivity, SecondActivity::class.java).apply {
val status = true
putExtra("Status", status)
}
startActivity(intent)
}
And in your second activity, in your need to override onCreate to parse your intents to decide your text want to display or not.
val status = intent.extras?.getBoolean("Status")
if(status) {
hideText()
} else {
showText()
}
the other approach you can deal with it is try to create singleton class to keep the status in this class, and based this singleton class status, you may choose to hide/show your text. However this solution isn't the recommended way to do it. Because global state is bad for testing and just pollute the code.
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 5 years ago.
My app runs well and shows no errors, but my button is not working ( is unClickable in device)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button11=(Button)findViewById(R.id.button11);
button11.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
new Intent(MainActivity.this, Main2Activity.class);
}
});
}
}
and XML file :
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50px"
android:layout_marginTop="200dp"
android:background="#color/colorPrimary"
android:text="صفحه اصلی"
android:onClick="onClick"/>/>
On the real device when I click on this button nothing happens !
try this way
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
EDIT: I wanted to add a bit of an explanation to help
Hi MRAK,
First things first, you do NOT need to create an onClick listener. Remove all of that completely. Android studio is a beast and automatically does that for you using the XML file. When you set "onClick" in the XML file, it automatically calls the name of whatever method you put in there. You should change it so it is not also called "onClick." I would prefer to call it "startAcitivty2" or so on so you are not confused later. I stuck with your method name for now.
See below for corrected code:
public void onClick(View v){
// note in the below line i'm just using "this"
Intent myIntent = new Intent(this, Main2Activity.class)
// Secondly, you need to end the current activity
finish();
// Third, you need to start your new activity...
// Creating an Intent does not the activity alone
startActivity(myIntent);
}
Also, this has so many downvotes because this has been asked 1000+ times. Please use google or the search bar above before asking. Google will reroute you to stackoverflow anyway :)
I'm creating my first mobile app and I'm trying to bind a button to go straight to another xml file. It seems like such an easy answer but I can't find the solution anywhere. I'm using Eclipse as my IDE and using the Android ADT bundle, if that's at all relevant.
Put this in onCreate:
findViewById(R.id.my_button).setOnClickListener(new View.OnClickListener {
#Override
public void onClick() {
startActivity(new Intent(MainActivity.this, OtherActivity.class))
}
});
Replace my_button with the id of your button and MainActivity.this with the name of your main activity class.this and OtherActivity with the name of your other activity.class.
Are you asking how to create a layout with button that brings different layout? If so, you can drag button on your layout and put method in the "On Click" property of the button, something like myClick and then in your code add method
public void myClick(View v) {
startActivity(new Intent(this, MyNewActivity.class));
}
Make sure to declare new activity in AndroidManifest
Hope it helps
In your xml file, add this line of code to your button:
<Button>
//other button properties here
android:onClick='onNextPage'
</Button>
and in the activity/java file for that page, do this:
private void onNextPage(View view){
Intent intent = new Intent(this, nextActivity.java);
startActivity(intent);
}
when you click the button, you'll go to nextActivity.java or whatever you named your second page to be.