private Button mfactbutton;
private TextView mfacttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_fun_fact);
Button mfactbutton = (Button) findViewById(R.id.button);
TextView mfacttext = (TextView) findViewById(R.id.textView2);
// now we need to make out button to click
View.OnClickListener Listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] facts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built." };
String fact = "";
// randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(facts.length);
fact = facts[randomNumber] + "";
}
};
mfactbutton.setOnClickListener(Listener);
}
}
Hey everyone! i need help! my button doesn't just simply clicl!!!!! heeeeeeeeelp!i'm just trying to make a simple button that changes the textview2 with each click! at first it was working but now it started not to work.
Your Button clicked properly but the main thing is you did not set fact value to TextView.
#. As you have declared Button and TextView outside onCreate(), no need to declare it again inside onCreate().
Use:
mfactbutton = (Button) findViewById(R.id.button);
mfacttext = (TextView) findViewById(R.id.textView2);
Instead of:
Button mfactbutton = (Button) findViewById(R.id.button);
TextView mfacttext = (TextView) findViewById(R.id.textView2);
#. In onClick() method show fact value on TextView or show Toast message:
// TextView
mfacttext.setText(fact);
// Toast
Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show();
Here is the working code:
public class MainActivity extends AppCompatActivity {
private Button mfactbutton;
private TextView mfacttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mfactbutton = (Button) findViewById(R.id.button);
mfacttext = (TextView) findViewById(R.id.textView2);
// now we need to make out button to click
View.OnClickListener Listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] facts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built." };
String fact = "";
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(facts.length);
fact = facts[randomNumber] + "";
mfacttext.setText(fact);
Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show();
}
};
mfactbutton.setOnClickListener(Listener);
}
}
OUTPUT:
Do you set your selected fact to your textView ? Add
mfacttext.setText(fact);
after selecting your random fact.
Random randomGenerator = new
int randomNumber = randomGenerator.nextInt(facts.length);
fact = facts[randomNumber] + "";
mfacttext.setText(fact);
use this
mfactbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code goes here
}
});
Hi I just created a simple project. if you click on the button, it will generate random text on the textview2.
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView secondTextView;
String[] Textlist = { "Hello man ", "sonam", "tashi","i am man","hellow world"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
secondTextView = (TextView) findViewById(R.id.textView2);
Button mybtn = (Button) findViewById(R.id.btn);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeTextView_two_text();
}
});
}
private void changeTextView_two_text() {
Random random = new Random();
String rand = Textlist[random.nextInt(Textlist.length)];
// String randomText = TextList[random.nextInt(TextList.length)];
secondTextView.setText(rand);
}
}
and activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hello.googlemap.MainActivity">
<Button
android:id="#+id/btn"
android:text="click me"
android:layout_width="368dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp" />
<TextView
android:gravity="center"
android:id="#+id/textView2"
android:text="no text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/></LinearLayout>
Hope this helps you.
Related
I am completely lost and don't have enough knowledge on coding/android studio.
I have the EditTexts (FoodIncomeCounter, FoodCampX, and FoodUpgradeX) as inputs.
Then the TextView (FoodIncomeResult) as output.
The 1st button (IncomeSubmitButton) Works properly. The 2nd button (FoodCampSubmitButton) does not, I want it to take the input of the FoodCampXs and FoodUpgradeXs then do a calculation and put that into integer TotalFood, then output TotalFood to the FoodIncomeResult TextView.
How the heck do I do this? I feel like I am close but I don't know what is wrong.
Thank you for the help!!!
public class MainActivity extends AppCompatActivity {
// These are the global variables
EditText FoodIncomeCounter;
EditText FoodCamp1Counter, FoodCamp2Counter, FoodCamp3Counter, FoodUpgrade1Counter, FoodUpgrade2Counter, FoodUpgrade3Counter;
TextView FoodIncomeResult;
int FoodIncome;
int FoodCamp1, FoodCamp2, FoodCamp3, FoodUpgrade1, FoodUpgrade2, FoodUpgrade3;
int TotalFood;
Button IncomeSubmitButton, FoodCampSubmitButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//to get from user input and into variable form
FoodIncomeCounter = (EditText) findViewById(R.id.FoodIncomeCounter);
FoodIncomeResult = (TextView) findViewById(R.id.FoodIncomeResult);
IncomeSubmitButton = (Button) findViewById(R.id.IncomeSubmitButton);
FoodCamp1Counter = (EditText) findViewById(R.id.FoodCamp1Counter);
FoodCamp2Counter = (EditText) findViewById(R.id.FoodCamp2Counter);
FoodCamp3Counter = (EditText) findViewById(R.id.FoodCamp3Counter);
FoodUpgrade1Counter = (EditText) findViewById(R.id.FoodUpgrade1Counter);
FoodUpgrade2Counter = (EditText) findViewById(R.id.FoodUpgrade2Counter);
FoodUpgrade3Counter = (EditText) findViewById(R.id.FoodUpgrade3Counter);
FoodCampSubmitButton = (Button) findViewById(R.id.FoodCampSubmitButton);
//Submit button
IncomeSubmitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//to receive the inputted values
Food = Integer.parseInt(FoodIncomeCounter.getText().toString());
//to show the inputted values into the result fields
FoodIncomeResult.setText(FoodIncomeCounter.getText().toString());
}
});
//Submit button
FoodCampSubmitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//receive the inputted values
FoodCamp1 = Integer.parseInt(FoodCamp1Counter.getText().toString());
FoodCamp2 = Integer.parseInt(FoodCamp2Counter.getText().toString());
FoodCamp3 = Integer.parseInt(FoodCamp3Counter.getText().toString());
FoodUpgrade1 = Integer.parseInt(FoodUpgrade1Counter.getText().toString());
FoodUpgrade2 = Integer.parseInt(FoodUpgrade2Counter.getText().toString());
FoodUpgrade3 = Integer.parseInt(FoodUpgrade3Counter.getText().toString());
//get food income and show
TotalFood = FoodCamp1 + (FoodCamp2 * 2) + (FoodCamp3 * 3) + (FoodUpgrade1 * 2) + (FoodUpgrade2 * 4) + (FoodUpgrade3 * 6);
FoodIncomeResult.setText(String.valueOf(TotalFood));
}
});
}
}
Just started learning Java for application development through Android Studio. Creating an application that collects the user's inputs of which include their Name and Age of which when a Submition button is clicked the output is a string based on their Age range. code below:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Adding Action to the Button
Button BtnSubmit = findViewById(R.id.BtnSubmit);
BtnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TbName = "";
int TbAge =0;
String TxtOuput;
if (TbAge>0 && TbAge<=18)
{
TxtOuput = TbName + ("You Are Still A Child");
}
else if(TbAge>18 && TbAge<=64)
{
TxtOuput = TbName + ("You Are Grown");
}
else if(TbAge>64)
{
TxtOuput = TbName + ("You Are About To Die") + ("R.I.P");
}
}
});
}
}
Tried various methods from google, youtube and other sources but the application will not still execute an output.
Application layout/blueprint:
You are not getting your text fields values in a variable anywhere in code you have shared. Similarly you are not setting those values back to Status text field as well.
So it should be something like this:
If I assume you have firstEditText EditText View for name, secondEditText for age, and resultTextView for status, then your code would be something like below:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Adding Action to the Button
Button BtnSubmit = (Button)findViewById(R.id.BtnSubmit);
EditText firstEditText = (EditText) findViewById(R.id.firstEditText);
EditText secondEditText = (EditText) findViewById(R.id.secondEditText);
TextView resultTextView = (TextView) findViewById(R.id.resultTextView);
BtnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TbName = firstEditText.getText();
int TbAge = Integer.parseInt(secondEditText.getText().toString());
String TxtOuput;
if (TbAge>0 && TbAge<=18)
{
TxtOuput = TbName + ("You Are Still A Child");
}
else if(TbAge>18 && TbAge<=64)
{
TxtOuput = TbName + ("You Are Grown");
}
else if(TbAge>64)
{
TxtOuput = TbName + ("You Are About To Die") + ("R.I.P");
}
resultTextView.setText(TxtOuput);
}
});
}
}
Try this and let me know
AS per your question, you want user to add his name and age in the editText fields. When clicked on SUBMIT button, that entered data should be displayed on the next screen(activity).
To achieve this:
use 2 editText fields and 1 button in your xml layout file.
Problem:
You have added only button view in your layout file and not the editText.
Let me know if you want the code for the same then.
Best and easy way of using onclick listener,
In your xml layout,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onclick="goToMethod"/>
</LinearLayout>
In your activity,
class MainActivity extends Activity{
public void goToMethod(View view){
//do your code here
}
}
Note: In tools:context you should mention the activity where you are using layout and onclick
friends. I started to learn Android app developinng with Android Studio and Java. Now i am trying to make one progressbar. When app is started we have two EditText fields where first is 100 by default(how will be the max value for progressbar ) and one EditText field for increment by(this is step) for progressbar. When we click Start it must show up via dialog.
I write the code, there is no more errors, but app is closing when i hit the Start Button. The progressbar is not working
This is the code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/txt_max"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max Value" />
<EditText
android:id="#+id/maximum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100.0" />
<TextView
android:id="#+id/txt_increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Increment by"/>
<EditText
android:id="#+id/increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5.0" />
<Button
android:id="#+id/butt_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
</LinearLayout>
This is the MainActivity.java:
public class MainActivity extends AppCompatActivity {
int increment;
ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.butt_Start);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
EditText et = (EditText) findViewById(increment);
increment = Integer.parseInt(et.getText().toString());
dialog = new ProgressDialog(MainActivity.this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
EditText max = (EditText) findViewById(R.id.maximum);
int maximum = Integer.parseInt(max.getText().toString());
dialog.setMax(maximum);
dialog.show();
Thread background = new Thread(new Runnable()
{
public void run()
{
try
{
while (dialog.getProgress() <= dialog.getMax())
{
Thread.sleep(500);
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e)
{
}
}
});
background.start();
}
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(increment);
}
};
});
}
}
There are some error with that code.. I will comment each by reporting your snippet:
public class MainActivity extends AppCompatActivity {
int increment;
ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.butt_Start);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
EditText et = (EditText) findViewById(increment);
Here is the first error: not really a wrong thing, but it's better to istantiate the EditText in your onCreate (outside the onClick), because this way you will re-create the EditText every time you click the button.
The error is double: you are using findViewById(increment) where, in your code, increment is an int variable whose value is actually 0. You have to use (as you did for the button)
findViewById(R.id.increment);
Going on:
increment = Integer.parseInt(et.getText().toString());
dialog = new ProgressDialog(MainActivity.this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
EditText max = (EditText) findViewById(R.id.maximum);
int maximum = Integer.parseInt(max.getText().toString());
Here the error is xml side: since you are assuming that max.getText().toString() is an Integer, add inputType to your xml with number value.
dialog.setMax(maximum);
dialog.show();
Thread background = new Thread(new Runnable()
{
public void run()
{
try
{
while (dialog.getProgress() <= dialog.getMax())
{
Thread.sleep(500);
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e)
{
}
}
});
background.start();
}
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(increment);
}
};
});
}
}
Another thing is that is better to avoid naming a variable with same name of your id, expecially because id should be representative of the control you are pointing at (for example call it "EditTextMaxValueMainActivity", this is a limit example but it is representative).
If I can suggest you, TheNewBoston channel/site is the best for tutorials, it has a couple of playlist with android basics to advanced and it is simple and really explicative. Good luck!
for other errors, we will need the logtrace
The Answer for me is here:
This line:
EditText et = (EditText) findViewById(increment);
Must be changed to:
EditText et = (EditText) findViewById**(R.id.increment)**;
And i go to design view of activity_main.xml and changed the inputType of increment EditText and maximum EditText to number.
And removed the decimal format from the numbers.
<EditText
android:id="#+id/increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
And here i had number with decimal, now i removed them.
<EditText
android:id="#+id/maximum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100" />
Hey everyone :) Ive been scratching my head on this one, and can't seem to find an appropriate answer that works even though I'm sure its a simple issue.
I have a program which generates a random letter text on a group of buttons when another button being pressed.
When I open the application it loads fine and the first time the button is pressed it generates correctly, but after that I can't seem to get it to regenerate random letters.
I can accomplish what I want by adding an Intent, and essentially "refreshing" (not sure of the wording) the main activity but I would like to add a counter for the button clicks, and it resets when the activity does.
{Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
overridePendingTransition(0, 0);
I just cant seem to wrap my mind around how to do it otherwise. It's seems like I need to rerun the java every time the button is clicked. Any suggestions?
Here is some code, I used buttons rather than textview because I a)wanted to set an easy background b)may make them clickable later. Thanks so much in advance!
public class MainActivity extends Activity
{
Button spin;
Button reel1,reel2,reel3,reel4;
private String rnd,rnd2,rnd3,rnd4;
int count1=50;
#Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();}
private static String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
private static String rndm2[] = {"A","B","C","D"};
{rnd2 = rndm2[(int) (Math.random() * rndm2.length)];}
private static String rndm3[] = {"A","B","C","D"};
{rnd3 = rndm3[(int) (Math.random() * rndm3.length)];}
private static String rndm4[] = {"A","B","C","D"};
{rnd4 = rndm4[(int) (Math.random() * rndm4.length)];}
public void perform_action(View v){}
public void addListenerOnButton() {
spin = (Button) findViewById(R.id.spin);
spin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
{Toast.makeText(getApplicationContext(),"button pressed", Toast.LENGTH_SHORT).show();}
{Button tv = (Button) findViewById(R.id.reel1);
tv.setText(String.valueOf(rnd));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel2);
tv.setText(String.valueOf(rnd2));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel3);
tv.setText(String.valueOf(rnd3));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel4);
tv.setText(String.valueOf(rnd4));
tv.setTextColor(Color.parseColor("#000000"));
I figured it out!!! Whoot. But I can't upvote myself for a correct answer.
public class MainActivity extends Activity
{
Button spin;
Button reel1,reel2,reel3,reel4;
private String rnd,rnd2,rnd3,rnd4;
int count1=50;
#Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();}
public void perform_action(View v){}
public void addListenerOnButton() {
spin = (Button) findViewById(R.id.spin);
spin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
String rndm2[] = {"A","B","C","D"};
{rnd2 = rndm2[(int) (Math.random() * rndm2.length)];}
String rndm3[] = {"A","B","C","D"};
{rnd3 = rndm3[(int) (Math.random() * rndm3.length)];}
String rndm4[] = {"A","B","C","D"};
{rnd4 = rndm4[(int) (Math.random() * rndm4.length)];}
{Toast.makeText(getApplicationContext(),"button pressed", Toast.LENGTH_SHORT).show();}
{Button tv = (Button) findViewById(R.id.reel1);
tv.setText(String.valueOf(rnd));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel2);
tv.setText(String.valueOf(rnd2));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel3);
tv.setText(String.valueOf(rnd3));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel4);
tv.setText(String.valueOf(rnd4));
tv.setTextColor(Color.parseColor("#000000"));
I moved my random Strings into my Onclick event and removed the private and static parts. Then whala! Things work like a charm!
Can anyone help me work out where I'm going wrong here. On the button click the media player plays one of the mfiles at random and I'm trying to set a textview depending on which file was played. Currently the setText if statements only match the audio playing half the time. Really not sure where I'm going wrong here.
private final int SOUND_CLIPS = 3;
private int mfile[] = new int[SOUND_CLIPS];
private Random rnd = new Random();
MediaPlayer mpButtonOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mfile[0] = R.raw.one;
mfile[1] = R.raw.two;
mfile[2] = R.raw.three;
//Button setup
Button bOne = (Button) findViewById(R.id.button1);
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final TextView textOne = (TextView)findViewById(R.id.textView1);
mpButtonOne = MediaPlayer.create(MainActivity.this, mfile[rnd.nextInt(SOUND_CLIPS)]);
if (mpButtonOne==null){
//display a Toast message here
return;
}
mpButtonOne.start();
if (mfile[rnd.nextInt(SOUND_CLIPS)] == mfile[0]){
textOne.setText("one");
}
if (mfile[rnd.nextInt(SOUND_CLIPS)] == mfile[1]){
textOne.setText("two");
}
if (mfile[rnd.nextInt(SOUND_CLIPS)] == mfile[2]){
textOne.setText("three");
}
mpButtonOne.setOnCompletionListener(new soundListener1());
{
}
So just to clarify the problem I am having is that the setText only matches the audio occasionally, not on every click. The rest of the time it displays the wrong text for the wrong audio.
You are choosing another random file
mfile[rnd.nextInt(SOUND_CLIPS)]
set that to a variable in onClick() then check against that variable in your if statement
public void onClick(View v) {
int song = mfile[rnd.nextInt(SOUND_CLIPS)];
final TextView textOne = (TextView)findViewById(R.id.textView1);
mpButtonOne = MediaPlayer.create(MainActivity.this, song);
if (song == mfile[0]){
textOne.setText("one");
}
Edit
To make it a member variable so you can use it anywhere in the class, just declare it outside of a method. Usually do this before onCreate() just so all member variables are in the same place and it makes your code more readable/manageable.
public class SomeClass extends Activity
{
int song;
public void onCreate()
{
// your code
}
then you can just initialize it in your onClick()
public void onClick(View v) {
song = mfile[rnd.nextInt(SOUND_CLIPS)];
final TextView textOne = (TextView)findViewById(R.id.textView1);
mpButtonOne = MediaPlayer.create(MainActivity.this, song);