I'm trying to use zoom in an ImageView. I've searched and tried so many codes but the problem is that I have ImageView inside an ScrollView because I have some other objects.
This is the last code if tried.
And here is xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context=".ConsumoActivity"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayoutLogoFactor_consumo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/imageViewLogoFactor_consumo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo_factorenergia" />
</LinearLayout>
<ScrollView
android:id="#+id/scrollViewConsumo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/linearLayoutConsumo2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewVerConsumo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="#string/etiqueta_ver_consumo"
android:textSize="#dimen/textSize"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textColor="#79b7e3" />
<LinearLayout
android:id="#+id/linearLayoutDireccionConsumo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:weightSum="1.0">
<TextView
android:id="#+id/textViewlabelDireccionConsumo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="#string/etiqueta_direccion"
android:textSize="#dimen/textSize"
android:textStyle="bold" />
<TextView
android:id="#+id/textViewDireccionConsumo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:textSize="#dimen/textSize" />
</LinearLayout>
<com.example.factorenergia.CustomImageView
android:id="#+id/imageViewConsumo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:adjustViewBounds="true"
android:scaleType="matrix" />
<RelativeLayout
android:id="#+id/linearLayoutImagenInferior_consumo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp" >
<ImageView
android:id="#+id/imageViewImagenInferior_consumo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="#drawable/la_electrica_de_las_empresas" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Image is ok but scroll is not working. Is there any way for solving that having ImageView inside ScrollView with some other objects?
This is probably because the overriden ImageView is handling all touch events. It should take only two-finger events and pass the rest of them back to the parent. You should return false from onTouchEvent, when the event is not for your ImageView.
The second option is to handle events in dispatchTouchEvent method of the ScrollView. You can handle one-finger events in the ScrollView and pass the rest of events to children. This is not recomended, because it may break a lot.
I've used an easy solution:
Let my layout as it was and when ImageView is clicked, go to a new one which only has the Image, using Mike Ortiz code at that question.
The answer provided by #Zielony clarified things for me, as I was struggling with a similar issue. I had a zoomable imageview inside scrollView, and both these views were conflicting. So I created a custom scrollView that intercepts touch events only if one finger is touching the screen. So, you will be able to interact with the imageview zooming feature with two fingers, and scroll our custom scrollView with one finger.
class CustomScrollView #JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0) : ScrollView(context, attrs, defStyleAttr) {
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
return if (ev.pointerCount < 2) {
super.onInterceptTouchEvent(ev)
} else {
false
}
}
}
Related
I am showing and hiding views on a particular actions using some animation, I used LayoutTransition and it seems to work fine except one thing, while the animation is going, the background of some TextViews changes and reset to the default white background!!
Following is the code I am using to apply animation after hide/show.
mRightTransitioner = new LayoutTransition();
mRightTransitioner.setDuration(1000);
vg_rightContainer.setLayoutTransition(mRightTransitioner);
Edited: even without animation i tried, same problem, the background resets once view's position changes
Edited: here how i declared the views in the XML
<LinearLayout
android:id="#+id/ll_req_reasonwrapper"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:gravity="right"
android:orientation="horizontal"
android:visibility="gone" >
<EditText
android:id="#+id/et_req_reasons"
android:layout_width="400dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:gravity="right|center_vertical"
android:inputType="text"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_preasons" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_req_timewrapper"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:gravity="right"
android:orientation="horizontal"
android:visibility="gone" >
<LinearLayout
android:id="#+id/ll_req_endtimewrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/ftv_req_endtime"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:freezesText="true"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_endtime" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_req_starttimewrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="50dp" >
<TextView
android:id="#+id/ftv_req_starttime"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:freezesText="true"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_starttime" />
</LinearLayout>
</LinearLayout>
for instance: if i hide 'll_req_reasonwrapper', then the LinearLayout 'll_req_timewrapper' moves up, then the backgrounds of the textViews like 'ftv_req_starttime' becomes white!!
Please help.
Edited Answer:
if your are using linear layout then if the child is not being displayed, it is not going to be laid out and not going to be the part of the measurements ( as it looks like ). I recommend to use the RelativeLayout.
You can try
android:background="#null"
for your buttons. if your are using some custom class for button then add
.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
for this specific views.
Need more code. The fact that disabling the LayoutTransition does not solve your issue, means that the animation is not the issue.
How do you reinstantiate child views or populate your view. Are you removing views with setVisibility(View.GONE) and then setting them back with setVisibility(View.VISIBLE)
OR
Are you removing childAt(i) and then dynamically adding custom views
I'm sorry I couldnt add a comment since I dont have the reputation for that but I think I am able to answer your question if you post more code.
I have a bar layout with four ImageViews.
The code is showed below
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/contact_bar_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/contact2"
android:orientation="vertical"
android:visibility="gone" >
<ImageView
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/contact_phone_state" />
<ImageView
android:id="#+id/contact_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/contactcall_state" />
<ImageView
android:id="#+id/contact_call2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/contact_phone_state" />
</LinearLayout>
<ImageView
android:id="#+id/contact_bar_trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/contact_phone_state" />
</LinearLayout>
At first the ImageView(contact_bar_trigger) would be showed on the screen only,
after touch(keep action_down) it, the LinearLayout(contact_bar_layout) will show up,
and the problem comes, is it possible to choose one ImageView from contact_bar by the touch motionevent action_up?
if not, please give me some suggestion to implement this. Thank you~~
I don't really understand your problem but, if I've good understood what you're excepting, you'd like to select a picture from your layout (with the 3 ImageView ; called contact_bar_layout) by a motionevent action_up.
To do it, you have to use code (and not just XML) and for each ImageView, you add a listener for each of them (you can use a method for all or do it separately). Then in each listener, you define what you want to do when this motionevent action_up is done.
Hope it helps.
To be able to select the item (the row) AND sub items on the row, I use android:descendantFocusability="blocksDescendants". And I'm using the onItemSelectevent....
I as well use a custom background for my row.
Now the problem is, when I touch a row, the checkbox get's it's selected background as well (the blue default background, when you select a checkbox), how do I avoid this? I only want the row itself to adjust it's background to the item state, when I touch the row.
I'm using following row layout as a row in a listview in an adapter:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/list_gradient"
android:descendantFocusability="blocksDescendants" >
<CheckBox
android:id="#+id/cbSelected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/tvDayName"
style="#style/text_list_info_big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/cbSelected"
android:text="Tag" />
<RelativeLayout
android:id="#+id/drag_handle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" >
<ImageView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="#drawable/grabber" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/s"
android:layout_toRightOf="#+id/tvDayName"
android:orientation="vertical" >
<TextView
android:id="#+id/tvCount"
style="#style/text_list_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="" />
<TextView
android:id="#+id/tvInfo"
style="#style/text_list_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="" />
<TextView
android:id="#+id/tvInfo2"
style="#style/text_list_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="" />
</LinearLayout>
</RelativeLayout>
PS: I know I could handle the onClick event of the row and the sub items in my adapter instead of using onItemClick, but I'm using the DragAndSortList view and doing it that way does not work with the drag&drop there...
I found a solution. Overriding SetPressed of the row didn't work.
Now I'm using following RelativeLayout and that's solving my problem:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class UnpressableRL extends RelativeLayout
{
public UnpressableRL(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
protected void dispatchSetPressed(boolean pressed) {
// avoid handing on the event to the child views
}
}
I have been trying to find a solution for this for hours and it feels like I searched every question on stackoverflow and tried everything in the layout.
Before I tried the layout displayed below, the edittext and the two imagebuttons were inside the scrollview. In this old layout the ontouch worked perfectly in my fragment, however once I pulled out the relative layout with the edittext and the two image buttons the ontouch was not fired. Can someone help me with this problem? Any help is appreciated. Thanks
View fragmentView = inflater.inflate(R.layout.request, container, false);
fragmentView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#BDCCE0" >
<EditText
android:id="#+id/etsearch"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="10dp"
android:hint="search..."
android:singleLine="true"
android:imeOptions="actionDone" />
<ImageButton
android:id="#+id/bsearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/etsearch"
android:layout_marginTop="9dp"
android:src="#drawable/search" />
<ImageButton
android:id="#+id/bupdaterequests"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toRightOf="#id/bsearch"
android:layout_marginTop="9dp"
android:src="#drawable/refresh" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="#BDCCE0"
android:layout_below="#id/etsearch" >
<TableLayout
android:id="#+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</RelativeLayout>
A TouchEvent starts at the "highest" element and trickles "down" the View hierarchy until it is consumed by some event. In this case the ScrollView, Buttons, or EditText all consume the event before it reaches the RelativeLayout...
basically I only want to that when someone touches outside the edit text the keyboard disappears.
I addressed this by simply making the root layout clickable and focusable.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
android:clickable="true" resolved the issue..
In my application I want to get the touch event of all child view's in my parent view's onTouchListener but I could not get this.
Example:
In my activity's view I have one frame layout which has some buttons and edittexts in it. So whenever I touch buttons or edit text I want to get this touch event in framlayout's onTouchListeners.
Here is my code..
Activity:
private FrameLayout rootView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rootView = (FrameLayout) findViewById(R.id.rootview);
rootView.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Log.e("Touch Event: ", " X: " + event.getX() + " Y: " + event.getY());
return false;
}
});
}
main.xml:
<?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"
android:padding="10dip"
android:id="#+id/rootview">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
android:layout_gravity="center"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="4dp"
android:text="Login"
android:textColor="#android:color/black"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Username"
android:textColor="#android:color/black"
android:textSize="14dp"
android:textStyle="bold" />
<EditText
android:id="#+id/edttextusername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi"
android:singleLine="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Password"
android:textColor="#android:color/black"
android:textSize="14dp"
android:textStyle="bold" />
<EditText
android:id="#+id/edttextpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi"
android:password="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp" >
<Button
android:id="#+id/btnlogin"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:enabled="false"
android:text="Login"
android:padding="5dip"
android:textColor="#android:color/black"
android:textStyle="bold"
/>
<Button
android:id="#+id/btncall"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:padding="5dip"
android:text="Cancel"
android:textColor="#android:color/black"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Problem: So whenver I touch the buttons or edittext I can't get touch co-ordition in my frame layout's onTouchListener (It's not called).
Note: I don't want to add separate onTouchListener for all childViews.
Thanks in advance.
You could accomplish that by overriding dispatchTouchEvent in the layout.
public class MyFrameLayout extends FrameLayout {
#Override
public boolean dispatchTouchEvent(MotionEvent e) {
// do what you need to with the event, and then...
return super.dispatchTouchEvent(e);
}
}
Then use that layout in place of the usual FrameLayout:
<com.example.android.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:id="#+id/rootview">
...
You could also skip the super call entirely if you need to prevent child views from receiving the event, but I think this would be rare. If you need to recreate some, but not all, of the default functionality, there is a lot to rewrite:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/view/ViewGroup.java#ViewGroup.dispatchTouchEvent%28android.view.MotionEvent%29
I have tried similar design with (only one onTouch Listener on a root FrameLayout) and it worked. I get all the points provided that onTouch return true instead of false. Otherwise, I just get the first point. I couldn't find out the reason for this "return" issue since I expect opposite behaviour.
However, based on my experience, if you set any of child view clickable, then its parent's onTouch will not work. If you have another solution and if you share, that would be great.
2 solutions:
Use onInterceptTouchEvent on the ViewGroup, as shown here
Avoid having the layout handle the touch events, and have a view that is the child view of the layout, that covers its whole size, to handle the touch events.
It's not as efficient as extending classes, but it is much easier and doesn't require to create new files/classes.
example:
Just add the touch events to the view, and it will handle them instead of any of the other views.
It can be achieved by using "onInterceptTouchEvent". Please try this,it may work
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
onTouchEvent(ev);
return false;
}