I am developing an application in which when an activity loads it shows a textfile(.txt) in a scrollview. Just Below the text file I have two buttons(playAnimation and playAudio) which when pressed play the corresponding files(swf and mp3 respectively).
Now I want to modify the current code to make it work for multiple files(I can do the same by having multiple activities but that will be redundant and not a good way of programming). I have many number of text files and their corresponding animation and audio files. I want to use the same code and dynamically only change the file names (i.e. file names of text, animation and audio files)
My MainActivity is as below(Please see comments made in the code. These are the spaces where the changes are to be done):
public class MainActivity extends Activity
{
Button playAnimationButton,playAudioButton;
MediaPlayer mp;
Context contextObject;
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);
ReadText readText=new ReadText(this);
TextView helloTxt = (TextView)findViewById(R.id.displaytext);
String fileName="textone";
// code to be written for getting the current page name and storing it in the String fileName
helloTxt.setText(readText.readTxt(fileName));
String audioName="pg3";
//Code to be written for getting the the current audioName
AssetFileDescriptor afd;
try {
afd=getAssets().openFd(audioName + ".mp3");
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepareAsync();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
View mainview = (View) findViewById(R.id.mainView);
// 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 false;
}
return true;
}
});
playAnimationButton=(Button)findViewById(R.id.playanimation);
playAnimationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause();
Intent startAnimation=new Intent(MainActivity.this,PlayAnimationActivity.class);
startAnimation.putExtra("SWF_NAME","a");
// code for sending the particular animation file corresponding to the current page
startActivity(startAnimation);
}
});
playAudioButton=(Button)findViewById(R.id.playaudio);
playAudioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//code for dynamically sending the particular audio file associated with the current page
if(mp.isPlaying())
{
mp.pause();
}
else
{
mp.start();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyGestureDetector extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > GlobalVariables.SWIPE_MAX_OFF_PATH) {
return false;
}
// right to left swipe
if(e1.getX() - e2.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {
// The Code for loading the next text file with its corresponding audio and animation on buttons.
// left to right swipe
} else if (e2.getX() - e1.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {
// The code for loading the previous text file with its corresponding audio and animation buttons.
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}
Any suggestions related to how I can proceed with this task are very much appreciated.
Thanks in advance. (someone down voted the question. I don't mind that. But atleast specify the reason before doing so)