How to get text from button to edit text? - java

this code work very well but now when question is CAR button1 = A button2 = R button3 = C when I click button3 button1 = INVISIBLE edit text = A and when I click button2 edit text = A I want when click on button edit text get text from button and check text in edit text
requires: edit text get char form button, add this code to function , remove last index from array
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
LinearLayout linearlayout;
Button button;
EditText txt;
String Array[]= {"car","blue","red","fox"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout = findViewById(R.id.linearlayout);
txt = findViewById(R.id.txt);
int random = (int) (Math.random() * Array.clone().length);
StringBuilder word = new StringBuilder(Array[random]);
for (int i = 0; i < Array[random].length(); i++) {
char id = 'b';
button = new Button(this);
button.setId(id + i);
linearlayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setVisibility(View.INVISIBLE);
txt.setText(txt.getText().toString() + button.getText());
}
});
}
}
}

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
LinearLayout linearlayout;
//Button button; //TODO: is local var!
EditText txt; //Global var
String Array[]= {"car","blue","red","fox"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout = findViewById(R.id.linearlayout);
txt = findViewById(R.id.txt);
int random = (int) (Math.random() * Array.clone().length);
StringBuilder word = new StringBuilder(Array[random]);
for (int i = 0; i < Array[random].length(); i++) {
char id = 'b';
final Button button = new Button(this); //local var
button.setId(id + i);
linearlayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//the problem was here (var button)
button.setVisibility(View.INVISIBLE);
txt.setText(txt.getText().toString() + button.getText());
}
});
}
}
}

Not sure of the questions, but the one thing I did find to be an issue with the code.
txt.setText(txt.getText().toString() + button.getText());
The bolded part doesn't returns a char sequence. Should be the same thing as the edit text.
button.getText().toString();

Related

Android How to save a String from edittext into an array?

package com.example.addarray;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
int mCounter = 0;
TextView textView;
TextView textView2;
TextView textView3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(view -> {
mCounter++;
textView = findViewById(R.id.textView1);
textView.setText(Integer.toString(mCounter));
textView2 = findViewById(R.id.textView2);
editText = findViewById(R.id.editText);
String [] string = new String[mCounter];
for(int i =mCounter-1; i>mCounter; i++){
string[i] = editText.getText().toString();
}
textView2.setText(Arrays.toString(string));
textView3 = findViewById(R.id.textView3);
textView3.setText(editText.getText().toString());
});
}
}
I would like to add the input from the edit text into an array upon each click of the button, however, the element of the array only displayed null as shown in the picture. Is there any way to fix it? Thank you.
You create a new array with each click. No need to create a new array on every click. It is possible to save new values in ArrayList.
public class MainActivity extends AppCompatActivity {
private ArrayList<String> list = new ArrayList<String>();
// *******
button.setOnClickListener(view -> {
// *******
list.add(editText.getText().toString());
// ******
}
}

Logic is off somewhere, trying to get input from user between 1-100

I am making a calculator and I am stumped on this part. I want to have the value inputted from the user for inputG only to be between numbers 1-100. Then, grab that value and use it in a formula.
My logic is off somewhere and I need help finding what I did wrong. I am using Java, not Kotlin.
package com.example.rvbtcal;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView inputMoles = (EditText) findViewById(R.id.txtMoles);
EditText inputML = (EditText) findViewById(R.id.txtmL);
EditText inputG = (EditText) findViewById(R.id.txtG);
TextView outputAnswer = (TextView) findViewById(R.id.txtAnswer);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Spinner spinner = (Spinner) findViewById(R.id.spnN);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayN, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
double iM = Double.parseDouble(inputMoles.getText().toString());
double iML = Double.parseDouble(inputML.getText().toString());
double ioA = Double.parseDouble(inputG.getText().toString());
double textN = Double.parseDouble(spinner.getSelectedItem().toString());
if (ioA >= 0 || ioA <= 101){
Toast.makeText(MainActivity.this,"Enter a number between 1-100", Toast.LENGTH_SHORT);
}
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double Answer1 = (iM * iML * textN) / (ioA / 1000);
double FinalAnswer=(Answer1*1000000);
outputAnswer.setText(String.format("%.3f",FinalAnswer));
// String FinalAnswer2=String.valueOf(FinalAnswer);
// outputAnswer.setText(FinalAnswer2);
}
});
}
}
I now have my code like this below, however, the calculations are still made even with the If statement. I tried an else statement but then the answer is blank no matter if the numbers for input g fall within the parameters.
package com.example.rvbtcal;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView inputMoles = (EditText) findViewById(R.id.txtMoles);
EditText inputML = (EditText) findViewById(R.id.txtmL);
EditText inputG = (EditText) findViewById(R.id.txtG);
TextView outputAnswer = (TextView) findViewById(R.id.txtAnswer);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Spinner spinner = (Spinner) findViewById(R.id.spnN);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayN, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double iM = Double.parseDouble(inputMoles.getText().toString());
double iML = Double.parseDouble(inputML.getText().toString());
double ioA = Double.parseDouble(inputG.getText().toString());
double textN = Double.parseDouble(spinner.getSelectedItem().toString());
if (ioA < 0 || ioA > 100) {
Toast.makeText(MainActivity.this, "Enter a number between 1-100", Toast.LENGTH_SHORT);
}
double Answer1 = (iM * iML * textN) / (ioA / 1000);
double FinalAnswer = (Answer1 * 1000000);
outputAnswer.setText(String.format("%.3f", FinalAnswer));
}
});
}
}
My final code for others who may struggle with this in the future here is the finished product (for now):
package com.example.rvbtcal;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView inputMoles = (EditText) findViewById(R.id.txtMoles);
EditText inputML = (EditText) findViewById(R.id.txtmL);
EditText inputG = (EditText) findViewById(R.id.txtG);
TextView outputAnswer = (TextView) findViewById(R.id.txtAnswer);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Spinner spinner = (Spinner) findViewById(R.id.spnN);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayN, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double iM = Double.parseDouble(inputMoles.getText().toString());
double iML = Double.parseDouble(inputML.getText().toString());
double ioA = Double.parseDouble(inputG.getText().toString());
double textN = Double.parseDouble(spinner.getSelectedItem().toString());
if (ioA <= 0 || ioA > 100) {
Toast.makeText(MainActivity.this, "Enter a number between 1-100", Toast.LENGTH_LONG).show();
outputAnswer.setText("");
return;
}
double Answer1 = (iM * iML * textN) / (ioA / 1000);
double FinalAnswer = (Answer1 * 1000000);
outputAnswer.setText(String.format("%.3f",FinalAnswer));
}
});
}
}
You need to move the following lines into the btnCalculate.setOnClickListener,
double iM = Double.parseDouble(inputMoles.getText().toString());
double iML = Double.parseDouble(inputML.getText().toString());
double ioA = Double.parseDouble(inputG.getText().toString());
double textN = Double.parseDouble(spinner.getSelectedItem().toString());
if (ioA < 1 || ioA > 100) {
Toast.makeText(MainActivity.this,"Enter a number between 1-100", Toast.LENGTH_SHORT).show();
return;
}
You need to get the user input after the user taps on the btnCalculate because that's where you can get the latest input of the user.
What you are doing is fetching the value as soon as the EditText view is being created. Hence it returns an empty string. And when you try to pass an empty string to double it should give you an Exception. Unless you have assigned a text value to the EditText in your XML.

EditText all possible number in random output in Android

I'm new to Android. I'm developing an app which gives all the possible combination number based on the user input and separated it by comma.
For example the user input is: 1234
The output should be list all the possible number combination from "1234" like
"1234,1243,1324,1342,1423,1432,2134,etc.."
And there's also another EditText that limit the output by setting it to 1 or 2 or 3 or 4(max).
For example the user input is: 2
The output should be list all the possible number combination from "1234" by "2" digits
"12,13,14,21,23,24,31,32,34,41,42,43"
This is my MainActivity.java
package com.example.androidtest1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
EditText et_input1, et_output1;
Button b_generate, b_clear;
CheckBox checkBox1;
Random r;
int min,output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText mEdit = (EditText) findViewById(R.id.et_output1);
mEdit.setEnabled(false);
r = new Random();
et_input1 = (EditText) findViewById(R.id.et_input1);
et_output1 = (EditText) findViewById(R.id.et_output1);
b_generate = (Button) findViewById(R.id.b_generate);
b_clear = (Button) findViewById(R.id.b_clear);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
b_generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (et_input1.length()==0){
et_input1.setError("Insert numbers");
}else if(checkBox1.isChecked()){
min = Integer.parseInt(et_input1.getText().toString());
output = (min);
et_output1.setText("" + output + "*");
et_input1.setError(null);
}
else{
et_input1.setError("Check the checkbox");
}
}
});
b_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et_output1.getText().clear();
et_input1.getText().clear();
return;
}
});
}
}

How to set a RelativeLayout background color?

I am trying to set a RelativeLayout backgroundColor and I get cannot resolve symbol
here is my code
package com.example.butka.clickme;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
//set layout
super.onCreate(savedInstanceState);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setBackgroundColor(Color.BLACK);
//LayoutParameters
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.CENTER_VERTICAL);
//button
Button btn = new Button(this);
btn.setText("Click me");
btn.setBackgroundColor(Color.WHITE);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
colors();
}
});
//add stuff
layout1.addView(btn, params);
setContentView(layout1);
}
//void on button click
private void colors()
{
Random random = new Random();
short num1 = (short)random.nextInt(9);
if(num1 == 0)
{
layout1.setBackgroundColor(Color.BLACK);
}
}
}
everything runs good, until the color void. the error is cannot resolve symbol But the interesting thing is that I can set the color using layout.setBackgroundColor() before the void.
So the question is, how do you set a layout backgroudColor?
Use this:
layout1.setBackgroundColor(ContextCompat.getColor(this, R.color.black));
or
layout1.setBackgroundColor(Color.parseColor("#000000"));
Your RelativeLayout is in the onCreate() method scope, you must move it to class scope. Like this:
public class MainActivity extends AppCompatActivity {
RelativeLayout layout1; // Make it class scope.
#Override
protected void onCreate(Bundle savedInstanceState) {
//set layout
super.onCreate(savedInstanceState);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setBackgroundColor(Color.BLACK);
...
}
// Then you can access it from other method:
private void colors() {
Random random = new Random();
short num1 = (short)random.nextInt(9);
if(num1 == 0) {
layout1.setBackgroundColor(Color.BLACK); // You can access it now.
}
}

mapping text to buttons from within a method

I'm in the very early stages of developing this android app, and I'm trying to setText to buttons from within a method. Doing it not in a method is fine, but I want to call the method over and over. However when I try to create the method to do this, I get a "Non-static method findViewById(int) cannot be referenced from a static context" error.
eg: Button button = (Button) findViewById(R.id.button);
button.setText(options[0]);
The findViewById will be red highlighted
package com.example.lp1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private Button button;
private Button button2;
private Button button3;
private Button button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int score = 0;
int count = 3;
static int getQuestion(){
ArrayList set = addingThree();
final int a = (int) set.get(0);
final int b = (int) set.get(1);
final int c = (int) set.get(2);
final int d = (int) set.get(3);
final int ans = (int) set.get(4);
String qText = (String) set.get(5);
String[] options = new String[] {Integer.toString(a),
Integer.toString(b),Integer.toString(c),Integer.toString(d)};
Button button = (Button) findViewById(R.id.button);
button.setText(options[0]);
Button button2 = (Button) findViewById(R.id.button2);
button2.setText(options[1]);
Button button3 = (Button) findViewById(R.id.button3);
button3.setText(options[2]);
Button button4 = (Button) findViewById(R.id.button4);
button4.setText(options[3]);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(qText);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
static ArrayList addingThree(){
Random rand = new Random();
int numa = rand.nextInt(99);
int numb = rand.nextInt(99);
int numc = rand.nextInt(99);
int ans = numa + numb + numc;
int fake1 = rand.nextInt(200) + 50;
int fake2 = rand.nextInt(200) +50;
int fake3 = rand.nextInt(200) +50;
int[ ] options = {fake1, fake2, fake3, ans};
String stringQuestion = ("What is the sum of " + numa + ", " + numb + ",
and " + numc + "? ");
Arrays.sort(options);
ArrayList parcel = new ArrayList();
parcel.add(options[0]);
parcel.add(options[1]);
parcel.add(options[2]);
parcel.add(options[3]);
parcel.add(ans);
parcel.add(stringQuestion);
return parcel;
}
}
Remove the
static
from your functions.
Also remove the functions from the onCreate callback, they don't belong there.
When you're invoking the functions in onCreate, the functions/methods cannot be static
If you want to use findViewById() in static method then make your rootview Global variable

Categories