I've a Button for 8 CheckBoxs and i want when i click on button a piece of my code running for example case 1 in this code for showing tick on chk1 (note.done1) .and for second time when i clicked on button,my app recognize chk1 is checked and now checking chk2(note.done2) and too for 6 another CheckBoxs.
but this code is wrong because when i click on button, chk1 and chk3 and chk5 are true.also I'm trying else if and Sharedpreferences and array .
i can't use ischecked() method because i use listview with 1000(in less) item.just i can use note.done where link to checkboxs in each item .i can use array where link to each checkboxs and sqlite too!
so can anyone help me?
btnOk.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
switch (G.result_s1[position]) {
case 0: {
note.done1 = true;
note.c = Color.RED;
G.database_s.execSQL("UPDATE mystate SET s1='1' WHERE s_id=" + (position + 1));
G.result_s1[position] = 1;
//AdapterNote.img.setBackgroundColor(Color.CYAN);
}
break;
case 1:
{
note.done2 = true;
note.c = Color.GREEN;
G.database_s.execSQL("UPDATE mystate SET s2='1' WHERE s_id=" + (position + 1));
G.result_s2[position] = 1;
}
break;
}
switch (G.result_s3[position]) {
case 0: {
note.done3 = true;
note.c = Color.MAGENTA;
G.database_s.execSQL("UPDATE mystate SET s3='1' WHERE s_id=" + (position + 1));
G.result_s3[position] = 1;
}
break;
case 1: {
note.done4 = true;
note.c = Color.YELLOW;
G.database_s.execSQL("UPDATE mystate SET s4='1' WHERE s_id=" + (position + 1));
G.result_s4[position] = 1;
}
break;
}
dialog.dismiss();
finish();
}
});
Why don't you use ischecked()?
Whenever you click the button you check another checkbox
btnOk.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(!checkbox1.ischecked()){
// check checkbox1
}else if(checkbox1.ischecked()){
// check checkbox2
}else if(checkbox1.ischecked() && checkbox2.ischecked()){
// check checkbox3
}....
}
}
Related
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.");
}
As I mentioned in the title ,I want to add text to a Textview without replacing the previous text .
In my application I have a TextView and 7 buttons .On every button click I set the text of button to the TextView.
If the button is clicked on first time ,Setting the text to TextView ,and if the same button is clicked 2nd time I am removing that button's text from TextView.
Here What I want to do is for 7 buttons I want to set positions(uniqueness for sun to sat) in TextView and when the respective button is clicked that text is set to the TextView and if the button is clicked 2nd time that specific position of the text should remove .
Here text shouldn't replace the previous text that is important to have and if some button's are selected and again that are deselected means TextView should show the default text as "Never"
I tried to get source from SO but I can't find a clear solution for this .
If anyone helps me to come out from this ,that's much helpful for me .
coding
public class CreateAlarm extends Activity implements View.OnClickListener {
private Button mbtn_Sun, mbtn_Mon, mbtn_Tue, mbtn_Wed, mbtn_Thu, mbtn_Fri, mbtn_Sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mRepeat = (TextView) findViewById(R.id.mRepeat);
mbtn_Sun = (Button) findViewById(R.id.mbtn_Sun);
mbtn_Mon = (Button) findViewById(R.id.mbtn_Mon);
mbtn_Tue = (Button) findViewById(R.id.mbtn_Tue);
mbtn_Wed = (Button) findViewById(R.id.mbtn_Wed);
mbtn_Thu = (Button) findViewById(R.id.mbtn_Thu);
mbtn_Fri = (Button) findViewById(R.id.mbtn_Fri);
mbtn_Sat = (Button) findViewById(R.id.mbtn_Sat);
mbtn_Sun.setOnClickListener((View.OnClickListener) this);
mbtn_Mon.setOnClickListener((View.OnClickListener) this);
mbtn_Tue.setOnClickListener((View.OnClickListener) this);
mbtn_Wed.setOnClickListener((View.OnClickListener) this);
mbtn_Thu.setOnClickListener((View.OnClickListener) this);
mbtn_Fri.setOnClickListener((View.OnClickListener) this);
mbtn_Sat.setOnClickListener((View.OnClickListener) this);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mRepeat.setText("");
} else
mRepeat.setText("Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mRepeat.setText("");
} else
mRepeat.setText("Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mRepeat.setText("");
} else
mRepeat.setText("Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mRepeat.setText("");
} else
mRepeat.setText("Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mRepeat.setText("");
} else
mRepeat.setText("Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mRepeat.setText("");
} else
mRepeat.setText("Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mRepeat.setText("");
} else
mRepeat.setText("Sat");
break;
default:
mRepeat.setText("Never");
}
}
}
Image :
By default the TextView text is "Never".
You can define a TreeMap as:
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
as a field of your class and add/remove the days to/from the TreeMap when the corresponding button is clicked. So the implementation of onClick method will be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
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());
}
You should set each button id first,add this to your xml for each specific button : android:id="sun" and ...
My suggestion is: use a single TextView can make your logic quite complex
Use a horizontal LinearLayout instead, you will have 7 TextView inside with predefine text and position. Just simply show/hide them according to which button is clicked and you don't have to deal with any complex string analize.
change the text of the button once clicked to P then again that same button is clicked the text should change to A then again the same button is clicked the text should change to H then again when the button is pressed the text should change to L in android Studio how c
final Button button = (Button) findViewById(R.id.number);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (curr) {
case 0:
button.setText("P");
curr = curr + 1;
break;
case 1:
button.setText("A");
curr = curr + 1;
break;
case 2:
button.setText("H");
curr = curr + 1;
break;
case 3:
button.setText("L");
curr = 0;
break;
}
}
});
This will work 100%
public class MainActivity extends AppCompatActivity {
private int current = 0;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (current){
case 0:
button.setText("P");
current = 1;
break;
case 1:
button.setText("A");
current = 2;
break;
case 2:
button.setText("H");
current = 3;
break;
case 3:
button.setText("L");
current = 0;
break;
}
}
});
}
}
You're going to need three things for this:
A way to react to the button being clicked
A way to set and get the state of the button
A way to change the text on the button
Thankfully, these are all easy tasks.
To react to a button click, set its OnClickListener
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateButtonState();
}
});
To set and get the state of the button, we need a way of representing the state. Since you described a simple state of P -> A -> H -> L we can represent each point as an integer.
final int BUTTON_STATE_P = 0;
final int BUTTON_STATE_A = 1;
final int BUTTON_STATE_H = 2;
final int BUTTON_STATE_L = 3;
We can then simply store the given state into a variable change this variable according to our state logic and then read whatever value the variable is at.
int buttonState = BUTTON_STATE_P;
private void updateButtonState(){
if (buttonState == BUTTON_STATE_P){
buttonState = BUTTON_STATE_A;
}
else if (buttonState == BUTTON_STATE_A){
buttonState = BUTTON_STATE_H;
}
else if (buttonState == BUTTON_STATE_H){
buttonState = BUTTON_STATE_L;
}
}
Then we just need a way of setting the text of the button depending upon what state we're in. To do this we need to know what each state looks like in terms of text.
button.setText(getTextForButtonState(buttonState));
private String getTextForButtonState(int buttonState){
if (buttonState == BUTTON_STATE_P){
return "P";
}
else if (buttonState == BUTTON_STATE_A){
return "A";
}
else if (buttonState == BUTTON_STATE_H){
return "H";
}
else if (buttonState == BUTTON_STATE_L){
return "L";
}
return null;
}
You'll notice that the most important step was step 2. You can make lots of different decisions as to how you might want to handle a state. You might decide that you want to put all of your state code into its own class and then just call methods of that class, as one suggestion.
Hope this helps.
I have one TextView in my application and want to change the Background color of the same TextView .When i click 1st time it would be red , click on same 2nd time it would be green and 3rd time click it would be blue color background by problematically.
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Drawable d = textType.getBackground();
Log.e("textType "," click !!! ");
if(d.getConstantState() == getResources().getDrawable(R.drawable.red_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.green_circle_shape);
}
if(d.getConstantState() == getResources().getDrawable(R.drawable.green_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.blue_circle_shape);
}
if(d.getConstantState() == getResources().getDrawable(R.drawable.blue_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.red_circle_shape);
}
}
});
This cod is not working.Thanks to appropriate.
Create a global variable x initialize it with 0. Then code like this:
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if(x<4)
{
x= x+1;
}
else{
x = 1;
}
if(x==1)
{
// red color
}
else if(x==2)
{
// blue color
}
else if(x==3)
{
// green color
}
}
});
Use the below code,
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
private int mCounter = 0;
#Override
public void onClick(View v)
{
if (mCounter == 0)
v.setBackgroundResource(R.drawable.red_circle_shape);
else
if (mCounter == 1)
v.setBackgroundResource(R.drawable.green_circle_shape);
else
v.setBackgroundResource(R.drawable.blue_circle_shape);
mCounter++;
}
});
Try this;
Define a global Variable int type.
Inside your onClick(), increment the int Variable
use switch or if statement to change the color when the variable increases e.g.
If(variable == 1)
// change color to Blue
else if (variable == 2)
// change color to Yellow
Hi Use the following code to Change the Color. Paste these lines inside your textView Onclick. Variable count is a global variable.
if (count == 0)
txtView.setTextColor(colorcode1);
else if (count == 1)
txtView.setTextColor(colorcode2);
else
txtView.setTextColor(colorcode3);
count++;
if (count > 2)
count = 0;
Try below code
TextView textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(current)
{
case 1:
tv.setBackgroundColor(Color.parseColor("#00ff00"));
current = 2;
break;
case 2:
tv.setBackgroundColor(Color.parseColor("#0000ff"));
current = 3;
break;
case 3:
tv.setBackgroundColor(Color.parseColor("#ff0000"));
current = 1;
break;
default:
break;
}
}
});
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.