How to display a spinner when a radio button clicked - java

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();
}
};

Related

How to add a new button in Main Activity after user clicks a button from a Popup?

I'm developing an Android app on my free time to learn about Android development. I'm trying to make a Grade/GPA Calculator App. I currently have a button called "+ New Semester" whose purpose is to open a popup where the user inputs the semester name. This can be seen in the following two images:
User clicks on "+ New Semester" and the popup appears prompting the user to add the name of that semester.
Now, what I want to do is that when the user clicks on the "Done" button a new button with the text that the user typed in into the text box is created below the "+ New Semester" button, but I can't figure out how to do it. I would appreciate any help.
This is the code I currently have:
package com.example.gradecalculator;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Private Fields
private Dialog d;
private ImageButton newSemesterButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d = new Dialog(this);
}
// When user clicks on "+ New Semester" button open a popup where the user is prompted to
// type in the Semester Name and when "Done" is clicked the new semester appears in the view
public void newSemesterPopup(View v) {
TextView closePopup;
ImageButton doneButton;
d.setContentView(R.layout.new_semester_popup);
doneButton = (ImageButton) d.findViewById(R.id.doneButton);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
closePopup = (TextView) d.findViewById(R.id.exitButton);
closePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
}
// Open Main Activity
public void openMainActivity() {
Intent main = new Intent(this, MainActivity.class);
startActivity(main);
}
}
You can achieve that by first getting the reference to the layout where you want to add your button. Lets say, the name of the layout where your button should be has the name btn_layout,
Bind the layout. (i.e. LinearLayout layout = findViewById (R.id.btn_layout); )
When the DONE button is clicked, create your new button and add it to the layout. For example:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setText("Your text from edittext");
layout.addView(btn, params);
Even better if you can declare the Button and Layout as fields
You have to get the reference from your buttons parent layout. then create a button and add it to the view. Something like this:
public void newSemesterPopup(View v) {
TextView closePopup;
ImageButton doneButton;
d.setContentView(R.layout.new_semester_popup);
doneButton = (ImageButton) d.findViewById(R.id.doneButton);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
closePopup = (TextView) d.findViewById(R.id.exitButton);
closePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myParentLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
params = (LinearLayout.LayoutParams) new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
yourEditText = d.findViewById(R.id.YourEditText);
myNewButton = new Button(this);
String buttonText = yourEditText.text.toString();
myNewButton.setText(buttonText);
myParentLayout.addView(myNewButton, params);
d.dismiss();
}
});
d.show();
}
Just make that new button on main screen and set visibility to false = newButton.setVisibility(false);
and if you press button done on popup just set visibility to true hope that will help, not sure if that's what you mean

going back to previous layout makes the button stop working

I am testing my knowledge of android studio and seem to be running into a problem I am trying to make an app that takes the value of a edittext and puts that value into a textview when you click a button. After that it brings you to a page with a back button on it and when I click that it brings me to the first page. But when I try to click the first button it doesn't work anymore.
Code:
public class TestApp extends AppCompatActivity {
private EditText ET;
private Button Add;
private TextView TV;
public String Array[] = new String[9999999];
private Button GoBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
ET = (EditText) findViewById(R.id.ET);
Add = (Button) findViewById(R.id.Add);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getC = ET.getText().toString();
setContentView(R.layout.value);
TV = (TextView) findViewById(R.id.TV);
GoBack = (Button) findViewById(R.id.GoBack);
if (getC.length() > 0){
for (int i = 0; i < Array.length; i++){
if (Array[i] == null){
Array[i] = getC;
TV.setText(Reminders[i]);
}
}
}
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_calender_for_glasses);
}
});
}
});
}
}
thank you for your help.
If not necessary, don't modify the current content view. If you want multiple screens, you have to create multiple activities or fragments...
Moreover :
at the beginning, you inflate R.layout.activity_test_app
when you click on goBack button (1), you inflate R.layout.activity_calender_for_glasses and it's not the same as the first screen
(1) be careful to coding rules: https://source.android.com/setup/contribute/code-style

How do I change EditTexts when clicking next?

I have an EditText box that appears and the hint in it is actually a question that needs to be answered. Once the user enters the value and hits next, how can I make a different EditText box take the originals place. I want to do this so there is a new hint(question) and I can manipulate that input differently than with the first input.
In my example, my first EditText is enter_one and I want it to change to enter_two after the next_button is clicked.
Thank you for your help!
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String newValue = getInput.getText().toString().trim();
int input = Integer.parseInt(newValue);
System.out.println("Your value is " +input);
}
});
Create two edit text in XML layout, and align both in same position, one over another. Then declare the android:visibility="gone" for edittext enter_two.
Then onClick of next should be:
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
final EditText getInput_two = (EditText)findViewById(R.id.enter_key_two);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String newValue = getInput.getText().toString().trim();
int input = Integer.parseInt(newValue);
System.out.println("Your value is " +input);
getInput_two.setVisibility(View.VISIBLE);
getInput.setVisibility(View.GONE);
}
});
You can just change the hint and clear the EditText :
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getInput.setHint(newQuestion);
getInput.setText("");
}
});

Only click Checkbox in clickable Relative Layout

I'm trying to design a layout with a checkbox inside of a relative layout. The relative layout is clickable and works as it is supposed to, but when the checkbox is click, it runs the checkbox code, and the relative view code.
Is there anyway to ignore the relative layout's OnClickListener while running the checkbox?
RelativeLayout holder = new RelativeLayout(context);
contactHolder.setId(View.generateViewId());
TextView name = new TextView(context);
final CheckBox checkBox = new CheckBox(context);
checkBox.setId(View.generateViewId());
name.setText(contact.getName());
name.setId(View.generateViewId());
holder.addView(name);
holder.addView(checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!checkBox.isChecked()) {
checkBox.setChecked(true);
}
else if (checkBox.isChecked()){
checkBox.setChecked(false);
}
}
});
holder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!checkBox.isChecked()) {
checkBox.setChecked(true);
}
else if (checkBox.isChecked()){
checkBox.setChecked(false);
}
}
});
userView.addView(holder);
}
use this in Relativelayout
android:clickable="true"
Remove the listener from the check box. Then change the logic in RelativeLayout's listener to act based on the state of the check box.

Getting the selected radio button inside OnClick for a button

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

Categories