I'm a beginner as you'll see. I can't see where I can legally put the Button statements and the setText statements so this will compile. If I move one the other doesn't work and vice versa. I understand that the OnCreate is protected and so the "buttonOne" won't be passed to the playPhrases method but I've been changing thing around for a while now and nothing works. Simple explanations please.
package com.example.android.languageapp;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.button;
public class MainActivity extends AppCompatActivity {
MediaPlayer myMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMediaPlayer = MediaPlayer.create(this, R.raw.hello);
String[] phrases = getResources().getStringArray(R.array.phrase);
Button buttonOne = (Button) findViewById(R.id.button1);
Button buttonTwo = (Button) findViewById(R.id.button2);
Button buttonThree = (Button) findViewById(R.id.button3);
Button buttonFour = (Button) findViewById(R.id.button4);
Button buttonFive = (Button) findViewById(R.id.button5);
Button buttonSix = (Button) findViewById(R.id.button6);
Button buttonSeven = (Button) findViewById(R.id.button7);
Button buttonEight = (Button) findViewById(R.id.button8);
buttonOne.setText(phrases[0]);
buttonTwo.setText(phrases[1]);
buttonThree.setText(phrases[2]);
buttonFour.setText(phrases[3]);
buttonFive.setText(phrases[4]);
buttonSix.setText(phrases[5]);
buttonSeven.setText(phrases[6]);
buttonEight.setText(phrases[7]);
}
public void playPhrases(View clickedButton) {
int id = clickedButton.getId();
Log.i("Button id", "" + id);
if (id == buttonOne.getId()) {
myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);
myMediaPlayer.start();
}
}
}
Thanks for your help