I have a few imageview which have onclicklistener. If I press one (not release), I can press click others or I can click them same time. I do not want it. Everytime when I press one of them others should be disable to click.
imageview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getMethod();
}
});
I guess, I tried setClickable(false); but it did not work properly, if I clicked one button after that it worked.
Try using onTouchListener instead of onClickListener and calling setEnabled(false); on the other views there. Here's a fairly basic example:
OnTouchListener onTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
imageView1.setEnabled(false);
imageView2.setEnabled(false);
}
return true;
}
};
And then apply it to the image views with:
imageView1.setOnTouchListener(onTouchListener);
That should work. One thing is, though, that while you'll only be able to push one button no matter what, you also won't be able to push anything after you let go - but, you can fix that by adding some logic to see if the view actually got clicked or if the user touched it, changed their mind and slid away. The (event.getAction() == MotionEvent.ACTION_DOWN) check will be true even if the user is just scrolling.
//button on which press u want to disable others
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2.setEnabled(false); //button which u want to disable
button3.setEnabled(false); //button which u want to disable
}
});
//update fixed a spelling error
try to disable the button and
button.setEnable(false);
enable the button
button1.setEnable(true);
Related
This is so strange, but if you put an onClickListener on a TextView (or non-editable EditText) which has android:textIsSelectable="true" - it needs not one tap, but two.
I checked it on 3 phones and all of them perform onClick only after second tap.
Of course, if you make focusable="false" or android:textIsSelectable="false" it works from the 1st tap, but text selection doesn't work.
Please, help me with that issue
Set in XML to your TextView:
android:textIsSelectable="true"
After that set onTouchListener to your TextView and in them do this:
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) view.requestFocus();
It's set focus for every tap on TextView.
After all set onClickListener to your TextView.
I have the same problem with a ViewHolder in my RecyclerView.Adapter. So, I cut it for you if you need:
class RollHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener {
private TextView textView;
RollHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view);
textView.setOnClickListener(this);
textView.setOnTouchListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.text_view:
// Do here that you need
break;
}
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (view.getId()){
case R.id.text_view:
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) view.requestFocus();
break;
};
return false;
}
}
I had the same problem and it's hard to ask and search for a resolution.
Here are two things that I noticed in addition to the double tap behavior:
if you really double tap (quickly) on a TextView with textIsSelectable, it selects the word you tapped, even when the focus is on something else, which means the view somehow registered the first touch as well.
if you long tap while the focus is somewhere else, it works and starts the selection action mode as if it was focused already
Here's how I managed to make it work. It's not beautiful, but everything works fine so far: in the XML you only need to add textIsSelectable, no other focusable / focusableInTouchMode / clickable / enabled attributes needed; then you need two listeners, one is the existing onClick which works, but needs a double take and the other is an onFocusChange where you handle the exceptional first tap:
hint = (TextView)view.findViewById(R.id.hint);
hint.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
handleHintClick();
}
});
hint.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) { handleHintClick(); }
}
});
Here is an alternative solution in a related question which I don't like and didn't even try: wrap the TextView in a FrameLayout and add the listener to that.
Here is another related question which has more solutions.
Use onTouchListener to detect clicks and redirect them to the container view:
textView.setOnTouchListener { _, event ->
if (event.action == 1 && !textView.hasSelection()) {
containerView.callOnClick()
}
false
}
This will keep the ability to select and unselect text without calling onClick event.
android:longClickable="false"
android:clickable="false"
Disable the button with setEnabled(false) until it is safe for the user to click it again.
May this helpful to you
Try this.
use in XML file
android:onclick"your Name"//for example I used "onImageListClick"
public void onImageListClick(View view)
{
//do your task.
//Intent intent = new Intent(this, ImageListActivity.class);
//intent.putExtra(Extra.IMAGES, IMAGES);
//startActivity(intent);
}
or
txtboxname.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
////do you task.
}
});
I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}
I have 2 buttons and I need to read onClick event from second button when first is pressed down now and v.v. Like in keyboards. How to do that?
Edit
No, no! I don't need to check was first button clicked or not. I need to listen another onClick events when first or second button is in ACTION_DOWN state couse if I press first button, I can't press second, but I have multitouch.
May be You could try the following code :
Declare a boolean variable in class.
private boolean button1IsPressed = false;
Write following code for button 1 :
button1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
button1IsPressed=true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
button1IsPressed=false;
}
}
};
For Button 2 You can do the following:
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(button1IsPressed){
//Write your to do code here
}
}
});
You could try with onTouchListeners. In the first button modify a boolean on the down and up event, in the second button, only perform an action when the boolean is true.
If the buttons are toggle buttons, what I think is the case then:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Do something
} else {
// Disable vibrate
}
}
The main thing here is the isChecked() function, which may be used when checking which one is checked, so to execute something then. You can set in the XML of the two buttons the following:
android:onClick="onToggleClicked" then with isChecked determine which one is checked like this:
boolean on1 = ((ToggleButton) view1).isChecked();
boolean on2 = ((ToggleButton) view2).isChecked();
if (on1)
//do something with button2
if (on2)
//do something with button1
Cheers
There's a sample code in android-16/ApiDemos project called "Views -> Splitting Touches across Views" (SplitTouchView.java). In that sample enclosing LinearLayout has an attribute android:splitMotionEvents="true" which allows to scroll two list views simultaneously.
According to Android 3.0 API Overview this attribute appeared in this api version:
Previously, only a single view could accept touch events at one time. Android 3.0 adds support for splitting touch events across views and even windows, so different views can accept simultaneous touch events.
In my application when I click an EditText, I have to perform some logic. I have the code. But it is not going into the click method.
My code:
EditText des=(EditText)findViewById(R.id.desinc);
des.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
java.lang.System.out.println("Inside click");
EditText income=(EditText)findViewById(R.id.editText1);
// TODO Auto-generated method stub
String inc=income.getText().toString();
int indexOFdec = inc.indexOf(".");
java.lang.System.out.println("index="+indexOFdec);
if(indexOFdec==0)
{
java.lang.System.out.println("inside index");
income.setText(inc+".00");
}
}
});
What am I doing wrong? Help me.
Try overriding onTouch by setting up an onTouchListener in the same way as an onClickListener. Use this code as a reference.
EditText dateEdit = (EditText) findViewById(R.id.date);
date.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//anything you want to do if user touches/ taps on the edittext box
}
return false;
}
});
UPDATE(why this behavior):
The first click event focuses the control, while the second click event actually fires the OnClickListener. If you disable touch-mode focus with the android:focusableInTouchMode View attribute, the OnClickListener should fire as expected.
You can also try this: set android:focusableInTouchMode="false" for your EditText box in the xml. See if it works with the existing code.
You should use OnFocusChangeListener()
Try clicking EditText twice because at first instance EditText gets focus and after that EditText's click event executes. So, if you want your code to execute on first click write your code for focus change of EditText using OnFocusChangeListener().
There are a lot of options on how to define a click/tap on the touchscreen. One of them for example is setting a boolean.
Example for boolean:
boolean buttonClicked = true;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (buttonClicked) {
//do that and this
}
}
});
And there's a isPressed() method:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (button.isPressed()) {
//do that and this
}
}
});
What exactly is the difference between them? And when and why do I use boolean and the method isPressed()?
Because you are referring to a button in both of your examples, I assume that you are referring to the user tapping on a button, not just a random touch on the screen.
That being said, both of the examples you provided are not good.
In your first example, the boolean is useless because it is always true, so //do that and this will always be reached.
In your second example, your if statement is useless, because the onClick method by its nature is only reached when the button is tapped.
A good way to listen for a button press is using a click listener like this:
Button button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Code placed here will run every time the button is tapped
}
});
...where R.id.buttonId is the ID of your button in the layout.
If you need to define click event for a View you can use onClickListener, onTouchListener.
For more information check for Android official Documentation.
onTouchListener
onTouchListener
When considering your first code snippet, You can use boolean to perform another operation on button click event. as example something like this ,
boolean buttonClicked = false;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//true after button clicked
buttonClicked = true;
}
});
//if buttonClicked equals true
if (buttonClicked){
//perform operation only after button clicked
}
when considering your second code snippet, no need of button.isPressed() inside
button's onClick() callback. Because what you want to do by checking button.isPressed() is done without it inside button's onClick() callback.
Keep in mind these things.
isPressed() is a public method of View Class
Button is a subclass of View Class
isPressed() is a public method of Button Class as well.
About isPressed() from Android official documentation.
Indicates whether the view is currently in pressed state. Unless
setPressed(boolean) is explicitly called, only clickable views can
enter the pressed state.
Returns true if the view is currently pressed, false otherwise.