Reputation:
I need some help with a small code. Basically I have a button and some generated checkboxes (don't know how many). The button should be hidden and disabled as long as no check-box is checked. When one or more check-boxes are checked the button should be shown and enabled . How can I do this?
Upvotes: 0
Views: 1466
Reputation: 21
putting in an example ...
XML file:
<CheckBox android:id="@+idMain/check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+idMain/check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+idMain/check3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+idMain/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
Activity:
public class MainActivity extends Activity {
LinearLayout idLayoutThisXml;
Button btn;
public boolean verifyChecked(){
idLayoutThisXml = (LinearLayout) findViewById(idMain.layout);
for (int i = 0; i < idLayoutThisXml.getChildCount(); i++) {
View v = idLayoutThisXml.getChildAt(i);
if(v instanceof CheckBox){
if(((CheckBox)v).isChecked()){
return true;
}
}
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(idMain.button);
if(verifyChecked()){
btn.setVisibility(View.VISIBLE);
}
else{
btn.setVisibility(View.INVISIBLE);
}
}
}
so I have a layout that has children of type CheckBox, and testing will all CheckBox llinearLayout, meet at least one marked. the button becomes visible.
Upvotes: 1
Reputation: 21
you have a check box in the XML ok? handle the checkBox in activity with
CheckBox c = (CheckBox) findViewById (idThisCheckBoxInXml)
and an
if (c.isActivated ()) {
botao.setVisibility (View.INVISIBLE) or botao.setVisibility (View.VISIBLE)
}
would be something like this? excuse my bad english ...
Upvotes: 0
Reputation: 21
would be something like this?
public class MainActivity extends Activity {
LinearLayout idLayoutThisXml;
Button btn;
public boolean verifyChecked(){
idLayoutThisXml = (LinearLayout) findViewById(idCadastro.linearLayout);
for (int i = 0; i < idLayoutThisXml.getChildCount(); i++) {
View v = idLayoutThisXml.getChildAt(i);
if(v instanceof CheckBox){
if(((CheckBox)v).isChecked()){
return true;
}
}
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(idMain.button);
if(verifyChecked()){
btn.setVisibility(View.VISIBLE);
}
else{
btn.setVisibility(View.INVISIBLE);
}
}
}
Upvotes: 1
Reputation: 44571
Simply create a counter variable
public class MyActivity extends Activity
{
int counter = 0;
//onCreate() and other code
then set an onCheckedChangedListener()
on your CheckBoxes
and increment/decrement the value based on if the box was checked
onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
counter++;
}
else
{
counter--;
}
/* if (counter > 0)
change visibility
*/
}
Obviously you cannot copy/paste this code but should give you a pretty good idea.
Upvotes: 1
Reputation: 46
Set the button visibility to invisible
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:visibility="invisible"/>
then call
Button btn = (Button)findViewById(R.id.thebuttonid);
btn.setVisibility(View.VISIBLE); //View.GONE, View.INVISIBLE are available too.
When conditions are met. http://developer.android.com/reference/android/view/View.html
Conditionally displaying button
Upvotes: 0
Reputation: 1469
myButton.setVisibility(View.INVISIBLE);
And then, in the CheckBox
's OnClickListener
, do myButton.setVisibility(View.VISIBLE);
Upvotes: 0