Java Android question
I have x, say 5, buttons in a row.
Each button has a different number value displayed on the button.
Button one is active, the rest are not- not clickable. They are greyed out.
To show Button 1 is active it fades up and down.
Once clicked the button pops up a message. The user Ok's that, this activates Button 2, and deactivates Button 1.
Then it happens through all buttons, one by one. The final button doesn't produce the pop up message.
My question...
I want to create a method that sets the first button as current and then once clicked sets the next as current, and so on.
Can anyone tell me how to do this? I don't need to know how to fade buttons etc, its literally how to set button as current, and within that method the user click sets the next button as current.
Many thanks in advance.
EDIT
OK, I've had a go...its not working, but it seems so close...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_one);
int[] buttonIds = new int[] {R.id.button_1,R.id.button_2,R.id.button_3,R.id.button_4,R.id.button_5};
setButton(buttonIds);
}
private void setButton(int[] buttId){
int isCurrent = 0;
while(isCurrent < 5) {
Button currentButton = (Button) findViewById(buttId[isCurrent]);
//TODO Make current button pulse
currentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.clearAnimation();
v.setBackgroundColor(0xFF00FF00);
v.setFocusable(false);
v.setFocusableInTouchMode(false);
v.setClickable(false);
setTimer();
isCurrent++;
}
});
I know that the problem is the isCurrent++ is not accessible outside the onClick method. How do I right this? Am I close or is this a major funk up and do I have to rethink?
Just use a global variable which track the current button, and check this variable to identify the current active button for determining the action in onClickListener. To fade out a button try this code snippet
button.setClickable(false);
button.setBackgroundColor(Color.parseColor("#808080"));
You need something like this:
private int activeButton = 1;
private void buttonClickHandler(){
switch(activeButton++){
case : 1
button1.setEnabled(true):
// show popup, hide/animate for button 1
break;
case : 2
button2.setEnabled(true);
// same for button 2
case : 3
// same for button 3
case : 4
// same for button 4
}
Related
I have textview typing in it by using buttons and every button clicked it will be invisible
backspace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//extra code remove last char
//txt.setText(txt.getText().toString().substring(0, txt.getText().toString().length() - 1));
StringBuffer easer = new StringBuffer(txt.getText());
easer.reverse();
easer.deleteCharAt(0);
easer.reverse();
String x = new String(easer);
txt.setText(x);
//my button 1 get char from array
b1.setVisibility(View.VISIBLE);
//my button 2 get char from array
b2.setVisibility(View.VISIBLE);
//my button 3 get char from array
b3.setVisibility(View.VISIBLE);
//my button 4 get char from array
b4.setVisibility(View.VISIBLE);
//my button 5 get char from array
b5.setVisibility(View.VISIBLE);
}
});//end click listener
If your buttons have text that appear in your TextView then you can use TextWatcher. With the help of TextWatcher you may get event afterTextChanged(Editable arg0). In that function you can set condition related to your text and visible button after backspace button clicked.
If this is not what you wanted then please specify your question a bit more.
I have a floating action button that, when clicked, an editText field opens up where the user can enter text. The text would then be taken via getText().toString() and entered into the text of a new button. I can make it so that each time the user goes through this process, a new button is created and positioned below the previously created button. The problem is, each new button is not unique. It creates a new instance of the exact same button so that every button has the exact same id. How do I generate a new, completely unique button with unique id in order to reference each button individually later on?
confirmBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!editTitle.getText().toString().isEmpty()) {
projectBtn = new Button(getActivity());
projectBtn.setId(R.id.projNewBtn);
projectBtn.setText(editTitle.getText().toString());
project_layout.addView(projectBtn);
}
editTitle.setText("");
project_layout.removeView(editTitle);
project_layout.removeView(linearLayout);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
});
I just solved my own problem by simply adding two lines of code; one to initialize a button ArrayList, and one to add the button to the array each time a new button is created. I simply set the id as a variable int so that once stored in the array, I am able to reference it via the corresponding number position.
Suppose I have one InputText which on clicking a button leads to several events (e.g. 4). On Clicking button,
Event 1 happens and gives output to buffer 1. On checking buffer 1 is filled,
Event 2 happens and gives output to buffer 2. On checking buffer 2 is filled,
Event 3 happens and gives output to buffer 3. On checking buffer 3 is filled,
Event 4 happens.
That is the Events only occurs if the previous buffer is filled. Else its in Event=0 mode.
How do I do this in Android?
You can add an .setOnClickListener() on the inputText, and when inputText is clicked you do the code in that section. After that code has run you can call another event, and do this as many times as you want.
Put these in the OnCreate method:
EditText inputText = (EditText) findViewById(R.id.edittextname);
inputText.setOnClickListener(action1);
After OnCreate, put:
public void action2 () {
//action 2 code here
//add other actions here
}
View.OnClickListener action1 = new View.OnClickListener() {
public void onClick (View v) {
//action 1 code here
action2();
}
I want a Button (or any View for that matter) to expand / stretch out sideways with an animation and I want it to happen at the click of another Button. For example, before Button 1 is pressed, the Button 2 shouldn't exist. But when Button 1 is pressed, the Button 2 should slowly come into existence with both its sides smoothly expanding to left and right to a certain size.
The following is a photo I drew to explain what I'm trying to achieve
I have looked at several tutorials, but most of them had sliding animation from one fixed end to the other. I have also tried scaling the X & Y of the button from value 0 to 1, but they just don't give the smooth animation effect (they just appear there out of nowhere) and the button doesn't expand / stretch sideways.
Any idea how this can be achieved?
Thank you for your time!!
the smooth animation effect ,you can use ObjectAnimtor.
// scale down button2 at the start of an activity
btn2.setScaleX(0.1f);
// on the click of button 1 let the button2 scale
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn2.animate()
.scaleX(4.0f)
.setDuration(1000)
.setInterpolator(new OvershootInterpolator(2.0f)) //Will give bounce effect to the scaling
.start();
}
});
I think you can use this library for Button 2 animation.
In order to it happen at the click of another Button just call View.performClick() for Button 2 from Button 1 onClick() method.
first define 2 objects of Animation and Button then create a On Click listener for Button and write these codes in it:
//here i define :Button btnoff
//Animation animation1
//set on click listener in oncreate
btnOff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
animation1 = new ScaleAnimation(0,2,1,1,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation1.setDuration(1200);
btnOff.startAnimation(animation1);}
});
you can learn more in this link.
I just want to know how to start an Action (like starting a new Activity) after for example 3 Button clicks. So the Activity will only start if the Button has been clicked for 3 Times.
Keep a counter of the times you've clicked the Button anywhere you like, such as a global variable or if you want it to be cleaner, the Button itself. Add a listener to your button, you can simply add android:onClick="buttonClick" to the xml and then implement a method with that name like so:
public void buttonClick(View yourButton){
if(yourButton.getTag() == null){ //We have no tags, so first click :)
yourButton.setTag(1);
}
if((Integer)yourButton.getTag() == 3){
//Do whatever
}else{
//Increment the value of the tag
yourButton.setTag(((Integer)yourButton.getTag())+1);
}
}
You can maintain a counter .. Increment it after every click... Check if value equals desired value then build intent and start the activity .. Reset the counter before starting the activity.