Toast selected toggle buttons - java

I have 7 ToggleButtons created and one button. Each named Monday, Tuesday, ... Sunday.
When I select ("Switch ON") for example Monday and Thursday, I want to touch a button and display "you have selected Monday and Thursday".
Any ideas how do it on the MainActivity.java?
Thank you in advance.

You can solve this by setting onCheckedChangeListener for all the seven toggle buttons and appending it to a string. On click this string could be displayed.

public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton_Monday;
private ToggleButton toggleButton_Thursday,toggleButton_wednesday;
private Button buttonSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton_Monday=(ToggleButton)findViewById(R.id.toggleButton1);
toggleButton_Thursday=(ToggleButton)findViewById(R.id.toggleButton2);
toggleButton_wednesday=(ToggleButton)findViewById(R.id.toggleButton3);
buttonSubmit=(Button)findViewById(R.id.button1);
buttonSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
StringBuilder result = new StringBuilder();
if(toggleButton_Monday.isChecked()) {
result.append("Monday ");
}
if(toggleButton_Thursday.isChecked()) {
result.append("Thursday ");
}
if(toggleButton_wednesday.isChecked()) {
result.append("Wednesday ");
}
//Displaying the message in toast
Toast.makeText(getApplicationContext(),"You have selected "+result.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
You can do like this.

Related

I Would Like To Have A Single Button Display A Different Line Of Text Each Consecutive Time It's Pressed in Android Studio

I'm extremely new to coding, so apologies if this question is trivial or the answer is easily found somewhere. I have searched, but I cannot find anything that helps.
Basically, I'm trying to code a simple, 2 button app in Android Studio.
Button1 is meant to simply display a series of commands to the user via text box.
Button2 merely resets.
My problem is, I would like Button1 to change what's displayed in the text view each time it is pressed, but I cannot figure out how to do so. I don't want to make 6 or 7 buttons.
Basically I would like it to run as follows;
Text = "Pick a number"
user presses Button1
Text = "Add 15" (This is as far as I've gotten)
user presses Button1
Text = "Multiply times 5"
user presses Button1 etc. etc. etc.
If anybody could please explain or usher me in the right direction, I would be greatly appreciative.
You can use button.setOnClickListener
public class MyActivity extends Activity {
EditText et;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Perform your Logic here.
et.setText("New text");
}
});
}
}
you can use a globle and a swithchcase
public class MyActivity extends Activity {
EditText et;
int CLICKS=0;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CLICKS++;
switch(clicks)
{
case 1:
et.setText("Pick a number");
break;
case 2:
et.setText("Add 15");
break;
case 3:
et.setText("Multiply times 5");
break;
}
}
});
}
}

Stuck with Android studio

I am currently building an app where the user enters their name and when you press the button it should say "Welcome, (whatever name the user entered) in a Toast.
I feel like I have everything write but it's not working. It lets me type in my name but when I click the button nothing happens. What am I doing wrong?
Here's my code
public class MainActivity extends AppCompatActivity {
private EditText input;
private Button click;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.editText5);
click = (Button) findViewById(R.id.outputBTN);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), input.getText(). "Welcome ", Toast.LENGTH_SHORT).show();
}
});
}
}
You have to use the toString(); method because input.getText() returns you an Editable Object!
With the toString() you can convert your Object into a String.
See getText() method: https://developer.android.com/reference/android/widget/EditText.html
Solution:
Toast.makeText(getApplicationContext(),"Welcome," + input.getText().toString(), Toast.LENGTH.SHORT).show();

Multiple Step Actions on one Button Click

I am currently trying to solve a java based problem on Android Studio.
I have puzzled my head over this problem looking in many forums and webpages to not find any solution in the last two days. So I am seeking for help here now.
I have programmed a Button that when clicked causes a textview to swipe out of the screen with an animation. After that I would like the old text ("First Text") of the Textview to be replaced with another text ("New Text") appearing on the same place where the old text was. All of this should happen with only one click on the button step-after-step.
My problem with my code is that the old text is replaced by the new text first and then causes the animation.
Does anybody now a solution for this problem?
I would be really very grateful for any help!
This is my code below.
public class FirstActivity extends AppCompatActivity {
Animation slideleft;
Button btn1;
TextView txt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
btn1 = (Button) findViewById(R.id.btn1);
txt1 = (TextView) findViewById(R.id.txt1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
slideleft = AnimationUtils.loadAnimation(FirstActivity.this, R.anim.slide_left);
txt1.startAnimation(slideleft);
txt1.setText("New Text");
}
});
}
}
you can use onAnimationEnd method of animation listener and change the text inside it.
animation.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationEnd(Animation anim) {
txt1 = (TextView) findViewById(R.id.txt1);
txt1.setText("New Text");
}
});
you can place which event execute after the animation place your code
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
//your_code
}
});

Setting EditText visibility [duplicate]

This question already has answers here:
How to make Views with an Invisible attribute 'Visible' after clicking a button
(4 answers)
Closed 7 years ago.
I have four EditText that I set to invisible in the XML and when the button is clicked, I want them to be visible in pairs. For example, when the button is clicked, I want et1 and et2 to be visible, then when the button is clicked again eet1 and eet2 to be visible. And when all of them are visible, i want the TextView to be visible .
public class app extends ActionBarActivity {
EditText et1;
EditText et2;
EditText eet1;
EditText eet2;
TextView sum;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
et1 = (EditText)findViewById(R.id.et1);
et2 = (EditText)findViewById(R.id.et2);
eet1 = (EditText)findViewById(R.id.eet1);
eet2 = (EditText)findViewById(R.id.eet2);
sum = (TextView)findViewById(R.id.sum);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick();
}
});
}
public void onClick() {
et1.setVisibility(View.VISIBLE);
eT1.setVisibility(View.VISIBLE);
eet1.setVisibility(View.VISIBLE);
eet2.setVisibility(View.VISIBLE);
}
}
Set up a variable where you store how many times you clicked the button:
public class app extends ActionBarActivity {
int counter;
...
Then in onClick you increment counter and distinguish cases:
public void onClick() {
switch(counter):
case 0:
et1.setVisibility(View.VISIBLE);
...
break;
case 1:
...
break;
case ...
}
counter++;
}
public void onClick() {
if (if (et1.getVisibility() == View.INVISIBLE &&
et2.getVisibility() == View.INVISIBLE)) {
et1.setVisibility(View.VISIBLE);
eT1.setVisibility(View.VISIBLE);
} else if() ///... you Get the idea
}

Android button with multiple click simple

i am new in android programming, and i am working on my first application, so i just want to know how every time when i click same button it does some action, for example if i have a button called ( next ) and i want to click on it and an image will appear, this one i did it, but i want to click on the same button and show another image view in the same activity.
i have tried some code but with no results
so please if anyone can post a code that explain how i can do it.
Here is a quick example of code...
Rather than show a random image, it will show a random String. All you need to do is just modify it to show images instead.
public class MainActivity extends Activity {
private String[] names = {"Joe", "Mark", "Amanda", "Kelly", "Michael", "Jenny"};
private Button button;
private TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get our views
name = (TextView) findViewById(R.id.name);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
next(); // show random name
}
});
}
// Display random name on button click
private void next() {
name.setText(names[rand()]);
}
// Pick a random number from 0 to names.length
private int rand() {
return new Random().nextInt(names.length);
}
}

Categories

Resources