Setting textview text in a method - java

I am trying to create a method to determine the winner of the game, not worried about the logic of the finding the winner.
In the code I was attempting to add a getWinner() method that would determine the winner when the counter hit 10 moves. In the getWinner() method I took my textview object tv and wrote tv.setText("worked");. The app would crash. If i skip adding the getWinner() method and just toss in the tv.setText() straight into the code it works though.
Since I am not sure how well I explained what's going on maybe my comments throughout the code can give a better understanding.
public class MainActivity extends AppCompatActivity {
public int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
final Button button6 = (Button) findViewById(R.id.button6);
final Button button7 = (Button) findViewById(R.id.button7);
final Button button8 = (Button) findViewById(R.id.button8);
final Button button9 = (Button) findViewById(R.id.button9);
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button1.setText("O");
}
else
{
button1.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button2.setText("O");
}
else
{
button2.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button3.setText("O");
}
else
{
button3.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button4.setText("O");
}
else
{
button4.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button5.setText("O");
}
else
{
button5.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button6.setText("O");
}
else
{
button6.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button7.setText("O");
}
else
{
button7.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button8.setText("O");
}
else
{
button8.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner();
}
}
});
button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button9.setText("O");
}
else
{
button9.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
/**
public void getWinner() {
tv.setText("does not work");
}
**/
}
}

Try passing textview obj to the method getWinner and then try to set it.getWinner should be outside oncreate method.

As other answer has pointing out, you're creating a method inside a method which is a code error and you're trying to access a variable inside of another method which is a method scope.
First, we need to resolve the first problem.
If we simplify your code, it will be something like this
public class MainActivity extends AppCompatActivity {
public int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
// get all the view.
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
button5.setOnClickListener(listener);
button6.setOnClickListener(listener);
button7.setOnClickListener(listener);
button8.setOnClickListener(listener);
button9.setOnClickListener(listener);
/**
public void getWinner() {
tv.setText("does not work");
}
**/
}
}
From the above code, you can see that you're trying to create getWinner() inside of onCreate() which is wrong.
It should be:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
}
public void getWinner() {
tv.setText("does not work");
}
}
Now the code is working but it will giving you an error because getWinner() can't found the tv variable which is only inside the onCreate() method. So, you need to make it a class scope like this:
public class MainActivity extends AppCompatActivity {
// make it class scope.
private TextView tv;
protected void onCreate(Bundle savedInstanceState) {
...
TextView tv = (TextView) findViewById(R.id.winnerTextView);
...
}
public void getWinner() {
tv.setText("does not work");
}
}
Both of the problems happen because you're creating a method with a too much line of code. You should always make your method not more than 15 line. This is the best practice to make your code more maintainable in the future.
Don't do too much logic in a single method. Focus only solving one problem in a method. Always use a Divide et impera principal to separate a bigger problem to many smaller problems.

Try it by putting the getWinner() method outside your onCreate() function and pass the textView reference to it.
I guess it should work.

What you may do is the,
Declare the TextView objects globally.
you can not use the background thread to set text so be careful if you uses any background thread.

Define tv at globally and no need to put getWinner() into on create so pickup it out
public class MainActivity extends AppCompatActivity {
public int counter = 1;
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
final Button button6 = (Button) findViewById(R.id.button6);
final Button button7 = (Button) findViewById(R.id.button7);
final Button button8 = (Button) findViewById(R.id.button8);
final Button button9 = (Button) findViewById(R.id.button9);
tv = (TextView) findViewById(R.id.winnerTextView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button1.setText("O");
}
else
{
button1.setText("X");
}
counter++;
if (counter == 10)
{
getWinner();
}
}
});
}
public void getWinner() {
tv.setText("does not work");
}
}

make getWinner() method outside of onCreate() and make tv global variable. it should work.

Related

How do I pass the string from a textview to another texview on the same activity?

I am working with this bingo callboard project in Android Studio. The principle is when I click a number button, it will show on the first textview. When I click the second number button, the first textview will show the current number pressed and the previous string will be passed to the second textview. How would I do this? Screenshot of app included. A sample of the MainActivity.java code is here:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Activity mainActivity;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = MainActivity.this;
final Button button1 = findViewById(R.id.b1);
final Button button2 = findViewById(R.id.b2);
final Button button3 = findViewById(R.id.b3);
final Button button4 = findViewById(R.id.b4);
final Button button5 = findViewById(R.id.b5);
final TextView txt = (TextView) findViewById(R.id.presentcall);
final TextView lst = (TextView) findViewById(R.id.lastcall);
button1.setOnClickListener(new View.OnClickListener() {
Drawable background = button1.getBackground();
#Override
public void onClick(View v) {
txt.setText("B1");
if (button1.getText().equals("1"))
{
button1.setText(" 1 ");
button1.setBackgroundResource(R.drawable.onclick_border);
}
else if (button1.getText().equals(" 1 "))
{
button1.setText("1");
button1.setBackground(background);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
Drawable background = button2.getBackground();
#Override
public void onClick(View v) {
txt.setText("B2");
if (button2.getText().equals("2"))
{
button2.setText(" 2 ");
button2.setBackgroundResource(R.drawable.onclick_border);
}
else if (button2.getText().equals(" 2 "))
{
button2.setText("2");
button2.setBackground(background);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
Drawable background = button3.getBackground();
#Override
public void onClick(View v) {
txt.setText("B3");
if (button3.getText().equals("3"))
{
button3.setText(" 3 ");
button3.setBackgroundResource(R.drawable.onclick_border);
}
else if (button3.getText().equals(" 3 "))
{
button3.setText("3");
button3.setBackground(background);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
Drawable background = button4.getBackground();
#Override
public void onClick(View v) {
txt.setText("B4");
if (button4.getText().equals("4"))
{
button4.setText(" 4 ");
button4.setBackgroundResource(R.drawable.onclick_border);
}
else if (button4.getText().equals(" 4 "))
{
button4.setText("4");
button4.setBackground(background);
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
Drawable background = button5.getBackground();
#Override
public void onClick(View v) {
txt.setText("B5");
if (button5.getText().equals("5"))
{
button5.setText(" 5 ");
button5.setBackgroundResource(R.drawable.onclick_border);
}
else if (button5.getText().equals(" 5 "))
{
button5.setText("5");
button5.setBackground(background);
}
}
});
Screenshot
Hey so from what I understand you need to update the lst textview with the data of txt textView. Well you can write a common method which can be called from any button click with the new data. something like this:
public class MainActivity extends AppCompatActivity {
private Activity mainActivity;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = MainActivity.this;
final Button button1 = findViewById(R.id.b1);
final Button button2 = findViewById(R.id.b2);
final Button button3 = findViewById(R.id.b3);
final Button button4 = findViewById(R.id.b4);
final Button button5 = findViewById(R.id.b5);
final TextView txt = (TextView) findViewById(R.id.presentcall);
final TextView lst = (TextView) findViewById(R.id.lastcall);
button1.setOnClickListener(new View.OnClickListener() {
Drawable background = button1.getBackground();
#Override
public void onClick(View v) {
txt.setText("B1");
updateTextView("B1");
if (button1.getText().equals("1"))
{
button1.setText(" 1 ");
button1.setBackgroundResource(R.drawable.onclick_border);
}
else if (button1.getText().equals(" 1 "))
{
button1.setText("1");
button1.setBackground(background);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
Drawable background = button2.getBackground();
#Override
public void onClick(View v) {
txt.setText("B2");
updateTextView("B2");
if (button2.getText().equals("2"))
{
button2.setText(" 2 ");
button2.setBackgroundResource(R.drawable.onclick_border);
}
else if (button2.getText().equals(" 2 "))
{
button2.setText("2");
button2.setBackground(background);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
Drawable background = button3.getBackground();
#Override
public void onClick(View v) {
txt.setText("B3");
updateTextView("B3");
if (button3.getText().equals("3"))
{
button3.setText(" 3 ");
button3.setBackgroundResource(R.drawable.onclick_border);
}
else if (button3.getText().equals(" 3 "))
{
button3.setText("3");
button3.setBackground(background);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
Drawable background = button4.getBackground();
#Override
public void onClick(View v) {
txt.setText("B4");
updateTextView("B4");
if (button4.getText().equals("4"))
{
button4.setText(" 4 ");
button4.setBackgroundResource(R.drawable.onclick_border);
}
else if (button4.getText().equals(" 4 "))
{
button4.setText("4");
button4.setBackground(background);
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
Drawable background = button5.getBackground();
#Override
public void onClick(View v) {
txt.setText("B5");
updateTextView("B5");
if (button5.getText().equals("5"))
{
button5.setText(" 5 ");
button5.setBackgroundResource(R.drawable.onclick_border);
}
else if (button5.getText().equals(" 5 "))
{
button5.setText("5");
button5.setBackground(background);
}
}
});
}
private void updateTextView(String newData){
String oldData = txt.getText().toString();
lst.setText(oldData);
txt.setText(newData);
}
}
This will update your last textview with your txt textview data. YOu can call this method from your button clicks with the new data as a parameter . Hope this helps you
There is nothing wrong with p.matthew13's code. The problem is with the code sequence. I may also have to delete a line on my code because it's already redundant.
The redundant code is txt.setText(""); which contradicts txt.setText(newData);
I have to delete my setText code and replace it with the updateTextView("");
Also, I did place p.matthew13's code before my public void.
And that fixes my problem, after trying to shuffle up the code execution sequence.
Thanks very much!

whenever i perform any operations like add sub equals etc in andriod studio EditText box , my app crashes

I have been trying to make a simple calculator on Android studio. It shows the calculations perfectly fine. but whenever I try to press equals or plus sub or minus operation when the EditText box is empty, my app always crashes.
I tried different ways but it didn't help.
Is there any way that I can do it and stop my app from crashing?
Here's my java code
package com.example.mathcalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button0,button1,button2,button3,button4,button5,button6,button7,button8,button9,buttonClear,buttonMul,buttonSub,buttonAdd,buttonDec,buttonEqual,buttonDiv;
float val1,val2;
EditText result;
boolean mAdditon,msubtraction,mMultiplication, mDivision;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonMul = (Button) findViewById(R.id.buttonMul);
buttonDiv = (Button) findViewById(R.id.buttonDiv);
buttonClear = (Button) findViewById(R.id.buttonClear);
buttonEqual = (Button) findViewById(R.id.buttonEqual);
buttonDec = (Button) findViewById(R.id.buttonDec);
buttonSub = (Button) findViewById(R.id.buttonSub);
result = (EditText) findViewById(R.id.result);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"9");
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+".");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (result == null)
{
result.setText("");
}
else
{
val1 =Float.parseFloat(result.getText()+"");
mAdditon=true;
result.setText(null);
}
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
msubtraction=true;
result.setText(null);
}
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
mDivision=true;
result.setText(null);
}
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
mMultiplication = true;
result.setText(null);
}
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText("");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
val2 = Float.parseFloat(result.getText() + "");
if (mAdditon == true) {
result.setText(val1 + val2 + "");
mAdditon = false;
}
if (msubtraction == true) {
result.setText(val1 - val2 + "");
msubtraction = false;
}
if (mMultiplication == true) {
result.setText(val1 * val2 + "");
msubtraction = false;
}
if (mDivision == true) {
if (val2 == 0) {
result.setText("Can't Divide by Zero");
return;
} else {
result.setText(val1 / val2 + "");
msubtraction = false;
}
}
}
});
}
}
You say it crashes when the EditText box is empty, so most likely, what is happening is that when you do
Float.parseFloat(result.getText() + "");
result.getText() returns "", or you end up doing
Float.parseFloat("");
which result in java.lang.NumberFormatException: empty String
Try replacing
val1=Float.parseFloat(result.getText()+"");
with
val1 = 0;
if(!result.getText().isEmpty())
val1 = Float.parseFloat(result.getText());
NOTE:
Also, as you use it several times, you can make a method for it
private float getResult() {
int result = 0;
if(!result.getText().isEmpty())
val1=Float.parseFloat(result.getText());
return result;
}
and then replace everywhere with val1 = getResult();
as above code when get data from EditText use edit.getText().toString()
and for Parsing it always wrap it with try/catch block as EditText it is initially empty
some hints about the code
- here if (result == null) result.setText(""); result is always not null.
so for better solution always use try/catch when parsing EditText.

Android Programming mistake

I create an app name of android wallpaper but the problem is that when i clicked on left and right button the next and previous wallpaper does not comes. I tried it lots but failed.
public class Wallpapers extends AppCompatActivity {
WallpaperManager managerr;
Button B;
Button b1;
Button b2;
ImageView m;
int a[]={R.drawable.w1,R.drawable.w2,R.drawable.w3};
int aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wallpapers);
click();
}
public void click()
{
m = (ImageView) findViewById(R.id.imageView);
B=(Button)findViewById(R.id.button3);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
managerr = WallpaperManager.getInstance(getApplicationContext());
m.setImageResource(R.drawable.w1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if()
{
m.setImageResource(R.drawable.w2);
}
else if(m.getDrawable()==getResources().getDrawable(R.drawable.m2).getCurrent())
{
m.setImageResource(R.drawable.w3);
}
}
});
}
}
That's the code.
That's the image of app:
you have set onClickListener only for b1.
change your code follows
int currentPosition = 0;
public void click(){
m = (ImageView) findViewById(R.id.imageView);
B= (Button) findViewById(R.id.button3);
left = (Button) findViewById(R.id.button);
right = (Button) findViewById(R.id.button2);
managerr = WallpaperManager.getInstance(getApplicationContext());
m.setImageResource(R.drawable.m1);
left.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
if(currentPosition > 0){
m.setImageResource(previuosDrawable);
currentPosition--;
}
}
}
right.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
if(currentPosition < pictures.length){
m.setImageResource(nextDrawable);
currentPosition++;
}
}
}
}
First of all, if() has no conditions so it can't works add first if(m.getDrawable()==getResources().getDrawable(R.drawable.m2).getCurrent())
and then add all the else if you need and finally the else including m.setImageResource(R.drawable.w2);
Second you need is to setOnClickListener to both Button objects because you only set the listener to b1 and not to b2
For what's B?
Why you have an array called a with the drawables if you don't access to it fields? You can access to drawables using a[index] and increasing or reducing the index number from 0 to 3 or from 3 to 0 when you click b1 or b2. It will be better...

android Clear Button in a simple counter

I have a counter program for number. I define 3 buttons for Plus, Minus and Clear.
When I use clear button for clear TextView it's good. But after using Plus and Minus it it is Continuation last counter. that is my code.
please Help me.
public class CounterActivity extends Activity {
private Button btnPlus;
private Button btnMinus;
private Button btnClear;
private TextView txtCounter;
int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counter_menu);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnClear = (Button) findViewById(R.id.btnClear);
txtCounter = (TextView) findViewById(R.id.txtCounter);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter++;
txtCounter.setText(counter + "");
}
});
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter--;
txtCounter.setText(counter + "");
}
});
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtCounter.setText(0 + "");
}
});
}
}
Change your clear button onClick method as follows:
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
counter = 0;
txtCounter.setText(0 + "");
}
});

How to set 4 button on one Listener?

This is simple method but i don't know the code so i mean is like this
button1 = (Button)findViewById(R.id.b1);
button2 = (Button)findViewById(R.id.b2);
button3 = (Button)findViewById(R.id.b3);
button4 = (Button)findViewById(R.id.b4);
//button1-button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View Button1-4){
{
//Do same method for button1-4
}
}
});
button5 = (Button)findViewById(R.id.b5);
button6 = (Button)findViewById(R.id.b6);
button7 = (Button)findViewById(R.id.b7);
button8 = (Button)findViewById(R.id.b8);
//button5-button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View Button5-8){
{
//Do same method for button5-8
}
}
});
button9 = (Button)findViewById(R.id.b9);
button10 = (Button)findViewById(R.id.b10);
button11 = (Button)findViewById(R.id.b11);
button12 = (Button)findViewById(R.id.b12);
//button5-button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View Button9-12){
{
//Do same method for button9-12
}
}
});
so i have 12 button on my project that i put in 3 group(layout), each group have 4 button inside which i mean the 4 button in each group do same thing.
Can anyone help me to give some sample code? Thank's
This is one template. Hope this help.
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff; You can called your method from here also.
break;
case R.id.button2:
// do stuff;
break;
...
}
}
You can also set it in your layout xml using the android:onclick attribute.
android:onClick="onClick"
Then in your activity class add the onClick method.
public void onClick(View v) {
//Write your code here
if(v.getId==R.id.button1 || v.getId==R.id.button2 || v.getId==R.id.button3 ||v.getId==R.id.button4){
// Do stuff
}
}
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button10.setOnClickListener(this);
button11.setOnClickListener(this);
button12.setOnClickListener(this);
#Override
public void onClick(View v) {
if(v.getID==R.id.button1 || v.getID==R.id.button2 || v.getID==R.id.button3|| v.getID==R.id.button4){
// fill
}else if(v.getID==R.id.button5 || v.getID==R.id.button6 || v.getID==R.id.button7|| v.getID==R.id.button8){
// fill
}else if(v.getID==R.id.button9 || v.getID==R.id.button10 || v.getID==R.id.button11|| v.getID==R.id.button12){
// sadsda;
}
}
try this
try this I hope your problem is solve..
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3,button4;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId==R.id.button1 || v.getId==R.id.button2 || v.getId==R.id.button3 ||v.getId==R.id.button4){
// Do somthin what you want
}
}
}
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff; You can called your method from here also.
break;
case R.id.button2:
// do stuff;
break;
...
}
}
this itself should do the trick

Categories

Resources