Java/Android - Hide a link as text - Freaky™ - May 2, 2015
Hey guys I really need some help with this for my android app I am developing, could anyone help me out here?
Quote:Hey guys I have been trying to figure this out and have looked over a number of questions here but can't seem to find the answer to my problem. I am making an app that displays dinners at random from an array. I would like these dinners to be clickable and take the user to a web page but I have no idea how to make that happen so at the moment I have just added the link below the dinner which looks pretty ugly.
http://stackoverflow.com/questions/30009793/java-android-hide-a-link-as-text
* - -Base- - May 2, 2015
Store the links in a separate array make sure the indexes of the links line up with the location in the other array. So:
Code public class CookBook {
public String[] mfood =
{
"Chicago Deep Dish Pizza",
"Spaghetti Bolognese",
"Bourbon Chicken",
};
public String[] mLinks =
{
"http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes",
"http://www.bbcgoodfood.com/recipes/1502640/the-best-spaghetti-bolognese",
"http://www.food.com/recipe/bourbon-chicken-45809",
};
public int getRandomFoodIndex() {
//Randomly select a dinner
Random randomGenerator = new Random(); //Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(mfood.length);
//Convert random number to text
return randomNumber;
}
public String getFood(int index) {
return mfood[index];
}
public String getLink(int index) {
return mLink[index];
}
}
and then
Code public class DinnerTimeActivity extends Activity {
private CookBook mCookBook = new CookBook();
private ColourWheel mColourWheel = new ColourWheel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dinner_time);
final TextView dinnerLabel = (TextView) findViewById(R.id.DinnerTextView);
final Button showDinnerButton = (Button) findViewById(R.id.showDinnerButton);
final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.relativeLayout);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int index = mCookBook.getRandomFoodIndex();
String food = mCookBook.getFood(index);
String link = mCookBook.getLink(index);
//Update the label with the dinner
dinnerLabel.setText(food);
/** open link with the link variable */
int colour = mColourWheel.getColour();
relativelayout.setBackgroundColor(colour);
showDinnerButton.setTextColor(colour);
}
};
showDinnerButton.setOnClickListener(listener);
}
}
* - Freaky™ - May 2, 2015
(May 2, 2015 08:44 PM)-Base- Wrote: Store the links in a separate array make sure the indexes of the links line up with the location in the other array. So:
Code public class CookBook {
public String[] mfood =
{
"Chicago Deep Dish Pizza",
"Spaghetti Bolognese",
"Bourbon Chicken",
};
public String[] mLinks =
{
"http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes",
"http://www.bbcgoodfood.com/recipes/1502640/the-best-spaghetti-bolognese",
"http://www.food.com/recipe/bourbon-chicken-45809",
};
public int getRandomFoodIndex() {
//Randomly select a dinner
Random randomGenerator = new Random(); //Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(mfood.length);
//Convert random number to text
return randomNumber;
}
public String getFood(int index) {
return mfood[index];
}
public String getLink(int index) {
return mLink[index];
}
}
and then
Code public class DinnerTimeActivity extends Activity {
private CookBook mCookBook = new CookBook();
private ColourWheel mColourWheel = new ColourWheel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dinner_time);
final TextView dinnerLabel = (TextView) findViewById(R.id.DinnerTextView);
final Button showDinnerButton = (Button) findViewById(R.id.showDinnerButton);
final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.relativeLayout);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int index = mCookBook.getRandomFoodIndex();
String food = mCookBook.getFood(index);
String link = mCookBook.getLink(index);
//Update the label with the dinner
dinnerLabel.setText(food);
/** open link with the link variable */
int colour = mColourWheel.getColour();
relativelayout.setBackgroundColor(colour);
showDinnerButton.setTextColor(colour);
}
};
showDinnerButton.setOnClickListener(listener);
}
}
Thanks but when I click the dinner name it doesn't do anything.
* - -Base- - May 2, 2015
(May 2, 2015 09:06 PM)Freaky™ Wrote: Thanks but when I click the dinner name it doesn't do anything.
yeah you never put the code to open the link. Look it up to find out how and then put that snippet into the onClick listener
* - nOrr - May 3, 2015
Honestly, usually plain HTML should be clickable from Android apps. I had a link like something along the lines of this in my app and it worked just fine:
Code < a href="http://google.com">Google home page</a >
* - Freaky™ - May 3, 2015
(May 3, 2015 01:34 AM)nOrr Wrote: Honestly, usually plain HTML should be clickable from Android apps. I had a link like something along the lines of this in my app and it worked just fine:
Code < a href="http://google.com">Google home page</a >
Yeah I managed to fix it all up, thank you for your help guys!
|