How to make checkbox visible only after the first checkbox is checked - android

Am still a learner in android, please am trying to do the following in android. I want my checkbox to show my other checkbox only after I have click the first checkbox, but if the first is unchecked the rest should be invisible. For example Class 1: checkbox, should only show Class 2:checkbox only when Class 1: checked has been selected and so on for the rest checkbox. Please find my code below. Am willing to accept answer immediately once it works
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.chk_clas1:
//do stuff
if (chk_clas1.isChecked()) {
if(c1.equals("0")){adddate(txt_clas1);}
clas="1";
fdate=txt_clas1.getText().toString();
} else {
txt_clas1.setText("");
}
break;
case R.id.chk_clas2:
//do stuff
if (chk_clas2.isChecked()) {
if(c2.equals("0")) {adddate(txt_clas2);}
clas="2";
fdate=txt_clas2.getText().toString();
} else {
txt_clas2.setText("");
}
break;
case R.id.chk_clas3:
//do stuff
if (chk_clas3.isChecked()) {
if(c3.equals("0")){
adddate(txt_clas3);}
clas="3";
fdate=txt_clas3.getText().toString();
} else {
txt_clas3.setText("");
}
break;
case R.id.chk_clas4:
//do stuff
if (chk_clas4.isChecked()) {
if(c4.equals("0")){
adddate(txt_clas4);}
clas="4";
fdate=txt_clas4.getText().toString();
} else {
txt_clas4.setText("");
}
break;
case R.id.chk_clas5:
//do stuff
if (chk_clas5.isChecked()) {
if(c5.equals("0")){
adddate(txt_clas5);}
clas="5";
fdate=txt_clas5.getText().toString();
} else {
txt_clas5.setText("");
}
break;
case R.id.chk_clas6:
//do stuff
if (chk_clas6.isChecked()) {
if(c6.equals("0")){
adddate(txt_clas6);}
clas="6";
fdate=txt_clas6.getText().toString();
} else {
txt_clas6.setText("");
}
break;
case R.id.chk_service1:
//do stuff
if (chk_service1.isChecked()) {
if(s1.equals("0")){
adddate(txt_service1);}
sday="1";
sdate=txt_service1.getText().toString();
} else {
txt_service1.setText("");
}
break;
case R.id.chk_service2:
//do stuff
if (chk_service2.isChecked()) {
if(s2.equals("0")){
adddate(txt_service2);}
sday="2";
sdate=txt_service2.getText().toString();
} else {
txt_service2.setText("");
}
break;
case R.id.chk_service3:
//do stuff
if (chk_service3.isChecked()) {
if(s3.equals("0")){
adddate(txt_service3);}
sday="3";
sdate=txt_service3.getText().toString();
} else {
txt_service3.setText("");
}
break;
case R.id.chk_service4:
//do stuff
if (chk_service4.isChecked()) {
if(s4.equals("0")){
adddate(txt_service4);}
sday="4";
sdate=txt_service4.getText().toString();
} else {
txt_service4.setText("");
}
break;

This is not code, just an idea ..
Something out of my head:
//Initially
chkBx2.setVisibility(View.INVISIBLE);
onCheckListener-> //just showing idea, not code
if(chkbx1.isChecked()){
chkBx2.setVisibility(View.VISIBLE)
}

Related

How to clean some of EditText?

My app has some instances of EditText. I have to clean all the EditTexts when I
click the button. I need to write a condition cleaning all the EditTexts
in which some of EditTexts are empty. How I will do it?
public class Fragment1 extends Fragment implements View.OnClickListener
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.fragment1,null);
seekBar1 = v.findViewById(R.id.fragment1SeekBar1);
seekBar2 = v.findViewById(R.id.fragment1SeekBar2);
seekBar3=v.findViewById(R.id.fragment1SeekBar3);
edTxt1=v.findViewById(R.id.fragment1EditText1);
edTxt2=v.findViewById(R.id.fragment1EditText2);
edTxt3=v.findViewById(R.id.fragment1EditText3);
fragment1button1=v.findViewById(R.id.fragment1Button1);
fragment1button1.setOnClickListener(this);}
#Override
public void onClick(View view){
int val1 = Integer.parseInt( edTxt1.getText().toString());
int val2=Integer.parseInt( edTxt2.getText().toString());
int val3=Integer.parseInt( edTxt3.getText().toString());
switch(view.getId()){
case R.id.fragment1Button1:
if(edTxt1.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
}else if(edTxt2.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();}
else if (edTxt3.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();}
break;}
}
}
If you want to set 'EditText` is empty just use
if(edTxt1.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
} else if(edTxt2.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
} else if (edTxt3.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
}
break;
or if you want to remove edittext
if (edTxt1.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
} else if(edTxt2.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
} else if (edTxt3.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
}
break;
if(edTxt1.equals(""))
Your conditions are wrong. They should look like this:
if(edTxt1.getText().toString().equals(""))
If you want to set text of an edit text to null:
edTxt1.setText("");
Otherwise you can disappear it setting to invisible.
edTxt1.setVisibility(View.INVISIBLE);
Try this way
switch(view.getId())
{
case R.id.fragment1Button1:
if (TextUtils.isEmpty(edTxt1.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
} else if (TextUtils.isEmpty(edTxt2.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
} else if (TextUtils.isEmpty(edTxt3.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
}
break;
}
For every EditText do it like this
if (!edTx.getText().toString().equals("")) {
edTx.setText("");
}

How to validate which checkbox is checked?

I am new to android ,Here I have 7 check boxes in my application and I have customized it as in the image .Each checkbox is represents a day of the week .
What I want to do is ,If a user clicked on a checkbox the text of the clicked checkbox should appear on the above TextView (by default if there is any checkbox is not clicked Textview text should be as "Never").
Here the Textview text should be in a ordered way ,I mean If I select all the check boxes randomly but the TextView text should be in a ordered way like Sun,Mon,.....Sat.
I know how to validate a checkbox is checked or not ,but When it comes to the above situation I don't know how it should be .
Can anyone help me to get this .
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
public void onClick(View v) {
switch (v.getId()) {
case R.id.mSun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
Toast.makeText(getApplicationContext(),"Sun is clicked",Toast.LENGTH_LONG).show();
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mMon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mTue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mWed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mThu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mFri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mSat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}
I suggest to create one checkbox listener for all checkboxes which will iterate over them and build string for textview
Something like this:
CheckBox sunday = ...;
CheckBox monday = ...;
//......
CheckBox saturday = ...;
TextView label = ...;
CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
List<String> days = new ArrayList<>();
if (sunday.isChecked()) {
days.add("Sun");
}
if (monday.isChecked()) {
days.add("Mon");
}
//....
if (saturday.isChecked()) {
days.add("Sat");
}
if (days.size() != 0) {
label.setText(TextUtils.join(", ", days));
} else {
label.setText("Never");
}
}
};
sunday.setOnCheckedChangeListener(changeListener);
monday.setOnCheckedChangeListener(changeListener);
//......
saturday.setOnCheckedChangeListener(changeListener);
It really depends how you have the text set... but you can do something like.
if (checkBox.isChecked()) {
System.out.println("checkBox clicked");
}
try this for every checkbox.
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
Log.i("Checkbox", "Checkbox selected.");
}else{
Log.i("Checkbox", "Checkbox Not selected.");
}

Stop a method till condition is true

I'm developing an Android app and I have a question:
Is there a way to pause a method until the user presses a view? I am considering doing something like this:
Make a boolean for my class called "wait".
When someone clicks on the view, it will change the boolean to true.
Finally, in the main method, do something like:
while (!wait)
{
//do nothing
}
Is there any better way to do this?
I suppose you are doing it like this...
boolean wait = false;
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
doSomething();
break;
case R.id.button2:
wait = true;
break;
}
}
public void doSomething() {
//
// Do Task 1
//
while(!wait) {}
//
// Do Task 2
//
}
Instead, you can perform it like this...
boolean viewClicked = false;
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
doSomething();
break;
case R.id.button2:
viewClicked = true;
doSomething();
break;
}
}
public void doSomething() {
if(!viewClicked)
{
//
// Do Task 1
//
}
else
{
//
// Do Task 2
//
}
}
I hope this helps. :)

How to make a single menu button do on and off?

I wanted to make a plain button in the menu act as a on/off button like a toggle button. But I'm not sure how I can make a single button act like a switch?
switch(item.getItemId()){
case R.id.switcher:
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.parseColor("#80FFFFFF"));
//I want to change the color of the background by clicking once
//and set the background color back to normal. How will I achieve this ?
return true;
Try this:
#Override
public void onClick(View v) {
if ((v.getId() == R.id.my_button){
buttonOnClick(v);
}
}
private void buttonOnClick(View v) {
switch (v.getId()) {
case R.id.my_button: {
if (v.isSelected()) {
// is selected, deselect!
v.setSelected(false);
//do your staff here
} else {
// is not selected, select!
v.setSelected(true);
//do your staff here
}
break;
}
default:
break;
}

Multiple OnClickListeners Android

I have 10 buttons set up which are the answers to ten questions. When a certain button is clicked, I have a switch statement set up in my onClick method shown below. My question is what is the best way to set up the OnClickListeners for all the buttons seeing that I need to pass 2 arrays to the onClick method in order to tell if it is correct or not? Also, I need to return and integer value. Thanks
public void onClick(View v, int[] qaarray, int questionorder) {
int x=0;
switch(v.getId())
{
case R.id.imageButton0:
if(qaarray[0] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton1:
if(qaarray[1] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton2:
if(qaarray[2] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton3:
if(qaarray[3] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton4:
if(qaarray[4] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton5:
if(qaarray[5] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton6:
if(qaarray[6] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton7:
if(qaarray[7] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton8:
if(qaarray[8] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton9:
if(qaarray[9] == questionorder){
//correct
}else{
//incorrect
}
break;
default:
throw new RuntimeException("Unknown button ID");
}
}
The OnClickListener only gives you one parameter, which is the View:
void onClick(View v);
But you don't have to pass the questions and 'order' to the method to have what you want. One of the technique you can use is the setTag() method of View:
int[] button = new int[] { R.id.imageButton1, R.id.imageButton2.... };
private class AnswerPair{
public int questionOrder;
public int answer;
}
public void onCreate(Bundle bundle){
for(int i=0; i<NO_OF_BUTTON; i++){
AnswerPair ans = new AnswerPair();
ans.questionOrder = i;
ans.answer = 0; // SET this
getViewById(button[i]).setTag(ans);
getViewById(button[i]).setOnClickListener(this);
}
}
public void onClick(View v){
if (v.getTag() == null) return;
try{
AnswerPair answer = (ans)v.getTag();
// Check answer == question order? index?
}catch(exception e) return;
}
You can implement as many OnClickListeners as you want and assign different listeners for each button.
public void onCreate(Bundle bundle){
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.myButton)
b.setOnClickListener(new MyListener());
}
private class MyListener implements OnClickListener {
#Override
public void onClick(View v) {
// Your code here
}
}
i think a lot of people know this already, but there's a shortcut you can use instead of having different instances of onClickListeners and assigning them in code using setOnClickListener(x).
In your button XML, give it the android:onClick property, and assign it a string you like, for example,
android:onClick="clickOne"
In the activity the sets this xml as its content view, create a method named clickOne with a View parameter.
public void clickOne(View view)
Whatever you place on this method will be executed when you click the button.

Categories

Resources