-2

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);
    }
}
}
Lars Allan
  • 45
  • 1
  • 6
  • What kind of touch function, where? – User May 28 '12 at 20:49
  • "This is what it do. So when it shows the picture the app is finish." Do you mean it's force closed? Is it your problem? "What i want to do now is to create a touch function that directs me back to the apps startup page." What you need is just an Intent then: http://developer.android.com/reference/android/content/Intent.html – Michał Klimczak May 28 '12 at 20:51
  • Well when the picture shows the app is done and i will have to exit the program by pressing return on the phone. But i want it to redirect back to startup so i can run the app multiple times until i close it. I was hoping to do this by a touch function, but other possebilities are welcome :) – Lars Allan May 28 '12 at 21:21
  • You should add a "restart" button, or something similar. With a click listener. When it's clicked it starts your startup activity. Using an intent. – User May 28 '12 at 21:43

1 Answers1

2

After you are done use the following code to restart your activity:

Intent intent = getIntent();
finish();
startActivity(intent); //

Reference


You can do this on the click of a button/ can also automate the button click using performClick() method on button object or whenever you want.

Community
  • 1
  • 1
Imran Rana
  • 11,899
  • 7
  • 45
  • 51