I'm working on an application that contains an activity "MainActivity" this activity contains 9 edittext and this activity connect to the custom keyboard that contains number buttons from 0 to 9 and three button (delete, replay and help) all works perfectly but el rest two buttons (replay and help) I want to click on the button play again (new activity) activity MainActivity restart ... help me please
Inside your code, first findViewById your replay button, then add a OnClickListener like below.
Button replay = findViewById(...);
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent restartIntent = new Intent(this,MainActivity.class);
startActivity(restartIntent)
}
});
Related
i have a problem
i have button A in first activity and button B in second activity,i want when someone click button B in second activity then color of A button is change permanently it never reverse to previous colour again when ever user not uninstall the app
This is not how you ask a question in SO, you should try something first and when you hit a problem, then you can ask your question as specific you can, along with all things you have done. you can read about how you can ask a good question in here.
Now you can try something like this:
//create a method in your first activity, (where the button color should change):
public void changeColorInFirstActivity(){
Button btnA = (Button) findViewById(R.id.myButtonA);
btnA.setBackgroundColor(getResources().getColor(R.color.red));
}
And add this in your second activity where you want to click on a button to change the first activity button color:
Button btnB = (Button) findViewById(R.id.myButtonB);
btnB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirstActivity secondActivity = new FirstActivity();
firstActivity.changeColorInFirstActivity();
}
});
Now after setting the color, save the color int in shared preferences and set the value you get on your button color in your First activity
I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});
Here is the scenario : I have a gui which contains two buttons.Now is there any way by which second button is clickable only after first button is clicked ?
say you have activity with two buttons defined in the xml layout : button1 and button2
in activity onCreate method write:
button2.setEnabled(false);
In the in click listener of first button write
button2.setEnabled(true);
so finally
in onCreate method of the activity we have
button2.setEnabled(false);
private OnClickListener l = new OnClickListener() {
public void onClick(View v) {
button2.setEnabled(true);
}
};
button1.setOnClickListener(l);
Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView
I am Having 8 Screenns.I have prepared 8 Activities for that.
In First Activity I have given this code
To Switch from Ist Activity to IInd
On Image Button gives On Clickpublic void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), Activity2.class);
v.getContext().startActivity(myIntent);
});
What to do to Switch on 2nd Activity to 3rd Activity ,
3rd Activity to 4th Activity , and so on.
Pls help me in regard.
Here's 1 way you could do it below. In this example, you'd put 3 buttons on the screen. These are buttons I defined and laid out in my XML file. Click on any of the 3 different buttons, and it takes you to the corresponding activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Here is code to go grab and layout the Buttons, they're named b1, b2, etc. and identified as such.
Button b1 =(Button)findViewById(R.id.b1);
Button b2 =(Button)findViewById(R.id.b2);
Button b3 =(Button)findViewById(R.id.b3);
// Setup the listeners for the buttons, and the button handler
b1.setOnClickListener(buttonhandler);
b2.setOnClickListener(buttonhandler);
b3.setOnClickListener(buttonhandler);
}
View.OnClickListener buttonhandler=new View.OnClickListener() {
// Now I need to determine which button was clicked, and which intent or activity to launch.
public void onClick(View v) {
switch(v.getId()) {
// Now, which button did they press, and take me to that class/activity
case R.id.b1: //<<---- notice end line with colon, not a semicolon
Intent myIntent1 = new Intent(yourAppNamehere.this, theNextActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent1);
break;
case R.id.b2: //<<---- notice end line with colon, not a semicolon
Intent myIntent2 = new Intent(yourMainAppNamehere.this, AnotherActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent2);
break;
case R.id.b3:
Intent myIntent3 = new Intent(yourMainAppNamehere.this, a3rdActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent3);
break;
}
}
};
}
Basically we're doing several things to set it up. Identify the buttons and pull them in from the XML layout. See how each has an id name assigned to it. r.id.b1 by example is my first button.
Then we set up a handler, which listens for clicks on my buttons. Next, need to know which button was pushed. The switch / case is like an "if then". If they press button b1, the code takes us to what we assigned to that button click. Press on b1 (Button 1), and we go to that "intent" or activity we've assigned to it.
Hope this helps a little Don't forget to vote up the answer if it's of any use. I'm just getting started on this stuff myself.
Thanks,
let's try to use the code snippet from below url and go through flags from developer guide.
Android; How can I initialise state in one activity, then have another refresh that?