I have made a simple Android program.
What it does: On startup it shows a text and a button. On the button is a number that increases every time its touched. At a random number on the button between 2 and 10, a picture shows up + a ssoundclip.
This is what it do. So when it shows the picture the app is finish.
What i want to do now is to create a touch function that directs me back to the apps startup page. For that i need your help. I have tried searching the internet for a solution, but since im a beginner at this i cant figure how to implement it in my code.
Here is my code:
package net.ibasic;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity implements OnClickListener {
private int i = 0;
public int low = 2;
public int high = 10;
public int num = low + (int) ( Math.random()*(high -low) + 0.5 );
MediaPlayer mpAudio;
MediaPlayer mpAudio1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//creating the setContentView by java-code instead of Xml
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
textView.setText(R.string.hello);
linearLayout.addView(textView);
//creating a button for the app
Button button = new Button(this);
linearLayout.addView(button);
setContentView(linearLayout);
update(button);
//adding buttonListener
button.setOnClickListener(this);
mpAudio = MediaPlayer.create(this, R.raw.kitten);
mpAudio1 = MediaPlayer.create(this, R.raw.scary);
}
private void update(Button button) {
button.setText("Amount of Clicks " + i++);
}
public void onClick(View button) {
update((Button)button);
if (i==num){
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
if (num % 2 == 0){
mpAudio.start();
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.kitten);
TextView textView1 = new TextView(this);
textView1.setText(R.string.kittentext);
linearLayout.addView(textView1);
linearLayout.addView(imageView);
}
else{
mpAudio1.start();
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.scary);
TextView textView1 = new TextView(this);
textView1.setText(R.string.scarytext);
linearLayout.addView(textView1);
linearLayout.addView(imageView);
}
setContentView(linearLayout);
}
}
}