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();
}
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.
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.
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
}
I have two elements (TextView) in my XML layout that when a LongClick is pressed it will prompt the user to enter in a new value and then when the DONE button is clicked it should show the newly inputed value to the tvScoreHome using setText().
When I do a Long Click on the mentioned element the edit field and keyboard appear as expected. However, it won't allow me to type anything. When I type something it nothing shows up (but the device vibrates as if a button was pressed) and when the DONE button is clicked it vibrates as well but it does not exit the keyboard and show anything in the tvScoreHome element.
Any ideas why?
// set the onLongClickListener for tvScoreHome
tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
final EditText userInput = (EditText) findViewById(R.id.userInput);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
userInput.setVisibility(View.VISIBLE);
imm.showSoftInput(userInput, 0);
tvScoreHome.setText( userInput.getText() );
userInput.setVisibility(View.INVISIBLE);
return true;
}
});
You need to give the user a chance to input some text before copying the data and hiding the EditText.
Remove these two lines from your listener:
tvScoreHome.setText( userInput.getText() );
userInput.setVisibility(View.INVISIBLE);
Perhaps you could use an OnFocusChangeListener to run these two lines when userInput loses focus.