Edit the TextView in custom AlertDialog - java

I have a layout with 10 buttons. When I click one of them a custom dialog shows up, that has 2 textviews - one for the title and one for the quote.
There's always an instant crash when I try to edit the TextView programmatically. The program crashes when I call tv1.setText.
I tried to create an another function called Qdialog. When I call it the program crashes as well.
package com.example.lud.fortunecookie;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Menuslection extends AppCompatActivity {
Button btback, bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10;
TextView tv1,tv2;
AlertDialog quotesalert;
AlertDialog.Builder quitbuilder,quotesbuilder;
int numb = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menuslecton);
char stline;
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
bt5 = (Button) findViewById(R.id.button5);
bt6 = (Button) findViewById(R.id.button6);
bt7 = (Button) findViewById(R.id.button7);
bt8 = (Button) findViewById(R.id.button8);
bt9 = (Button) findViewById(R.id.button9);
bt10 = (Button) findViewById(R.id.button10);
btback = (Button) findViewById(R.id.buttonback);
btback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
quitbuilder = new AlertDialog.Builder(Menuslection.this);
quitbuilder.setMessage("Are you sure to quit ?");
quitbuilder.setTitle("Are you already got my advise.");
quitbuilder.setCancelable(false);
quitbuilder.setNegativeButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//finish(); //dung de thoat khoi activity hien tai
Intent iquit = new Intent(getApplicationContext(), MainActivity.class);
startActivity(iquit);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
finish();
}
});
quitbuilder.setPositiveButton("no", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog Alertquit = quitbuilder.create();
Alertquit.show();
}
});
//View layout = inflater.inflate(R.layout.dialoglayouttest,null);
//View layout = inflater.inflate(R.layout.customdialog, null);
// quotesbuilder.setView(tv1);
// View content = inflater.inflate(R.layout.dialoglayouttest, null);
// quotesbuilder.setView(content);
//TextView tv1 = (TextView) content.findViewById(R.id.dia_tit);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numb=1;
Qdialog();
quotesalert.show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(2);
quotesalert.show();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(3);
quotesalert.show();
}
});
bt4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(4);
quotesalert.show();
}
});
bt5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(5);
quotesalert.show();
}
});
bt6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(6);
quotesalert.show();
}
});
bt7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(7);
quotesalert.show();
}
});
bt8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(8);
quotesalert.show();
}
});
bt9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(9);
quotesalert.show();
}
});
bt10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Qdialog();
// numb=10;
// quotesalert.show();
}
});
}
public int numbslection(int numb)
{//Slection Tittle Classic setup
if(numb==10)
numb=1;
//tv1.setText("");
else
if(numb==1||numb==2)
tv1.setText(R.string.Pgud);
else
if(numb<7&&numb>2)
tv1.setText(R.string.OK);
else
tv1.setText(R.string.Upset);
return numb;
}
public void Qdialog()
{
quotesalert.setContentView(R.layout.dialoglayouttest);
tv1 =(TextView) findViewById(R.id.dia_tit);
numbslection(numb);
quotesbuilder = new AlertDialog.Builder(Menuslection.this);
quotesalert=quotesbuilder.create();
}
}

To use custom layout to AlertDialog, you have to inflate layout and set view to AlertDiaolog:
public void Qdialog()
{
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialoglayouttest, null);
quotesbuilder = new AlertDialog.Builder(Menuslection.this);
quotesbuilder.setView(dialogView);
EditText editText = (EditText) dialogView.findViewById(R.id.dia_tit);
quotesalert = quotesbuilder.create();
quotesalert.show();
}
ALTERNATIVE:
If you want to use custom layout, use **Dialog** instead of **AlertDialog** :
public void Qdialog()
{
Dialog dialog = new Dialog(this)
dialog.setContentView(R.layout.dialoglayouttest);
dialog.setTitle("Title...");
TextView text = (TextView) dialog.findViewById(R.id.dia_tit);
dialog.show();
}
Hope this helps.

Related

How to make Bottom Sheet Dialog that shows one time only

I want to create a Bottom Sheet Dialog. It looks like the image below
enter image description here
public class MainActivity extends AppCompatActivity {
//this is Button that open dialog
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.nextBtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.exit(0);
}
});
bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
});
}
});
}
}
I have tried many methods such as SharedPreferences. But it not working
SharedPreferences preference = getSharedPreferences("mPreference", MODE_PRIVATE);
boolean isUserAgreed = preference.getBoolean("isUserAgreed", false);
if (!isUserAgreed){
// show bottomSheetDialog
} else {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.exit(0);
}
});
bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("mPreference", MODE_PRIVATE).edit();
editor.putBoolean("isUserAgreed", true);
editor.apply();
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
});
}
});

Android Studio Stop Sound

thats my first question here.
I hope i get a solution.
I coded a soundboard app and i want that if i click the button a sound play.
i got that easily.
the next point is that i want to if i click another button the other sound stop.
i hope you understand. i tried a lot with .stop or .pause...
thats my java code on this page:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
public void stop(){
}
Button btone1,btone2,btone3,btone4,btone5,btone6,btone7,btone8,btone9,btone10,btone11,btone12;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1_layout,container, false);
btone1 = (Button) rootView.findViewById(R.id.btone1);
final MediaPlayer mpone1 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone1.start();
}
});
btone2 = (Button) rootView.findViewById(R.id.btone2);
final MediaPlayer mpone2 = MediaPlayer.create(getActivity(), R.raw.sone2);
btone2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone2.start();
}
});
btone3 = (Button) rootView.findViewById(R.id.btone3);
final MediaPlayer mpone3 = MediaPlayer.create(getActivity(), R.raw.sone2);
btone3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone3.start();
}
});
btone4 = (Button) rootView.findViewById(R.id.btone4);
final MediaPlayer mpone4 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone4.start();
}
});
btone5 = (Button) rootView.findViewById(R.id.btone5);
final MediaPlayer mpone5 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone5.start();
}
});
btone6 = (Button) rootView.findViewById(R.id.btone6);
final MediaPlayer mpone6 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone6.start();
}
});
btone7 = (Button) rootView.findViewById(R.id.btone7);
final MediaPlayer mpone7 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone7.start();
}
});
btone8 = (Button) rootView.findViewById(R.id.btone8);
final MediaPlayer mpone8 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone8.start();
}
});
btone9 = (Button) rootView.findViewById(R.id.btone9);
final MediaPlayer mpone9 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone9.start();
}
});
btone10 = (Button) rootView.findViewById(R.id.btone10);
final MediaPlayer mpone10 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone10.start();
}
});
btone11 = (Button) rootView.findViewById(R.id.btone11);
final MediaPlayer mpone11 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone11.start();
}
});
btone12 = (Button) rootView.findViewById(R.id.btone12);
final MediaPlayer mpone12 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone12.start();
}
});
////
return rootView;
}
}
Use a single MediaPlayer object instead of creating multiple ones
then stop using this code
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}

whenever i perform any operations like add sub equals etc in andriod studio EditText box , my app crashes

I have been trying to make a simple calculator on Android studio. It shows the calculations perfectly fine. but whenever I try to press equals or plus sub or minus operation when the EditText box is empty, my app always crashes.
I tried different ways but it didn't help.
Is there any way that I can do it and stop my app from crashing?
Here's my java code
package com.example.mathcalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button0,button1,button2,button3,button4,button5,button6,button7,button8,button9,buttonClear,buttonMul,buttonSub,buttonAdd,buttonDec,buttonEqual,buttonDiv;
float val1,val2;
EditText result;
boolean mAdditon,msubtraction,mMultiplication, mDivision;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonMul = (Button) findViewById(R.id.buttonMul);
buttonDiv = (Button) findViewById(R.id.buttonDiv);
buttonClear = (Button) findViewById(R.id.buttonClear);
buttonEqual = (Button) findViewById(R.id.buttonEqual);
buttonDec = (Button) findViewById(R.id.buttonDec);
buttonSub = (Button) findViewById(R.id.buttonSub);
result = (EditText) findViewById(R.id.result);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"9");
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+".");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (result == null)
{
result.setText("");
}
else
{
val1 =Float.parseFloat(result.getText()+"");
mAdditon=true;
result.setText(null);
}
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
msubtraction=true;
result.setText(null);
}
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
mDivision=true;
result.setText(null);
}
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
mMultiplication = true;
result.setText(null);
}
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText("");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
val2 = Float.parseFloat(result.getText() + "");
if (mAdditon == true) {
result.setText(val1 + val2 + "");
mAdditon = false;
}
if (msubtraction == true) {
result.setText(val1 - val2 + "");
msubtraction = false;
}
if (mMultiplication == true) {
result.setText(val1 * val2 + "");
msubtraction = false;
}
if (mDivision == true) {
if (val2 == 0) {
result.setText("Can't Divide by Zero");
return;
} else {
result.setText(val1 / val2 + "");
msubtraction = false;
}
}
}
});
}
}
You say it crashes when the EditText box is empty, so most likely, what is happening is that when you do
Float.parseFloat(result.getText() + "");
result.getText() returns "", or you end up doing
Float.parseFloat("");
which result in java.lang.NumberFormatException: empty String
Try replacing
val1=Float.parseFloat(result.getText()+"");
with
val1 = 0;
if(!result.getText().isEmpty())
val1 = Float.parseFloat(result.getText());
NOTE:
Also, as you use it several times, you can make a method for it
private float getResult() {
int result = 0;
if(!result.getText().isEmpty())
val1=Float.parseFloat(result.getText());
return result;
}
and then replace everywhere with val1 = getResult();
as above code when get data from EditText use edit.getText().toString()
and for Parsing it always wrap it with try/catch block as EditText it is initially empty
some hints about the code
- here if (result == null) result.setText(""); result is always not null.
so for better solution always use try/catch when parsing EditText.

Why app forced me to use buttons sequently as they appear on MainActivity.java? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm new on android developing,
I created a simple app content of 4 buttons and when I tested it on my phone the problem:
button 4 didn't work until I pressed button 3
button 3 didn't work until I pressed button 2
etc..
I want to make them work separately
Sorry for may bad description
package com.example.my;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.btn);
final Button button1 = findViewById(R.id.button2);
final Button button3 = findViewById(R.id.button3);
final TextView textView = findViewById(R.id.tex);
final EditText et1 = findViewById(R.id.et1);
final EditText et2 = findViewById(R.id.et2);
final Button button4 = findViewById(R.id.button4);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String value = et1.getText().toString();
int n11 = Integer.parseInt(value);
String value2 = et2.getText().toString();
int n22 = Integer.parseInt(value2);
textView.setText(String.valueOf(n22 + n11));
Toast.makeText(getApplicationContext(), "done ", Toast.LENGTH_SHORT).show();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
textView.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
et1.setText("0");
et2.setText("0");
}
});
}
});
}
});
}
}
Why app forced me to use buttons sequently as they appear on MainActivity.java?
That's because you did not allow it to have OnClickListeners on all the Buttons from the outset - two of them will only be set if the user (you) clicks button, and button will only respond to clicks after the user clicked button3
You need to set all the OnClickListeners on the same "level", not one nested in another's onClick().
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//...
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//...
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//...
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//...
}
});
You are setting the clicklistener on button in wrong way, You are nesting them.
You should do the following -
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.btn);
final Button button1 = findViewById(R.id.button2);
final Button button3 = findViewById(R.id.button3);
final TextView textView = findViewById(R.id.tex);
final EditText et1 = findViewById(R.id.et1);
final EditText et2 = findViewById(R.id.et2);
final Button button4 = findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
et1.setText("0");
et2.setText("0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
textView.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String value = et1.getText().toString();
int n11 = Integer.parseInt(value);
String value2 = et2.getText().toString();
int n22 = Integer.parseInt(value2);
textView.setText(String.valueOf(n22 + n11));
Toast.makeText(getApplicationContext(), "done ", Toast.LENGTH_SHORT).show();
}
});
}
}
I can see too many problems in the code.
Firstly each setOnClickListener should be independent in itself.
setOnClickListener on button3 getting closed before button.
Secondly you are adding onclickListener inside onCLick which is wrong.

Buttons only work when used after one another

So I've got multiple buttons in one activity, and I can only use them one after another. I've read I need to do something with the (OnClickListener) part but I'm unsure what or how to do it. Any help is greatly appreciated. Code is below:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
B5 = findViewById(R.id.button11);
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
EDIT: thanks guys for all the help- out at the moment so when I get home I’ll test these. They look very similar to what I’ve read so they should work.. thanks!
You are setting your click listeners sequentially. If you want all the buttons to be clickable at any time, move the listeners into your onCreate():
protected void onCreate() {
B3.setOnClickListener(){};
B4.setOnClickListner(){};
// etc.
}
A click listener is just that – it "listens" for clicks. Your B3, for example, is the only one listening when the Activity gets created, so all the other buttons will ignore your clicks on them. When B3 is clicked, moveToDanceScheduleMenu() is invoked and B4 starts listening.
I hope that clears things up a bit.
Complete code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5 = findViewById(R.id.button11);
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
}
You can put all your code in OnCreate() method like this
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
You are making Buttons depended of other Buttons in a Sequential order.
Try Binding Button and their OnclickListners in oncreate.Like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
I think this code is work
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
private a=false,b=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
a=true ;
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(a==true ){
moveToWhatsOn();
}
}
});
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
b=true ;
B5 = findViewById(R.id.button11);
}
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(b==true){
moveToMainResultsMenu();
}
}
});
}
If you want to use a button only after a previous button was pressed, you should disable all next buttons in the begining in the onCreate() method and enable them later when the previous button was pressed
B3.setEnabled(true);
B4.setEnabled(false);
B5.setEnabled(false);
And then enable the next button after onClick
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
B4.setEnabled(true);
}
});
And as far for your code
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
private void moveToDanceScheduleMenu() {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn() {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
}
There are several ways to interact with the buttons:
First method: Class implementation
public class Main2Activity extends AppCompatActivity implements OnClickListener {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(this);
B4.setOnClickListener(this);
B5.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId()==B3.getId()){
moveToDanceScheduleMenu();
}else if(v.getId()==B4.getId()){
moveToWhatsOn();
}else if(v.getId()==B5.getId()){
moveToMainResultsMenu();
}
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
Second Method: Private variable (large blocks)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.button3).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button10).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button11).setOnClickListener(mGlobal_OnClickListener);
}
//Global On click listener for all views
final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
public void onClick(final View v) {
switch(v.getId()) {
case R.id.button3:
moveToDanceScheduleMenu();
break;
case R.id.button10:
moveToWhatsOn();
break;
case R.id.button11:
moveToDanceScheduleMenu();
break;
}
}
};
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
Third Method: Online (small blocks)
public class Main2Activity extends AppCompatActivity{
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}

Categories

Resources