Setting ImageView and TextView at predefined XY coordinates - Xamarin Android - android

I have a Greeting Card activity where one can place Text and Image at desired locations by moving ImageView and TextView using Touch events. After moving to location I want to save its position and use same for other greetings. But neither view.GetX(), view.GetY() nor view.Left, view.Top is working.
I can capture the XY after moving but when using same XY coordinates on SetX & SetY or Left & Top the view is placed at weird locations.
This is my XML layout and using Touch even I move imgV1, txtName and txtSignature.
<RelativeLayout
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/layoutControl"
android:layout_below="#+id/toolbar1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/pBar"
android:layout_gravity="center_vertical" />
</LinearLayout>
<FrameLayout
android:orientation="vertical"
android:id="#+id/GreetingFrame"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/layoutPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
android:id="#+id/imgV1"
android:layout_width="135dp"
android:layout_height="135dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="65dp"
android:scaleType="matrix"/>
</FrameLayout>
<ImageView
android:id="#+id/frameImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/digicard"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"/>
<TextView
android:textSize="18sp"
android:textColor="#000000"
android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="35dp"
android:layout_marginBottom="55dp"
android:text="Your Name"/>
<TextView
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
android:id="#+id/txtSignature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="80dp"
android:layout_marginBottom="2dp"
android:text="9999999999"/>
</FrameLayout>
</RelativeLayout>
TextView touch event
private void TxtName_Touch(object s, TouchEventArgs e)
{
TextView v = (TextView)s;
if (e.Event.Action == MotionEventActions.Move)
{
v.SetX(e.Event.RawX - v.Width / 2.0f);
v.SetY(e.Event.RawY - v.Height / 2.0f);
}
}
Any suggestions ?

Modify your code as below ,it works fine on my side .
float dX, dY;
private void TextView_Touch(object sender, Android.Views.View.TouchEventArgs e)
{
TextView view = (TextView)sender;
int X = (int)e.Event.RawX;
int Y = (int)e.Event.RawY;
switch (e.Event.Action)
{
case MotionEventActions.Down:
dX = view.GetX() - X;
dY = view.GetY() - Y;
break;
case MotionEventActions.Move:
view.Animate().X(X + dX).Y(Y + dY).SetDuration(0).Start();
break;
}
}
Refer to https://stackoverflow.com/a/31094315/8187800.

Related

Scrollable Pager Content Inside a NestedScrollView Issue

I have a viewpager inside of a nested scroll view that may or may not contain data. If it does contain data, it is a two column staggered grid view that paginates. I want to be able to scroll down far enough where the above view is completely hidden and I can scroll the recycler view full screen. The problem I have is once the data loads in, I can only scroll a little bit more. It looks like the nested scroll range is extended by the height of the first item that appears in the recycler view. Is there something that I am missing?
Here is what is currently happening:
Where The entire view is scrollable when the items in the recycler view load but the scroll range only expands to the height of the first Item. I want to be able to scroll to the top of the tabs once the recycler loads like this image:
I am just confused on exactly what is happening here.
Here is the xml for the view in question
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/comment_layout_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/card_background">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/comment_footer"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/image_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/collection_image"
android:layout_width="match_parent"
android:layout_height="#dimen/item_detail_container_height"
android:background="#color/color_black"
android:scaleType="centerCrop"
android:layout_centerInParent="true"
/>
<Button
style="#style/Button.LightPurple.Inverse"
android:id="#+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="#string/collection_edit"
android:visibility="gone"
/>
</RelativeLayout>
<include layout="#layout/item_detail_multi_nav_layout" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_list_divider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/collection_title"
android:layout_width="397dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="#dimen/body_font_padding"
android:gravity="center_vertical"
android:text="#string/live_action_item_name"
android:textColor="#color/on_board_clubs_join_btn"
android:textSize="18sp" />
<TextView
android:id="#+id/collection_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="#dimen/body_font_padding"
android:autoLink="web"
android:gravity="center_vertical"
android:maxLines="3"
android:text="#string/medal_item_description_placeholder"
android:textColor="#color/color_dark_gray"
android:textSize="14sp" />
<TextView
android:id="#+id/collection_item_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="#dimen/body_font_padding"
android:gravity="center_vertical"
android:text="#string/item_collection_count"
android:textAppearance="#style/TextAppearance.Large.Black.MontserratSemiBold"
android:textColor="#color/search_follow_button"
android:textSize="12sp" />
<TextView
android:id="#+id/collection_uploadedby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="#dimen/body_font_padding"
android:layout_marginTop="15dp"
android:gravity="center_vertical"
android:text="#string/item_uploaded_by"
android:textAppearance="#style/TextAppearance.Large.Black.Montserrat" />
<include layout="#layout/action_feed_header_for_uploadedby" />
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tab_picker"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center"
android:elevation="0dp"
app:tabPaddingStart="5dp"
app:tabPaddingEnd="5dp"
app:tabMinWidth="0dp"
app:tabIndicatorColor="#color/colorAccent"
app:tabIndicatorHeight="4dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/bpDark_gray"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabTextColor="#color/bpWhite" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_list_divider" />
<com.gemr.android.widgets.misc.WrapContentViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/card_background"
android:nestedScrollingEnabled="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<LinearLayout
android:id="#+id/comment_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#color/card_background"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_list_divider" />
<LinearLayout
android:id="#+id/comment_box_layout"
style="#style/comment_box_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bpWhite"
android:orientation="horizontal">
<EditText
android:id="#+id/comment_box"
style="#style/comment_box_text"
android:layout_width="0dp"
android:layout_height="#dimen/comment_text_box_height"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#drawable/edit_text_post"
android:gravity="center_vertical"
android:hint="#string/comment_box_hint"
android:inputType="textCapSentences|textMultiLine"
android:maxLines="3"
android:selectAllOnFocus="true" />
<TextView
android:id="#+id/comment_post"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/card_view_selector"
android:gravity="center_vertical"
android:padding="4dp"
android:text="#string/comment_post"
android:textAppearance="#style/TextAppearance.Large.Gray.Montserrat" />
</LinearLayout>
</LinearLayout>
And here is the RecyclerView that is set up with the pager:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/card_background">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/feed_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="3"/>
</android.support.v4.widget.SwipeRefreshLayout>
<include layout="#layout/fab_layout" />
Am I overlooking something very simple here?
Try this may be this will help you
public class CustomNestedScrollView extends NestedScrollView {
private int slop;
private float mInitialMotionX;
private float mInitialMotionY;
public CustomNestedScrollView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
ViewConfiguration config = ViewConfiguration.get(context);
slop = config.getScaledEdgeSlop();
}
public CustomNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private float xDistance, yDistance, lastX, lastY;
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final float x = ev.getX();
final float y = ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();
// This is very important line that fixes
computeScroll();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - lastX);
yDistance += Math.abs(curY - lastY);
lastX = curX;
lastY = curY;
if (xDistance > yDistance) {
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}
The problem is that the Recyclerview is set to match_parent. There for it's size will equal to the screen size minus the size of all other things in the NestedScrollView combined.
In a device which needs the whole screen to contain the other things in the NestedScrollView, the size of the Recyclerview will be even 0.
Step 1:
Change the height of the Recyclerview and its parents (SwipeRefreshLayoutand FrameLayout) to wrap_content.
Step 2:
In case this doesn't work as expected, set a fixed size to Recyclerview (only to Recyclerview and the parents should remain wrap_content).
Step 3:
If you want it to work exactly as the provided image, I think you need to calculate the size at run-time. In that case you can undo the step 2.
Why don't you try the combination of Coordinate Layout and AppBarLayout to acheive this. Its much more simpler. You can check this link for different scrolls.
https://github.com/codepath/android_guides/wiki/Handling-Scrolls-with-CoordinatorLayout
This link shows you exactly what you want to acheive
https://gist.github.com/iPaulPro/1468510f046cb10c51ea

Android views tate changes resets layout

I have a list with Swipe Actions on it using IOnTouchListener, as dragging occurs I am changing the Left of a LinearLayout to reveal a second LinearLayout behind it. That layout has two buttons, one is hidden. If the user does a "quick" swipe then I move the Left of the top view to a specific value and fire some code. This code changes the invisible button to be visible. When the ViewState of that button changes it makes the top view's Left snap back to 0 instead of staying where I place it. Any idea why it would do this or how to work around it.
The XML looks like... (But will change slightly based on the answer to another question I posted)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_vertical"
android:background="#color/black"
android:id="#+id/Swipe"
>
<LinearLayout
android:id="#+id/LeftContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/yellow"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<Button
android:id="#+id/ApproveButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#2796C3"
android:text="Approve"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/ApproveUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#215681"
android:text="Undo"
android:layout_toRightOf="#id/ApproveButton"
/>
</LinearLayout>
<LinearLayout
android:layout_alignParentRight="true"
android:id="#+id/RightContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="horizontal"
>
<Button
android:id="#+id/DenyButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="Deny"
/>
<Button
android:id="#+id/DenyUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#860000"
android:text="Undo"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/TopContentView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1F1F1F">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="match_parent" >
<ImageView
android:id="#+id/UnreadImage"
android:layout_height="match_parent"
android:layout_width="7dp"
android:src="#drawable/vertical_blue_bar"
android:background="#2796C3"/>
<LinearLayout
android:id="#+id/ListText"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:padding="12dp">
<TextView
android:id="#+id/Text_Line1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="11dip"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I am not using Java, I am using Xamarin with C#. Here are the relevant pieces of code...
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
OriginalTouchX = Math.Ceiling(e.RawX);
index = e.ActionIndex;
pointerId = e.GetPointerId(index);
VelocityTracker.AddMovement(e);
break;
case case MotionEventActions.Move:
VelocityTracker.AddMovement(e);
ChangeX(newX);
VelocityTracker.ComputeCurrentVelocity(1);
float velocity = Math.Abs(VelocityTracker.GetXVelocity(pointerId));
if (velocity > SlowDrag && Math.Abs(distanceMoved) > (DragThreshold / 3))
{
QuickApprove();
}
break;
}
}
void QuickApprove()
{
ToggleUndoButton(true, true);
WaitingForUndo = true;
ChangeX(DragThreshold);
this.ApproveTimer.Start(true);
}
private void ToggleUndoButton(bool ShowUndo, bool LeftSide)
{
this.ApproveUndoButton.Visibility = ViewStates.Visible;
this.ApproveButton.Text = "Approving...";
}
private void ChangeX(int newX)
{
int width = this.TopContentView.Right - this.TopContentView.Left;
this.TopContentView.Left = newX;
this.TopContentView.Right = this.TopContentView.Left + width;
}
If in the ToggleUndoButton method I comment out the line
this.ApproveUndoButton.Visibility = ViewStates.Visible;
Everything works fine. The Left of TopContentView changes to be equal to my DragThreshold, the text changes to Approving... the timer starts, It stays this way for 2 seconds, then the code in my timer tick fires and the TopContentView is moved back to a Left of 0. If I leave in the visibility change then the TopContentView is immediately moved back to 0 instead of waiting until the timer is done.

Android: Horizontal and vertical scroll for recyclerview

I am trying to implement horizontal and vertical scrolling for a Recycler view at the same time.I have to show a table of 8 columns, so I plan to implement horizontal and vertical scrolling at the same time.
I tried HorizontalScrollView in list row but it is scrolling horizontally in one row.
I also tried the HorizontalScrollView as the parent of recyclerview but its not working
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:textColor="#color/title"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaa"/>
<TextView
android:layout_toRightOf="#+id/title"
android:id="#+id/genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="bbb"/>
<TextView
android:layout_toRightOf="#+id/genre"
android:id="#+id/year"
android:textColor="#color/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content"
android:text="ccc"/>
</RelativeLayout>
Activity_main.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="horizontal" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
can any one please tell me an idea.
The best solution i found so far to have horizontal and vertical scrolling on one view is to put it inside one FrameLayout. Then your view must implement OnTouch (you can do this in your activity to) and you just have to scroll it as the touch pointer moves.
Here is an example :
xml :
<FrameLayout
android:id="#+id/TwoWaysScrollableContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false">
<YourView
android:id="#+id/GridContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</FrameLayout>
C# (i use xamarin, but Java wouldn't have so many differencies i think)
:
public class MyActivity : Activity
{
private MyView _myView; //Can be GridView, relative layout or wathever
private float _startX, _startY,_prevDx, _prevDy, _dx,_dy;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_myView = FindViewById<MyView>(Resource.Id.GridContainer);
}
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
{
mode = Mode.DRAG;
startX = e.GetX() - prevDx;
startY = e.GetY() - prevDy;
break;
}
case MotionEventActions.Move:
{
/* you also could add logic to prevent your view from scrolling out of you grid bounds*/
if (mode == Mode.DRAG)
{
dx = startX - e.GetX();
dy = startY - e.GetY();
_myView.ScrollBy((int)(dx), (int)(dy));
startX = e.GetX();
startY = e.GetY();
}
break;
}
case MotionEventActions.Up:
{
mode = Mode.NONE;
prevDx = dx;
prevDy = dy;
break;
}
}
return true;
}
}
I know this doesn't answer your question directly ( i don't know much about recycler view), but i hope it can still help you. I used this for a tile engine and it did the trick very well.

android onTouch event in listview items

I've searched around and has read this thread and this thread and so, but nothing helped me .
I have a listview that contains custom layouts. the layout has a custom seekbar. I've used the seekbar out of the listview and it works very well but when uses in list view the onTouch event does not called correctly and runs only when I'm start touching it.
this is a part of my code in getView:
newSimpleDim.layTouch1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (y < 0)
y = 0;
if (y > dimHeight)
y = dimHeight;
G.log("Event : " + event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
setHeight(newSimpleDim.layGray1, y);
setHeight(newSimpleDim.layGreen1, dimHeight - y);
setPadding(newSimpleDim.imgGreen1, -y);
newSimpleDim.txtlbl1.setText("" + (100 - ((int) ((y / (double) dimHeight) * 100))));
break;
}
return true;
}
});
and this is list item layout :
<LinearLayout
android:id="#+id/dimmerOne"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layTouch1"
android:layout_width="wrap_content"
android:layout_height="202dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layGray1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgGray1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="#drawable/lay_component_dimmer_slider_off" />
</LinearLayout>
<LinearLayout
android:id="#+id/layGreen1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgGreen1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="#drawable/lay_component_dimmer_slider_on" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/txtlbl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextView"
android:typeface="monospace" />
</LinearLayout>
This problem seems similar to the one I was facing, I am has checkbox and on itemClickListener was not working, did the following to make itemClickListener work,
Added the following properties to the checkbox,
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
and ItemClickListner started working.
for you if onTouch is not getting invoked for the seek bar then add the above code to the layout.
For detailed example for checkbox clickable in listview you can go through the link, and you can apply the same concept to make your onTouch wor
http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/
Hope it helps Cheers!!

Detect finger leave of image button in android

I have an image button, i am trying to record an audio on touch of a button & when user leaves a button i stop recording.I am successfull in doing that.I have a problem when user starts moving its finger within that bounds of imagebutton.I am using on ACTION_MOVE to handle this, i am not getting 100% success , sometimes it detects on leave , sometimes not. Please help. Below is code i am using..
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.btn_audio :
final int action = event.getAction();
// get the View's Rect relative to its parent
v.getHitRect(rect);
// offset the touch coordinates with the values from rect to obtain meaningful coordinates
final float x = event.getX() + rect.left;
final float y = event.getY() + rect.top;
switch (action) {
case MotionEvent.ACTION_DOWN :
audioBtn.setImageDrawable(getResources().getDrawable(R.drawable.img_audio_pressed));
mHandler.sendEmptyMessage(MSG_START_TIMER);
timer.setVisibility(View.VISIBLE);
playBtn.setVisibility(View.GONE);
break;
case MotionEvent.ACTION_MOVE :
Log.e(TAG, rect.top+","+rect.bottom+","+rect.left+","+rect.right);
Log.e(TAG, (int)x+","+(int)y);
if (!rect.contains((int) x, (int) y)) {
Log.e(TAG, "In action move,outside");
audioBtn.setImageDrawable(getResources().getDrawable(R.drawable.img_audio));
playBtn.setVisibility(View.VISIBLE);
if (stop)
mHandler.sendEmptyMessage(MSG_STOP_TIMER);
} else {
Log.e(TAG, "In action move,inside");
audioBtn.setImageDrawable(getResources().getDrawable(R.drawable.img_audio_pressed));
timer.setVisibility(View.VISIBLE);
playBtn.setVisibility(View.GONE);
}
break;
case MotionEvent.ACTION_UP :
// the user raised his finger, cancel the Runnable
audioBtn.setImageDrawable(getResources().getDrawable(R.drawable.img_audio));
playBtn.setVisibility(View.VISIBLE);
if (stop)
mHandler.sendEmptyMessage(MSG_STOP_TIMER);
break;
}
break;
default :
break;
}
return false;
}
XML ::
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/actvity_background"
android:padding="15dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/record_review"
style="#style/textview.review"
android:layout_below="#id/rating_app"
android:layout_marginTop="20dp"
android:text="Record a review" />
<LinearLayout
android:id="#+id/record"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/record_review"
android:orientation="horizontal"
android:splitMotionEvents="false"
android:weightSum="2" >
<RelativeLayout
android:id="#+id/audio_record"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<ImageButton
android:id="#+id/btn_audio"
style="#style/imagebutton"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:background="#android:color/transparent"
android:contentDescription="#string/desc_provider_thumb"
android:focusableInTouchMode="true"
android:src="#drawable/img_audio" />
<TextView
android:id="#+id/timer"
style="#style/textview.review"
android:layout_below="#id/btn_audio"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="00:30"
android:visibility="gone" />
<ImageButton
android:id="#+id/btn_play"
style="#style/imagebutton"
android:layout_below="#id/btn_audio"
android:layout_marginLeft="6dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/timer"
android:background="#android:color/transparent"
android:contentDescription="#string/desc_provider_thumb"
android:src="#drawable/img_play"
android:visibility="gone" />
<ImageButton
android:id="#+id/btn_pause"
style="#style/imagebutton"
android:layout_below="#id/btn_audio"
android:layout_marginLeft="6dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/timer"
android:background="#android:color/transparent"
android:contentDescription="#string/desc_provider_thumb"
android:src="#drawable/img_pause"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/video_record"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<ImageButton
android:id="#+id/btn_video"
style="#style/imagebutton"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:background="#android:color/transparent"
android:contentDescription="#string/desc_provider_thumb"
android:src="#drawable/img_video" />
<RelativeLayout
android:id="#+id/video_preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<ImageView
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/img_dumy_home_video_thumbnail" />
<ImageButton
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/img_play" />
</RelativeLayout>
<Button
android:id="#+id/btn_rerecord"
style="#style/button.review"
android:layout_below="#id/video_preview"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:text="#string/re_record"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="#+id/btn_submit_review"
style="#style/button.review"
android:layout_below="#id/record"
android:text="Submit review" />
</RelativeLayout>
return true
instead of
return false
in your onTouch method
Instead of v.getHitRect(rect); use getLocationInWindow() which returns screen co-ordinates instead of co-ordinates relative to the parent. Update your onTouch code as follows:
//same here
...
...
int[] location = new int[2];
v.getLocationInWindow(location); // get position on the screen.
rect = new Rect(location[0], location[1], location[0]+v.getWidth(), location[1]+v.getHeight());
final float x = event.getX(); // screen x position
final float y = event.getY(); // screen y position
...
...
//same here

Categories

Resources