Top Picks

March 11, 2013


Android: swipe to change the text in the activity


Hello guys I am implementing a project in android where in I am dealing with text files(.txt) and flash files(.swf). I need to implement an activity in such a way that when I swipe, only the content on the page should change(in this case the next text file) alongwith its corresponding flash file.

My MainActivity is:

package com.example.testflashfile; import; import; import; import; import android.content.Intent; import; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.TextView; import android.view.MotionEvent; public class MainActivity extends Activity { Button nextButton; Button playButton; private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; GestureDetector gestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); setContentView(R.layout.activity_main); gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector()); View mainview = (View) findViewById(; // Set the touch listener for the main view to be our custom gesture listener mainview.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }); playButton=(Button)findViewById(; playButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent startAnimation=new Intent(MainActivity.this,PlayAnimationActivity.class); startAnimation.putExtra("SWF_NAME","a"); startActivity(startAnimation); } }); TextView helloTxt = (TextView)findViewById(; helloTxt.setText(readTxt()); nextButton=(Button)findViewById(; nextButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(, menu); return true; } private String readTxt(){ InputStream inputStream = getResources().openRawResource(R.raw.textone); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int i; try { i =; while (i != -1) { byteArrayOutputStream.write(i); i =; } inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return byteArrayOutputStream.toString(); } class MyGestureDetector extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) { return false; } // right to left swipe if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Intent i= new Intent(MainActivity.this,SecondActivity.class); startActivity(i); // left to right swipe } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Intent i= new Intent(MainActivity.this,FifthActivity.class); startActivity(i); } return false; } @Override public boolean onDown(MotionEvent e) { return true; } } }

As you can see here I am trying to go to the next activity to make those to simple changes of changing the text file and the swf file. that way there is a lot of repeatition of the code. i think there must be a better way.
I want to swipe and move to the next text file. Is it possible to do that without making a new activity? if yes. then I dont know how to do that. people have suggested View Pager or ViewFlipper. But all I find online is image flipper examples. I dont know how to implement it on text files. Any help is appreciated. Thanks in advance.



You probably actually want to use a ViewPager and a FragmentPagerAdapter here with a Fragment for each page of contents you want to display. The ViewPager will automatically handle the swiping between Views for you, so you can get rid of the gesture detector all together. There’s a simple example of how to use a ViewPager here.

Professional Hosting fro Just Host

Filed under: Android Coding FAQ



No Comments

Leave a reply