I'm teaching myself to write an Android app. I use the following code to successfully choose the different database for my app:
public void manageDB()
{
setContentView(R.layout.dbmanager);
ScrollView ll = (ScrollView) findViewById(R.id.lstDb);
final RadioGroup rg = new RadioGroup(this);
rg.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT));
rg.setOrientation(RadioGroup.VERTICAL);
for (int i=0; i < mDBList.items.size(); i++)
{
RadioButton rb = new RadioButton(this);
rb.setId(VIEW_RADIO_ID + i);
rb.setText(mDBList.items.get(i).dictionaryName);
rb.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rg.addView(rb,i);
if (mDBFile != null && mDBFile.fileName.equals(mDBList.items.get(i).fileName))
{
rg.check(VIEW_RADIO_ID + i);
}
}
ll.addView(rg);
Button btnOk = (Button) findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.main);
int selectedIndex = rg.getCheckedRadioButtonId();
if (selectedIndex < 0)
{
selectedIndex = 0;
}
mDBFile = mDBList.items.get(selectedIndex - VIEW_RADIO_ID);
savePreferences();
setContentView(R.layout.main);
menuMain();
}
});
}
With this code, I can click on a radio button to choose a database, and the click on the OK button to confirm and return to the main screen. Now I want to assign a button on the main screen to do this task so that I don't have to leave the main screen when choosing a database.
Since I have two pieces of database, ideally I want a two-way button to do this task. I mean when I click on the button, db_1 is chosen. When the button is clicked again db_2 is chosen, and vice versa.
I have no idea how to adapt this code to meet this requirement of mine. I wonder if you guys can give me a little help. Thank you very much in advance.
If your need is to change database in one click instead of two, you could use a radiogroup containing your 2 radiobuttons, and listen for checked radiobutton change.
Then in onCheckedChange() you load the right database.
Why don't you use "Switch" I think it will solve your purpose. Look at the link [here:][1]
[1]: http://developer.android.com/reference/android/widget/Switch.html Or you can also use the toggle buttons. Hope it helps.
Related
I am new to Android Studio. I am working on a ArCore sample project.
I want to add a button, when clicked the plane finding mode will be vertical otherwise horizontal.
Before voting negative, I have gone through these posts:
How can I write “if button clicked” in an If statement in android studio?
Checking if a button has been clicked
The following is the snippet of my code:
if(y > 0){
Config config = new Config(session);
//config.setPlaneFindingMode(Config.PlaneFindingMode.VERTICAL); If button clicked, this
config.setPlaneFindingMode(Config.PlaneFindingMode.HORIZONTAL); //otherwise this
session.configure(config);
Globals.notRecording = true;
}
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Just use this code in OnClickListener.
when you tap button call this
config.setPlaneFindingMode(Config.PlaneFindingMode.VERTICAL);
Here is my code, I want to unselect a radio button when I click on it again. Since I have some code that only works when none of the radiobuttons are selected.
if (TablesOn == true) {
TablesOn = false;
} else {
TablesOn = true;
}
if (jRadioButton1.isVisible()==true) {
jRadioButton1.setVisible(false);
jRadioButton2.setVisible(false);
jRadioButton3.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
} else {
jRadioButton1.setVisible(true);
jRadioButton2.setVisible(true);
jRadioButton3.setVisible(true);
jLabel3.setVisible(true);
jLabel4.setVisible(true);
jLabel5.setVisible(true);
}
To deselect a RadioButton which is inside of RadioGroup and be able to then select it again, firstly get your RadioGroup:
RadioGroup rg = (RadioGroup) findViewById(R.id.your_radio_group_name_in_layout);
And then do:
rg.check(-1);
Of course, you need some code to save last checked button number, and if it is pressed again, deselect it using the code above.
Hope you all are well.
I would like to hide a Button (btnAppointment) if my test readings in my EditText are below or equal 13.5 or greater than 33 in my Android Application window. The Button (btnAppointment) should only appear on the screen if my EditText field inputted values are between 13.60 and 32.99. And not be shown onscreen if outside these parameters.
I was wondering whether an IF statement with button.setEnabled(false); would do the trick and if so where about would I need to input it into my Code. Whether it be protected void onCreate(Bundle savedInstanceState) or Create my own public void appointmentTeacherOnClick?
Below I have inserted my code for calculating and displaying my Inputted Test field prompts.
public void calculateTest(View v){
String status;
test = Double.parseDouble(edtData.getText().toString());
String result = String.format("%.2f", test);
Log.d("MyActivity", result);
if( test < 9.5) {
status = "Normal - Well Done =)";
} else if (test >= 9.5 && test < 13.5){
status = "Caution - Keep on Track =|";
} else if (test >= 13.5 && test < 33.0) {
status ="Action Needed =(";
} else {
status = "Normal Results are between 0 - 33";
}
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Result Feedback...");
alertDialog.setMessage(status);
alertDialog.setButton("Acknowledged", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
if you want to control the visibility of button, enabling or disabling the button won't help. Enabling/Disabling property will still show the button and will only decide whether button can be clicked or not.
You need two things to achieve your task,
EditText - text change listener
Button Visibility property
How to do both the task are already answered on SO, here is the most popular one,
How to hide a button programmatically? (For hiding button).
Counting Chars in EditText Changed Listener (For creating edittext change listener)
To hide a view you need to use setVisibility() and set it to View.INVISIBLE
Button btnAppointment = (Button) findViewById(R.id.btn_appointment);
btnAppointment.setVisibility(View.INVISIBLE);
In your XML file:
<Button
android:id="#id+/btn_appointment
...
/>
See this question: How to hide a button programmatically?
I have an up button which I want to move up onClick but when I click on it it moves way to far and gets to the end of the screen and then shrinks down untill you cant see it. It moves up too much, but why? I only increase it by one unit?
final Button upbutton = (Button)findViewById(R.id.upbutton);
upbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
upbutton.getLayoutParams();
mParams.bottomMargin += 1;
upbutton.setLayoutParams(mParams);
}
});
}
Because you're not assigning mParams to your buttons params.
mParams = upbutton.getLayoutParams();
You seem to be increasing parameters for your RelativeLayout then assigning those to your button. So the button gets confused. Try looking for a set margin option or something on the actual button view.
I have a button that I have created in code, which has a listener for Click events. Every time that the button is clicked, it should generate another button and add it below the original button. However, no matter how many times I click the first button, it will only add a dynamic button once, and not add any more.
Here is my coding:
public class DynaminControlActivity extends Activity {
private RelativeLayout container;
private int mainIdCnt = 0;
private int mainId = 100;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createMainButton();
}
public void createMainButton() {
container = (RelativeLayout) findViewById(R.id.workLayout);
Button b = new Button(this);
b.setId(mainIdCnt + mainId);
CharSequence text = "Main +";
b.setText(text);
container.addView(b);
if (mainId > 0) {
mainId++;
}
b.setOnClickListener((new View.OnClickListener() {
public void onClick(View v) {
createDynamicButton();
}
}));
}
public void createDynamicButton() {
container = (RelativeLayout) findViewById(R.id.workLayout);
Button b = new Button(this);
CharSequence text = "Main +";
b.setText(text);
RelativeLayout.LayoutParams relLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayout.addRule(RelativeLayout.BELOW, mainIdCnt + mainId);
container.addView(b, relLayout);
if (mainId > 0) {
mainId++;
}
}
A few things...
If your main layout is a LinearLayout, you shouldn't need to add a rule to indicate that the button should appear underneath the existing button - it will automatically be added to the very bottom (vertical alignment) or very right (horizontal alignment) of the layout.
All your buttons have the same text. Are you certain that you're clicking the first button each time? I note that only your first button has a listener on it, so if you're accidentally clicking one of the other buttons then nothing will happen.
If you're intending to add multiple buttons, it will quickly expand to be larger than the screen size, so you should make sure that your main layout is within a ScrollView so that you can see all the buttons you add
The call to setId() might be stuffing around with the internal workings of Android. Rather than setting an ID, you should let Android generate the ID automatically, and just retrieve that value if you need to reference it.