Lots of if statements - android

I want to make some kinda dictionary,But not exactly a dictionary,
I have two text views, One for the word, The other one for the meaning.
I have two Buttons which they change an integer's number
And finally I have lots of if, For Example:
(if i==1){
txt1.setText("x");
txt2.setText("y");
}
(if i==2){
txt1.setText("n");
txt2.setText("m");
}
Q1: Its better with switch right?
Q2: Where should I put those Switch? In each Button's ClickListener ?
Q3: Can you suggest a better way to do that?

Switch is definitely a better option.
You can have a common API that will handle this logic like:
toggleText(int i){
switch(i){
case 1:
txt1.setText("x");
txt2.setText("y");
break;
case 2:
txt1.setText("n");
txt2.setText("m");
break;
}
}
Then u can call this API from ur button cick:
button.setOnClickListener(new View.OnClickListener{
#Override
onClick(){
toggleText(pass your value here)
}
});
Let me know if u need any help.

Switch is good option than if else
Make a function and put all switch conditions in it and call that function on button click

Related

View objects id

I have several objects in an activity and I would like to check their id before I perform any actions on them. However I'm not sure how to go about it, I tried :
if (v.getId().contains("empty")){
but that gives an error and then I tried
if (v.getId() == "[attr^=empty]"){
and that is also wrong. To be honest I am aware that both of these don't use proper assignments and so on but I'm really lost on how to check whether an id contains a certain substring.... I would appreciate it if someone could help
PS: v is a View object
v.getId() returns an integer. In order to check id before doing something, I'd recommend something like:
switch (v.getId()){
case R.id.ViewElement1:
//do something here
break;
case R.id.ViewElement2:
//do something here
break;
case R.id.ViewElement3:
//do something here
break;
}
What this code does is that it gets the Id of your View then compares it through a switch-case block. If the id taken from v.getId(), the code in the case block executes. The break; is important, otherwise, the code in the cases below the one executed will also run. You can also do this for an if-else if that fits your style more.
If you want to fetch the id as a string, you can try:
String ResourceIdAsString = v.getResources().getResourceName(v.getId());
and then you can do your if contains operations on the String object.
I am not entirely sure what you are trying to achieve. So there's some overview of how id works.
First you have to id all of your views in xml like:
<TextView
android:height="wrap_content"
android:weight="match_parent"
android:id="#+/myTextView" />
Then in your JAVA code use a switch case like:
switch(view.getId()){
case R.id.myTextView1:
break;
}
If you have some views that do same thing you can use them like:
switch(view.getId()){
case R.id.myTextView1:
case R.id.myTextView2:
case R.id.myTextView3:
case R.id.myTextView4:
perfomeSomeAction();
break;
}

Multiple views sharing same OnClickListener -- listen one at a time?

I'm working on a tile memory game.
A user can press any one of the tiles on a 9x9 board.
However, I want to limit the user to being only to press one at a time.
I've tried researching using a TableLayout, setting beforeAscendants=true, and having that try to manage where a user's click goes, but that proves tedious and saving all those coordinates seems inefficient to the possibility of an easier solution existing.
Right now, I call .setOnClickListener on each of those tiles and setting them to the same onClickListener, but there's no way to limit how many tiles can be pressed at once. I've tried A) setting a synchronized() section around the code inside the anonymous onClickListener code, B) having a boolean to set to true if one tile is already in the onClickListener code but it's simply ignored and C) banging my head against the wall, didn't help
Thanks!
This is the most straightforward and simplest way I can come out with. You should optimize the logic too, it's pretty lengthy here.
onClick(View v) {
switch (v.getId()) {
case R.id.tile1:
// Tile 1 is clicked
// Disable other tiles (try setClickable(false))
break;
...
default:
break;
}
}

How to change button background when button state is changed

Wondering how can I change my button background programmatically by setting onClickListener.
I mean that when I firstly pressed my button it changes its background image and save it even if I release finger from it. And then if i press it the second time it must change background image again. I know that I must check what background is there at the moment but can't understand how to do it.
I've tried use getBackground method but it wasn't helpful for me. I even tried to create an XML file with selector which contains three state of my button, but it worked only until the moment I release finger from button.
You could have a global variable storing the background int:
private int backgroundNumber = 0;
Then, in onClick() you could do something like this:
backgroundNumber++;
switch (backgroundNumber % numberOfBackgrounds) { // numberOfBackgrounds is a constant of how many backgrounds there are
case 1:
button.setBackgroundResource(R.drawable.background1);
break;
// Do cases for all the backgrounds
}
I think that should work.
Try like this.
You know how many states you have. Use an int variable (lest say buttonState) to save button state (ex. states 1,2,3. MAX_STATE = 3).
On click just change state and replace background depending on the current buttonState variable value.
#Click(R.id.button_action)
void onButtonActionClicked() {
buttonState = ++buttonState % BTN_STATE_MAX;
switch (buttonState){
case BTN_SAVE:
button.setBackgroundResource(R.drawable.button_save);
break;
case BTN_LOAD:
button.setBackgroundResource(R.drawable.button_load);
break;
case BTN_DELETE:
button.setBackgroundResource(R.drawable.button_delete);
break;
}
}

How to use a simple code determine actions for an array of square images

I'm starting to learn about Android application development and so I'm working on some small apps to exercise programming and better understand some stuff.
The situation is as follows:
I have 25 square images as a 5x5 array, and I want to specify a different action for clicking on each one.
I declared the images IDs as matrix elements:
square11, square12, ... square15
square21, square22, ... square25
... ... ...
square51, square52, ... square55
I tried to do something like this:
public void onClick(View v){
switch(v.getId()){
case R.id.square11:
//do something
break;
case R.id.square12:
//do something
break;
case R.id.square13:
//do something
break;
case R.id.square14:
//do something
break;
//and all other cases...
}
}
But clearly it's not working. Am I missing something about this OnClickListener?
Probably it's a newbie's question, but I hope someone can help. I really looked at other code, but they didn't seem to help.
The thing is, do you know what am I doing wrong? And better, do you know if there is a better and cleaner way to do this?
Thanks a lot!
Best regards,
Rafael
Do you call these views' setOnClickListener method?
I think using GridView and AdapterView.OnItemClickListeneris a better way to do this.

Effectivity, Android switch or click listener for each button

I cant found the topic, perhaps someone can teach me some about android effectivity when the topis is about onclicklistener for buttons.
Lets say I got 10 buttons on a page (just an example now)
What's the best thing to do?
A switch that switch id for the buttons?
A onClickListener for each button?
What method is the faster one, and why?
Is there any different at all?
Best practice is to go with 1st option: A switch that switch id for the butttons.
As per my experience, i would suggest you to assign android:onClick attribute with same value, say for example: android:onClick="btnClicker"
And now you have to implement the same method inside the activity class as:
public void btnClicker(View v)
{
switch(v.getId())
{
case R.id.btn1:
break;
case R.id.btn2:
break;
case R.id.btn3:
break;
}
}
About 2nd option:
I don't prefer it because it increase number of code lines because just think you are having 10 buttons and you assign separate click listener for each buttons. And now compare it with the above 1st option, you will realize it.
So i would suggest you to go with 1st option i have suggested with example above, main reason is it decrease number of code lines and better readability of code.
Why there is better readability in 1st option i have suggested above?
Because you know you just have to check this particular function only for the code for every buttons, because everything is here inside a function.
I would be surprised if there is any significant difference. I would go with #2 because I think it leads to clearer code.
I think answer 1 is effectivity.
because create object will slow down the app.
and the RAM limit in mobile phone should be considered.

Categories

Resources