I'm making a information app for Android and I can't figure out how to change a button depending on which button was pressed in a previous class.
I made this picture that shows how the app works:
After clicking the first button you get to a new class with 2 new buttons that sends you to the same class but the 3 buttons there is supposed to change depending on which one of the two buttons you pressed, the three buttons you get sent to sends you to the same class but there is a TextView that changes text depending on what button you pressed.
So I need to add some kind of information to the button (an intent?) so it knows what you pressed earlier.
I'm kinda new to Java and Android so I'm sorry if I explained in a weird way.
Thanks in advance
Each time you start a new Activity, but some extras in that intent telling the next activity what the buttons should display. In the next activity, read the extras from the intent you got, and programatically set your buttons' text accordingly.
Here's some psuedo code.
public class FirstActivity extends Activity{
//setup button 1 and two first, then set their onClickLiseners like so
View.OnClickListener clickListener = new View.OnClickListener(){
public void onClick(View view){
Intent newActivity = new Intent(FirstActivity.this, SecondActivity.class);
if(view == button1){
//add extra to intent indicating button1 was clicked
}
else{
//add extra to intent indicating button2 was clicked
}
startActivity(newActivity);
}
};
// other stuff in your activity
}
public class SecondActivity extends Activity{
protected void onCreate(Bundle icicle){
Intent startedBy = getIntent();
if(started by has the extra indicating button 1 was clicked){
//do button1 stuff
}
else{
//do button2 stuff
}
}
}
}
Related
What I want, when I click on the login button of the first activity the yellow part slides down and the next activity opens. when I click on the signup button of the second screen(login screen) the yellow part of the second screen slides up and the first activity (sign up Activity)opens. I have used slide-down animation on the linear layout on the first screen it works but not working smoothly. Any help??
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mSignUpButton = findViewById(R.id.btnSigUp);
linearLayout=findViewById(R.id.linearLayout1);
mGotoLoginActivityButton=findViewById(R.id.btnLoginSignUpActivity);
slideDown= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_down);
//listener for Login button
mGotoLoginActivityButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//setValidation();
Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
startActivity(intent);
linearLayout.startAnimation(slideDown);
}
});
}
you shouldn't use two separated Activities for this purpose, use one and login and create account views should be packed into Fragments. this way will be way easier to animate between two views/fragments in on Activity, but if you really must use Activity then use Transitions (probably with shared elements), not animations, as these are working in one Activity during its runtime when visible (and you are currently running new Activity, which cover old one)
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.
I want to make buttons once I click on the button I go to another activity?
and the problem is only the first button is working!
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageOne.class);
v.getContext().startActivity(myIntent);
Button PageTwo = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageTwo.class);
v.getContext().startActivity(myIntent);
}
{}
});
}
});
}
}
Think it is because most of your code is closed inside the scope of the first onClickListener, try something like this.
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
});
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
});
Using v.getContext() should be ok, this is just how I usually would do as the Activity itself is indeed a valid context. I guess it just seems more readable to me.
Edit:
Just as a clarification to the current state of your code. The second button is assigned a onClickListener only after the first button is pressed. But since the first button takes the app to a new Activity, inherently destroying the Main Activity, the second button will never have a chance to reach it's onClickListener.
Hope it makes sense, nevertheless the code above should fix the issue.
There are a couple of issues currently in your code. The first issue is that your second button is being defined inside the first button's declaration. The next issue is that you're setting the second OnClickListener to the wrongly named button. You've made a typo and instead of PageTwo, which you've called the Button (presumably you wanted to call it PageTwoButton in accordance with the first Button) and then set the OnClickListener to PageTwoButton instead. Seeing as you're also using multiple Buttons, it's a lot cleaner and more efficient to use a GroupOnClickListener. I'd probably also suggest using 'this' instead of 'v.getContext()' as well when setting up your Intents. Change your code to be like so:
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageOneButton.setOnClickListener(addGroupOnClickListener);
PageTwoButton.setOnClickListener(addGroupOnClickListener);
private OnClickListener addGroupOnClickListener = new OnClickListener() {
public void onClick(View v) {
if (v == PageOneButton) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
} else if (v == PageTwoButton) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
}
}
};
Hope this helps!
Two words: Code Indentation
Were you to indent your code properly, you would have noticed that you're setting OnClickListener INSIDE your first buttons' listener. Move it outside your first listener, as has already been advised by others.
There's also an extra pair of {}, which is redundant.
Also, #edwoollard noticed that for the second button, you're using two different names, PageTwo and PageTwoButton. Keep that in mind, unless it's a typo.
I have a TabHost Activity with three tabs. On the second tab I have a button, clicking which would open a new Activity class (not part of the TabHost). On clicking a button in the new Activity class, it should return to the Tab 2 of the TabHost class but it returns to Tab 1. How should I fix it?
Here is the code in my new Activity class:
ImageButton btn1 = (ImageButton)findViewById(R.id.close);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ES_pic2.this, Work.class);
startActivity(intent);
ES_pic2.this.finish();
}
});
The code of my TabHost class:
private void tabs(){
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("WORK 1");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("WORK 2");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag3");
spec.setContent(R.id.tab3);
spec.setIndicator("WORK 3");
tabs.addTab(spec);
}
I did have look at other questions with similar problems, but they didn't help me.
In the onResume of the activity in which tabs are, use setCurentTab function to set the current tab. If you want to set the tab to the same index which was selected before you started the new activity, then you can save the index in SharedPreference or something. Then use that to set the current tab.
So, I have a menu activity and there a user can choose a game to play. When he does that, in game he has an option to go back to menu (either on backPress or on my Exit button in game). I need, when that happens, to disable that game button, so the user cannot play it again. How can I do that?
I tried to make an object from Menu class and then disable the button but get error. Also tried:
public void disableButton(){
button.setEnabled(false);
}
and then calling it:
obj.disableButton
But I get stactOverflow errors on line where I created my obj as soon my menu activity starts. How to do this?
EDIT:
In Menu class:
public void disableButton(){
button.setEnabled(false);
}
In Game class:
Menu objMenu = new Menu();
And then in exit onClickListener:
objMenu.disableButton();
EDIT2:
Here's how I call my game activity:
Intent i = new Intent(this, Game.class);
startActivityForResult(i, GAME);
Then in game activity:
Intent resp = new Intent();
resp.putExtra("score", numberOfPoints);
setResult(1, resp);
finish();
And again in Menu class:
final private static int AS = 1;
#Override
protected void onActivityResult(int reqCode, int respCode, Intent i) {
if(respCode == 1) {
switch(reqCode) {
case AS: receivedA = i.getIntExtra("score", receivedA);
button.setEnabled(false);
break;
}
So, this is what I do when game ends, and I disable that button succesfully.
I think the issues is not only in button.setEnabled(false);. As it is hard to say what is the problem without seeing code's details, I am suggesting another solution
start the game activity from menu activity using startActivityForResult
when the game is finished ,in the return intent put a specific data
In Menu activity, in onActivityResult check if the result intent has that specific data
basing on step 3 you can enable or disable the button