I am making a calculator and below is my code. I was wondering is there any way to shorten the code, I have 18 buttons and I have to write 50 lines of code just to take reference from XML and add click listener to it
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String SelectedOpertator;
int num1, num2, result;
EditText input;
Button b1, b2, b3, b4, b5, b6 ,b7, b8, b9, b0, bdot;
Button bc, bs, bd, bp, bmi, bm, be;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
b7 = (Button) findViewById(R.id.b7);
b8 = (Button) findViewById(R.id.b8);
b9 = (Button) findViewById(R.id.b9);
b0 = (Button) findViewById(R.id.b0);
bdot = (Button) findViewById(R.id.bdot);
bc = (Button) findViewById(R.id.bc);
bs = (Button) findViewById(R.id.bs);
bd = (Button) findViewById(R.id.bd);
bp = (Button) findViewById(R.id.bp);
bmi = (Button) findViewById(R.id.bmi);
bm = (Button) findViewById(R.id.bm);
be = (Button) findViewById(R.id.be);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b0.setOnClickListener(this);
bdot.setOnClickListener(this);
bc.setOnClickListener(this);
bs.setOnClickListener(this);
bd.setOnClickListener(this);
bp.setOnClickListener(this);
bmi.setOnClickListener(this);
bm.setOnClickListener(this);
be.setOnClickListener(this);
}
Is there any other way to write this code in shorter way?
If you add a method to do the findViewById and setOnClickListener, you can reduce the lines for each button from two to one:
private Button findAndSetClickListener(int id) {
Button button = (Button) findViewById(id);
button.setOnClickListener(this);
return button;
}
Then:
b1 = findAndSetClickListener(R.id.b1);
// etc.
int ids[] = new int[] {R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7, R.id.b8, R.id.b9, R.id.b0}
for(int i = 0; i < ids.length, i += 1){
findViewById(ids[i]).setOnClickListener(this);
}
There's no need for storing reference to each button as class members. In the onClick listener we can determine which button was clicked.
public onClick(View v){
int number = Arrays.asList(ids).indexOf(v.getId()) + 1;
// Button 'number' was clicked
}
try this type code.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String SelectedOpertator;
int num1, num2, result;
EditText input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
((Button) findViewById(R.id.b1)).setOnClickListener(this);
((Button) findViewById(R.id.b2)).setOnClickListener(this);
((Button) findViewById(R.id.b3)).setOnClickListener(this);
((Button) findViewById(R.id.b4)).setOnClickListener(this);
((Button) findViewById(R.id.b5)).setOnClickListener(this);
((Button) findViewById(R.id.b6)).setOnClickListener(this);
((Button) findViewById(R.id.b7)).setOnClickListener(this);
((Button) findViewById(R.id.b8)).setOnClickListener(this);
((Button) findViewById(R.id.b9)).setOnClickListener(this);
((Button) findViewById(R.id.b0)).setOnClickListener(this);
((Button) findViewById(R.id.bdot)).setOnClickListener(this);
((Button) findViewById(R.id.bc)).setOnClickListener(this);
((Button) findViewById(R.id.bs)).setOnClickListener(this);
((Button) findViewById(R.id.bp)).setOnClickListener(this);
((Button) findViewById(R.id.bmi)).setOnClickListener(this);
((Button) findViewById(R.id.bm)).setOnClickListener(this);
((Button) findViewById(R.id.be)).setOnClickListener(this);
}
You could write an array where you store the views.
So that you can do something like
for (View v : array.getView()) {
v.setOnClickListener(this)
}
I think the actual findViewById would be a little more complicated to simplify
You could shorten it at about 50% (and make it a lot more readable) with a private method:
private Button getButtonWithListener(int id) {
Button btn = (Button) findViewById(id);
btn.setOnClickListener(this);
return btn;
}
and call this at every button: (saves you the setOnClickListener)
b1 = getButtonWithListener(R.id.b1);
b2 = getButtonWithListener(R.id.b2);
b3 = getButtonWithListener(R.id.b3);
b4 = getButtonWithListener(R.id.b4);
b5 = getButtonWithListener(R.id.b5);
b6 = getButtonWithListener(R.id.b6);
b7 = getButtonWithListener(R.id.b7);
b8 = getButtonWithListener(R.id.b8);
b9 = getButtonWithListener(R.id.b9);
b0 = getButtonWithListener(R.id.b0);
bdot = getButtonWithListener(R.id.bdot);
bc = getButtonWithListener(R.id.bc);
bs = getButtonWithListener(R.id.bs);
bd = getButtonWithListener(R.id.bd);
bp = getButtonWithListener(R.id.bp);
bmi = getButtonWithListener(R.id.bmi);
bm = getButtonWithListener(R.id.bm);
be = getButtonWithListener(R.id.be);
This way you're not losing the reference to the button. If you need those references to the buttons it won't become much shorter. If you don't need them, then there are some excellent possibilities in the other answers here!
If you need to shorten the button code, Use View's in OnClick() with help of switch statement.
In your XML Layout:
Create the number of buttons that you want with different button id. But use same method name for all button's onClick attribute. Eg: android:onClick="submitBTN" used for all buttons.
In your MainActivity:
Implement that method name to perform different operations using switch statement
public void submitBTN(View view) {
switch (view.getId()) {
case R.id.btnClick1: // Code of button 1 Click
break;
case R.id.btnClick2: // Code of button 2 Click
break;
case R.id.btnClick3: // Code of button 3 Click
break;
case R.id.btnClickn: // Code of Button n Click
}
}
Try using butter knife library. You can use syntax like:
#Onclick({R.id.b1, R.id.b2...})
public void handleClick(Button btn){
// handle click event here
}
Related
I am completely lost and don't have enough knowledge on coding/android studio.
I have the EditTexts (FoodIncomeCounter, FoodCampX, and FoodUpgradeX) as inputs.
Then the TextView (FoodIncomeResult) as output.
The 1st button (IncomeSubmitButton) Works properly. The 2nd button (FoodCampSubmitButton) does not, I want it to take the input of the FoodCampXs and FoodUpgradeXs then do a calculation and put that into integer TotalFood, then output TotalFood to the FoodIncomeResult TextView.
How the heck do I do this? I feel like I am close but I don't know what is wrong.
Thank you for the help!!!
public class MainActivity extends AppCompatActivity {
// These are the global variables
EditText FoodIncomeCounter;
EditText FoodCamp1Counter, FoodCamp2Counter, FoodCamp3Counter, FoodUpgrade1Counter, FoodUpgrade2Counter, FoodUpgrade3Counter;
TextView FoodIncomeResult;
int FoodIncome;
int FoodCamp1, FoodCamp2, FoodCamp3, FoodUpgrade1, FoodUpgrade2, FoodUpgrade3;
int TotalFood;
Button IncomeSubmitButton, FoodCampSubmitButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//to get from user input and into variable form
FoodIncomeCounter = (EditText) findViewById(R.id.FoodIncomeCounter);
FoodIncomeResult = (TextView) findViewById(R.id.FoodIncomeResult);
IncomeSubmitButton = (Button) findViewById(R.id.IncomeSubmitButton);
FoodCamp1Counter = (EditText) findViewById(R.id.FoodCamp1Counter);
FoodCamp2Counter = (EditText) findViewById(R.id.FoodCamp2Counter);
FoodCamp3Counter = (EditText) findViewById(R.id.FoodCamp3Counter);
FoodUpgrade1Counter = (EditText) findViewById(R.id.FoodUpgrade1Counter);
FoodUpgrade2Counter = (EditText) findViewById(R.id.FoodUpgrade2Counter);
FoodUpgrade3Counter = (EditText) findViewById(R.id.FoodUpgrade3Counter);
FoodCampSubmitButton = (Button) findViewById(R.id.FoodCampSubmitButton);
//Submit button
IncomeSubmitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//to receive the inputted values
Food = Integer.parseInt(FoodIncomeCounter.getText().toString());
//to show the inputted values into the result fields
FoodIncomeResult.setText(FoodIncomeCounter.getText().toString());
}
});
//Submit button
FoodCampSubmitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//receive the inputted values
FoodCamp1 = Integer.parseInt(FoodCamp1Counter.getText().toString());
FoodCamp2 = Integer.parseInt(FoodCamp2Counter.getText().toString());
FoodCamp3 = Integer.parseInt(FoodCamp3Counter.getText().toString());
FoodUpgrade1 = Integer.parseInt(FoodUpgrade1Counter.getText().toString());
FoodUpgrade2 = Integer.parseInt(FoodUpgrade2Counter.getText().toString());
FoodUpgrade3 = Integer.parseInt(FoodUpgrade3Counter.getText().toString());
//get food income and show
TotalFood = FoodCamp1 + (FoodCamp2 * 2) + (FoodCamp3 * 3) + (FoodUpgrade1 * 2) + (FoodUpgrade2 * 4) + (FoodUpgrade3 * 6);
FoodIncomeResult.setText(String.valueOf(TotalFood));
}
});
}
}
My code works with an if statement that counts until 10 moves are made and then checks who the winner is in getWinner() but, that is an issue when somebody wins before all 10 moves are made.
I originally had just the getWinner(); method with no if statement assuming this would check for a winner every time a button is clicked. This did not give me any errors but it simply did not work properly.
What else can I try? Why does just adding the getWinner(); without the if statement not work?
public class MainActivity extends AppCompatActivity {
public int counter = 1;
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
//This repeats for all buttons, I left it all out for the sake of making the post shorter.
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (button1.getText().toString().equals("")) {
if (counter % 2 == 0) {
button1.setText("O");
} else {
button1.setText("X");
}
counter++;
}
if (counter == 10) {
getWinner();
}
}
});
//add the other buttons here...
public void getWinner() {
String b1, b2, b3, b4, b5, b6, b7, b8, b9;
b1 = button1.getText().toString();
// code here
}
Looks like none of the buttons aside from button1 matters:
b1 = button1.getText().toString();
b2 = button1.getText().toString();
b3 = button1.getText().toString();
b4 = button1.getText().toString();
b5 = button1.getText().toString();
b6 = button1.getText().toString();
b7 = button1.getText().toString();
b8 = button1.getText().toString();
b9 = button1.getText().toString();
Based on your comment: "I originally had just the GetWinner(); method with no if statement assuming this would check for a winner every time a button is clicked. This did not give me any errors but it simply did not work properly."
Assuming the code was not being executed as you expected when you attempted the above. I would suggest making a callback to GetWinner() every time the button is clicked.
With all the Strings getting their value from the first button did it work if you clicked the first button?
Hey everyone :) Ive been scratching my head on this one, and can't seem to find an appropriate answer that works even though I'm sure its a simple issue.
I have a program which generates a random letter text on a group of buttons when another button being pressed.
When I open the application it loads fine and the first time the button is pressed it generates correctly, but after that I can't seem to get it to regenerate random letters.
I can accomplish what I want by adding an Intent, and essentially "refreshing" (not sure of the wording) the main activity but I would like to add a counter for the button clicks, and it resets when the activity does.
{Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
overridePendingTransition(0, 0);
I just cant seem to wrap my mind around how to do it otherwise. It's seems like I need to rerun the java every time the button is clicked. Any suggestions?
Here is some code, I used buttons rather than textview because I a)wanted to set an easy background b)may make them clickable later. Thanks so much in advance!
public class MainActivity extends Activity
{
Button spin;
Button reel1,reel2,reel3,reel4;
private String rnd,rnd2,rnd3,rnd4;
int count1=50;
#Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();}
private static String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
private static String rndm2[] = {"A","B","C","D"};
{rnd2 = rndm2[(int) (Math.random() * rndm2.length)];}
private static String rndm3[] = {"A","B","C","D"};
{rnd3 = rndm3[(int) (Math.random() * rndm3.length)];}
private static String rndm4[] = {"A","B","C","D"};
{rnd4 = rndm4[(int) (Math.random() * rndm4.length)];}
public void perform_action(View v){}
public void addListenerOnButton() {
spin = (Button) findViewById(R.id.spin);
spin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
{Toast.makeText(getApplicationContext(),"button pressed", Toast.LENGTH_SHORT).show();}
{Button tv = (Button) findViewById(R.id.reel1);
tv.setText(String.valueOf(rnd));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel2);
tv.setText(String.valueOf(rnd2));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel3);
tv.setText(String.valueOf(rnd3));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel4);
tv.setText(String.valueOf(rnd4));
tv.setTextColor(Color.parseColor("#000000"));
I figured it out!!! Whoot. But I can't upvote myself for a correct answer.
public class MainActivity extends Activity
{
Button spin;
Button reel1,reel2,reel3,reel4;
private String rnd,rnd2,rnd3,rnd4;
int count1=50;
#Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();}
public void perform_action(View v){}
public void addListenerOnButton() {
spin = (Button) findViewById(R.id.spin);
spin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
String rndm2[] = {"A","B","C","D"};
{rnd2 = rndm2[(int) (Math.random() * rndm2.length)];}
String rndm3[] = {"A","B","C","D"};
{rnd3 = rndm3[(int) (Math.random() * rndm3.length)];}
String rndm4[] = {"A","B","C","D"};
{rnd4 = rndm4[(int) (Math.random() * rndm4.length)];}
{Toast.makeText(getApplicationContext(),"button pressed", Toast.LENGTH_SHORT).show();}
{Button tv = (Button) findViewById(R.id.reel1);
tv.setText(String.valueOf(rnd));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel2);
tv.setText(String.valueOf(rnd2));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel3);
tv.setText(String.valueOf(rnd3));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel4);
tv.setText(String.valueOf(rnd4));
tv.setTextColor(Color.parseColor("#000000"));
I moved my random Strings into my Onclick event and removed the private and static parts. Then whala! Things work like a charm!
i build complicated application on android studio and i have this code which i have to put it in another function upon click it:
EditText e1 = (EditText) findViewById(R.id.Name);
EditText e2 = (EditText) findViewById(R.id.Room);
EditText e3 = (EditText) findViewById(R.id.Date);
EditText e4 = (EditText) findViewById(R.id.Age);
EditText e5 = (EditText) findViewById(R.id.Height);
EditText e6 = (EditText) findViewById(R.id.Weight);
EditText e7 = (EditText) findViewById(R.id.SerumC);
EditText e8 = (EditText) findViewById(R.id.SUN);
EditText e9 = (EditText) findViewById(R.id.ALB);
SResult.setVisibility(View.INVISIBLE);
r11b.setVisibility(View.INVISIBLE);
iup.setVisibility(View.INVISIBLE);
alert.setVisibility(View.INVISIBLE);
rup.setVisibility(View.INVISIBLE);
r11.setVisibility(View.INVISIBLE);
e1.setVisibility(VISIBLE);
e2.setVisibility(VISIBLE);
e3.setVisibility(VISIBLE);
e4.setVisibility(VISIBLE);
e5.setVisibility(VISIBLE);
e6.setVisibility(VISIBLE);
e7.setVisibility(VISIBLE);
e8.setVisibility(VISIBLE);
e9.setVisibility(VISIBLE);
Male.setVisibility(VISIBLE);
Female.setVisibility(VISIBLE);
Black.setVisibility(VISIBLE);
NonBlack.setVisibility(VISIBLE);
Stable.setVisibility(VISIBLE);
NonStable.setVisibility(VISIBLE);
Scrlabel.setVisibility(VISIBLE);
clearH.setVisibility(VISIBLE);
clearHN.setVisibility(VISIBLE);
calcH.setVisibility(VISIBLE);
calcHN.setVisibility(VISIBLE);
menuH.setVisibility(VISIBLE);
menuHN.setVisibility(VISIBLE);
equatlab.setVisibility(VISIBLE);
r12equ.setVisibility(VISIBLE);
printC.setVisibility(View.INVISIBLE);
printCH.setVisibility(View.INVISIBLE);
homeC.setVisibility(View.INVISIBLE);
homeCH.setVisibility(View.INVISIBLE);
returnC.setVisibility(View.INVISIBLE);
returnCH.setVisibility(View.INVISIBLE);
par.setVisibility(VISIBLE);
but as you see it's long one and with time i will face a hard problem to make any change in my app, so i need to store this in a custom function and upon click the button the action of this function start like this:
public class visistore {
EditText e1 = (EditText) findViewById(R.id.Name);
EditText e2 = (EditText) findViewById(R.id.Room);
EditText e3 = (EditText) findViewById(R.id.Date);
EditText e4 = (EditText) findViewById(R.id.Age);
EditText e5 = (EditText) findViewById(R.id.Height);
EditText e6 = (EditText) findViewById(R.id.Weight);
EditText e7 = (EditText) findViewById(R.id.SerumC);
EditText e8 = (EditText) findViewById(R.id.SUN);
EditText e9 = (EditText) findViewById(R.id.ALB);
SResult.setVisibility(View.INVISIBLE);
r11b.setVisibility(View.INVISIBLE);
iup.setVisibility(View.INVISIBLE);
alert.setVisibility(View.INVISIBLE);
rup.setVisibility(View.INVISIBLE);
r11.setVisibility(View.INVISIBLE);
e1.setVisibility(VISIBLE);
e2.setVisibility(VISIBLE);
e3.setVisibility(VISIBLE);
e4.setVisibility(VISIBLE);
e5.setVisibility(VISIBLE);
e6.setVisibility(VISIBLE);
e7.setVisibility(VISIBLE);
e8.setVisibility(VISIBLE);
e9.setVisibility(VISIBLE);
Male.setVisibility(VISIBLE);
Female.setVisibility(VISIBLE);
Black.setVisibility(VISIBLE);
NonBlack.setVisibility(VISIBLE);
Stable.setVisibility(VISIBLE);
NonStable.setVisibility(VISIBLE);
Scrlabel.setVisibility(VISIBLE);
clearH.setVisibility(VISIBLE);
clearHN.setVisibility(VISIBLE);
calcH.setVisibility(VISIBLE);
calcHN.setVisibility(VISIBLE);
menuH.setVisibility(VISIBLE);
menuHN.setVisibility(VISIBLE);
equatlab.setVisibility(VISIBLE);
r12equ.setVisibility(VISIBLE);
printC.setVisibility(View.INVISIBLE);
printCH.setVisibility(View.INVISIBLE);
homeC.setVisibility(View.INVISIBLE);
homeCH.setVisibility(View.INVISIBLE);
returnC.setVisibility(View.INVISIBLE);
returnCH.setVisibility(View.INVISIBLE);
par.setVisibility(VISIBLE);
}
then, in my main activity :
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
return true;
}
if (event.getAction()== MotionEvent.ACTION_DOWN)
{
visistore
}
return true;
}
});
Is this right?! or if it's wrong so i need to correct me
If i understood your problem correctly then , it is simple as Just define a boolean isButtonClicked
make this isButtonClicked = true onClick event
then put if(isButtonClicked) condition in your touchClick event.
Hope you got it
You don't want to set an OnTouchListener but an OnClickListener. The in the onClick() method of the OnClickListener call the appropriate function.
I have some buttons and I have some text set ot them and I need that text to be visible when activity starts. After that I set some text to all my buttons from sqlite database, but I don't want that text to be visible until user clicks of a button.
The simplest solution would be to use button.setText(c.getString(int)); and set that text from db when a button is clicked, but my c cursor is not in activity scope so I can't use it in my onClick method of my buttons. I tried to place everything inside of my nextQuestion() method in activity scope, but that gives me errors.
So I was thinking I should hide all text set from db until a button is pressed, that unhide it. How to do that? Or if first idea is possible somehow it's even better.
Here's my game class:
public class Game extends Activity implements View.OnClickListener{
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
MediaPlayer buttonClicks;
public boolean music;
LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();
private String generateWhereClause(){
StringBuilder result = new StringBuilder();
for (Long l : mAnsweredQuestions){
result.append(" AND _ID <> " + l);
}
return result.toString();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
addListenerOnButton();
nextQuestion();
}
private void addListenerOnButton() {
buttonClicks = MediaPlayer.create(this, R.raw.button);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b7 = (Button) findViewById(R.id.button7);
b8 = (Button) findViewById(R.id.button8);
b9 = (Button) findViewById(R.id.button9);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
}
private void nextQuestion() {
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestData(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
b1.setText(c.getString(2));
b2.setText(c.getString(3));
b3.setText(c.getString(4));
b4.setText(c.getString(5));
b5.setText(c.getString(6));
b6.setText(c.getString(7));
b7.setText(c.getString(8));
b8.setText(c.getString(9));
b9.setText(c.getString(10));
}
finally{
mDbHelper.close();
}
}
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
if(music == true){
buttonClicks.start();
}
b1.setText(c.getString(2));// this is a good option, but can't use cursor
b2.setEnabled(true);
break;
case R.id.button2:
if(music == true){
buttonClicks.start();
}
b3.setEnabled(true);
break;
case R.id.button3:
if(music == true){
buttonClicks.start();
}
break;
case R.id.button4:
if(music == true){
buttonClicks.start();
}
}
}
}
There is no builtin method to hide only the text on a button, but you could simply store the values from the database in a bunch of variables and not set those against the buttons until they're pressed.
That means you should change:
b1.setText(c.getString(2));
b2.setText(c.getString(3));
...
b9.setText(c.getString(10));
To something like:
String mButtonText1, mButtonText2, ... mButtonText9; // <-- class members
mButtonText1 = c.getString(2);
mButtonText2 = c.getString(3);
...
mButtonText9 = c.getString(10);
(alternatively, you could put these values in an array)
And then in onClick():
b1.setText(mButtonText1);
To 'hide' the text on the button, simply call setText() with "" or null.
You may be able to achieve the same effect by setting the button's text color to 'transparent', but I haven't tried that.
How about passing the String-s to some intermediate storage in some variables and then, at appropriate time, setting the Buttons' texts from those strings?
You're trying to find a solution for a symptom of a larger, underlying problem. If you're having difficulties sharing data between activities, you certainly shouldn't be heading in this direction.
That being said, to answer your specific question, you could set textColor to android.R.color.transparent.
I think the best way is:
Setting text null or empty and in your java implementation, you set text.
Like this:
teste.xml:
<Button android:id="#+button/testebutton" android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
Teste.java:
Button testeButton = (Button) findViewById(R.button.testebutton);
testeButton.setText("NEW TITLE");
How about making the cursor an instance variable (of the class) instead of method-local variable? And maybe using http://en.wikipedia.org/wiki/Lazy_initialization ...