1

Based on json ArrayList size I'm creating TextView's.

By using the class Display , made each TextView height and width to cover the entire screen.

MOTTO

Only 1 TextView should be visible on the screen. By swiping it should move to next view which will again occupy the entire screen.

Swipe down and Swipe up will move the screens i.e., views... swipe left and swipe right should do some other tasks,such as changing activity

Swipe is enabled by using GestureDetector.SimpleOnGestureListener

So far I've tried using ViewFlipper, TextView array to enable switching between TextView.But FAILED :(

Code snippet:

        for(int i=0;i<name.size();i++)
        {
            text = new TextView(MainActivity.this);
            text.setText(name.get(i));
            text.setId(i);
            text.setTextColor(Color.parseColor("#000000")); 
            text.setLayoutParams(new LinearLayout.LayoutParams(realWidth, realHeight));
            text.setGravity(Gravity.CENTER);
            text.setTextSize(40);
            text.setClickable(true);
            vf.addView(text);

           /* 
           //I've tried the following code while using TextView array
            myTextViews[i] = text;
            myTextViews[i].setId(i);
            myTextViews[i].setTextColor(Color.BLACK);
            myTextViews[i].setText(name.get(i));
            myTextViews[i].setGravity(Gravity.CENTER);
            myTextViews[i].setTextSize(40);
            myTextViews[i].setLayoutParams(new LinearLayout.LayoutParams(realWidth, realHeight));
            myTextViews[i].onWindowFocusChanged(false);
            LL.addView(myTextViews[i]);
*/

            View lines = new View(getApplicationContext());
            lines.setBackgroundColor(Color.parseColor("#000000"));
            lines.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
            vf.addView(lines);

            final int finalI = i;


            text.setOnTouchListener(new MainActivity()
            {
                @Override
                public void onSwipeLeft()
                { 
                    if (vf.getDisplayedChild() == 0)
                        Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
                    else
                        vf.showNext();
                }

                @Override
                public void onSwipeRight()
                {
                    if (vf.getDisplayedChild() == 0)
                        Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
                    else
                        vf.showPrevious();
                }
            });
        }

Errors:

  1. While using ViewFlipper

    E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback

  2. Array:

    E/InputEventReceiver﹕ Exception dispatching input event. -- java.lang.ArrayIndexOutOfBoundsException

EDIT

I found this Question related to ios. Searching the same for android

I'm trying to develop a app similar to SimplEye which will be used by Visually disabled people.

For that, I need to control the swipes on the screen so that entire app could be handled only through the help of swipes.

ViewPager , ViewFlipper , SimpleOnGestureListener are not matching the requirement.

Kindly suggest what Technique should be used. Thank you

Community
  • 1
  • 1
Prabs
  • 4,923
  • 6
  • 38
  • 59

2 Answers2

2

bases on the question what i can suggest is use ViewPager

which is alternative for your MOTTO not the solutions of your issue

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="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

ViewPagerActivity

    public class ViewPagerActivity extends Activity {
                String text[] = {"A", "B",
                        "C", "D",
                        "E", "F",
                        "G", "H"};

                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    MyPagerAdapter adapter = new MyPagerAdapter(this, text);
                    ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
                    myPager.setAdapter(adapter);
                    myPager.setCurrentItem(0);
//set Page Change Listner. to get callback on page changed or swiped
    myPager .setOnPageChangeListener(new OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    Log.e("Page Changed ", " YES ");
                    /// here you can check & perform on changed
                    Log.e("Current TextView Text ", text[position]);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

                }
            }

MyPagerAdapter

public class MyPagerAdapter extends PagerAdapter {

        Activity activity;
        int txtarray[];

        public MyPagerAdapter(Activity act, int[] imgArra) {
            txtarray = imgArra;
            activity = act;
        }

        public int getCount() {
            return txtarray.length;
        }

        public Object instantiateItem(View collection, int position) {
            TextView view = new TextView(activity);
            view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            view.setText(txtarray[position]);
            ((ViewPager) collection).addView(view, 0);
            return view;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);
        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }
user1140237
  • 5,015
  • 1
  • 28
  • 56
  • Thank you so much for the code.. I'll test it tomorrow and get back to you. Once again thankyou for your time – Prabs Nov 17 '15 at 13:41
  • I've implemented ViewPager and changed it from horizontal to vertical scroll.. but can we perform some other action on swipe left and swipe right.. can we override those methods?? Am not finding anything like that – Prabs Nov 18 '15 at 06:57
  • @Prabs you can add listener to viewpager to get callback on page change(like after swipe from left to right or right to left it will give common callback on ` onPageSelected `) for more details you can check [ViewPager.OnPageChangeListener](http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html) – user1140237 Nov 18 '15 at 07:39
  • Is that for vertical scroll – Prabs Nov 18 '15 at 08:01
  • Swipe down and Swipe up will move the screens i.e., views... swipe left and swipe right should change activity – Prabs Nov 18 '15 at 08:17
  • I've added toast and log in `onPageScrolled` , `onPageSelected`, `onPageScrollStateChanged` but everything is called in swipe left and right nothing on swipe up and down :( :( – Prabs Nov 18 '15 at 08:33
  • @Prabs have added listner to viewpager? – user1140237 Dec 16 '15 at 06:17
0

Use ViewPager for sliding the views (only by swipe gesture; if you need swipe during time then ViewFlipper is better approach). Make your ViewPager layout_width/layout_height attrs both match_parent (define ViewPager in xml layout not via code). Also make your TextView layout_width/layout_height match_parent too thus you don't need Display class at all.

EDIT. According to latest edition TS needs to handle gesture navigation across the whole app. I suggest the following:

  1. try to not use any widgets which handle gestures (click) by themselves (button, checkbox etc.). Use only uninteractable Views and implement onTouchEvent method inside your Activity which will receive all touch events in this case. In that method you can add any gesture handling you want.
  2. You can create your own ViewGroup implementation and override there onInterceptTouchEvent/onTouchEvent methods and perform there any gesture handling manually.

In both cases you need to create all gesture detection logic by yourself.

These links may be helpful

  1. Creating a simple Gesture Application in Android
  2. How to add our own gestures in android?
  3. Detecting Common Gestures

Never did it but the first link seems to be the most useful. It says that you can firstly create some kind of gesture description and then gesture API can check any gesture performed for match to that description.

Community
  • 1
  • 1
Lingviston
  • 5,479
  • 5
  • 35
  • 67
  • If I make `TextView` `layout_width/layout_height` `match_parent` then only one `TextView` is visible. I'm not getting multiple views – Prabs Nov 17 '15 at 13:14
  • Just went through the doc. Guess it's a big process. Are you sure that it will fulfill the requirement? Should I give a try.. I guess YES... By using `viewpager.setCurrentItem(int index);` I can access the required view.. rt?? – Prabs Nov 17 '15 at 13:25
  • @Prabs No, you can't access view this way. setCurrentItem displays the item you need. Setup of item done by adapter. Pros of ViewPager is that it implements swipe gesture the best way. Cons is that it can't swipe automatically by timer and it can't act as carousel (this actually can be done by workaround). – Lingviston Nov 17 '15 at 15:14
  • Edited the question, would you check it once @Lingviston – Prabs Nov 18 '15 at 12:53
  • But, am done with gesture. I can track which swipe is performed by using `GestureDetector.SimpleOnGestureListener`. I want to go to next view when swipe up or down is tracked i.e., at `@Override public void onSwipeRight() { //what should be here to go to next view }`. There I got stuck. – Prabs Nov 19 '15 at 06:12
  • For ViewPager - setCurrentItem(numberOfItem), for ViewFlipper - showNext. – Lingviston Nov 19 '15 at 07:05