I'm currently working on 100 Random Facts app that simply displays a new fact when the button is clicked. Every time a new fact is displayed the same screen pops up but with a different color as the background. I did this by storing the facts in an array and also the colors in an array and then randomly matching them, i want to do this but with images instead. How can I change the background to a customized image when a random fact pops up. So that each time the new fact button is pressed a fact pops up but instead of the background being a color, being an random image instead?
Any help is appreciated!
Here is my code, The Different Colors are stored in a java class called ColorWheel
ColorWheel.java
package com.example.android.funfacts;
import android.graphics.Color;
import java.util.Random;
public class ColorWheel {
//Member variable (propoerties about the object)
public String[] mColors = {
"#39add1", // light blue
"#3079ab", // dark blue
"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7", // lavender
"#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#637a91", // dark gray
"#f092b0", // pink
"#b7c0c7" // light gray
};
String color="";
//Method (abilities:things the object can do)
public int getColor(){
//Randomly select a fact
Random randomGenerator =new Random(); // construct a new random generator
int randomNumber =randomGenerator.nextInt(mColors.length);
color = mColors[randomNumber];
int colorAsInt = Color.parseColor(color);
return colorAsInt;
}
}
FunFactsActivity.java
public class FunFactsActivity extends Activity {
public static final String TAG =FunFactsActivity.class.getSimpleName();
private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel= new ColorWheel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Declare our view variables and assign them the views from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
final Button showFactButton= (Button) findViewById(R.id.showFactButton);
final RelativeLayout relativelayout =(RelativeLayout) findViewById(R.id.relativeLayout);
View.OnClickListener listener= new View.OnClickListener() {
@Override
public void onClick(View view) {
String fact = mFactBook.getFact();
//Update the label with our dynamic fact
factLabel.setText(fact);
int color = mColorWheel.getColor();
relativelayout.setBackgroundColor(color);
showFactButton.setTextColor(color);
}
};
showFactButton.setOnClickListener(listener);
//Toast.makeText(this,"YAY! our activity was created",Toast.LENGTH_LONG).show();
Log.d(TAG, "We are Logging from the oncreate method");
}
}
activity_fun_facts.xml
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.austinzeller.funfacts.FunFactsActivity"
android:background="@drawable/image1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/did_you_knoww"
android:textSize="24sp"
android:textColor="@android:color/background_light"
android:textStyle="normal|bold" />
<Button
android:text="@string/new_random_fact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/showFactButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/background_light"
android:textColor="#51b46d" />
<TextView
android:text="@string/ants_stretch_when_they_wake_up_in_the_morningg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/factTextView"
android:textColor="@android:color/background_light"
android:textSize="21sp"
android:layout_above="@+id/showFactButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="129dp"
android:textStyle="normal|bold" />
</RelativeLayout>
FactBook.java
package com.example.android.funfacts;
import java.util.Random;
public class FactBook {
//Member variable (propoerties about the object)
public String[] mFacts = {
"There are 206 bones in the human body!",
"Left-handed people are better at sports that require good spatial
judgment and fast reaction, compared to right-handed individuals.",
"Your heart beats over 100,000 times a day!",
"One quarter of the human brain is used to control the eyes.",
"Europe is the only continent without a desert.",
"The tongue is the strongest muscle in the human body.",
"Food can only be tasted if it is mixed with saliva.",
"About 3000 years ago, most Egyptians died by the time they were
30!",
"Under extreme stress, some octopuses will eat their own arms.",
"A fully loaded supertanker traveling at normal speed takes a least
twenty minutes to stop.",
"Aluminum used to be more valuable than gold!",
"More than half the population of Kenya is under the age of 15."
built." };
String fact="";
//Method (abilities:things the object can do)
public String getFact(){
//Randomly select a fact
Random randomGenerator =new Random(); // construct a new random generator
int randomNumber =randomGenerator.nextInt(mFacts.length);
fact = mFacts[randomNumber];
return fact;
}
}
Aucun commentaire:
Enregistrer un commentaire