trying to input values to the selected editTexts - java

I recently started using android studio. So I come across a problem, which is like this: I have 2 buttons and 5 editTexts. Using onClickListener, I am trying to input values to the editTexts. But the problem is I can make only one editText to listen what button I am pressing. If I use different onClickListener for 5 editTexts, only the last one is responding to the buttons (which is obvious). I want to happen like this: Whatever etitText I select, when I press a button, the value will reflect on only that one. But it is not working. I was hoping something like a if statement, where if I select a specific editText the onClickListener will respond to that.
I created this following method.
private void selectBox(EditText e, Button b1, Button b2) {
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
e.append(b.getText().toString());
}
};
b1.setOnClickListener(listener);
b2.setOnClickListener(listener);
}
then I want something like this in onCreate method:
if(editText1 is selecte`enter code here`d in the emulator) {
selectBox(editText1, button1, button2);
} else if(editText2 is selected in the emulator) {
selectBox(editText2, button1, button2);
} ......
How would I proceed? Thanks.

Here You Go !
fun handleText() {
var flag: Int
txtBox1.setOnClickListener {
flag = 1
if (flag == 1) {
button.setOnClickListener {
txtBox1.text = "add text to field 1"
}
}
}
txtBox2.setOnClickListener {
flag = 2
if (flag == 2) {
button.setOnClickListener {
txtBox2.text = "add text to field 2"
}
}
}
}
Post: I don't like Java as much as I like Kotlin. That's why your question is Java and my answer is Kotlin.

Related

How to make radio buttons display a text in another activity when clicked in android studio?

I have 6 options that the user can select from and a button that takes them to the next page when clicked. I have two pages like this. After one choice from each page is selected, I would like to display certain text depending on the radio buttons clicked previously, in another activity. How can I do this in java in android studio?
If you are controlling the FragmentManager you can just pass the options as an argument to the next Fragment's constructor otherwise, you can save everything in a static variable (as long as no Views are in there, so don't store the radio buttons there) and access that variable from outside.
Like this:
public class MyFragment {
public static boolean isRadioButtonXPressed = false; //change to true if it's pressed by default
public void onCreate(...) {
radioButtonX.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
isRadioButtonXPressed = checked;
}
});
}
}
//from the other fragment
public class MyFragment2 {
public void onCreateView(...) {
if (MyFragment.isRadioButtonXPressed) {
//it's been pressed
} else {
//it's not been pressed
}
}
}
There are many different ways.
As you are using activity, you can use Intent to pass data. Here is a sample:
Intent page = new Intent(FirstActivity.this, SecondActivity.class);
page.putExtra("key", "This is my text");
startActivity(page);
For getting the value on Second Activity onCreate() method:
String value = getIntent().getStringExtra("key");

How to Replace timer with turn based onClick in Android?

I am trying to make a TicTacToe online multiplayer game in Android Studio using Firebase, below is the code I am using for the buttons (1 of the 9 blocks of TicTacToe). Right now I am using a timer to stop the user from clicking any other buttons after he once click the button to give the other user time to click. But I want to make it so that one user won't be able to click any button till the other user clicks a button. Is there a way to do so?
The default values of nothing & face & a1 is "zero" and default value of added is "one" whereas rface is just a string variable getting its value from "face".
//below clicking
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//below
//starting if condition
if (ra1.equals(nothing)){
if (rface.equals(nothing)){
a1.setBackgroundResource(R.drawable.rounded);
Firebase refChild = ref2.child("a1");
refChild.setValue("round");
refChild = ref2.child("face");
refChild.setValue("one");
}
else if (rface.equals(added)) {
a1.setBackgroundResource(R.drawable.crossed);
Firebase refChild = ref2.child("a1");
refChild.setValue("cross");
refChild = ref2.child("face");
refChild.setValue("zero");
}
//timer below
a1.setClickable(false);
a2.setClickable(false);
a3.setClickable(false);
b1.setClickable(false);
b2.setClickable(false);
b3.setClickable(false);
c1.setClickable(false);
c2.setClickable(false);
c3.setClickable(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
a1.setClickable(true);
a2.setClickable(true);
a3.setClickable(true);
b1.setClickable(true);
b2.setClickable(true);
b3.setClickable(true);
c1.setClickable(true);
c2.setClickable(true);
c3.setClickable(true);
}
}, 3000);
//timer above
}
}
});
I suggest having two different OnClickListeners rather than an if statement. One for Xs and the other for Os. Then you can set the listener back and forth. For example, at the end of the OnClickListener for X, it calls setOnClickListener() using the one for Os.
I finally came up with this algorithm for my solution:
online=0;
offline=1;
if (online!=offline){
offline=random;
online=offline;
if(shape=0)
{
click(set round);
}
else if(shape=1)
{
click(set cross);
}
}
where shaper is another integer which I am using as a switch.

Can a ToggleButton change the functionality of other buttons in the same Activity?

Summed up, can a ToggleButton change what other buttons in the activity do when toggled? If so, a more specific explanation of what I want to do is below:
Basically there are three buttons and a togglebutton. When the togglebutton is toggled, pressing any of the three buttons will take a picture and 'save it' for that button. When untoggled, pressing any of the three buttons simply displays their images. I think I can figure out the camera capture part, but I need some direction when it comes to the togglebutton.
Any help is appreciated and I can explain further if necessary.
What I would do is keep a couple flags for each state at the class level, like this:
public class MyClass {
private static final int STATE_SAVE = 0;
private static final int STATE_DISPLAY = 1;
private int currentState = STATE_DISPLAY;
// I made this default for the example,
// you should use what makes sense to your project.
}
Then, inside your toggle button, you can set the flag. Paraphrasing this code since I don't have an editor open:
toggleButton.setOnToggleListener(new OnToggleListener() {
#Override
public void onToggled(boolean toggled) {
if(toggled) {
currentState = STATE_SAVE;
} else {
currentState = STATE_DISPLAY;
}
});
Now, when the buttons are clicked, you can switch based on the state to do an action:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(currentState == STATE_SAVE) {
// Save the image.
} else if (currentState == STATE_DISPLAY) {
// Display the image.
}
});
Create a boolean such as isToggleOn that is true or false depending on the ToggleButton. Then for each of your buttons, you can simply do:
Button button1 = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isToggleOn){
//do one thing
} else {
//do other thing
}
}
});
Yes, you definitely can. Very rarely is anything impossible with code!
All you have to do is to change the listener of the three buttons when the togglebutton is pressed. You keep alternating between the listeners each time you toggle.
For your purpose, I'd suggest defining two sets of listeners - two for each of the three buttons and then keep changing between them.

Android button comparing

I've got very very basic problem in Java (Android) writing. Here it is, I've got code:
public void WriteValue (View sender){
Button bt=(Button)sender;
}
WriteValue is performed, when user click button. And now I want to compare button which user clicks, with button that Id I know. Something like
if(UserButton==ClearButton) Display.setText("0");
Thanks for help
Tux:)
You could just compare ids.-
public void WriteValue (View sender) {
Button bt = (Button)sender;
if(bt.getId() == R.id.clearButtonId) {
Display.setText("0");
}
}
step 1 : make your Class implement OnClickListener
step 2 :
// global variable
Button btOne ,bTwo;
step 3 :
// in onCreate() method
btOne = (Button)findViewById(R.id.btOne);
btTwo = (Button)findViewById(R.id.btTwo);
btOne.setOnClickListener(this);
btTwo.setOnClickListener(this);
step 4 : implement method from interface
puvlic void onClick(View v)
{
if(v==btOne)
{
// do work for btone
}
if(v==btTwo)
{
// do work for btTwo
}
}
You don't need to cast the View to a Button here. You can simply get the id of the View clicked which is the param sent to the function (assuming this is an onClick() function as defined in xml or by setting the listener in Java). So it could be something like this
public void WriteValue (View sender)
{
int id = sender.getId(); // get the id of the View clicked
swith (id) // switch logic on that id
{
case (R.id clearButtonId): // if the clearButton was clicked do this
Display.setText("0");
break;
default:
Display.setText("Some other String);
break;
}
}
You could also use if/else instead of a switch but this is more readable, IMHO. This easily allows you to add more logic for other Buttons.
Also consider using Java naming conventions. According to this code, I assume Display is a TextView or EditText so it should start with a lower-case letter (ex. display).

how to calculate the number of touches on a button in android

hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.
Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable
A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};

Categories

Resources