Intent does not work in switch statement - java

I have a switch case if signin button is pressed it should move to the next Activity. However, for some reason it does not work.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnAccSignup;
private Button btnSignin;
FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAccSignup = (Button) findViewById(R.id.btnAccSignup);
btnSignin = (Button) findViewById(R.id.btnSignin);
btnSignin.setOnClickListener(this);
mAuth = FirebaseAuth.getInstance();
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnAccSignup:
openSignupActivity(); // This part does not work. Everything else works fine.
break;
case R.id.btnSignin:
userSignin();
break;
}
}
public void openSignupActivity() {
Intent intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(intent);
}
}

You forgot to add the listener for btnAccSignup, you only added for btnSignin. Just add this:
btnAccSignup.setOnClickListener(this);

Related

My MediaPlayer and transition effect not working

Why I cannot put mp3 sound and transition effect to my button onClick?
While launching Activity2 my app crashing. How can I use MediaPlayer and my transition effect to my button onClick in private View.OnClickListener?
I'm using Transition effect (Bungee) from library
My code...
public class Activity2 extends AppCompatActivity {
private Button button3;
private Button entrycity;
private static final String NAME = "name";
private boolean isEnabled;
private SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_2);
button3 = findViewById(R.id.button3);
button3.setOnClickListener(onButton1Click);
entrycity = findViewById(R.id.entrycity);
entrycity.setOnClickListener(onButton2Click);
sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
isEnabled = sharedPreferences.getBoolean(winflagi.IS_ENABLED, false);
entrycity.setEnabled(isEnabled);
if (isEnabled){
entrycity.setBackgroundResource(R.drawable.oval);
}
else {
entrycity.setBackgroundResource(R.drawable.oval3);
}
}
final MediaPlayer mp = MediaPlayer.create(this, R.raw.menunewquite);
private View.OnClickListener onButton1Click = new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Activity2.this, flagi1.class));
mp.start();
Bungee.fade(this);
}
};
private View.OnClickListener onButton2Click = new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Activity2.this, cities1.class));
mp.start
Bungee.fade(this);
}
};
}
Try to declare MediaPlayer inside the body of the listener.

how to display the clicked radio button value on my second activity in android studio?

This is my java code:
public class jenis extends AppCompatActivity {
public Button button3;
RadioGroup radioGroup2;
RadioButton radioButton2;
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jenis);
radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
databaseReference = FirebaseDatabase.getInstance().getReference();
final RadioButton stock = (RadioButton)findViewById(R.id.radioButton11);
final RadioButton property = (RadioButton)findViewById(R.id.radioButton12);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (stock.isChecked()){
Variable newVar = new Variable();
newVar.setAmountt("jeniss");
databaseReference.child(newVar.getAmountt()).setValue("Stock Waqf");
}
else if(property.isChecked()){
Variable newVar = new Variable();
newVar.setAmountt("jeniss");
databaseReference.child(newVar.getAmountt()).setValue("Property Waqf");
}
if(stock.isChecked()){
Intent intent = new Intent(jenis.this, stock.class);
startActivity(intent);
}
if(property.isChecked()){
Intent intent = new Intent(jenis.this, property.class);
startActivity(intent);
}
}
});
}
}
How can I display the clicked radio button value on my second activity?
I really appreciate your help, thank you.
pass the value as intent
Intent intent = new Intent(youractivity.this,secondactivity.class);
intent.putExtra("key",radiobutton.getText());
startActivity(intent);
to access it on second activity
in OnCreate() method
getIntent().getStringExtra("key");
or else
you can also store your radiobutton checked value in shared preference and
then access it on another activity... It will work like session
public class stock extends AppCompatActivity {
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stock);
final TextView textViewOut = (TextView) findViewById(R.id.textViewOut2);
textViewOut.setText(" ");
}
public void OnCreate() {
getIntent().getStringExtra("jeniss");
}
}
what did i missed ?

Opening next Activity

I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.

2 buttons in my android layout, only 1 will work

In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}

button click event doesnt work

I'm trying to switch the views, but when I'm in the second view, the back event click doesnt work.. I don't know what's wrong.
Pls, see my code and help me!
Part1
Part2
public class t extends Activity implements OnClickListener {
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
volta = (Button) findViewById(R.id.button2);
volta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == volta) {
startActivity(new Intent(t.this, MainActivity.class));
}
}
}
You have to override onBackPressed. Change your MainActivity as below
public class MainActivity extends Activity {
private boolean goBack = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
goBack = true;
setContentView(R.layout.janela2);
}
});
}
#Override
public void onBackPressed() {
//If you have switched to R.layout.janela2 then go back
if (goBack){
setContentView(R.layout.activity_main);
goBack = false;
return;
}
//else do default action
super.onBackPressed();
}
}
Just do the following code, I hope it might help you
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, t.class);
startActivity(intent);
}
});
}
}
In t.java
public class t extends Activity{
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
}
#Override
public void onStop() {
super.onStop();
finish();
}
}
If you want two layouts then use viewflipper. If you want two activities (java classes) AND two layouts separately then use:
Intent i = new Intent (this, myClass.class);
startActivity(i);
To start the Activity and NOT setcontentview
So here:
public void onClick(View v) {
startActivity(new Intent (MainActivity.this, t.class));
OR IN THE CASE OF T.CLASS:
startActivity(new Intent (t.this, MainActivity.class));
}
You have to override onBackPressed() method if you want to back button functionality in your application. i.e.
public void onBackPressed() {
Intent start = new Intent(CurrentClass.this,Next_Activity.class);
startActivity(start);
finishActivity(0);
}

Categories

Resources