How to make a button unclickable but Touchable? - java

I have a button, when I click it it makes a scale animation, and also I can drag it with onTouch event.
I want to make it unclickable after first touch and this code does. Here OnClick
btnScale.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
v.startAnimation(animScale);
btnScale.setClickable(false);
}});
Also this code help me to drag it. Here OnTouch
btnScale.setOnTouchListener(new Button.OnTouchListener(){
public boolean onTouch(View v, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
oldXvalue = me.getX();
oldYvalue = me.getY();
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
//RelativeLayout rl = (RelativeLayout) findViewById(R.id.background);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
params.leftMargin = (int) (me.getRawX() - (v.getWidth() / 2));
params.topMargin = (int) (me.getRawY() - (v.getHeight()));
//LayoutParams params = new LayoutParams(v.getHeight(), (int) (me.getRawX() - (v.getWidth() / 2)), (int) (me.getRawY() - (v.getHeight())));
v.setLayoutParams(params);
}
return false;
}});
However I still want to drag it after I click it. However when I make it unclickable it cannot be dragable since it is unTouchable too
EDIT:
When I use if else state on onClick as boolean variable, I still able to click the button and a ugly square appears in my circular button.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#61d5ee">
<item>
<shape android:shape="oval">
<solid android:color="#61d5ee"/>
<corners android:radius="5dp" />
<stroke android:width="4px" android:color="#cdf3fb" />
</shape>
</item>
</ripple>
What to do?

Create a boolean to say if it is enabled or not, change manually the state in your onClick and change the button's design too. In your Onclick use something like:
if(enabled){
do something
}else{
//do nothing
}
You don't need the else, it's just an example.
Then... he is touchable enabled and disabled.

Related

How to continuously move object in Android Studio

How to continuously move an ImageView in Android Studio when a Button is held down, but stop when it isnt clicked anymore? With other words: how to detect if a button is "unclicked§ or direktly detect if it is held down. Thanks for the help
To detect if a button is pressed/released(down/up), TouchListener is used.
imageView = findViewById(R.id.image);
button = findViewById(R.id.button);
root_layout = findViewById(R.id.root_layout); //parent layout
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
#Override
public void run() {
float x = imageView.getX();
if(x > root_layout.getWidth())
x = 0;
else
x += 6; //Increase value of '6' to move the imageView faster
imageView.setX(x);
handler.postDelayed(this,0); //increase delay '0' if facing lag.
// This is the rate at which the x value of our imageView is being updated
}
};
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
handler.post(runnable); //Start moving imageView
break;
case MotionEvent.ACTION_UP:
handler.removeCallbacks(runnable); //Stop moving imageView
break;
}
return true;
}
});
Try this solution
Step 1:- need to define anim folder in your res folder.
now place rotate.xml file in anim folder.
rotate.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="600"
android:repeatMode="restart"
android:repeatCount="infinite"
android:interpolator="#android:anim/cycle_interpolator"/>
</set>
Step 2:- Declare animation programmatically
// Animation
Animation animFadein;
in onCreate()
// load the animation
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
Step 3:- Detect user touch like this to start and stop animation
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//start animation here
imageView.startAnimation(animFadein);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// stop animation here
imageView.stopAnimation(animFadein);
}
return true;
}
});

Android ImageButton swap resource leaves 'residue'

I have 2 Images:
IMG_1:
IMG_1_PRESSED:
I display IMG_1 by default, but when I click on it, the image should change to IMG_1_PRESSED.
Here's a code snippet of the button itself, where be is an enum containing the correct drawables:
ImageButton ib = new ImageButton(this.context);
ib.setImageResource(be.getDrawable());
ib.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);
ib.setBackgroundColor(Color.TRANSPARENT);
ib.setPadding(0, 0, 0, 0);
ib.setAdjustViewBounds(true);
ib.setMaxWidth(width);
ib.setMaxHeight(height);
setStrengthListener(ib, be);
And here is the setStrengthListener method:
private void setStrengthListener(final ImageButton ib, final ButtonEnum be){
ib.setOnTouchListener(new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ib.setImageResource(be.getDrawablePressed());
return true;
case MotionEvent.ACTION_UP:
ib.setImageResource(be.getDrawable());
return true;
}
return false;
}
});
}
Now both images are 480x240 and width and height are set to device's width/3 and device's width/6 respectively (I want to display 3 images, hence the /3).
Here's the problem, if the button is pressed, I get this:
There's this little line under the pressed button... I've tried a lot to fix this but somehow that little line has made itself my greatest enemy today.
As an extra effort: the line doesn't show anymore if I set the original resource to IMG_1_PRESSED:
ImageButton ib = new ImageButton(this.context);
ib.setImageResource(be.getDrawablePressed());
ib.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);
ib.setBackgroundColor(Color.TRANSPARENT);
ib.setPadding(0, 0, 0, 0);
....etc
But obviously I don't want to start with a pressed button.
So dear Android experts, what am I doing wrong here?
This is the simplest solution as i know,
Try this
create Xml inside your Drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_pressed_img"android:state_pressed="true" />
<item android:drawable="#drawable/btn_normal_image" />
</selector>
Then add this drawable file as background to your View
Example :
<ImageView
android:id="#+id/enter_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="100dip"
android:maxWidth="100dip"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="#drawable/enter_btn" />
or in Java code
yourView.setImageResource(R.drawable.*drawablefile*);
You can use ImageButton also but ImageView would look better than ImageButton since it doesn't have button borders.
So after a hell of a lot of trial and error, I managed to get that grey line away. In the end it was very simple really, though I'm not completely sure why this fixed it:
The main layout:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setGravity(Gravity.TOP);
The view creation (layout for imageview is unchanged):
ImageView iv = tbb.getTopBarButton(ButtonEnum.IMG_1);
LinearLayout iViewLayout = new LinearLayout(this);
iViewLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
iViewLayout.setOrientation(LinearLayout.HORIZONTAL);
iViewLayout.addView(iv);
ll.addView(iViewLayout);
Adding a separate layout per imageview seemed to remove the grey line. Would love to know WHY this works though, but for future reference, at least I hope to help others.

How to move a view in circle with onTouch in android

I am facing a problem in an project. I have a activity in which i had create a Round shape layout with Framelayout. My problem is that i don;t know how to implement items in layout in round shape. and move the items with ontouch event and when the items come on the specific postion a toast display or alert dialog show. I am attach the image for demo.
What you are looking for is a View that implements the onTouch event with a Drag feature.
1) In order to give your view the round shape, you need to create a file that contains those specifications in the drawable folder.
Create your drawable/your_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
</shape>
2) In your View that you want to make round, set the background to that same drawable like
<YourView
android:id="#+id/your_id"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/your_circle"/>
And then, apply the following procedure to the Activity
public class DraggableActivity extends Activity {
float dX;
float dY;
int lastAction;
View.OnTouchListener touchListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drag_view_layout);
// 1 - Create the touch listener
touchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN) {
Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show();
}
break;
default:
return false;
}
return true;
}
};
// 2 - Add a reference to your view that already is stated on the layout
final View dragView = findViewById(R.id.your_id);
// 3 - Attac the the TouchListener to your view
dragView.setOnTouchListener(this);
}
}
Let me know if it works.
Regards,

How can I make a drop down hidden menu

I want to make a drop down menu, like a status menu, that is hidden when the activity starts, and when it's pressed or slid it opens like the image below..
http://i.stack.imgur.com/jeq5z.png
My layout currently has a RelativeLayout for the top bar and a ScrollView for the text.. between those, i'd like to put the menu..
I'm not doing this app on phonegap or anything like that, just java and xml..
Thanks in advance
Edit:
Thank you all for your help! I end up doing a FrameLayout that was set off the screen with the translationY and then, when clicked, just slide up and down.. Here's the snipped.. I'll just leave it here in case someone else needs it.
on layout.xml
<FrameLayout
android:id="#+id/layout_FrameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffffff" >
<!-- stuf -->
</FrameLayout>
on activity.java
private FrameLayout statusDrawer = null;
private int statusDrawerHeight; // height of the FrameLayout (generated automatically)
private int statusDrawerDragButtonHeight = 30 + 5; //height of the DragButton + height of the border
private boolean statusDrawerOpened = false;
private int statusDrawerDuration = 750; //time in milliseconds
private TimeInterpolator interpolator = null; //type of animation see#developer.android.com/reference/android/animation/TimeInterpolator.html
#Override
protected void onCreated(Bundle savedInstanceState){
statusDrawer = (FrameLayout) findViewById(R.id.layout_FrameLayout);
interpolator = new AccelerateDecelerateInterpolator();
statusDrawer.post(new Runnable() {
#Override
public void run() {
statusDrawerHeight = statusDrawer.getHeight();
statusDrawer.setTranslationY(-statusDrawerHeight+statusDrawerDragButtonHeight);
}
});
statusDrawer.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
if(statusDrawerOpened) {
statusDrawer.animate()
.translationY(-statusDrawerHeight+statusDrawerDragButtonHeight)
.setDuration(statusDrawerDuration)
.setInterpolator(interpolator)
.start();
} else {
statusDrawer.animate()
.translationY(0)
.setDuration(statusDrawerDuration)
.setInterpolator(interpolator)
.start();
}
statusDrawerOpened = !statusDrawerOpened;
}
});
}
Use a FrameLayout as the root layout. Add the drop menu layout as in the right side of your picture. Call
menuView.setTranslationY(-view.getHeight);
on this view to initially hide the drop down menu when the activity is started. Make sure menuView only refers to the drop down view part without the small tab button. When the user touches the tab animate translationY to 0 so that the layout will slide down
ObjectAnimator.ofFloat(dropDownView, "translationY", -view.getHeight, 0).setDuration(200).start();
whereby dropDownView refers to the complete drop down menu.
Using ObjectAnimator requires API level 11. If you need to support older API levels, use http://developer.android.com/reference/android/view/animation/TranslateAnimation.html (which has some down sides).
If you instead want add a sliding effect, e.g. the sliding menu is moving with together with the finger, install a OnTouchListener:
dropDownTab.setOnTouchListener(new OnTouchListener() {
public void onTouch(View v, MotionEvent e) {
// Make the drop down menu finger follow the finger position.
// Use again dropDownView.setTranslationY(...) to move the view.
// If the drop down menu has been dragged a certain distance, make it move out by itself using the animation as above.
}
});

Android change text size in TextView on click

I have few buttons containing only labels. They are made using 2 image resources (pressed is little bit bigger than not pressed)
button_image.png
button_image_pressed.png
and xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_image" android:state_pressed="false"/>
<item android:drawable="#drawable/button_image_pressed" android:state_pressed="true"/>
</selector>
And i want to use custom TextView insted of this.
Is it possible to chage TextView textSize when user touches text and change textSize back when text user releses screen?
I tried different solutions, but they didn't work.
You can try using an onTouchListener().
textView.setOnTouchListener( new OnTouchListener() {
#Override
public boolean onTouch( View v, MotionEvent event ) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
textView.setTextSize( ... );
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
textView.setTextSize( ... );
}
return false;
}
} );

Categories

Resources