How To Make OtpEditText And last in first out (Stack)? - java

Like this https://androidexample365.com/content/images/2019/05/OtpEditText.gif
if wordbox1.setText("C") get clicked wordbox1 become invisible and matchbox1.getText("C") when matchbox1 get clicked wordbox1 become visible and matchbox1.getText(null)
I want check if matchbox1 , matchbox2 ,matchbox3 = getText(null) to fill who null
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//my answer buttons
//if you click on any button of my question buttons look for
// who is null first of my answer buttons
Button matchbox1,matchbox2,matchbox3;
//my question buttons wordbox1 = "C" wordbox2 = "A" wordbox3 = "R"
// car is the correct answer
Button wordbox1,wordbox2,wordbox3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
matchbox1= (Button)findViewById(R.id.matchbox1);
matchbox2= (Button)findViewById(R.id.matchbox2);
matchbox3= (Button)findViewById(R.id.matchbox3);
wordbox1= (Button)findViewById(R.id.wordbox1);
wordbox2= (Button)findViewById(R.id.wordbox2);
wordbox3= (Button)findViewById(R.id.wordbox3);
//for example if matchbox1.getText(R) wordbox3.setVisibility(View.Visible);
matchbox1.setOnClickListener(this);
//for example if matchbox2.getText(A) wordbox2.setVisibility(View.Visible);
matchbox2.setOnClickListener(this);
//for example if matchbox3.getText(C) wordbox1.setVisibility(View.Visible);
matchbox3.setOnClickListener(this);
wordbox1.setOnClickListener(this);
wordbox2.setOnClickListener(this);
wordbox3.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.wordbox1:
matchbox1.setText("C");
wordbox1.setVisibility(View.INVISIBLE);
break;
case R.id.wordbox2:
matchbox2.setText("A");
wordbox2.setVisibility(View.INVISIBLE);
break;
case R.id.wordbox3:
matchbox3.setText("R");
wordbox3.setVisibility(View.INVISIBLE);
break;
//for example any button pressed
case R.id.matchbox1:
wordbox3.setVisibility(View.VISIBLE);
matchbox1.setText("");
break;
case R.id.matchbox2:
wordbox2.setVisibility(View.VISIBLE);
matchbox2.setText("");
break;
case R.id.matchbox3:
wordbox1.setVisibility(View.VISIBLE);
matchbox3.setText("");
break;
}
if (matchbox1.getText().toString().equals("C") &&
matchbox2.getText().toString().equals("A")&&
matchbox3.getText().toString().equals("R")){
matchbox1.setBackgroundColor(Color.GREEN);
matchbox2.setBackgroundColor(Color.GREEN);
matchbox3.setBackgroundColor(Color.GREEN);
}
}
}
I hope i find the answer becase I need it very much

Related

How to unchecked the checked checkbox?

how to clear the checked box after we get the checked result ?
something like selection.clear(); but, that only clear the output, not the checkbox.
what i am trying to do is, to set the checkbox to the original state, which is unchecked.
after user checked the checkbox then click button to get result of checked checkbox, i wish to clear all the selection in the checkbox. how please help?
public class DessertIngAvail extends Dessert {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dessert_ing_avail);
final_text = (TextView)findViewById(R.id.final_result);
final_text.setEnabled(false);
}
public void selectItem(View view){
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId()) {
case R.id.checkBox181:
if(checked) {
if(!selection.contains("Tebaloi"))
selection.add("Tebaloi");
if(!selection.contains("Tumpik"))
selection.add("Tumpik");
}
break;
case R.id.checkBox182:
if(checked) {
if(!selection.contains("Ambuyat"))
selection.add("Ambuyat");
}
break;
case R.id.checkBox183:
if(checked) {
if(!selection.contains("Tumpik"))
selection.add("Tumpik");
}
break;
case R.id.checkBoxCM:
if(checked) {
if(!selection.contains("Honey Frankincense Cake"))
selection.add("Honey Frankincense Cake");
if(!selection.contains(" Ray Heart Cake"))
selection.add(" Ray Heart Cake");
}
break;
}
}
public void finalSelection(View view) {
String final_fruit_selection = "";
for(String Selection : selection) {
final_fruit_selection = final_fruit_selection + Selection + "\n";
}
final_text.setText(final_fruit_selection);
selection.clear();
final_text.setEnabled(true);
}
}
You can use
checkBox.setChecked(boolean);
//to clear the check box
checkBox.setChecked(false);
Updated finalSelection(View view) method;
public void finalSelection(View view){
String final_fruit_selection = "";
for(String Selection : selection){
final_fruit_selection = final_fruit_selection + Selection + "\n";
}
final_text.setText(final_fruit_selection);
selection.clear();
final_text.setEnabled(true);
//now clear checkboxes
checkBox181.setChecked(false);
checkBox182.setChecked(false);
checkBox183.setChecked(false);
checkBoxCM.setChecked(false);
}
Changing really really really basic things
//right below TextView final_text; at the top add this
CheckBox checkBox181,checkBox182,checkBox183,checkBoxCM;
//declare then in onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dessert_ing_avail);
final_text = (TextView)findViewById(R.id.final_result);
final_text.setEnabled(false);
checkBox181=(CheckBox)findViewById(R.id.checkBox181);
checkBox182=(CheckBox)findViewById(R.id.checkBox182);
checkBox183=(CheckBox)findViewById(R.id.checkBox183);
checkBoxCM=(CheckBox)findViewById(R.id.checkBoxCM);
}
I suggest you to follow up some java course online. This is a good place to start.
To start learning android development check here.
To check:
checkBox.setChecked(true);
and to uncheck:
checkBox.setChecked(false);
Use setChecked():
checkBox.setChecked(true/false)
Docs: https://developer.android.com/reference/android/widget/CheckBox.html
I'm not sure I understand your question but you can set a checkbox by using checkBox.setChecked(false). To uncheck all checked checkboxes, simply loop over them and uncheck the checked ones.
If you'r sure checkbox are checked, You can toggle them.
checkbox.toggle();
Otherwise
checkBox.setChecked(false); // pass true if you want to select.

Tell which is pressed using Array of ImageButtons

I have a basic hangman game, and the buttons to make the guesses are all placed on screen with ImageButtons (I had this working perfectly).
I declare the image button ID's like so:
private static final int[] BUTTON_IDS = {
R.id.imageButtonA, R.id.imageButtonB, R.id.imageButtonC, R.id.imageButtonD, R.id.imageButtonE,
R.id.imageButtonF, R.id.imageButtonG, R.id.imageButtonH, R.id.imageButtonI, R.id.imageButtonJ,
R.id.imageButtonK, R.id.imageButtonL, R.id.imageButtonM, R.id.imageButtonN, R.id.imageButtonO,
R.id.imageButtonP, R.id.imageButtonQ, R.id.imageButtonR, R.id.imageButtonS, R.id.imageButtonT,
R.id.imageButtonU, R.id.imageButtonV, R.id.imageButtonW, R.id.imageButtonX, R.id.imageButtonY, R.id.imageButtonZ
};
private List<ImageButton> buttonLetters = new ArrayList<ImageButton>();
Edited to show solution: I register their OnClick by doing:
for (int id: BUTTON_IDS) {
final ImageButton buttonLettersUsage = (ImageButton)findViewById(id);
if (buttonLetters != null) {
assert buttonLettersUsage != null;
buttonLettersUsage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(HangmanActivity.this, " " + buttonLettersUsage.getId(), Toast.LENGTH_SHORT).show();
switch (buttonLettersUsage.getId()){
case R.id.imageButtonQ:
strGuessed = "Q";
break;
case R.id.imageButtonW:
strGuessed = "W";
break;
case R.id.imageButtonE:
strGuessed = "E";
break;
// ... Repeat for rest of buttons ...
default:
break;
}
btnGuessClicked(theIncorrectGuesses, theWord);
buttonLettersUsage.setVisibility(View.INVISIBLE);
}
});
}
}
The problem is now, I've been working on the app some more since implementing this part, and all the buttonLettersUsage.getId()'s have changed.
My question is:
Will they change at any time?
I've not put any new components onto the activity, why have they changed?
Is there a better way to find out which button has been pressed, and give a different value for strGuessed depending on which has been pressed?
Many thanks.
Don't use the numerical ids in the switch case use it as follows.
switch (buttonLettersUsage.getId()){
case R.id.imageButtonA:
strGuessed = "Q";
break;
case R.id.imageButtonB:
strGuessed = "W";
break;
case R.id.imageButtonC:
strGuessed = "E";
break;
// ... Repeat for rest of buttons ...
default:
break;
}
And also don't implement onClickListener like this, use your Activity to implement View.OnClickListener and then set the listener in loop as
buttonLettersUsage.setOnClickListener(this);
Where this is the context of your activity which implements the OnClickListener
YourActivity implements View.OnClickListener{
then in oncreate(){
//add is buton
View buttonAdd = findViewById(R.id.add);
buttonAdd.setOnClickListener(this);}
then
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
{
}
break;}

Subtraction App Java

Hi I'm a college student and I'm struggling with an app I have to make. The app is a subtraction app and I can't seem to get it to work. Every time I press the subtraction button it crashes.
My code:
public void onClick(View view){
switch (view.getId()) {
case R.id.subtract_button:
setContentView(R.layout.activity_main);
EditText firstNumber = (EditText) findViewById(R.id.input_box_1);
int num1 = Integer.parseInt(firstNumber.getText().toString());
EditText secondNumber = (EditText) findViewById(R.id.input_box_2);
int num2 = Integer.parseInt(secondNumber.getText().toString());
int result = num1 - num2;
setContentView(R.layout.activity_result);
TextView subResult = (TextView)findViewById(R.id.result_view);
subResult.setText(Integer.toString(result));
break;
case R.id.exit_button: // the 'exit' button has been pressed.
// Delay the exit by 1 second at this time!
new Handler().postDelayed(new Runnable() {
public void run() {
//Finish the app
finish();
}
}, 1000);
break; // End of case.
}
}
At First Post your Logcat
Problem for your wrong setContentView placing .
Why multiple setContentView ?? Must be One .
Don't
switch (view.getId()) {
case R.id.subtract_button:
setContentView(R.layout.activity_main); // Problem here i guess
Do
setContentView call in your onCreate(Bundle savedInstanceState) section.
An activity is a single, focused thing that the user can do. Almost
all activities interact with the user, so the Activity class takes
care of creating a window for you in which you can place your UI with
setContentView(View).
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_name);
I thought you missed button declaration check it
Button button;
button=(Button) findViewById(R.id.buttonid);
or else post ur log

Random XML Layout on button click

I have 20 XML layouts. What I want to happen is to show random xml layouts when the button is clicked. I tried and read same problem as mine but i didnt worked.
For example in Level1 class when the user clicked the PositiveButton in the AlertDialog, random XML Layout will be opened (Level 20 or Level 15 not Level 2).
This is code in Level1 class(the same pattern applies for the rest of the classes)
public class Luzon1 extends Activity {
private String [] answers;
private Button answerButton;
private TextView scoreTxt, showClue;
private EditText answerText;
int scoreNew=0;
public Button yes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_luzon1);
}
public void init()
{
//correct answer
answers=new String[]{"Tarlac"};
(R.id.AnswerButton);
answerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkAnswer();
}
});
}
public boolean isCorrect(String answer)
{ return(answer.equalsIgnoreCase(answers[currentQuestion])); }
public void checkAnswer()
{ String answer=answerText.getText().toString();
if(isCorrect(answer))
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Congratulations! You will receive P10!");
builder.setMessage("Did you know that Former bla bla bla Did you know that Former bla bla bla Did you know that Former bla bla bla");
builder.setIcon(android.R.drawable.btn_star_big_on);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String userData=scoreTxt.getText().toString();
int userNumber=Integer.parseInt(userData);
Intent intent=new Intent(Luzon1.this, Luzon2.class);
intent.putExtra("parameter name", userNumber);
startActivity(intent);
Luzon1.this.finish(); System.exit(0);
} });
AlertDialog alert = builder.create();
alert.show(); // Show Alert Dialog
Thank you so much in advance. and any code snippet will be a great help.
Setting the layout from inside onClick()
If you want this code to be in the onClick() method in your previous activity (the one shown above), use the following code:
#Override
protected void onClick(DialogInterface dialog, int which) {
Random generator = new Random();
int number = generator.nextInt(NUMBER_OF_LEVELS) + 1;
Class activity;
switch(number) {
case 1:
activity = LevelOne.class;
break;
case 2:
activity = LevelTwo.class;
break;
case 3:
activity = LevelThree.class;
break;
case 4:
activity = LevelFour.class;
break;
case 5:
activity = LevelFive.class;
break;
...
case 20:
activity = LevelTwenty.class;
break;
}
Intent intent = (getBaseContext(), activity);
startActivity(intent);
}
Try this:
Declare a public static int count = 0 before onCreate..
then, In onClick increment the count by 1(count++)..
Use switch statement like,(Dont forget to reset counter to 0 when count becomes 20)
void onClick(){
count++;
switch(count) {
case 1:
setContentView(R.layout.yourLayout1);
break;
case 2:
setContentView(R.layout.yourLayout2);
break;
case 3:
setContentView(R.layout.yourLayout3);
break;
case 4:
setContentView(R.layout.yourLayout4);
................................
................................
case 20:
setContentView(R.layout.yourLayout20);
break;
}
if(count==20){
count = 0;
}
}
And also instead of incrementing count each time user clicks, you can use Math.random() and assign it to count (Remember to check(use if statement) Math.random() is returning a value which is below or equal to 20..)
Hope it will help you..

How to reset class variable to default value 0?

If anybody can tell me different way to do this, I would appreciate. How to reset class variable to 0, or default value? I use class variable cause I don't know another way to do this. After my game ends I place result in class variable, cause I have two rounds of my game, and after first round ends I add the result, and class variable is good for this cause even after I restart my game method it still holds my previous result. After second round is over I add that result to previous result and then close the activity and set text result as text to a button. But when I click New game, that button still holds that text, cause class variable still holds it. How to reset that class variable when I go on New game?
Here's my game code, some of it (100 points are start amount, and it gets lower in game progress):
public class Asocijacije extends Activity implements OnClickListener{
int brojPoenaAsocijacije = 100;
public static int brojPoenaUkupno;
Then I skip here a lot of code and here's where I add points. brojPoenaAsocijacije are points earned in that round:
brojPoenaUkupno = brojPoenaUkupno + brojPoenaAsocijacije;
Here's my main activity where I set points from my class variable to a button (where I added comment):
public class Izbor extends Activity implements OnClickListener{
Asocijacije poeni = new Asocijacije();
Button toploHladno, asocijacije, cigle, spojnice, nazad, poeniTH, poeniAso, poeniCigle, poeniSpojnice;
TextView naslov;
public boolean music;
MediaPlayer buttonClicks, buttonBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.izbor);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
music = getPrefs.getBoolean("checkbox", true);
addListenerOnButton();
}
private void addListenerOnButton() {
buttonClicks = MediaPlayer.create(this, R.raw.click);
buttonBack = MediaPlayer.create(this, R.raw.button31);
Typeface naslovType = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
naslov = (TextView) findViewById(R.id.tvIzborNaslov);
toploHladno = (Button) findViewById(R.id.bIzbor1);
asocijacije = (Button) findViewById(R.id.bIzbor2);
cigle = (Button) findViewById(R.id.bIzbor3);
spojnice = (Button) findViewById(R.id.bIzbor4);
nazad = (Button) findViewById(R.id.bIzborNazad);
poeniTH = (Button) findViewById(R.id.bPoeniTH);
poeniAso = (Button) findViewById(R.id.bPoeniAso);
poeniCigle = (Button) findViewById(R.id.bPoeniCigle);
poeniSpojnice = (Button) findViewById(R.id.bPoeniSpojnice);
naslov.setTypeface(naslovType);
toploHladno.setTypeface(dugmad);
asocijacije.setTypeface(dugmad);
cigle.setTypeface(dugmad);
spojnice.setTypeface(dugmad);
nazad.setTypeface(dugmad);
poeniAso.setTypeface(dugmad);
toploHladno.setOnClickListener(this);
asocijacije.setOnClickListener(this);
cigle.setOnClickListener(this);
spojnice.setOnClickListener(this);
nazad.setOnClickListener(this);
}
#Override
protected void onStart() {
super.onStart();
poeniAso.setText("" + poeni.brojPoenaUkupno); //I do it here
}
public void onClick(View v) {
switch(v.getId()){
case R.id.bIzbor1:
if(music == true){
buttonClicks.start();
}
startActivity(new Intent("rs.androidaplikacije.toplo_hladno.GAME"));
break;
case R.id.bIzbor2:
if(music == true){
buttonClicks.start();
}
startActivity(new Intent("rs.androidaplikacije.toplo_hladno.ASOCIJACIJE"));
break;
case R.id.bIzbor3:
if(music == true){
buttonClicks.start();
}
break;
case R.id.bIzbor4:
if(music == true){
buttonClicks.start();
}
break;
case R.id.bIzborNazad:
if(music == true){
buttonBack.start();
}
poeniAso.setText("");
finish();
break;
}
}
}
Because I don't find the piece of code where you start the new game, I can only say:
Asocijacije.brojPoenaUkupno = 0;
Probably want to reset your state when you close the in-game activity.
#Override
protected void onDestroy() {
super.onDestroy();
Asocijacije.brojPoenaUkupno = 0;
//whatever other things need to be reset.
}

Categories