Transfer a choice made with radiogroup to another intent - java

I have 4 radiogroups (like a couple of yes or no) in Main3Activity and I would like these choices to be sent to another intent (Main4Activity). I use the button12 for change intent.
I tried to do this code
public class Main3Activity extends AppCompatActivity {
int snoring = 0;
int pressure = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button btnNew = (Button) findViewById(R.id.button12);
btnNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(Main3Activity.this, Main4Activity.class);
if(snoring == 1) {intent.putExtra("snoring", "1");} else {intent.putExtra("snoring", "0");}
if(pressure == 1) {intent.putExtra("pressure", "1");} else {intent.putExtra("pressure", "0");}
startActivity(intent);
}
});
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radioButton1:
if (checked)
snoring = 1;
break;
case R.id.radioButton2:
if (checked)
snoring = 0;
break;
case R.id.radioButton3:
if (checked)
pressure = 1;
break;
case R.id.radioButton4:
if (checked)
pressure = 0;
break;
}
}
}
In next intent i try to show the result with method similiar to this:
String snoring = getIntent().getExtras().getString("snoring");
String pressure= getIntent().getExtras().getString("pressure");
but "snoring" and "pressure" is always 0.
How can I recover snoring and pressure data from previous intent?

Just do-:
btnNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(Main3Activity.this, Main4Activity.class);
intent.putExtra("snoring",snoring); //value that came from radio button
intent.putExtra("pressure",pressure) //value that came from radio button
startActivity(intent);
}
});
and on other activity-:
if(intent.hasEXtra("snoring))
{
get the snoring value
}
else if(intent.hasEXtra("pressure))
{
get the pressure value
}

Related

How to use switch case with buttonclick to check the user response

I am trying to create a android application to check the user click. the user listen to the number to click for exemple if hear number zero he must click on button 0 if not a wrong message display.
this is my code java
private static final int[] idArray ={R.id.btn0,R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,R.id.btn5,R.id.btn6,
R.id.btn7,R.id.btn8,R.id.btn9,R.id.btn10,R.id.btn11,R.id.btn12,R.id.btn13,R.id.btn14,R.id.btn15};
private Button[] button = new Button[idArray.length];
MediaPlayer bravo,ton,faux;
ImageView image ;
private int mQuestionNumber;
private CorrectActivity correctDialog;
private FauxActivity wrongDialog;
private EcoutAudio ecoutAudio;
int FLAG= 0;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ball);
image=(ImageView) findViewById(R.id.num);
correctDialog=new CorrectActivity(this);
wrongDialog= new FauxActivity(this);
ecoutAudio=new EcoutAudio(this);
updateQuestion();
for ( i = 0; i < idArray.length-1; i++) {
button[i] = (Button) findViewById(idArray[i]);
button[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn0:
if(button[i].getText() == "0"){
//Toast.makeText(getApplicationContext(), "0", Toast.LENGTH_SHORT).show();
correctDialog.correctDialog(BallActivity.this);
FLAG=1;
ecoutAudio.setAudioforAnswer(FLAG);
updateQuestion();
}
else{
wrongDialog.WrngDialog(BallActivity.this);
FLAG=2;
ecoutAudio.setAudioforAnswer(FLAG);
updateQuestion();
}
break;
case R.id.btn1:
But when he click the wrong message is display when he choose the right button.
Thank you
when the user choose the right response the message error is displayed

Android Spinner how to update the selected position (getSelectedItemPosition())?

So I have a Spinner and a EditText to login. The array of the Spinner owns "Anonymous" and " Owner" (means "Anonymous" is 0 and "Owner" is 1 in the array). When you choose "Anonymous" the password is "0000" and when you choose "Owner" the password is "1234".
But when I choose "Owner", the password "1234" is wrong and the Logcat shows "Anonymous". How can I make "Owner" selected? Maybe getSelectedItemPosition() is wrong?
My Code:
public class PinEnterActivity extends AppCompatActivity {
Button nextButton;
EditText pinEditText;
Spinner pinRoleSpinner = null;
private String TAG = "PinEnterActivity";
private Byte selectedUserRole = 0;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pin_enter);
nextButton = findViewById(R.id.nextActivity)
pinEditText = findViewById(R.id.pinET);
pinRoleSpinner = findViewById(R.id.roleSpinner);
selectedUserRole = (byte) pinRoleSpinner.getSelectedItemPosition();
switch (selectedUserRole) {
case 0:
Log.i(TAG, "Anonymous");
SharedPreferences sharedpreferences = getSharedPreferences("My_Prefs", 0);
final String password = sharedpreferences.getString("pass", "");
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (pinEditText.getText().toString().equals("0000")) {
Intent intent = new Intent(PinEnterActivity.this, NextActivity.class);
startActivity(intent);
} else {
pinEditText.setError("Password incorrect");
Animation shake = AnimationUtils.loadAnimation(PinEnterActivity.this, R.anim.shake);
pinEditText.startAnimation(shake);
return;
}
}
});
break;
case 1:
Log.i(TAG, "Owner");
SharedPreferences preferences = getSharedPreferences("My_Prefs", 0);
final String password2 = preferences.getString("pass", "");
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (pinEditText.getText().toString().equals("1234")) {
Intent intent = new Intent(PinEnterActivity.this, NextActivity.class);
startActivity(intent);
}else{
pinEditText.setError("Password incorrect");
Animation shake = AnimationUtils.loadAnimation(PinEnterActivity.this, R.anim.shake);
pinEditText.startAnimation(shake);
return;
}
}
});
}
}
I finally found out the right answer.
pinRoleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(selectedUserRole) {
case 0:
Log.i(TAG, "Anonymous");
// Code
break;
case 1:
Log.i(TAG, "Owner");
// Code
break;
default;
// Code
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

How to validate that at least one checkbox should be selected? and shows alert message

I want to validate at least one checkbox is selected. Below is the code I'm using.
public classGejalaKariesGigiActivityextendsAppCompatActivity {
Intent intentData;
Button buttonSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gejala_karies_gigi);
intentData = new Intent(GejalaKariesGigiActivity.this,HasilDiagnosaActivity.class);
buttonSend = (Button) findViewById(R.id.buttonselesai1);
buttonSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intentData);
}
});
}
public void selectItem(View view){
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId()) {
case R.id.checkBoxgigiberlubang:if (checked){
intentData.putExtra("gigi_berlubang","Gigi Berlubang");
}else {
intentData.removeExtra("gigi_berlubang");
}break;
case R.id.checkBoxngilu:if (checked){
intentData.putExtra("ngilu","Ngilu");
}else {
intentData.removeExtra("ngilu");
}break;
case R.id.checkBoxadabercak:if (checked){
intentData.putExtra("ada_bercak_hitam_pada_gigi","Ada Bercak Hitam Pada Gigi");
}else {
intentData.removeExtra("ada_bercak_hitam_pada_gigi");
}break;
default:break;
}
}
}
You have to check every checkbox whether it is Checked or not in if else statement.
if (chkbox1.isChecked()){
//do your operation
}else if (chkbox2.isChecked()){
//do your operation
}else if (chkbox3.isChecked()){
//do your operation
}else{
// show your alert dialog
}

Passing data from one activity to another when choosing a menu option from action bar in Android app

I don't know if anyone can offer some guidance on this.
I have a main class where a user will input their name. I then want the user to choose a game from the options available in the action/app bar. When they choose their game, the name they entered in the main class will then be showing on the game class. I hope that makes sense?
I know this involves passing data from one activity to another using intents but every tutorial I see involves setting up a button in the main class (so using something like onclicklistener) but I don't want a button. I want choosing the menu option to do the same job but I can't find anywhere how to do it.
Thanks in advance.
EDIT - ATTEMPTED CODE ADDED
Main class:
public class GameCentral extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_central_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.commonmenus, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.guessingGame) {
startActivity(new Intent(this, Task3Activity.class));
switch (item.getItemId()) {
case R.id.playerNameEntered:
startActivity(this, Task3Activity.class).putExtra(playerNameInput, value);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
else if (id ==R.id.placeOne) {
Toast.makeText(this, "Game Placeholder One option is Clicked", Toast.LENGTH_SHORT).show();
}
else if (id ==R.id.placeTwo) {
Toast.makeText(this, "Game Placeholder Two option is Clicked", Toast.LENGTH_SHORT).show();
}
else if (id ==R.id.placeThree) {
Toast.makeText(this, "Game Placeholder Three option is Clicked", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Game Class
public class Task3Activity extends GameCentral {
public Button rtnBtn;
// button to return to Game Central at any point when clicked
public void init() {
rtnBtn = (Button)findViewById(R.id.returnBtn);
rtnBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent homeScreen = new Intent(Task3Activity.this, GameCentral.class);
startActivity(homeScreen);
}
});
}
String value;
int attempts = 0;
final int maxAttempts = 1;
Random randGen = new Random();
int ranNum;
private SoundPlayer sound;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task3_layout);
init();
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getString("Value1");
if (value != null) {
TextView dataRcvd = (TextView) findViewById(R.id.playerNameEntered);
dataRcvd.setText(value );
}
}
sound = new SoundPlayer(this);
final TextView textResponse = (TextView) findViewById(R.id.txtResponse);
final TextView guessText = (TextView) findViewById(R.id.txtAnswer);
final EditText userGuess = (EditText) findViewById(R.id.etNumber);
Button pressMe = (Button) findViewById(R.id.btnGuess);
final Button rtnButton = (Button) findViewById(R.id.returnBtn);
int min = 1;
int max = 19;
randGen = new Random();
// Generate number once
ranNum = randGen.nextInt(max - min + 1) + min;
final Toast toast = Toast.makeText(getApplicationContext(), "Please guess between 0 and 20", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 350);
// When the button is clicked, it shows the text assigned to the txtResponse TextView box
pressMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean correct = false;
final AlertDialog.Builder alert = new AlertDialog.Builder(Task3Activity.this);
alert.setTitle("Unlucky");
alert.setCancelable(false);
alert.setMessage("You have guessed incorrectly three times. " +
"The answer was " + ranNum + ". " + "Would you like to play again?")
//.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//dialog.dismiss();
Intent i = new Intent(Task3Activity.this, Task3Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
alert
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
//Task1Activity.this.finish();
dialog.dismiss();
//finishAffinity();
Intent toy = new Intent(Task3Activity.this, GameCentral.class);
startActivity(toy);
}
;
});
final AlertDialog.Builder alert2 = new AlertDialog.Builder(Task3Activity.this);
alert2.setTitle("You Did It!");
alert2.setCancelable(false);
alert2.setMessage("The answer was " + ranNum + ". " + "Would you like to play again?")
//.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//dialog.dismiss();
Intent i = new Intent(Task3Activity.this, Task3Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
alert2
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
//Task1Activity.this.finish();
dialog.dismiss();
Intent toy = new Intent(Task3Activity.this, GameCentral.class);
startActivity(toy);
//finishAffinity();
}
;
});
int userNumber = Integer.parseInt(userGuess.getText().toString());
if ((userNumber < 1) || (userNumber > 19)) {
//guessText.setText("Please guess between 0 and 20");
//guessText.setBackgroundColor(Color.WHITE);
toast.show();
} else if (userNumber < ranNum) {
guessText.setText("Your answer is too low. Guess again!");
guessText.setBackgroundColor(Color.YELLOW);
//sound.playBuzzerSound();
} else if (userNumber > ranNum) {
guessText.setText("Your answer is too high. Guess again!");
guessText.setBackgroundColor(Color.RED);
//sound.playBuzzerSound();
} else if (userNumber == ranNum) {
ranNum = randGen.nextInt(20);
//guessText.setText("You did it!");
//guessText.setBackgroundColor(Color.WHITE);
correct = true;
alert2.show();
sound.playApplauseSound();
}
if (attempts++ > maxAttempts && !correct) {
alert.show();
sound.playBooSound();
//guessText.setText("You have guessed incorrectly three times. The answer was " + ranNum);
} else if (correct) {
} else {
String randText = "";
randText = Integer.toString(ranNum);
textResponse.setText("");
userGuess.setText("");
}
}
}
);
}
}
Because you are not passing a Bundle in your intent, you don't need to use getIntent().getExtras();.
GameCentral:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.guessingGame:
Intent intent = new Intent(GameCentral.this, Task3Activity.class);
intent.putExtra("PlayerName", "Name");
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Task3Activity:
public class Task3Activity extends AppCompatActivity {
...
#Override
public void onCreate(Bundle savedInstanceSate) {
...
String playerName = getIntent().getStringExtra("PlayerName");
if (playerName != null) {
TextView dataRcvd = (TextView) findViewById(R.id.playerNameEntered);
dataRcvd.setText(playerName);
}
}
...
}
intent.putExtra("PlayerName", "Name"); where "Name" is the string for where you get the player's name from. You need to create the EditText object within the scope of the Activity if you want to get the player's name from a text box.
if (id == R.id.guessingGame) {
startActivity(new Intent(this, Task3Activity.class));
switch (item.getItemId()) {
case R.id.playerNameEntered:
Bundle b = getIntent().getExtras();
value= b.getString("playerNameInput");
startActivity(this, Task3Activity.class).putExtra("playerNameInput", value);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In my case, I use bundle to get string in onOptionsItemSelected function evertime.
To active this, should send data(playerNameInput) when you change activity everytime.

Cannot parse "#" sign in dialer

I'm making a custom dialer and everything till now was perfect. Now while testing, I saw that the "#" sign was not working properly. It shows on the dialer but while calling, it goes away. For eg, if I dial *121#, it becomes *121 when the call is being made through the stock dialer. Here is the code for my activity-
public class DialPadActivity extends Activity {
private EditText numberField = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeButtonArrowClickListeners();
}
public void dialButtonClick(View v) {
int buttonId = v.getId();
putNumberToEditText(buttonId);
}
public void buttonPhone_click(View v) {
if (numberField != null) {
String phone = numberField.getText().toString();
String uriString = "tel:" + phone;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uriString));
startActivity(intent);
}
}
public void putNumberToEditText(int buttonId) {
switch (buttonId) {
case R.id.dial_button0:
putNumber("0");
break;
case R.id.dial_button1:
putNumber("1");
break;
case R.id.dial_button2:
putNumber("2");
break;
case R.id.dial_button3:
putNumber("3");
break;
case R.id.dial_button4:
putNumber("4");
break;
case R.id.dial_button5:
putNumber("5");
break;
case R.id.dial_button6:
putNumber("6");
break;
case R.id.dial_button7:
putNumber("7");
break;
case R.id.dial_button8:
putNumber("8");
break;
case R.id.dial_button9:
putNumber("9");
break;
case R.id.dial_button_s:
putNumber("*");
break;
case R.id.dial_button_p:
putNumber("#");
break;
}
}
public void putNumber(String number) {
if (numberField == null) {
numberField = (EditText) findViewById(R.id.phone_number_field);
}
numberField.append(number);
}
private void initializeButtonArrowClickListeners() {
ImageButton buttonArrow = (ImageButton) findViewById(R.id.button_arrow);
buttonArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (numberField != null && numberField.length() > 0) {
String previousNumbers = numberField.getText().toString();
String numbersMinusTheLast = previousNumbers.substring(0,
numberField.length() - 1);
numberField.setText(numbersMinusTheLast);
}
}
});
buttonArrow.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (numberField != null) {
numberField.setText("");
}
return false;
}
});
}
Where am I doing the wrong?
Use URLEncoder.encode(string, "UTF-8");
For example:
String uriString = "tel:" + URLEncoder.encode(phone, "UTF-8");
Replace your # symbol with %23.
That is: *121%2311 instead of *121#11
String uri = "tel:" + "*6133%235345";
Intent intent;
intent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(intent);
the output for this is : *6133#5345

Categories

Resources