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.
Related
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.
I have three functions in my Main class, onCreate,onClick and click, I have two options, declare TextViews and some other views at the beginning of the project as global variables, which means they will stay in the entire app lifetime, or get them separately in each function(will cause the computer to work a bit more but the variables will not stay in memory the whole time). To describe the question further:
OPTION 1:
public static final int L = 6;
TextView[] textViews = new TextView[L];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rand = new Random();
int[] digits = {rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10)};
for (int i = 0; i < L; i++) {
textViews[i] = (TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(i), "id", getPackageName()));
textViews[i].setOnClickListener(this);
textViews[i].setText(String.valueOf(digits[i]));
}
}
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.num0:
click(0);
break;
case R.id.num1:
click(1);
break;
case R.id.num2:
click(2);
break;
case R.id.num3:
click(3);
break;
case R.id.num4:
click(4);
break;
case R.id.num5:
click(5);
break;
}
}
public void click(int clicked) {
textViews[clicked].setText("Clicked");
}
OPTION 2: (Note to shorten your time - the change is in the last function).
public static final int L = 6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rand = new Random();
int[] digits = {rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10)};
for (int i = 0; i < L; i++) {
textViews[i] = (TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(i), "id", getPackageName()));
textViews[i].setOnClickListener(this);
textViews[i].setText(String.valueOf(digits[i]));
}
}
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.num0:
click(0);
break;
case R.id.num1:
click(1);
break;
case R.id.num2:
click(2);
break;
case R.id.num3:
click(3);
break;
case R.id.num4:
click(4);
break;
case R.id.num5:
click(5);
break;
}
}
public void click(int clicked) {
((TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(clicked),"id",getPackageName())).setText("Clicked");
}
Take into consideration I have more than a TextView array, and in some function I have to get 5 views again, will it be better to declare them as global variables? I read somewhere most of the time using global variables is not good and it is against the whole idea of functions, but it seems more simple here.. Sorry about my English and thank you all.
First off, you do not need to worry about performance issues if the textViews object is declared globally. It is minuscule.
Now in your code,if the only reason to declare textViews array object is to reference it in click() method, then there is no need of declaring it globally.
Instead, you can pass the view object provided by the overridden onClick() method to your click() method.
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.num0:
click(view);
break;
case R.id.num1:
click(view);
break;
case R.id.num2:
click(view);
break;
case R.id.num3:
click(view);
break;
case R.id.num4:
click(view);
break;
case R.id.num5:
click(view);
break;
}
}
public void click(View viewClicked) {
((TextView))viewClicked.setText("Clicked")
}
I'm trying to make a simple matched pairs app. The app starts with all TextViews having "#" as a text. When I press a text field, it changes to a letter which was stored for that field.
Anyway, I want to open two fields (I mean to change their strings) and compare them. If they are equal, they remain "opened" with new letters (e.g. both are "A"). In the case with equal letters, everything works fine, but I have difficulties when the letters differ.
If they are not equal, I want to make them to show themselves with new text for one second and after that to go to the previous sign (i.e. "#"). When I press the first field, it changes its text but when I go for the second field and if their stored letters are not equal, the second field doesn't change the text and the text from the first field goes back to the previous sign.
What do I need to do to show fields with new text for a short period of time if their Strings are not equal?
This is how my table looks:
public class MainActivity extends AppCompatActivity {
TextView textView;
int counterForPressedFields= 0;
int textViewForComparationCounter = 0;
TextView firstTextViewForComparation;
TextView secondTextViewForComparation;
int[] nonPressedFields= {1, 1, 1, 1};
int pressedField = 2;
int tag = 0;
public void show(View view) throws InterruptedException {
textView = (TextView) view;
tag = Integer.parseInt(textView.getTag().toString());
if (nonPressedFields[tag] == 1) {
nonPressedFields[tag] = pressedField;
if (counterForPressedFields< 2) {
textViewForComparationCounter += 1;
counterForPressedFields+= 1;
switch (tag) {
case 0:
textView = (TextView) findViewById(R.id.front1);
textView.setText("A");
break;
case 1:
textView = (TextView) findViewById(R.id.front2);
textView.setText("B");
break;
case 2:
textView = (TextView) findViewById(R.id.front3);
textView.setText("A");
break;
case 3:
textView = (TextView) findViewById(R.id.front4);
textView.setText("B");
break;
}
if (textViewForComparationCounter == 1) {
firstTextViewForComparation = (TextView) view;
firstTextViewForComparation = textView;
} else if (textViewForComparationCounter == 2) {
secondTextViewForComparation= (TextView) view;
secondTextViewForComparation= textView;
}
}
if(counterForPressedFields == 2){
if(firstTextViewForComparation.getText().toString().equals(secondTextViewForComparation.getText().toString())){
counterForPressedFields= 0;
textViewForComparationCounter = 0;
}else{
firstTextViewForComparation.setText("#");
secondTextViewForComparation.setText("#");
nonPressedFields[Integer.parseInt(firstTextViewForComparation.getTag().toString())] = 1;
nonPressedFields[Integer.parseInt(secondTextViewForComparation.getTag().toString())] = 1;
counterForPressedFields= 0;
textViewForComparationCounter = 0;
}
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Try using a handler. You can update your textViews back to "#" with its postDelayed method.
private final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
//update your textViews here
}
};
handler.postDelayed(runnable, millisecondsOfDelay);
Hope it helps!
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
}....
}
}
Am beginner to android..I developed an app like if i press prev and next button it will display prevous and next images....
But I had issue in my app..The problem here was, if app execute i set an current image source as IMAGE_IDS[0]... So that if i pressed left arrow app gets force close, actually if i press left arrow it shows last image in the array IMAGE_IDS... Any idea ??? Thank You !
public class MainActivity extends Activity implements OnClickListener{
ImageButton play;
ImageButton ib_left_arrow, ib_right_arrow;
ImageView slidingimage;
Animation rotateimage;
private int[] IMAGE_IDS = { R.drawable.image1, R.drawable.image2,
R.drawable.image3};
int imglength=IMAGE_IDS.length;
int img_position;
int img_minus;
int img_plus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movies);
ib_left_arrow = (ImageButton) findViewById(R.id.iv_left_arrow);
ib_right_arrow = (ImageButton) findViewById(R.id.iv_right_arrow);
ib_left_arrow.setOnClickListener(this);
ib_right_arrow.setOnClickListener(this);
slidingimage = (ImageView) findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[0]);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iv_left_arrow:
img_minus=--img_position;
slidingimage.setImageResource(IMAGE_IDS[img_minus% IMAGE_IDS.length]);
rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_left);
slidingimage.startAnimation(rotateimage);
break;
case R.id.iv_right_arrow:
img_plus=++img_position;
slidingimage.setImageResource(IMAGE_IDS[img_plus% IMAGE_IDS.length]);
rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_right);
slidingimage.startAnimation(rotateimage);
break;
}
}
}
I think the problem is img_plus% IMAGE_IDS.length when setting the image position in the array.
I would try:
#Override
protected void onCreate(Bundle savedInstanceState) {
img_position = 0;
imglength = IMAGE_IDS.length;
slidingimage.setImageResource(IMAGE_IDS[img_position]);
// ...
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iv_left_arrow:
img_position--;
break;
case R.id.iv_right_arrow:
img_position++;
break;
}
// this is to check if your current position is out of array bounds, you could
// handle here your exit if you want to close the app when pressing left and
// img_position = 0 || img_position < 0
if(img_position < 0) img_position = imglength-1;
if(img_position >= imglength) img_position = imglength -1;
slidingimage.setImageResource(IMAGE_IDS[img_position]);
Its happening because of this line
img_minus=--img_position;
and this calculation
img_plus % IMAGE_IDS.length
If it's at 0 then
img_minus=--img_position;//img_minus = --0 therefore img_minus = -1
//img_plus % IMAGE_IDS.length // -1 % 3 = 2
slidingimage.setImageResource(IMAGE_IDS[2]);//which is the last image.
So if you want to close the Activity if left is pressed then
img_minus=--img_position;
if(img_minus < 0({
finish();
return;
}
imgposition=0;
case R.id.iv_left_arrow:
if(0<img_position){
--img_position;
slidingimage.setImageResource(IMAGE_IDS[img_position]);
rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_left);
slidingimage.startAnimation(rotateimage);}
break;
case R.id.iv_right_arrow:
if(2>=counter)
{
++img_position;
slidingimage.setImageResource(IMAGE_IDS[img_position]);
rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_right);
slidingimage.startAnimation(rotateimage);}
break;