Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Java/Android - Hide a link as text

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/30009...nk-as-text
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);

    }
}
(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.
(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
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 >
[Image: TEGE4Zx.png]

Puzzle level: 2/5
(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!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by ʀ00ȶʐɨʟʟǟNov 20, 2017 02:26 AM 1 182 Nov 20, 2017 02:26 AM
Last Post: Aussie
[C#] by ForceJewDec 16, 2016 12:02 PM 13 594 Dec 16, 2016 12:02 PM
Last Post: ForceJew
[C#] by itcorpOct 23, 2016 07:31 AM 0 229 Oct 23, 2016 07:31 AM
Last Post: itcorp