john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Android ontouchevent read file

Coded on my android device and then compiled and run on the android device during long commutes

// 2012-05-06 johnpfeiffer
// TODO: random access bytes fill screen, not newlines
package org.me.mydemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.MotionEvent;
import android.widget.TextView;
import android.graphics.Color;

import android.view.Menu;
import android.view.MenuItem;


import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.lang.StringBuilder;

public class MainActivity extends Activity
{
  private LinearLayout mainView;
  private TextView viewA;
  private static final int MENU_QUIT = 0;
  private static final int MENU_NEW = 1;

  private int location = 10;
  int bufferSize = 3;

  @Override public void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );
    mainView = new LinearLayout( this );
    mainView.setOrientation( LinearLayout.VERTICAL );
    mainView.setBackgroundColor( Color.WHITE );
    mainView.setLayoutParams( new LinearLayout.LayoutParams( 500 , 600 ) );
    viewA = new TextView( this );
    viewA.setBackgroundColor( Color.WHITE );
    viewA.setTextColor( Color.BLACK );
    viewA.setTextSize( 16 );
    LinearLayout.LayoutParams layoutParams =
      new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT ,  LinearLayout.LayoutParams.WRAP_CONTENT );
 // layoutParams.setMargins(30,20,30,0);
    mainView.addView( viewA , layoutParams );
    setContentView( mainView ); 
  }


  @Override public boolean onTouchEvent( MotionEvent event )
  {
    boolean result = false;
    switch( event.getAction() )
    {
//ACTION_MOVE , ACTION_UP
      case MotionEvent.ACTION_DOWN:
        location += bufferSize;
        String buffer;
        String pathfile = "/mnt/sdcard/test.txt";
        if( new File( pathfile ).exists() )
        {
          buffer = getLines( location , bufferSize , pathfile );
        }else
        {  buffer = pathfile + " not found";
        }

        viewA.setText( Integer.toString( location ) + buffer );
        result = true;
      break;
    }
    return result;
  }


  String getLines( int offset , int lineCount , String pathfile )
  {
  StringBuilder str = new StringBuilder();
  try{
      FileReader inFile = new FileReader(
                              pathfile );
      BufferedReader br = 
             new BufferedReader( inFile );
      String line;
      int i=0;
      while ( ( line =
                br.readLine() ) != null
                && i < offset + lineCount )
      {
        if( i >= offset )
        {  str.append( line + "\n" );
        }
        i++;
      }
      br.close();
    }
    catch( Exception e )
    {  e.printStackTrace();
    }
    return str.toString();
  }

  public boolean onCreateOptionsMenu( Menu menu )
  {
    menu.add(0, MENU_NEW, 0, "New");
    menu.add(0, MENU_QUIT, 0, "Quit");
    return true;
  }

  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch( item.getItemId() )
    { 
      case MENU_NEW:
//      newGame();
      return true;
      case MENU_QUIT:
        this.finish();
      return true;
    }
    return false;
  }


}  //end class



/*

@Override public void onLongPress(MotionEvent e)
{ 
  viewA.setText( "-" + "LONG PRESS" + "-");
}
} //end class

@Override public boolean onScroll(
  MotionEvent e1, MotionEvent e2,
  float distanceX, float distance Y)
{ 
  viewA.setText("-" + "SCROLL" + "-");
  return true; 
}
@Override public void onShowPress(MotionEvent e)
{
  viewA.setText("-" + "SHOW PRESS" + "-");
}
*/

  • « ide on android javac dex terminalide
  • javascript continued »

Published

May 7, 2012

Category

android

~296 words

Tags

  • android 9
  • file 92
  • java 252
  • ontouchevent 1
  • read 14