Issue
I have quite a number of Buttons in an Android application and I want a separate class to be created to handle the Touch events.
So I created the following class:
public class OnTouchButtonEffects implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
v.setBackgroundResource(R.drawable.buttonbluepress);
else if(event.getAction() == MotionEvent.ACTION_UP)
v.setBackgroundResource(R.drawable.buttonblue);
}
}
Now I want that if view v
has event ACTION_DOWN
then I get the background resource and append "press" to it and then set that as background resource and if it's event ACTION_UP
then get background resource and remove "press" from the resource name and set it as its new background resource.
I tried using getResources()
but couldn't figure out how exactly that be done.
So the problem is how do I get the name of the background resource, and append/remove from its name.
Solution
I don't believe you can work with Resource addresses (i.e., R.id.nameOfResourse) the way you're asking.
I see what you're attempting to accomplish, and there is a much easier way to use customized buttons:
StateListDrawable http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/buttonbluepress" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/buttonblue" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@drawable/buttonblue" /> <!-- hovered -->
<item android:drawable="@drawable/buttonblue" /> <!-- default -->
</selector>
Answered By - mrres1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.