if(checkbox1.isChecked())
{
String text=checkbox1.getText().toString();
}
For all of your checkboxes use :
if(checkbox1.isChecked && checkbox2.isChecked)
{
//Do your stuff
}
For one of checkboxes:
if(checkbox1.isChecked || checkbox2.isChecked)
{
//Do your stuff
}
Related
I have RecyclerView which contains user bookmarks. The plan is bookmarked item will be marked with certain icon. This is my code in onBindViewHolder():
// ...
if (bookmarks != null) {
for (BookmarkModel bookmarkData : bookmarks) {
if (bookmarkData.getLetterId() == letter && bookmarkData.getEntryId() == entry) {
holder.imgBookmark.setVisibility(View.VISIBLE);
} else {
holder.imgBookmark.setVisibility(View.INVISIBLE);
}
}
}
However, the RecyclerView is not showing all bookmark icons, only a few of them. Currently I have 3 bookmarks yet it only shows 1 of them. I have debug it and verified that holder.imgBookmark.setVisibility(View.VISIBLE) have been called 3 times. How to update the image properly?
I forgot to put break when the letter and entry match. Because of it, only the last match would show the icon.
if (bookmarks != null) {
for (BookmarkModel bookmarkData : bookmarks) {
if (bookmarkData.getLetterId() == letter && bookmarkData.getEntryId() == entry) {
holder.imgBookmark.setVisibility(View.VISIBLE);
break;
} else {
holder.imgBookmark.setVisibility(View.INVISIBLE);
}
}
}
I'm new in android programming but I have a validation function after the user inserts some data like:source point,destination point,...etc
I have 2 spinners :source and destinations ,both include same values..and i want to be able to send a pop up if a user is selecting the same source and destination and the function is this one:
spSursa=is the source spinner
spDestinatie=is the destinations spinner
private boolean validare_Date()
{
if(spSursa.getSelectedItem().toString()!=spDestinatie.getSelectedItem().toString() )
{
if(ratb.isChecked()==false && metrorex.isChecked()==false && both.isChecked()==false)
{
Toast.makeText(getApplicationContext(), R.string.ADAUGA_RUTA_EROARE_TRANSPORT, Toast.LENGTH_SHORT).show();
return false;
}
else
{
return true;
}
}
else {
Toast.makeText(getApplicationContext(), R.string.ADAUGA_RUTA_EROARE_SURSA_DEST, Toast.LENGTH_SHORT).show();
return false;
}
}
Try to change
if(spSursa.getSelectedItem().toString()!=spDestinatie.getSelectedItem().toString() )
To
if(!spSursa.getSelectedItem().toString().equals(spDestinatie.getSelectedItem().toString()))
I started to work with Xamarin Studio a weeks ago, and could not find solution to the next problem:
created an edittext which will contains serial numbers. I'd like ro run a function after the Enter was pressed.
It's working fine, when I press Enter, the function runs without failure, but I can not modify the content of the edittext (I can't type into it).
The code:
EditText edittext_vonalkod = FindViewById<EditText>(Resource.Id.editText_vonalkod);
edittext_vonalkod.KeyPress += (object sender, View.KeyEventArgs e) =>
{
if ((e.Event.Action == KeyEventActions.Down) && (e.KeyCode == Keycode.Enter))
{
//Here is the function
}
};
This is the code of the control:
<EditText
p1:layout_width="wrap_content"
p1:layout_height="wrap_content"
p1:layout_below="#+id/editText_dolgozo_neve"
p1:id="#+id/editText_vonalkod"
p1:layout_alignLeft="#+id/editText_dolgozo_neve"
p1:hint="Vonalkód"
p1:text="1032080293"
p1:layout_toLeftOf="#+id/editText_allapot" />
I tried to use edittext_vonalkod.TextCanged with its arguments, the problem reserved. I can modify the content but can not handle Enter key.
Thanks!
The best approach would be to use the EditorAction event that is designed to be triggered on the Enter key press. It would be a code like this:
edittext_vonalkod.EditorAction += (sender, e) => {
if (e.ActionId == ImeAction.Done)
{
btnLogin.PerformClick();
}
else
{
e.Handled = false;
}
};
And to be able to change the text of the Enter button use imeOptions on your XML:
<EditText
p1:layout_width="wrap_content"
p1:layout_height="wrap_content"
p1:layout_below="#+id/editText_dolgozo_neve"
p1:id="#+id/editText_vonalkod"
p1:layout_alignLeft="#+id/editText_dolgozo_neve"
p1:hint="Vonalkód"
p1:text="1032080293"
p1:layout_toLeftOf="#+id/editText_allapot"
p1:imeOptions="actionSend" />
You need to mark the event as not being handled when the pressed key is ENTER. Place the following code inside your KeyPress handler.
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
// Code executed when the enter key is pressed down
}
else
{
e.Handled = false;
}
try this:
editText = FindViewById(Resource.Id.editText);
editText.KeyPress += (object sender, View.KeyEventArgs e) =>
{
e.Handled = false;
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
//your logic here
e.Handled = true;
}
};
Even better is to create reusable extension to EditText (EditTextExtensions.cs):
public static class EditTextExtensions
{
public static void SetKeyboardDoneActionToButton(this EditText editText, Button button)
{
editText.EditorAction += (sender, e) => {
if (e.ActionId == ImeAction.Done)
{
button.PerformClick();
}
else
{
e.Handled = false;
}
};
}
}
The Android EditText component must be focused. You can force it with:
editText.RequestFocus();
editText.KeyPress += (object sender, View.KeyEventArgs e) =>
{
if ((e.KeyCode == Keycode.Enter))
{
// `enter code here`
}
};
I have the below code:
if(cb.isChecked())
{
selectedPlanets.add(planet.getDisplayName());
}
if (!cb.isChecked())
{
selectedPlanets.remove(planet.getDisplayName());
}
testing();
}
private void testing() {
serverString.setText(null);
Iterator<String>i = selectedPlanets.iterator();
while(i.hasNext()){
String aNum1 = i.next();
serverString.append(aNum1+",");
}
I then need to iterate through selectedPlanets but the removed ones show up too. By setting the TextView null again, it works if three or more are selected. However, if two are selected, the checked planet also gets removed from the list.
EDIT: for anyone who runs into this problem, I solved it by using the else (programmer's block made me lose sense!) and then implementing some other methods that I needed to make it work.
Why don't you just do it like this?
if (cb.isChecked()) {
selectedPlanets.add(planet.getDisplayName());
} else {
selectedPlanets.remove(planet.getDisplayName());
}
Why not do this:
for(int i=0; i<selectedPlanet.size(); i++) {
if(planet.isChecked()) {
selectedPlanets.add(planet.getDisplayName());
} else {
selectedPlanets.remove(planet.getDisplayName());
}
}
What you are trying to do is
if isChecked true then add something
if(cb.isChecked())
{
selectedPlanets.add(planet.getDisplayName());
}
if isChecked is not true then remove something
if (!cb.isChecked())
{
selectedPlanets.remove(planet.getDisplayName());
}
Instead of using not true condition you must use else case like
if(cb.isChecked()) {
selectedPlanets.add(planet.getDisplayName());
} else {
selectedPlanets.remove(planet.getDisplayName());
}
I have problems when using a CheckBox in an else if statement. I have a CheckBox list like:
cbapel, cbmangga, cbjeruk, cbbelimbing.
when I tick the checkBoxes cbapel, cbmangga, and cbjeruk the result is "2" instead of "4". What's the solution?
if(MainActivity.cbapel.isChecked() && MainActivity.cbbelimbing.isChecked()) {
tx.setText("1");}
else if(MainActivity.cbapel.isChecked() && MainActivity.cbjeruk.isChecked()) {
tx.setText("2");}
else if(MainActivity.cbapel.isChecked() && MainActivity.cbmangga.isChecked()) {
tx.setText("3");}
else if(MainActivity.cbapel.isChecked() && MainActivity.cbmangga.isChecked() && MainActivity.cbjeruk.isChecked()) {
tx.setText("4");}
else {
tx.setText("NOT FOUND");
}
You are really overlooking the simple solution here, by far (assuming you are just counting the number of checked boxes)
int checked = 0;
if (MainActivity.cbapel.isChecked()) {
checked++;
}
if (MainActivity.cbbelimbing.isChecked()) {
checked++;
}
if (MainActivity.cbmangga.isChecked()) {
checked++;
}
if (MainActivity.cbjeruk.isChecked()) {
checked++;
}
if (checked == 0) {
tx.setText("NOT FOUND");
} else {
tx.setText(String.valueOf(checked));
}
If you aren't looking to count the checkboxes, then I think this follows the same logic as what you tried in your question, but accounts for one condition ever being entered.
String value = "";
boolean bool1 = MainActivity.cbapel.isChecked();
boolean bool2 = MainActivity.cbbelimbing.isChecked();
boolean bool3 = MainActivity.cbmangga.isChecked();
boolean bool4 = MainActivity.cbjeruk.isChecked();
if (bool1) {
if (bool3 && bool4) {
value = "4";
} else if (bool3 && !bool4) {
value = "3";
} else if (!bool3 && bool4) {
value = "2";
}
if (value.isEmpty() && bool2) {
value = "1";
}
} else {
value = "NOT FOUND";
}
tx.setText(value.isEmpty() ? "NOT FOUND" : value);
else if(MainActivity.cbapel.isChecked() && MainActivity.cbjeruk.isChecked()) {
Let's look at this...
Is MainActivity.cbapel.isChecked()? Yes
Is MainActivity.cbjeruk.isChecked()? Yes
So this evaluates to true and so it won't check any of the other conditional statements.
There are many different ways for this logic to flow. But, to know what is best for your situation, depends on what these variables actually mean and what you are doing.
The best advice I could give you from what you have provided is to check the most filtered situation first. So...
if(MainActivity.cbapel.isChecked() && MainActivity.cbmangga.isChecked()
&& MainActivity.cbjeruk.isChecked()) {
tx.setText("4");
}
then add the else ifs after that for the other checks.
Also, cricket has a good suggestion in his comment you are checking the same condition every time so you can remove that and wrap it all in one if to remove some duplication.
Use this listener to each check box and set your conditions accordingly
class CheckboxListeners implements CompoundButton.OnCheckedChangeListener {
private CheckBox checkbox;
CheckboxListeners(CheckBox checkbox) {
this.checkbox = checkbox;
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkbox.setChecked(isChecked);
}
}