I have this main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/z01"
/>
<ImageView
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/z02"
/>
</LinearLayout>
What I am trying to achieve is:
when the user touch and hold img1, the picture changs to the pressed pic (i.e. changed from z01.png to z01_pressed.png)
when (for example) the user moves from img1 to img2 while he holding, img2 get pressed (changed its picture from z02.png to z02_pressed.png) and img1 returns to its state (to z01.png).
to achieve this, I wrote this in onCreate method:
final ImageView img1 = (ImageView) findViewById(R.id.img1);
final ImageView img2 = (ImageView) findViewById(R.id.img2);
img1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
img1.setBackgroundResource(R.drawable.z01_pressed);
return false;
}
});
img2.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
img2.setBackgroundResource(R.drawable.z01_pressed);
return false;
}
});
However, this does not work. Am I getting something wrong?
You can use a button and set a custom image as the drawable of that button. That way, you won't have to manage the pressed and unpressed states yourself. See this link:
http://blog.androgames.net/40/custom-button-style-and-theme/
To those who might be interested:
This can be done using OnDragListener. It needs some work to accomplish this. I gave up on that to make my life easier.
Related
I am having linear layout, in which it has one imageview and one textview, when i touch the linear layout, it should highlight the image as well as text inside the linear layout with different color. I have tried using set background for the linear layout when it is pressed since i have used ontouch-listener it doesn't shows the color even i tried to change the colors programmatically but it didn't works.
Can anyone tell me how i can change the imageview and textview color when a linear layout is touched.
Xml:
<LinearLayout
android:id="#+id/linear_otherexpense"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#drawable/linearlayout_check"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="#+id/img_otherexpense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:scaleType="centerInside"
android:src="#mipmap/expense"
android:tint="#6666FF" />
<TextView
android:id="#+id/tv_otherexpense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:text="#string/dayexpense"
android:tint="#color/colorPrimary" />
</LinearLayout>
Selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/list_selector_background" />
Code:
li_otherexpense.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(NewDaybook_Activity.this, AddingNewExpense.class));
NewDaybook_Activity.this.finish();
return false;
}
});
You can use this code. this is working fine for me .May be it will helpfull
findViewById(R.id.linear_otherexpense).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
// When you Touch Down
// U can change Text and Image As per your Functionality
break;
case MotionEvent.ACTION_UP:
//When you Release the touch
// U can change Text and Image As per your Functionality
break;
}
return true;
}
});
Try this,
linearLayout = (LinearLayout) findViewById(R.id.linear_otherexpense);
imageView=(ImageView)findViewById(R.id.img_otherexpense);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
linearLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent));
imageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
return true;
}
});
I have basic layout with linear layout, ScrollView and ImageView.
ScrollView has registered onTouchListener and imageview onClicklistener.
If I tap on ImageView and I am pulling out of ImageView, I do not see log with "onclick".
If I tap out of imageView (on scrollView) and I am pulling somewhere, I see log with "on touch".
How can I catch onTouch event on my imageView, when I am pulling out of it?
Here is the code:
ImageView testButton = (ImageView) myView.findViewById(R.id.imageView1);
testButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(tag, "onclick");
}
});
ScrollView scrollView = (ScrollView) myView.findViewById(R.id.scrollView1);
scrollView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(tag, "ontouch");
return false;
}
});
And here is xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/com_facebook_blue"
android:scrollbarAlwaysDrawVerticalTrack="false" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/com_facebook_profile_picture_blank_square" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Set an OnTouchListener to the ImageView as well. When you pull away the touch from view's touchable region it will fire a Touch event. OnClick wont be fired when the ontouch is cancelled(MotionEvent.ACTION_CANCEL).You should not consume the onTouch() therefore return false in onTouch()
Set this to your scroll view:
ScrollView mainScroll=(ScrollView) findViewById(R.id.your_scroll_view)
mainScroll.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mainScroll.setFocusable(true); //If you need this
mainScroll.setFocusableInTouchMode(true); //If you need this
Then your imageView onclick listener will be triggered. This code could also substantially affect other parts of your functionality. So please check the view once you implement this and revert if there are any screen jumps which occur.
Well I have two imageviews and they look like this.
I want to add a TouchListener to just imageview1. but Listener not working properly.
I am adding imageview2 to
android:focusable="false"
but not working again
Try this. It works for me correctly even when you click on imageview2. MainActivity onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = (ImageView) findViewById(R.id.imageview1);
imageView1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("imageviewandontouchlistener", "imageView1 onTouch");
return false;
}
});
}
And in your Layout XML:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image1"/>
<ImageView
android:id="#+id/imageview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image2" />
</FrameLayout>
Set a custom OnTouchListener to imageview2 that always returns false in the onTouch method. This way the touch event will be passed to the views bellow until one of them returns true in the onTouch method.
I am using following layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lnrLayout_cVideo_LB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relative_cVideo_LB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".15" >
<VideoView
android:id="#+id/cVideo_LB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:tag="2" />
</RelativeLayout>
<TextView
android:id="#+id/txtVw_cVideo_LB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".85"
android:background="#FFFFFF"
android:gravity="center"
android:text=""/>
</LinearLayout>
So when I click on VideoView position of TextView should go to top of VideoView when I click again, I should restore.
For that I am using following code,
video.setOnTouchListener(new OnTouchListener() {
#Override
public void onTouch(View v) {
// TODO Auto-generated method stub
if(flag_video==0)
{
textview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,(float).85);
relative_LB.setLayoutParams(new LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,(float).15);
flag_video=1;
v.invalidate();
}
else
{
relative_LB.setLayoutParams(new LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,(float).15));
lb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,(float).85));
flag_video=0;
v.invalidate();
}
}
});
But it is not changing its position. I explored solution, I found that I can do it by View.removeView() and View.addView() method. But for my application purpose I can't use those methods. So anybody suggest me an alternate way??
Please dont suggest me View.removeView() and view.addView() methods
You have two problems:
You are using onTouch(View v) method instead of onTouch(View v, MotionEvent event) method. onTouch() method should have tow parameter. 2. Return type of onTouch() should be boolean type but you are using void.
Another problem is that you are setting LayoutParams to lb instead to textview in the else condition.
Now, replace the following listener code snippet with yours...I think, you will get it right.
video.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (flag_video == 0) {
textview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, (float) .85));
relative_LB.setLayoutParams(new LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, (float) .15));
flag_video = 1;
v.invalidate();
} else {
relative_LB.setLayoutParams(new LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, (float) .85));
textview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, (float) .15));
flag_video = 0;
v.invalidate();
}
return true;
}
});
You can set the visibility of the RelativeLayout to GONE, it should disappear and the textview go upside, like the following method (when the flag_video == 0 the textview go up):
video.setOnTouchListener(new OnTouchListener() {
#Override
public void onTouch(View v) {
// TODO Auto-generated method stub
if(flag_video==0)
{
//textview should go upside
relative_LB.setVisibility(RelativeLayout.GONE);
flag_video=1;
}
else
{
relative_LB.setVisibility(RelativeLayout.VISIBLE);
flag_video=0;
}
}
});
Hope it helps!
I'm new to Android, I've followed the hello world tutorial through and have a basic idea of what's going on. I'm particularly interested in the touch screen of my T-Mobile Pulse so just to get me started I want to be able to write the co-ordinates of a tocuh event on the screen, so say the user touched the co-ordinate 5,2 - a textview on the screen would display that.
At present I have a simple program that just loads an xml file which contains the textview I intend to write the co-ordinates in.
Thank you in advance, I did Google for help and searched stackoverflow but everything I found either went way over my head or wasn't suitable for this. Cheers.
You can use this function : http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)
You will probably put it in your onCreate method roughly this way (tested this time) :
Activity onCreate code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}
layout code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/touchView"
android:background="#f00"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, TestActivity"
/>
</LinearLayout>
Edit: I tried my code and indeed there were a few errors. Anyway, with the layout I give you here it works on my emulator. Can you provide maybe more code/context so I can see what's wrong?
It sounds like you want to get touch events from the whole area of your layout for this particular test. Try attaching the touch listener to your parent view rather than a separate target view.
This isn't a very common approach. In most cases you will want to listen for touch events in a specific child view and if you have other views in your layout that handle touch events (such as a button) they'll take priority over a parent view. See http://developer.android.com/guide/topics/ui/ui-events.html for more info about handling UI events.
Layout (touch_viewer.xml):
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/parent" >
<TextView android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
</LinearLayout>
And in your activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch_viewer);
final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
final TextView text = (TextView) findViewById(R.id.text);
parent.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent ev) {
text.setText("Touch at " + ev.getX() + ", " + ev.getY());
return true;
}
});
}
The following works using a touch screen on the jetboy sample android app.
Joes code worked with one tweak to make it shine the background thru.
The only line I added was
android:background="#0000"
android:layout_height="fill_parent"
android:layout_height="fill_parent"
Thanks Joe (above).
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}
layout code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.android.samples.jetboy.JetBoyView android:id="#+id/JetBoyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/touchView"
android:background="#0000"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello World, TestActivity"
/>
</FrameLayout>
The corrected version without the errors:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : "
+ String.valueOf(event.getX()) + "x"
+ String.valueOf(event.getY()));
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}