Getting the selected radio button inside OnClick for a button - java

I am building a simple app
which allow the user to enter his name and select the radio button "with name"
and a toast shows , Hello Name
and the other radio for guest to show hello guest , so How I can get the selected radio inside the OnClick do I need to get them from FindViewByID ??
here my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.button1) {
//getting the selected radio if its radio 0
Toast.makeText(this, "Hello Guest", Toast.LENGTH_LONG).show();
}
//getting the selected radio if its radio 1
EditText txt = (EditText) findViewById(R.id.editText1);
Toast.makeText(this, "Hello " + txt.toString(), Toast.LENGTH_LONG).show();
}

Try this instead:
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//YOUR CODE HERE
}
});
Put that in onCreate() and see how that works out for you. Have you also tried seeing if the Id of View v passed into onClick is actually the correct view? To see this, use log to check the Id.

better you directly check the radio button state on button click listener:
#Override
public void onClick(View v)
{
if(v == button)
{
if(rb1.isChecked() == true)
Toast.makeText(this, "Hello"+rb1.getText(), Toast.LENGTH_LONG).show();
if(rb2.isChecked() == true)
Toast.makeText(this, "Hello"+rb2.getText(), Toast.LENGTH_LONG).show();
}
}
where
rb1=(RadioButton)findViewById(R.id.optname);
rb2=(RadioButton)findViewById(R.id.optguest);
FOR MORE

Related

How to press multiple buttons at the same time?

I am developing an android application that consists of a 2 dimensional array of buttons. when i touch the buttons, at a time only one button is pressed. i want to press multiple buttons at the same time. please give me a sample code.
You can click a button programmatically by using the button.performClick() method inside your other onclicklistener button
To press another button(button2), just call button2.performClick() from button1 listeners onClick() method.
Here is an example:
Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "button 1 clicked", Toast.LENGTH_SHORT).show();
button2.performClick();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "button 2 clicked", Toast.LENGTH_SHORT).show();
// Do something
}
});

Android Studio cant resolve error setOnClickListener

Hi I can't seem to build this.. It states that it cannot resolve the OnClickListener. The onClick action is performed the back button that goes to the main activity.
Button bnCompute = (Button) this.findViewById(R.id.bnCompute);
bnCompute.setOnClickListener(new View.OnClickListener());
{
#Override
public void onClick (View view){
Toast.makeText(MainActivity.this, "You Compute All!", Toast.LENGTH_LONG).show();
EditText etBeauty = (EditText) MainActivity.this.findViewById(R.id.etBeauty);
EditText etBody = (EditText) MainActivity.this.findViewById(R.id.etBody);
EditText etIntelligence = (EditText) MainActivity.this.findViewById(R.id.etIntelligence);
int total = Integer.parseInt(String.valueOf(etBeauty.getText())) + Integer.parseInt(String.valueOf(etBody.getText()))
+ Integer.parseInt(String.valueOf(etIntelligence.getText()));
Intent actSummary = new Intent(MainActivity.this, Score.class);
actSummary.putExtra("total", Integer.toString(total));
MainActivity.this.startActivity(actSummary);
}
}
You have implemented onClickoutside the scope of listener.
It should be something like this below :
Button button = (Button) findViewById(R.id.button1);
//Your mistake is on this line.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});

How to display a spinner when a radio button clicked

I want to display a spinner on the screen only if a radio button is clicked but I couldn't find a solution. Here is my current code.
RadioGroup radio_grp=(RadioGroup) rootView.findViewById(R.id.radio_grp);
RadioButton rb1 = (RadioButton) rootView.findViewById(R.id.radioButton1);
RadioButton rb2 = (RadioButton) rootView.findViewById(R.id.radioButton2);
View.OnClickListener button1Listener = new View.OnClickListener() {
public void onClick(View v) {
// Toast message etc.
}
};
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
// I want to show a spinner if they click the second radio button
}
};
rb1.setOnClickListener(button1Listener);
rb2.setOnClickListener(button2Listener);
return rootView;
First initialize the spinner like:
Spinner mSpinner = (Spinner) rootView.findViewById(R.id.mSpinner);
Of course, before that, add a spinner in your layout somewhere, and set it's android:visibility="invisible" if the first selected radio button is 1, else set it to visible. Then add listeners:
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
// I want to show a spinner if they click the second radio button
if (v.getId() == R.id.radioButton1) mSpinner.setVisibility(View.INVISIBLE);
if (v.getId() == R.id.radioButton2) mSpinner.setVisibility(View.VISIBLE);
}
};
If you don't want it to occupy any space once it's not on the screen, use View.GONEinstead of View.INVISIBLE. Refer here for the difference.
Put ((Spinner) findViewById(R.id.mySpinner)).performClick(); in second radiobutton click
View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick(View v) {
((Spinner) findViewById(R.id.mySpinner)).performClick();
}
};

Can't show Spinner DropDown list inside a dialog

I have created a button "settings", when i click in, a dialog dispaly for authentication.
I just make a test, if the edit text is empty the dialog dismiss else another dialog box display which contains a spinner.
Here's the code:
case R.id.bsettings:
// Create Object of Dialog class
final Dialog login = new Dialog(MainActivity.this);
// Set GUI of login screen
login.setContentView(R.layout.login_dialog);
login.setTitle("Settings connection");
// Init button of login GUI
Button btnLogin = (Button) login.findViewById(R.id.btn_set_Login);
Button btnCancel = (Button) login.findViewById(R.id.btn_set_Cancel);
final EditText Id = (EditText)login.findViewById(R.id.id_setting);
final EditText txtPassword = (EditText)login.findViewById(R.id.Password_setting);
// Attached listener for login GUI button
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(Id.getText().toString().trim().length() > 0 && txtPassword.getText().toString().trim().length() > 0)
{
// Validate Your login credential here than display message
Toast.makeText(MainActivity.this,
"Login Sucessfull", Toast.LENGTH_LONG).show();
// Redirect to dashboard / home screen.
login.dismiss();
final Dialog settingdialog = new Dialog(MainActivity.this);
settingdialog.setContentView(R.layout.setting_dialog);
settingdialog.setTitle("Settings Menu");
spinner = (Spinner)findViewById(R.id.languagespinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,language);
spinner.setAdapter(adapter);
settingdialog.show();
}
else
{
Toast.makeText(MainActivity.this,
"Please enter Username and Password", Toast.LENGTH_LONG).show();
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
login.dismiss();
}
});
// Make dialog box visible.
login.show();
login.getWindow().setGravity(Gravity.TOP);
break;
The problem is when i click in a login button the second dialog didn't display.
Should you tell me what's wrong in my code ?
You find the spinner directly.
Chanage
spinner = (Spinner)findViewById(R.id.languagespinner);
to
spinner = (Spinner)settingdialog.findViewById(R.id.languagespinner);

Toast.. on android

Hello can you please help me.. I would like to add toast on my code.. But I dont know where to put it.. I just want toast to appear after pressing button..
public class Question1 extends Fragment{
RadioButton q1a2;
Button btn1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.question1, null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
q1a2 = (RadioButton)getView().findViewById(R.id.q1a2);
btn1 = (Button)getView().findViewById(R.id.btnq1);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
if (q1a2.isChecked()){
editor.putInt("answer_value", 1);
} else {
editor.putInt("answer_value", 0);
}
editor.commit();
}
});
}
}
Inside
onClick(View v){
// some else
// showing toast
Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).show();
}
On click display toast
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(getActivity(),"Button pressed",Toast.LENGHT_SHORT).show();
...//rest of the code
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
To Display Toast
Do like this on your onClick of the button listener
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* here ->*/ Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_LONG).show();
}
You can very easily add a Toast inside the OnClickListener. When the listener is invoked, the code inside onClick() is executed, so you could modify this to include your toast:
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
Toast.makeText(getActivity(), "onClick() executed", Toast.LENGTH_SHORT).show();
...
}
});
Bear in mind that Toast.makeText() simply creates a new Toast object. Invoking show() on that object will cause it to appear. The duration of the toast can be controlled using Toast.LENGTH_SHORT or Toast.LENGTH_LONG.
I'd suggest getting a little more acquainted with Android by understanding how events and listeners work.

Categories