I managed to change the color of a certain text with a spinner. But now I wanted to change the size of the same text with another spinner. I put the color cases inside a switch function to change the color.
like that
switch (i) {
case 0:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.WHITE).apply();
break;
case 1:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.BLUE).apply();
break;
I wrote all down but I dont know how to call the TextSize inside the case.
I thought it would work like that:
switch (d) {
case 0:
description2.setText(des2[d]);
preferences2.edit().putInt(SELECTED_SIZE, ????); <==
}
}
But I cant use TextSize or something like that.
To change the color I used Color.BLUE/RED/GREEN/... but now I want to change the TextSize... It is as always kind of difficult to explain my problem^^ sry for that.
If you want to use shared preferences and a switch statement like you do for your colors, you can do something similar to the following:
In your switch statement, similar to how you are handling color, add the text size associated with the selected spinner index to your shared preferences:
switch(i) {
case 0:
preferences.edit().putInt(SELECTED_SIZE, 16).apply();
break;
case 1:
preferences.edit().putInt(SELECTED_SIZE, 18).apply();
break;
// other cases go here...
}
Then, in the activity that includes the TextView whose text size you want to change (in this example, named textViewToChange), retrieve the saved value, and use it to set the text size:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
int selectedTextSize = preferences.getInt(SELECTED_SIZE, 0);
textViewToChange.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedTextSize);
Related
So I have a POJO with a question, and in my recycler I wanna show a question itself with answers and correct answer should have the background color.
When I try to add background color, items get colored based on some serious magic.
First few items do not get any color, and closer to the bottom of the recycler here is what happens.
enter image description here
The further the better, when I scroll from the bottom to the top, items at the top get the color as well.
public void bind(Question question) {
int correctAnswerNumber = question.getCorrectAnswer();
switch (correctAnswerNumber) {
case 1:
answer1.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 2:
answer2.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 3:
answer3.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 4:
answer4.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
}
}
The context here is passed from the activity, and I have to ideas what to do.
I tried to remove the logic to the constructor, change the context, address items through holder, didn't change much.
You have a classic case of "stale state from a recycled view item".
Your Problem
You only set the background for a correct answer. When that item view is recycled and used for an incorrect answer, you are not updating it and it keeps the old background that was set on it.
The Solution
Always explicitly set the full state of a recycler item view. In this case, set the background for whatever it should be when it's not a correct answer.
public void bind(Question question) {
int correctAnswerNumber = question.getCorrectAnswer();
// Reset all backgrounds to default before setting the current correct one
answer1.setBackground(getDefaultBackgroundBorder());
answer2.setBackground(getDefaultBackgroundBorder());
answer3.setBackground(getDefaultBackgroundBorder());
answer4.setBackground(getDefaultBackgroundBorder());
switch (correctAnswerNumber) {
case 1:
answer1.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 2:
answer2.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 3:
answer3.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 4:
answer4.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
}
}
I am a basic beginner at android studio and I am developing a weather app. I want to put conditionals for icon images to weather condition.
For example:
sun = sun image
rain = rain image
snow = snow image
I understand a switch case might be useful:
switch (id) {
case "Raining":
icon = "rainIcon";
break;
case "Snowing":
icon = "snowIcon";
break;
case "Drizzling":
icon = "drizzlingIcon";
break;
case "Foggy":
icon = "fogIcon";
break;
case "Thunderstorm":
icon = "thunderIcon;";
break;
case "Sunny":
icon = "sunIcon";
break;
}
However, I am confused how to implement it into my code as I am retrieving my data from my AsyncTask and need to implement the image change in that class but there is no proper method to change my ImageView variable there.
I understand there are ways to implement this on the onCreateView method but this is the way my code is implemented right now
I suggest to create an enum of type Weathers.
public enum Weathers {
RAINING(R.drawable.ic_raining),
SNOWING(R.drawable.ic_snowing);
private int iconResId;
private Weathers(int iconResId) {
this.iconResId = iconResId;
}
public int getIconResId() {
return iconResId;
}
}
This might lessen the conditionals (The switch case above). To display into imageview, get an instance of the Weather instance by using Weathers.valuesOf(String) where String is upper-cased.
Weathers snowing = Weathers.valueOf("Snowing".toUpperCase(Locale.ENGLISH));
imageViewIcon.setImageResource(snowing.getIconResId());
So I've done layouting my android application, the problem now is that, how could I combine the two buttons, like when I click the first block and the 3rd block that will generate a associate "letter" So basically in every combination of buttons there is a corresponding "letter"
Or please do correct me if I'm wrong if using a "button" to implement what I wanted is the right thing to use? If not, please state what's the right way to do to implement what I wanted.
If I haven't explain well, I would state some examples: For e.g clicking the 1st block, 3rd block and the 5th block that generates a letter "z".
Please pardon me if my explanation is confusing. Sorry I am still learning java.
What you can do is create your layout with all the buttons and respective ids and values for example I am giving you one button
<Button
android:id="#+id/btnA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:textSize="16sp" />
just like this create buttons with respective ids and text values...
Now inside your activity setOnClickListener for all buttons.
inside your onCreate()
like this
Button A = (Button)findViewById(R.id.btnA);
Button B = (Button)findViewById(R.id.btnB);
.
.
.
. just like this
then setOnClickListener
A.setOnClickListener(this);
B.setOnClickListener(this);
After this inside your onClick() method just based on the which button clicked append their value to a string. and first declare one global string variable for example String letter;
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnA:
letter += "A";
break;
case R.id.btnB:
letter += "B";
break;
.
.
Hope this will help
Ok so now I am editing my answer...
m just gonna give you small idea depending on that you have to decide
so as per your question what you want is suppose user press btnA and btnB you want to write z on the screen. This is not a simple job you its actually lengthy. First of all you have to decide on what combination you want to generate what just like btnA and btnB you need to generate z
so keep your clicks same as i have given above just inside your onClick function change the code that I am giving you.
so first
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnA:
letter += "A";
checkCombo(letter);
break;
case R.id.btnB:
letter += "B";
checkCombo(letter);
break;
.
.
So now create one function named as checkCombo(String letter) with one string parameter
Now the actual part comes where you have to decide what combinations you want so if you want on A and B clicks to generate Z then create a switch case like below
function checkCombo(String mletter) {
String generatedLetter;
switch(letter) {
case "AB":
generatedLetter = 'Z';// or you can set it to your textview here
letter = ""; // if it goes inside any of your switch case clear your
global variable
break;
case "DEF":
generatedLetter = 'P'
letter = "";
break;
}
}
This is just a lengthy solution but will work
Get the button id on click and do your logic like below
First set onClickListener for all buttons then onClick method do like this
#Override
Public void onClick(View v){
switch(v.getId()) {
case R.id.first_button_id:
case R.id.second_button_id:
case R.id.third_button_id:
String value = "your character";
break;
}
I have a simple calculator app. When the user presses calculate the edit text where they entered their number changes color and outputs the result. When reset is pressed I would like the line under the edit text to re-appear but it doesn't show up the way I current have my code written. This is a snipped of my current code.
#Override
public void onClick(View v){
switch (v.getId()){
case R.id.calculate:
myFunction.doWork();
voltageInputEditText.setBackgroundColor(Color.DKGRAY);
voltageInputEditText.setTextColor(Color.WHITE);
break;
case R.id.reset:
myFunction.reset();
voltageInputEditText.setText("");
voltageInputEditText.setBackgroundColor(Color.WHITE);
break;
}
}
This is the desired result
This is the current result
This is the look after I run myFunction.(doWork);
When you call setBackgroundColor internally it is calling setBackgroundDrawable with a ColorDrawable, so you're replacing the original background, the one that displays the line inside EditText.
Setting a white background color to begin with would equally remove this line.
It should work if you save the original background drawable via getBackground and later restore it via setBackgroundDrawable.
You should simply save it.
String savedInputText = "";
#Override
public void onClick(View v){
switch (v.getId()){
case R.id.calculate:
myFunction.doWork();
savedInputText = voltageInputEditText.getText();
voltageInputEditText.setBackgroundColor(Color.DKGRAY);
voltageInputEditText.setTextColor(Color.WHITE);
break;
case R.id.reset:
myFunction.reset();
voltageInputEditText.setText(savedInputText);
voltageInputEditText.setBackgroundColor(Color.WHITE);
break;
}
}
I've tried quite a few different tactics to achieve my desired result, but nothing is making these buttons do what they're 'sposed to... Basically I have 14 buttons. Four with the text "X", digitOne, digitTwo, digitThree and digitFour. Then, there are 10 with "1", "2", etc, named "one", "two", etc. All the buttons are tied to the same OnClickListener that will use a switch statement to determine which button was pressed, then find the soonest display button (buttons initially marked "X"), and change that buttons text to the entered digit. What I want to happen is:
Say someone clicks the "5" button. If its the first button pressed, the first "digit" button will change from displaying "X" to "5", and so-on, so-forth. This is not what is happening... In fact, nomatter what I've tried, nothing is happening. Not even an error... An error would be nice, at least I'd know where my logical flaw is -_-. Here's the code:
The button declarations:
one=(Button)findViewById(R.id.button1);
two=(Button)findViewById(R.id.Button2);
three=(Button)findViewById(R.id.Button3);
four=(Button)findViewById(R.id.Button4);
five=(Button)findViewById(R.id.Button5);
six=(Button)findViewById(R.id.Button6);
seven=(Button)findViewById(R.id.Button7);
eight=(Button)findViewById(R.id.Button8);
nine=(Button)findViewById(R.id.Button9);
zero=(Button)findViewById(R.id.Button0);
add=(Button)findViewById(R.id.buttonAdd);
digitOne=(Button)findViewById(R.id.Number1);
digitTwo=(Button)findViewById(R.id.Number2);
digitThree=(Button)findViewById(R.id.Number3);
digitFour=(Button)findViewById(R.id.Number4);
one.setOnClickListener(listener);
two.setOnClickListener(listener);
three.setOnClickListener(listener);
four.setOnClickListener(listener);
five.setOnClickListener(listener);
six.setOnClickListener(listener);
seven.setOnClickListener(listener);
eight.setOnClickListener(listener);
nine.setOnClickListener(listener);
zero.setOnClickListener(listener);
The OnClickListener private inner class (I guess that's what you'd call it. It's inside Activity class):
private OnClickListener listener = new OnClickListener(){
public void onClick(View button) {
switch(button.getId()){
case R.id.Button0:
addANumber(0);
break;
case R.id.button1:
addANumber(1);
break;
case R.id.Button2:
addANumber(2);
break;
case R.id.Button3:
addANumber(3);
break;
case R.id.Button4:
addANumber(4);
break;
case R.id.Button5:
addANumber(5);
break;
case R.id.Button6:
addANumber(6);
break;
case R.id.Button7:
addANumber(7);
break;
case R.id.Button8:
addANumber(8);
break;
case R.id.Button9:
addANumber(9);
break;
}
}
};
And finally, the "addANumber" method being called:
public void addANumber(int i){
if(digitOne.getText()=="X"){
digitOne.setText(i);
}else if(digitTwo.getText()=="X"){
digitTwo.setText(i);
}else if(digitThree.getText()=="X"){
digitThree.setText(i);
}else if(digitFour.getText()=="X"){
digitFour.setText(i);
}
}
I've done this before... I know I'm missing something so blatantly stupid it deserves a smack in the head...
Before all:
digitOne.getText()=="X" should be "X".equals(digitOne.getText())
you need checking for string equality in terms of content, not in term of reference.
Nothing happens because with == none of your if condition is evaluated to true and addANumber() simply results as an empty method