Color combination background change android studio kotlin - android

I'm a beginner and I want to make an app that combine basic colors depending on user input. For example input 1 = red, and input 2 = yellow, click combine button then the background will change to color orange. (using kotlin)
I have no idea how to do it but here is my progress
package com.example.calculator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var b1=findViewById(R.id.combine) as Button
var e1=findViewById(R.id.color1enter) as EditText
var e2=findViewById(R.id.color2enter) as EditText
b1.setOnClickListener{
if e1 = "red" and e2 = "yellow"{
make background orange
}
}
}
}
input 1 = red
input 2 = yellow
background changes to color orange

If you are wanting to change the background of the app, once you define the colors then you can call the layout background change in the button click.
Example
// set button 1 with its id
button1 = findViewById(R.id.btVar1);
// set button 2 with its id
button2 = findViewById(R.id.btVar2);
// set relative layout with its id
relativeLayout = findViewById(R.id.rlVar1);
// onClick function for button 1
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// set the color to relative layout
relativeLayout.setBackgroundResource(R.color.cool);
}
});
// onClick function for button 2
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// set the color to relative layout
relativeLayout.setBackgroundResource(R.color.warm);
}
});
This is based off two buttons as an example. If you want all of this on one button then you can use if statement in the click event of one button to change the colors on the background.
There is several ways you can achieve this. But this is a great example. To combine the colors you will have set the palette color as well

Related

How to iterate over buttons in Android / Java mobile application to change their text

I'm trying to create a tic tac toe game for practice using simple buttons that change their text to x or o. Once a winner is found, all buttons should be reset to have the text "..." instead of x/o. It feels stupid to be doing this for each button but I can't figure out how to iterate over them. The buttons are named button1, button2, ..., and button9. Right now I'm using the same 3 lines of code for each button and just repeating that and that's not very DRY-friendly.
Tried to do a for loop and string concatenation (sort of like: findViewById(R.id."button"+i), but that obviously doesn't work hahah). I'm sure this is a stupid question but I've had this problem a couple of times now and I really wanna figure out how to so this better. [Image of the App][1]
if(winnerFound){
// print winner on screen and reset game
resetGame();
}
}
public void resetGame(){
// set all buttons clickable again and set text to "..."
// could be done in a for loop but idk how
Button button1 = (Button) findViewById(R.id.button1);
button1.setText("...");
button1.setEnabled(true);
Button button2 = (Button) findViewById(R.id.button2);
button2.setText("...");
button2.setEnabled(true);
Button button3 = (Button) findViewById(R.id.button3);
button3.setText("...");
button3.setEnabled(true);
Button button4 = (Button) findViewById(R.id.button4);
button4.setText("...");
button4.setEnabled(true);
// ... (9 buttons in total)'''
[1]: https://i.stack.imgur.com/1ocbZ.jpg
Simple solution based on your code:
Create a member variable holding the list of your buttons List<Button> and iterate them setting the text to empty.
class MyActivity: Activity {
private val buttonList: MuttableList<Button>
private val button1: Button
private val button2: Button
fun onCreate(...) {
button1 = findViewById(R.id.button_01)
button2 = findViewById(R.id.button_02)
buttonList.add(button1)
buttonList.add(button2)
....
}
}
More consideration about your code:
You shouldn't use findViewById every time you want to use a button referente. It should be ideally only used once in your onCreate method of your activity(onViewCreated if it's a fragment) and set to a member variable .
Example
class MyActivity: Activity {
private val button1: Button
fun onCreate(...) {
button1 = findViewById(R.id.button_01)
}
}

How to get color from buttons?

I need to find the red buttons on Activity and set orange background color to these buttons.
When I click on the button I set it to red:
view.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
When I click on another button, the red buttons should turn orange.
public void Active(View view){
for (View but : buttons) {
but.setClickable(true);
but.setBackgroundTintList();
}
}
I do not know how to get the id of the colors
For me is unclear your question but I'll try to answer it.
Supposing buttons is a List<Button>, so what you can do is.
for(View but : buttons){
int color = ((ColorDrawable)but.getBackground()).getColor();
if(color == Color.Red){
//This button is red change it to orange
but.setBackgroundColor(R.colors.orange);
}
}
And when you are clicking the button use
button.setBackgroundResource(Color.Red);
but.setBackgroundColor(Color.RED);
use that

how to prevent a user from clicking on a button after another button has been clicked?

I am new to android programming and am building a quiz app in which a question has 4 options and if the user clicks on one of the options the other options should be unclickable. I am currently able to only make a single button unclickable. Here is the java code.
package com.example.android.quiz;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//This method is called when option 1 of question 1 is selected
public void verifyQuestion1Option1(View view) {
Button QuestionOption1 = (Button) findViewById(R.id.question1_option1);
QuestionOption1.setBackgroundColor(getResources().getColor(R.color.solid_red));
question1Answer();
}
public void verifyQuestion1Option2(View view) {
Button Question1Option2 = (Button) findViewById(R.id.question1_option2);
Question1Option2.setBackgroundColor(getResources().getColor(R.color.solid_red));//solid red is not a predefined colour. It is declared in colors.xml
question1Answer();
}
public void verifyQuestion1Option3(View view) {
Button Question1Option3 = (Button) findViewById(R.id.question1_option3);
Question1Option3.setBackgroundColor(getResources().getColor(R.color.solid_green));
question1Answer();
}
public void verifyQuestion1Option4(View view) {
Button Question1Option4 = (Button) findViewById(R.id.question1_option4);
Question1Option4.setBackgroundColor(getResources().getColor(R.color.solid_red));//We call the getResources() method because R.colour.solid_red passed the id of the color not the actual colour value.
question1Answer();
}
public void question1Answer() {
TextView q1Answer = (TextView) findViewById(R.id.question1_answer);
String answer = "Rajinish Kumar is the current Chairman of SBI who took over after Arundhati Bhattacharya retired on 6 October.Shikha Sharma is the Managing Director and CEO of Axis Bank and Chanda Kochhar is the managing director and CEO of ICICI Bank";
q1Answer.setText(answer);
}
}
Either you can use a buttongroup which will have only 1 active button at any point of time or else, you need to disable other button programatically.
To disable the button you can use the following code:
Button button = (Button) findViewById(R.id.button);
button.setEnabled(false);
You can use .setEnabled(false); to disable a button. That button will grey out and does not respond to click events any more.
To disable all buttons, get the handle to each button and set them to disabled.
Button Question1Option1 = (Button) findViewById(R.id.question1_option1);
Button Question1Option2 = (Button) findViewById(R.id.question1_option2);
Button Question1Option3 = (Button) findViewById(R.id.question1_option3);
Button Question1Option4 = (Button) findViewById(R.id.question1_option4);
Question1Option1.setEnabled(false);
Question1Option2.setEnabled(false);
Question1Option3.setEnabled(false);
Question1Option4.setEnabled(false);
That way, all buttons of this question become disabled.
You can also come up with a solution where you save that the button is already pressed and ignore further click events. You could introduce some sort of variable bool question1answered = false; that is set to true as soon as the onClick event is fired.
public void verifyQuestion1Option4(View view) {
if (question1Answered == true) {return;}
question1Answered =true;
//Do the rest of your checks here
}
Two tips for for programming Java:
Java (in contrast to i.e C#) used lower letter variables as convention.
Button question1Option1 = (Button) findViewById(R.id.question1_option1); would be a better way.
If you have more questions, it would make sense to put them in some sort of array and reuse the same four buttons multiple times. That would save you much programming overhead and code rewrites if you have to change something. And it keeps the code cleaner.

When button gets clicked, make it dissapear, randomely change bg color and load text from database?

I am busy creating an app. I succeeded in creating the button with a custom font and all. Now what I'd want is that when I click the button, it must disapear, the background color of the view must randomely change and text must be loaded from an database.
How does one go about this?
Matthew
Well, the disappearing can be make like this:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click - Disappear in your case...
v.setVisibility(View.INVISIBLE); //can be View.GONE as well...
}
});
}
}
Then for the background I guess you can create an array with all the colors you want (or something that generates a random HEX code) and then do setBackground(X) where X is the HEX code that you just generated... You need to specify more about the database part though.

extracting color code on button click-android

Is there any way to extract the color code (background color) of a button on clicking it and using it in the curresponding java code?
The purpose is to get same identifier on clicking same colored buttons in different activities.
You can use getCurrentTextColor()
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int color = button.getCurrentTextColor();
}
});

Categories

Resources