Add OnGestureListener to Button? - android

I'm trying to figure out how to add an OnGestureListener or some other way to detect a touch drag across the screen to a button, or some other widget that can be added to a View. I've been trying to figure out how to do this for a while and I can't. I've seen and successfully been able to do it to an activity. Can anyone spell this out for me, because I seem to be having a lot more trouble than I thought I would with this. Thanks.

I'm assuming you've already gone through http://developer.android.com/resources/articles/gestures.html. The trick is that you can have the GestureOverlayView as a transparent layer on top of another View. You can then interpret any gestures detected as belonging to the underlying View.
In the article, they show you a layout like this:
<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.gesture.GestureOverlayView>
Any gestures detected could then be interpreted as coming from the ListView. In your case replace the ListView with your Button, and you should be all set.

It sounds like you should be doing dragging and dropping,
http://developer.android.com/guide/topics/ui/drag-drop.html

Related

Android fixed background image in scrollview while scrolling

I have a situation where I need a scrollview to have a background image that shouldn't scroll along with it's parent when moving. Before any of you suggest me the links for setting background image and this that, I have already tried and it's not working.
The whole story goes like: I have an activity with fragments which have their own backgrounds with some input fields. When focusing over input fields, keyboard appears and background image squeezes. For that I put an image on background of scrollview that fixed my issue of squeezing background but raised another concern that background Image should stay static while scrolling the parent scrollview.
The second solution any of you may suggest is setting background of my activity rather playing with scrollview. That's right, but for that I had to make a style element with background of theme which appears odd while transitioning different fragments plus it adds overhead when I have a lot of code and fragments to move forward and back.
That's the point where I am stuck. I have gone through links below, if you just need to know that I tried it or not.
link1
link2
link3
... and so on
below is the layout I am using for my fragments (it's all being done programmatically)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</ScrollView>
</LinearLayout>
So if you guys have any better solution keeping in mind the situation I have, will be warmly welcomed. Thank you
None of the above works for me with some reason but this things works.
getWindow().setBackgroundDrawableResource(R.mipmap.img_reg_bg);
put it in oncreate method hope it will help.
If I guess right you try to fix your hotfix to get it working propperly. If I'm right you sould go back to scratch and do it without your scrollview.
If you have a look at following Link:
http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
you'll see softkeyboard mode can be adjusted. "adjustPan" should solve your problem.
"adjustPan"
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
Add someimgage to your drawable folderes, like drawable-mdpi, drawable-hdpi.
In your LinearLayout "#+id/top_layout" add attribute:
android:background="#drawable/someimage"

How does overlaying views work in Android?

In Android, i noticed that you can have a fixed view on top of another. For example, when you open your browser, and tap the search box, a keyboard prompt pops up (on top of a listview). However, notice that you can still scroll up and down on the listview without the keyboard going away. Like:
would someone please explain (preferrably some sample code in addition) how this works?
What i'm trying to do is just have a custom listview that always has a floating navigation bar on top of the listview and also on the bottom of the list view (it's not actually a header/footer of the listview, it's more like a header/footer of the screen). It would be similar to the example i just described, where the user can interact with both the navigation bar as well as the listview "underneath" the nav bar.
I am somewhat new to Android development, so please be nice and provide a little bit of details if you would :) much thanks in advance!!
whoops. looks like someone had a similar issue:
Layout Layers? Z-Axis?
and this post http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html explains how FrameLayout works and also how works, which is an even better alternative.
FrameLayout lays object in a different Z-axis, so this is the solution i was looking for.
There are many ways to achieve that, the simpler i can think of is using linear layout :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/header">
//Here you add whatever you want in your "header"
</LinearLayout>
//create your listview
<ListView
android:id="#+id/content_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:layout_marginRight="10dip"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:scrollbars="none"
android:paddingBottom="5dp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/footer">
//Here you add whatever you want in your "footer"
</LinearLayout>

Android scroller paging with page indicator

I am about finishing my first android app, and i have some questions to which i could not find an answer.
I would like to use a horizontal scroller in which to display several pictures. For that i need two things:
Paging enabled, so that the user can see the pictures one by one in the scroller.
Some kind of indicator to show me the index of the picture currently displayed.
If i manage to do the paging, i could probably display a text like 1/4 (2/4 and so on) if i had 4 pictures, but it is not very nice. I would like to have something more like the iPhone has with the gray/white dots. Is there anything like that, or would i have to implement it by adding content at runtime? (adding imageviews according to the number of the pictures and then changing images for them as the user scrolls to show progress)
Thank you.
Because I was already using ViewPager for the swiping UI, I used the excellent ViewPagerIndicator. It took about 5 minutes to integrate.
<?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="#000000">
<include android:id="#+id/titlebar_include" layout="#layout/titlebar"/>
<include layout="#layout/menu"/>
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:id="#+id/product_viewpager"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>
I found the answer i was looking for: i used the Gallery widget and for the indicator dots i used ImageViews and wrote a couple of lines of code to update the selected image dot.
Here is the solution you can try: PageViews and PageIndicator by GreenDroid.
You can also have a look at sample market app: https://market.android.com/details?id=com.cyrilmottier.android.gdcatalog

Sectioned ListView and ExpandableListView

I need to create a layout with a title bar on the top and a list view with n sections. The
list header of every section has got two distinct Buttons that must be clickable and focusable independently from each other. This is the code of the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/title_bar" />
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
/>
</LinearLayout>
I tried this two solutions but I've found issues that I am not able to solve in both:
1) Using a sectioned adapter (source code can be found here). It works fine without the title bar but with the title bar the default focus algorithm doesn't work any more.
2) Using an ExpandableListAdapter (in this case obviously the ListView in the layout becomes an ExpandableListView). It works fine but if I press the dpad center button on group layout both buttons are clicked. I did not find a way to solve this.
Anyone can help?
Thanks very much!
How about trying Merge adapter https://github.com/commonsguy/cwac-merge it allows you to add multiple list and buttons !!

Android ViewFlipper + Gesture Detector

I am using gesture detector to catch "flings" and using a view flipper to change the screen when this happens. Some of my child views contain list views. The the gesture detector wont recognize a swipe if you swipe on the list view. But it will recognize it if it is onTop of TextView's or ImageView's. Is there a way to implement it so that it will recognize the swipes even if they are on top of another view that has a ClickListener?
Thank you for your answer. In order to get it working how I wanted it to all I had to add was this:
myList.setOnTouchListener(gestureListener);
for each of my lists. Now they correctly recognize horizontal swipes to change views, and vertical movement for scrolling the list.
An example from here: http://android-developers.blogspot.com/2009/10/gestures-on-android-16.html
<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.gesture.GestureOverlayView>
I used this tutorial to look at Android's gesture overlay view. The example which this comes from has a ListView utilizing this GestureOverlayView to scroll the list.
There is another wonderful solution at android-journey.blogspot

Categories

Resources