This is original images and combine from 29 pieces of other small images and final result like this.
For example, when I click the particular area,the small piece will change to another colour.
Is there any solution that I can detect the particular area touch and know which piece of image should change?
set implements OnTouchListener for your class add the unimplement method then put all your image view in array and set setOnTouchListener
ImageView[] image = new ImageView[9];//for exapmle 9
image[i].setOnTouchListener(this);
#Override
public boolean onTouch(View v, MotionEvent event) {
boolean eventConsumed = true;
int x = (int)event.getX();
int y = (int)event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
for (int i=0;i<=8;i++)//change the loop size to image array size
{
if (v == image[i]) {
point=i;
// here i'm getting which image i have touched
System.out.println(" image"+i+i+i+i+i+i+i+i+i+i+"is clicked");
}
}else if (action == MotionEvent.ACTION_UP) {
//here wright the condition for which image what color have to give
}
If you only want to show the touched part by changing its color then maybe you should use a selector for each image.
selector.xml -> drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/colored_image"
android:state_pressed="true"/>
<item android:drawable="#drawable/normal_image" >
</selector>
ImageView:
<ImageView
android:id="#+id/change_city_small_down_ImageView"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/changeCityRelativeLayout"
android:layout_marginLeft="5dp"
android:src="#drawable/selector"
</ImageView>
It works, I just used it by overlapping imageviews:
Related
In my application the background of buttons should change to another one when the user touch, and get back to the original one when the finger is off, my problem is that touching for a long period makes it change while touching for a short one, doesn't!! here's how I implemented that:
Button b = (Button) findViewById(R.id.b_bb_e_l);
b.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
b.setBackgroundResource(R.drawable.button_bb_e_l_selec);
break;}
case MotionEvent.ACTION_UP: {
b.setBackgroundResource(R.drawable.button_bb_e_l);
break;}
}
return false;
}
});
You can use drawable and shape to implement it.
Create a file such btn_bg.xml in res/drawable
btn_bg.xml's content:
<?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/button_bb_e_l_selected"/>
<item android:drawable="#drawable/button_bb_e_l"/>
</selector>
Set background to your button:
<Button
android:background="#drawable/btn_bg"/>
If you are not required to use onTouch, you can create a style with background set to drawable. Create that drawable as xml that uses selectors and set the selectors' different states, state_pressed="true" and state_pressed="false" and apply the colors to those states. Then use that style on the buttons you want.
I've been trying to look for a solution, but it seems like no one tried it, or maybe its too simple that I did not find it.
Anyways, I'm trying to achieve a button press with an ImageView. What I want is, ImageView to display a default image, and if pressed (OnTouch) [which has a MotionEvent.ACTION_DOWN block] to run a frame animation. When you release the touch [which has a MotionEvent.ACTION_UP block], it should stop the animation and return to the default image.
Note: Frame animation is continuous repeating, and should not include the default image as one of the looped through image. (Button up image should not be displayed when touching)
Now the problem is, I have the animation working, but as per the android documentation, the first item in the <animation-list> tag will be displayed by default. But if I add the default image (button un-touched state) as the first item, it will be displayed in the loop. Also, when I release the touch, I use stop() method of the AnimationDrawable, which stops the animation at the current frame (image) and I can't seem to find any way to stop and go to default image state.
Here is my code:
button_anim.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="#drawable/button1_press1" android:duration="200" />
<item android:drawable="#drawable/button1_press2" android:duration="200" />
<item android:drawable="#drawable/button1_press3" android:duration="200" />
</animation-list>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
button = (ImageView)findViewById(R.id.imageView1);
button.setBackgroundResource(R.drawable.button_anim);
buttonAnim = (AnimationDrawable)button.getBackground();
button.setOnTouchListener(buttonTest);
}
private OnTouchListener buttonTest = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
buttonAnim.start();
// Log.d("Test", "Touch down");
} else if (action == MotionEvent.ACTION_UP) {
buttonAnim.stop();
// Log.d("Test", "Touch Stop");
}
return true;
}
};
Default image:- button1_inactive.png
buttonAnim.stop();
buttonAnim.reset();
// Log.d("Test", "Touch Stop");
AnimationDrawable mFrameAnimation = (AnimationDrawable)button.getBackground();
mFrameAnimation.stop();
mFrameAnimation.setVisible(true, true);
I would change imageView picture when it's pressed, and change it again when pressure is released. That ImageView contains picture of a button, and when is pressed i would give to user a feedback (like when he press a normal button) by change image. This is my code:
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("IMAGE", "motion event: " + event.toString());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
imageView.setImageResource(R.drawable.button_hover);
}
case MotionEvent.ACTION_UP: {
imageView.setImageResource(R.drawable.button);
}
}
return false;
}
});
and this is xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="std_button"
android:src="#drawable/button" />
but it doesn't work. If i use only ACTION_DOWN event, image change as i want,
What's wrong?
Make selector xml in drawable folder with content like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_green" android:state_pressed="true"/>
<item android:drawable="#drawable/ic_red"/>
</selector>
Try add break; after each case,
And as giozh said inside onTouch i return always false, so touch event result always not consumed. Just put return true after each case statement.
for change on event press you can try this.....
final ImageView imagen = (Imagen) findViewById(R.id.imageViewX);
ImageView imagen = (ImageView)findViewById(R.id.imageViewX);
imagen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageView perse = (ImageView) findViewById(R.id.imageViewX);
perse.setImageResource(R.drawable.backgroundtwo);
}
});
in my application i have one bag-round image with 5 icons how can i put on-click actions for individual icons in the android in my case 5 icons is place in the one image and that image used as bag-round image in my app.
Please help me thanks in Advance......
You can use ImageButton and listen its on click method:
imgB =(ImageButton)findViewById(R.id.Image_Button_ID);
imgB.setOnClickListener(new OnClickListener() {
// write your code here
});
Or if you want to specify the method in the xml:
<ImageButton android:id="#+id/Image_Button_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/blah"
android:onClick="cutsom_onclick_method" />
Hopefully I am not misunderstanding, it is not very clear to me what you need.
OnTouchListener will give you the location of the click which you can use to determine where you have clicked.
imageView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN){
int x = (int) e.getX();
int y = (int) e.getY();
}
// then do some calculation with x and y and where they are
return true;
}
});
The other way is to use five ImageButtons and five different images
I need to have some text with a drawable on the left and I want to execute some code when the user clicks/touches the image (only the image, not the text), so I used a LinearLayout with a TextView and an ImageView which is clickable and launches an onClick event. The XML parser suggests me to replace this with a TextView with a compound drawable, which would draw the same thing with far less lines of XML.. My question is "can I specify I want to handle an onClick event only on the drawable of the TextView and not on the TextView itself? I've seen some solutions which involves writing your own extension of TextView, but I'm only interested in being able to do it within the layout resource, if possible, otherwise I'll keep the following XML code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/home_feedback_title"
android:textColor="#android:color/primary_text_dark"
android:textStyle="bold"
android:paddingBottom="4dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/action_feedback"
android:clickable="true"
android:onClick="onClickFeedback"
android:contentDescription="#string/action_feedback_description"/>
</LinearLayout>
Its very simple. Lets say you have a drawable on left side of your TextView 'txtview'. Following will do the trick.
TextView txtview = (TextView) findViewById(R.id.txtview);
txtview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() <= txtview.getTotalPaddingLeft()) {
// your action for drawable click event
return true;
}
}
return true;
}
});
If you want for right drawable change the if statement to:
if(event.getRawX() >= txtview.getRight() - txtview.getTotalPaddingRight())
Similarly, you can do it for all compound drawables.
txtview.getTotalPaddingTop();
txtview.getTotalPaddingBottom();
This method call returns all the padding on that side including any drawables. You can use this even for TextView, Button etc.
Click here for reference from android developer site.
You can go either way. Using the compound drawable is faster though because it was intended to be an optimization. It uses less ram because you reduce 3 views into 1 and it's faster layout because you lose 1 depth.
If I were you I'd consider stepping back to see if both the text and the image intercepting the touch to do whatever action is possibly a good thing. In general having a larger touch region makes it easier to press. Some users may actually be inclined to touch the text instead of the image.
Lastly if you go that route of merging the 2 you might want to consider using a Button instead of a TextView. You can style the button to not have the rectangle around it. They call it a borderless button. It's nice because you get visual feedback that you clicked on a actionable item where as an ImageView or TextView normally aren't actionable.
How to Create Borderless Buttons in Android
#Vishnuvathsan's answer is almost perfect, but getRaw() returns an absolute x position of the touch point. If the textview is located not on the left edge of the view, you should compare with the absolute position of the textview by using getLocationOnScreen. Code below is an example to check both left drawable tap and right drawable tap.
textView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
int[] textLocation = new int[2];
textView.getLocationOnScreen(textLocation);
if (event.getRawX() <= textLocation[0] + textView.getTotalPaddingLeft()) {
// Left drawable was tapped
return true;
}
if (event.getRawX() >= textLocation[0] + textView.getWidth() - textView.getTotalPaddingRight()){
// Right drawable was tapped
return true;
}
}
return true;
}
});
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_DOWN = 3;
This click listener is getting in on touch listener
if (event.getAction() == MotionEvent.ACTION_DOWN)
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (event.getX() >= (tvFollow.getTop() - tvFollow.getCompoundDrawables()[DRAWABLE_TOP].getBounds().width())) {
// your action here
return true; } }
Since getRaw() and getRight() both returns in regards with the entire screen coordinates, this will not work if your views are not on the left edge of the screen. The below solution can be used anywhere regardless of layout position. This is for right side drawable touch.
textView.setOnTouchListener(OnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_UP)
{
if (event.x >= textView.width - textView.totalPaddingEnd)
{
<!---DO YOUR ACTIONS HERE--->
return#OnTouchListener true
}
}
true
})
PS: Kotlin code