How to make a button bigger when pressed? (Android Studios) - java

I'm working on an app in Android studios and would like my customised button to get bigger when pressed and go back to the default defined state when released. I wrote an xml file to define my button but it didn't work. Here's my xml file for the custom button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/play_button_pressedhdpi"
android:state_pressed="true" />
<item android:drawable="#drawable/play_button_defaulthdpi"
android:state_focused="true" />
<item android:drawable="#drawable/play_button_defaulthdpi" />
</selector>
This doesn't seem to work. Any help would be appreciated.
Edit:
I just tried the following code as mentioned in the first answer below:
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) v.getLayoutParams();
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
//sendMessage("Key1\n");
lp.width = 200;
lp.height = 200;
button1.setLayoutParams(lp);
}
if(event.getAction() == MotionEvent.ACTION_UP) {
lp.width=90;
lp.height=90;
button1.setLayoutParams(lp);
}
return false;
}
});
This makes sense but didn't work for some reason.
Any help would be appreciated...

I was finally able to solve the problem. I created two separate images of two different sizes and used the setOnTouchListener() to assign the appropriate image to the button for each of the states ACTION_DOWN and ACTION_UP in the following manner:
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
float x = (float) 1.25;
float y = (float) 1.25;
button1.setScaleX(x);
button1.setScaleY(y);
button1.setBackgroundResource(R.drawable.blue_220);
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
float x = 1;
float y = 1;
button1.setScaleX(x);
button1.setScaleY(y);
button1.setBackgroundResource(R.drawable.blue_206);
}
return false;
}
});
Result as desired.

I use MotiovEvent in this way to resize my button:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
RelativeLayout.LayoutParams lp = (LayoutParams) b.getLayoutParams();
if(event.getAction() == MotionEvent.ACTION_DOWN) {
sendMessage("key1\n");
lp.width=140;
lp.height=140;
button.setLayoutParams(lp);
}
if(event.getAction() == MotionEvent.ACTION_UP) {
lp.width=90;
lp.height=90;
button.setLayoutParams(lp);
}
return false;
}
});
Hope it helps.

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 app crashes when touched outside the ImageView

When i touched the ImageView it display the color from the image but when i touched outside the image the app crashes.
Here's my xml code:
<ImageView
android:id="#+id/colorimage"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="#drawable/color" />
<ImageView
android:id="#+id/displaycolor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/colorimage"/>
And here is the main code:
mImageView.setDrawingCacheEnabled(true);
mImageView.buildDrawingCache(true);
mImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE){
Bitmap bitmap = mImageView.getDrawingCache();
int pixel = bitmap.getPixel((int)event.getX(), (int)event.getY());
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
display.setBackgroundColor(Color.rgb(r , g , b));
}
return true;
}
});
Logcat error message:
Error Logs
Change the line if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) to :
if(v.getId() == R.id.colorimage){
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE)
{........}
}
The on touch listener has been set to the whole activity not a particular view and the bitmap operation cannot be done on the other views except this one so the view so it is crashing.

How to distinct between a tap on the right/ left side of the display?

I thought this is a pretty relevant and common question, but I couldnt find an answer.
At the moment I have this method:
public boolean onTouchEvent (MotionEvent evt){
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
do stuff ...
}
}
So if the user taps on the screen (wherever) the code is executed. Now I want the distinction between the right side of the display and the left side (left side means --> go back).
You can do this many ways. Here is one of them:
Attach onTouch listener to the view, which stretches to its edges. (For example your RelativeLayout which holds rest of views)
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float halfOfAScreen = mainLayout.getMaxWidth() / 2;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
float fingerPosition = event.getX();
if(fingerPosition < halfOfAScreen) {
onBackPressed();
}
return true;
default:
return false;
}
}
};
Refer to this post on how to get touch position.
It seems in your case you will use
int x = (int)event.getX();
int y = (int)event.getY();
and work within the bounds of your layout that you want the app to react to.

Android - Detecting when user touches a drawable left

I have a TextView that contains a DrawableRight, what I want to do is detecting when the user presses that icon in drawableRight, is that possible ? and if it is how can I do it ?
PS: I am working inside a fragment
TextView XML
<TextView
android:id="#+id/mTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textSize="22dp"
android:drawableRight="#mipmap/icn" //this is the drawable
/>
mTitle.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (mTitle.getRight() - mTitle.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
//drawable pressed
return true;
}
}
return false;
}
});

Creating a Drag and Drop ImageButton

I have looked tutorials on how to make my imagebutton capable of drag and drop. With the code I have the imagebutton just disappears when I click it. and when I click anywhere else it does not come back.
Here is my code for the imagebutton:
mainHut = (ImageButton) findViewById(R.id.mainHut);
mainHut.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Toast.makeText(getApplicationContext(), "in the movement", Toast.LENGTH_SHORT).show();
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
mainHutSelected = true;
}//end if
if (event.getAction() == MotionEvent.ACTION_UP)
{
if (mainHutSelected == true)
{
mainHutSelected = false;
}//end if
}//end if
else if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if (mainHutSelected == true)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.setMargins((int)event.getRawX() - 25, (int) event.getRawY() - 50, 0, 0);
layout = (LinearLayout) findViewById(R.id.gllayout);
layout.removeView(mainHut);
layout.addView(mainHut, params);
}//end if
}//end else
return false;
}//end onTouch function
});
here is the xml for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bgm" >
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mainhut" />
</LinearLayout>
Any help is appreciated. Thanks.
To answer my own question...this is how I fixed my issue. It took me a long time to figure this out but here it is. The parameters are a little messy when it comes to my own imagebutton so I will have to figure out what to use to keep my finger in the center of the button, but this moves the button like I was wanting:
button.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (me.getAction() == MotionEvent.ACTION_MOVE )
{
LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight());
params.setMargins((int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()), (int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()));
v.setLayoutParams(params);
}
return false;
}
});

Categories

Resources