Android button comparing - java

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).

Related

trying to input values to the selected editTexts

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.

How to disable an onClick method associated with multiple views?

I don't want to allow the user to be able to click on the various images (which have onClick="mark") and call the method mark().
Method mark:
public void mark(View view){
ImageView counter = (ImageView) view;
counter.setTranslationY(-1000f);
sett(view);
}
Instead I want only method retro() to be accessible when it is called.
Method retro:
public void retro(View v){
Button bq = (Button) findViewById(R.id.b1) ;
bq.setVisibility(Button.VISIBLE);
bq.animate().alpha(1f).setDuration(2000);
//restart(bq);
}
So what should I add to retro() in order to disable mark()
So you know which View is calling the method. It's like implementing the OnClickListener for you activity, the method created is onClick(View v) (or arg0 depending on your Eclipse), defining it from xml is just specifying a sort of listener for the View, and the method from the listener as that argument.
Once you're in the method, you can do a switch for the id of the button, to perform different actions:
public void myOnClickMethod(View v)
{
switch(v.getId())
{
case R.id.button1:
//Do something for button 1
break;
case R.id.button2:
//Do something for button 2
break;
}
}
In short. Android just implements the OnClickListener for you when you define the android:onClick="myOnClickMethod" attribute.

Android Identifying Multiple Button

I have morethan 100 button in my MainLayout.xml and i want to handle click on each. I want to group all the button and then listen to click of the group button, get the ID and put it into String and then use it as my base id to query from my Sqlite Databse Table. How can i do this? so far i have this on my MainClass.java.
MainClass.java
public class MainClass extends AppCompatActivity implements View.OnClickListener {
Button B0,B1,B2,B3....;
String baseId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
buttonInitializer();
}
public void buttonInitializer(){
B0 = (Button)findViewById(R.id.B0); B0.setOnClickListener(this);
B1 = (Button)findViewById(R.id.B1); B1.setOnClickListener(this);
...
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.B0:
baseId = "B0";
break;
case R.id.B1:
baseId = "B1";
break;
..........
default:
break;
}
QueryFire(); // Fire a query
}
Public void QueryFire(){
//Fire the query with id of the clicked button
}
}
As you can see if i continue doing this, i will have Long Code, i want to shorten my code, instead of the above code, i'd like to group the button and then listen of each click just like RadioButton, but NOTE. i dont want to use RadioButton. I'm trying to implement this seudo code.
// Put the button into RelativeLayout or something and use it to group the button
// OnClick of Each Button inside the GroupButton:
// Get the ID of the Clicked Button:
// Put the ID of Button into String:
// and FIRE my query with ID of Button
Any help woul be appreciated! Thank You Very Much!!!
You should be able to specify a String for the "android:tag" attribute for each button in your xml layout, as well as an "android:onclick" attribute. This should eliminate the need for all of the findViewById() and setOnClickListener() calls.
Then, in your onClick method, you can just do something like baseId = (String) view.getTag()
Might be a bit of a stretch but why not use a ListView with a custom adapter? each adapter will hold a button (or whatever you want) and with every click on button you will get the id (the position of clicked element). Its far more easy on the memory as well :)

Do I need to add many methods, or is it possible to call one method

I have a tic tac toe app, and I want to know whether it is possible to set all the tic tac toe buttons to one on_click event, and then create a variable to get the ID of the button clicked, then pass it as a parameter to another method which will do the actual functionality, OR do I need to create different on_click events for each button?
You can do something like this, and add as many "cases" as needed:
View.OnClickListener sharedClickHandler = new View.OnClickListener() {
public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
// handle first button
break;
case R.id.button2:
// handle second button
break;
}
}
}
You can just use one listener - the onClick method takes a View parameter, which is the view that was clicked on. You can then find out which of your buttons that was:
View.OnClickListener sharedClickHandler = new View.OnClickListener() {
public void onClick(View view) {
int id = view.getId();
// Do the right thing based on the ID
}
}
Exactly how you do what you need to do based on the ID is up to you. For simple examples you could just use a switch/case statement; in other cases if you're mapping from ID to something else (a mutable object representing game state for example) you could use a Map<Integer, GameObject> and just get the right one...
hello you can use the same click event for button, you can attach for example an integer as a tag to the button so that you can know which button was clicked and handle accordingly.
button1.setTag(1);
button2.setTag(2);
button3.setTag(3);
button1.setOnClickListener(buttonClick());
button2.setOnClickListener(buttonClick());
button3.setOnClickListener(buttonClick());
public View.OnClickListener buttonClick(){
View.OnClickListener click = new View.OnClickListener() {
#Override
public void onClick(View v) {
int numberClicked = v.getTag();
//You have now the button clicked
}
};
return click;
}

how to have one single method for all button with similar functionalities

I am making a basic calculator for Android by eclipse and Java. The task is there are two values, like input and result. So when the first number is pressed, it saves it, then a plus or minus sign is pressed ad then the second value which is result.
My idea was to create a method for every button something like this:
public void number1(View view){
value1 = value2;
value2 = 1;
displayValue();
}
But did not work and also I am repeating a few lines many times which isnt nice. So what about using switch, or any idea to have one single method and put it on all android:onClick"" for buttons.
So if you have any idea just explain it or write an example.
Thanks in advance.
So what about using switch,
Yes, you can use a switch statement inside of your method and switch on the id of the Button clicked. Something like
public void someMethod(View v)
{
switch (v.getId()) // get the id of the View clicked
{
case R.id.idOfButton1: // and compare it to your Button ids
// do stuff;
break;
case R.id.idOfButton2:
// do stuff;
break;
}
// put common code before or after switch statement
}
Here v is the Button clicked so you get its id and compare it.
any idea to have one single method and put it on all android:onClick"" for buttons.
In your xml for each Button add that line
<Button
...
android:onClick="someMethod"/>
The Button Docs explain how this works and the rules associated with doing it this way but fairly straightforward.
I would really advise you to use ButterKnife for this case as best practice. All you have to do is to use #OnClick annotation with requested ids.
#OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void pickDoor(DoorView door) {
if (door.hasPrizeBehind()) {
Toast.makeText(this, "You win!", LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Try again", LENGTH_SHORT).show();
}
}
And inject this views at onCreate of your Activity or Fragment.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Views.inject(this);
}
See this site for additional examples.

Categories

Resources