I have a list of items disposed vertically. Each item has a CheckBox and a TextView.
I'm trying to achieve a LongClick behavior for the CheckBox. More precisely i want it to change it with a trash icon while is LongClicked. On release it must become again a checkBox and also maintain the Check/Uncheck behavior on simple click.
How can i achieve that? How many approaches are there?
I apriciate any king of help!
I tried to just use setBackgroud() function, but it would just draw under the existing checkbox view, with the box still remaining on the top.
cb.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Resources res = tmp_context.getResources();
Drawable drawable = res.getDrawable(R.mipmap.ic_trash);
cb.setBackground(drawable);
return false;
}
});
UPDATE
First Put your chekcbox inside a framelayout :
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/one"
android:id="#+id/fl">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:longClickable="true"
android:id="#+id/two"/>
</FrameLayout>
you could use LongClickListener and OnTouchListener:
private View.OnLongClickListener pressHoldListener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View pView) {
// Do something when your hold starts here.
isSpeakButtonLongPressed = true;
mFrameLayout.setBackgroundResource(R.drawable.trash_can);
yourCheckBox.setVisibility(VIEW.INVISIBLE);
return true;
}
};
private View.OnTouchListener pressTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
// We're only interested in when the button is released.
if (pEvent.getAction() == MotionEvent.ACTION_UP) {
// We're only interested in anything if our speak button is currently pressed.
if (isSpeakButtonLongPressed) {
// Do something when the button is released.
yourCheckBox.setVisibility(View.VISIBLE);
mFrameLayout.setBackground(null);
isSpeakButtonLongPressed = false;
}
}
return false;
}
};
Inside you onCreate() :
yourCheckBox.setOnLongClickListener(pressHoldListener);
yourCheckBox.setOnTouchListener(pressTouchListener);
Also add
android:longClickable="true"
to your checkbox xml.
Related
I want to remove a gridView item's shadow when the user presses it, and then restore it once the user releases. (cant use selector.xml because items have a user chosen color)
this code removes shadow when first pressed but upon release it stays stuck down with no shadow.
gridItemView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
image.removeShadow();
image.invalidate();
}
else if(event.getAction()==MotionEvent.ACTION_UP){
image.setShadow();
image.invalidate();
}
return false;
}
});
I cant set it to true, because then .OnItemClickListener in the fragment dosent work. Also I kinda fixed it by setting the shadow to turn on in onItemClickListener, but if the user slides their thumb off the item instead of just clicking it will stay pressed
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainActivity.selectedItem = position;
if (lastView[0] != null) {
lastView[0].setBackgroundResource(R.drawable.nullr);
}
picker.setOldCenterColor(MainActivity.items.get(MainActivity.selectedItem).getColor());
picker.setColor(MainActivity.items.get(MainActivity.selectedItem).getColor());
View imageContainer = view.findViewById(R.id.imageContainer);
CircularImageView circleImage = (CircularImageView) view.findViewById(R.id.circleView);
artistText.setText(MainActivity.items.get(position).getArtist());
songText.setText(MainActivity.items.get(position).getSong());
int backgroundColor = Color.parseColor("#FFEECC");
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{backgroundColor, backgroundColor});
drawable.setShape(GradientDrawable.OVAL);
drawable.setStroke(25, MainActivity.items.get(MainActivity.selectedItem).getColor());
imageContainer.setBackgroundDrawable(drawable);
circleImage.setShadow();
circleImage.invalidate();
lastView[0] = imageContainer;
MainActivity.anItemIsSelected = true;
}
});
you forgot
else if(event.getAction()==MotionEvent.ACTION_UP){
image.setShadow();
image.invalidate();
}
what you can do here is try to create a custom Gridview and override onTouchEvent, something like this, (it is not precisely accurate though)
public class MyGridv extends GridView{
//implement all constructors;
//the override onTouchEvent(MotionEvent event);
#override
protected boolean onTouchEvent(MotionEvent event){
// put your ontouch code here and return true, you might need to do
//some changes because you can not get access to your methods or
//you can make this class a private class to your mainActivity
// now delete your ontouch for your gridview
}
}
the logic here is onTouchEvent() is initially called before your onTouchListenerand its the first to be called, returning true there will then pass the event to onTouch() and on onClick, there everything will work fine
hope its helpful
Try to add below on your View.
android:clickable="true"
I have this editText in my android activity
<EditText
android:id="#+id/payment_expiration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/payment_expiration_label"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:clickable="true"
android:onClick="update_expiration_date"
android:editable="false"
android:layout_marginTop="-4dp"
android:cursorVisible="false"
android:maxLength="7"
android:padding="10dp"
android:textSize="13dp" />
as you can see when the user click on
I call this method which launch a datePickerDialog :
public void update_expiration_date(View v){
Log.i("","cliqué");
picker.show();
can_update_expiration_date = true;
}
the problem I encouter is : in the first time just when I open this activity, the user must click two times to launch the dialog
but after that, one click is sufficient
how can I fix this issue
Check Similar Question
"The first click just sets the focus to the TextBox then the second click actually gets handled as a click. "
try setting android:focusable="false"
You could try the focusable solution, or just add a state tracking variable...
public class YourActivity extends Activity {
private boolean clicked;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
clicked = false;
}
public void update_expiration_date(View v){
if(clicked == false){
clicked = true;
return;
} else {
Log.i("","cliqué");
picker.show();
can_update_expiration_date = true;
}
}
}
begiPass solution works but there are a better.
With focusable false the user never can´t see which edittext is selected.
The better solution is using onTouchListener. My example code:
edit.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
edit.setText("");
v.setOnTouchListener(null);
return false;
}
});
Don´t forget set OnTouchListener to null. We want just clear the hint (only once time).
Regards!
Use OnTouchListener handle it:
when (view) {
edtName -> {
if (motionEvent.action == MotionEvent.ACTION_UP) {
edtName.requestFocus()
//TODO
}
}
edtPassword -> {
if (motionEvent.action == MotionEvent.ACTION_UP) {
edtPassword.requestFocus()
//TODO
}
}
}
return false
}```
I have an imageview - It has both the attributes -focusable and focusableintouchmode set to true
<ImageView
android:id="#+id/ivMenu01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="true"
android:focusableInTouchMode="true" >
</ImageView>
I have implemented the onFocusChangeListener in my activity-
#Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.ivMenu01:
if (hasFocus) {
ivMenu01.setImageBitmap(Utility
.getBitmap("Home_ford_focus.png")); // Focussed image
} else {
ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford.png")); // Normal image
}
break;
default:
break;
}
}
Also the onClickListener -
case R.id.ivMenu01:
ivMenu01.requestFocus();
Intent iFord = new Intent(HomeScreen.this, FordHome.class);
startActivity(iFord);
break;
Now when i click the ImageView the first click gives the focus to the ImageView and the second click performs the action.
I am not sure why this is happening .
The first click should request focus as well as perform the action.
Any help on how to do this will be highly appreciated.
It's the way the widget framework is designed.
When you look at View.onTouchEvent() code, you'll find out that the click action is performed only if the view has taken focus:
// take focus if we don't have it already and we should in
// touch mode.
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallback();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// click
}
}
So, as you noticed, the first click makes the view gain focus. The second one will trigger the click handler since the view already has focus.
If you want to alter the bitmap of the ImageView when it's pressed, you should implement an View.OnTouchListener and set it via ImageView.setOnTouchListener() method. That listener should look more or less like this:
private View.OnTouchListener imageTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// pointer goes down
ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford_focus.png"));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// pointer goes up
ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford.png"));
}
// also let the framework process the event
return false;
}
};
You can also use a Selector aka State List Drawable to achieve the same thing. See reference here: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
I have one linearlayout and have also a few button inside it.I want make it visible when touch and invisible when touch it again.
How can i make it??
LinearLayout one = (LinearLayout) findViewById(R.id.one);
one.setVisibility(View.GONE);
I suggest that you use GONE insteady of INVISIBLE in the onclick event because with
View.GONE the place for the layout will not be visible and the application will not appear to have unused space in it unlike the View.INVISIBLE that will leave the gap that is intended for the the layout
Add a boolean on your code
boolean flag = false;
then add android:clickable = true on your linear layout on xml
then use this code for reference
your_linear_layout = new OnClickListener(){
#Override
public void onClick(View v) {
if (flag){
// means true
your_linear_layout.setVisibility(View.INVISIBLE);
flag = false;
}
else{
your_linear_layout.setVisibility(View.VISIBLE)
flag = true;
}
}
};
Havent tried this yet but this should work..
Cheers
add setOnTouchListener to linearLayout get touch events as :
linearLayout.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// show-hide view here
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
// show-hide view here
return true;
}
return false;
}
});
for making View visible use yourview.setVisibility(View.VISIBLE) and for Invisible use yourview.setVisibility(View.INVISIBLE)
You should user
Invisible -: mButton.setVisibility(View.INVISIBLE);
Vsible -: mButton.setVisibility(View.VISIBLE);
Put this code in onclick listner of button With checking if condition.
I'm trying to display a RatingBar View that is only capable of showing a one star rating, or no stars. This seems so trivial...
My ratingbar is defined like so:
My view implements OnRatingBarChangeListener and OnTouchListener
My OnRatingBarChange handler has no code.
My OnTouch handler looks like so:
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(this.mRating.getRating() == 0)
{
this.mRating.setRating(1);
}
else
{
this.mRating.setRating(0);
}
return true;
}
return false;
}
No matter what, the onTouch event does not succeed at setting the rating back to zero. This seems way too trivial. What am I doing wrong?
Thanks!
Consider using ToggleButton and btn_star stock drawable instead. Here's example:
<ToggleButton
android:button="#android:drawable/btn_star"
android:background="#android:color/transparent"
android:textOn=""
android:textOff=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The RatingBar is extended from the SeekBar and the listener actually expects a slide action. Thus the RB can't be set to 0 (or not for this purpose) when there is only one star. The proper widget to use for this type of functionality (no surprise here) is the ToggleButton.
I had the same problem, trying to use a single star RatingBar as a favourite button. But the ToggleButton is exactly what you need for this type of thing.
This is what I implemented for the ToggleButton. (The _favourited bool was specifically used for something in my code, I can't remember now)
favourite_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!_favourited) {
// Do stuff
_favourited = true;
} else {
// Do stuff
_favourited = false;
}
}});