1

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.

Here is the class that contains the recipes:

package me.oak.dinnertime;

import java.util.Random;

public class CookBook {
    public String[] mfood =
            {
                    "Chicago Deep Dish Pizza \n \n http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes",
                    "Spaghetti Bolognese \n \n http://www.bbcgoodfood.com/recipes/1502640/the-best-spaghetti-bolognese",
                    "Bourbon Chicken \n \n http://www.food.com/recipe/bourbon-chicken-45809",
            };


    public String getFood() {


        
        String food = "";
        //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

        food = mfood[randomNumber];


        return food;
    }
}

And here is the main activity:

package me.oak.dinnertime;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;



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) {

                String food = mCookBook.getFood();
                //Update the label with the dinner
                dinnerLabel.setText(food);

                int colour = mColourWheel.getColour();
                relativelayout.setBackgroundColor(colour);
                showDinnerButton.setTextColor(colour);
            }
        };
        showDinnerButton.setOnClickListener(listener);

    }
}

And here is the XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".DinnerTimeActivity"
    android:background="#ff51b46d"
    android:id="@+id/relativeLayout">


    <TextView android:text="What&apos;s for dinner?" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#80ffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/DinnerTextView"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textColor="@android:color/white"
        android:text="Click the button to find out!"
        android:autoLink="web" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dinner Time"
        android:id="@+id/showDinnerButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:textColor="#ff51b46d" />

</RelativeLayout>

Sorry to give you so much, I just hope someone can help me out.

Matt
  • 75
  • 1
  • 16

3 Answers3

3

To use the LinkMovementMethod, try following:

change your array list content from:

Chicago Deep Dish Pizza \n \n http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes

to:

<a href=\"http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes\">Chicago Deep Dish Pizza</a>

And when set this text to your TextView, do it as:

(Updated: remove the underline and change text color, source: Remove underline from links in TextView - Android)

Spannable s = (Spannable) Html.fromHtml(foodString);
for (URLSpan u: s.getSpans(0, s.length(), URLSpan.class)) {
     s.setSpan(new UnderlineSpan() {
         public void updateDrawState(TextPaint tp) {
              //remove the underline
              tp.setUnderlineText(false);       
              //set text color            
              tp.setColor(getResources().getColor(R.color.orange));
          }
      }, s.getSpanStart(u), s.getSpanEnd(u), 0);
 }
 dinnerLabel.setText(s);
 dinnerLabel.setMovementMethod(LinkMovementMethod.getInstance());

Also remove the

android:autoLink="web"

in the xml.

As I have tested, "Chicago Deep Dish Pizza" will appear as a clickable link in the testview.

Community
  • 1
  • 1
Surely
  • 1,649
  • 16
  • 23
  • I can't change my array to that though, it isn't a string. – Matt May 03 '15 at 04:14
  • It still doesn't do anything when I click on the dinner name... Is this correct? http://i.imgur.com/eXMtt0V.png – Matt May 03 '15 at 04:24
  • @Matt, by the way, if it still does not work, try remove the android:autoLink="web" in your xml. I did not notice this previously. – Surely May 03 '15 at 04:24
  • Ok after I did that the link worked, however it crashed after I clicked it. Also is there a way to make it not have the blue link look and instead look normal while still being a link? – Matt May 03 '15 at 04:27
  • @Matt that is strange, I tested it in my phone and it works correctly. Do you have any crash exception log for this? – Surely May 03 '15 at 04:29
  • @Matt, are you running it in a emulator? it looks like there is no browser in the emulator, so there is nothing to handle the link. This link discussed the problem: http://stackoverflow.com/questions/14090034/no-activity-found-to-handle-intent-action-view-when-clicking-email-link – Surely May 03 '15 at 04:36
  • Oh, it opened links before though... I will try on my phone and see if all is fine. – Matt May 03 '15 at 04:40
  • @Matt hi matt, to change the style of the link, this may be helpful, you can customise the spannable object retrieved from Html.fromHtml method: http://stackoverflow.com/a/19133802/4298881 – Surely May 03 '15 at 04:43
  • It crashed on my phone as well (Samsung S4)... Ok I will check that link out, thanks. – Matt May 03 '15 at 04:54
  • Can you post the log for the crash? – Victor Santiago May 03 '15 at 05:09
  • @Matt hi Matt, I think I know the problem. Please check your link array content, for the href part, there should be only 1 "\" escape char, if you copy the content from my answer, another two \ will be inserted. You need to remove them. – Surely May 03 '15 at 05:13
  • @Matt you are welcome. For the styling part. Check out the link above. – Surely May 03 '15 at 05:17
  • @Matt Hi, I have updated the answer, you can remove the underline and change color of the text based on it. I just copy the code from the link. – Surely May 03 '15 at 05:48
3

just use something like this in your string file and refrence it in the textview <string name="links"><a href="www.google.com\">Google</a> shalom is a boy<a href="www.google.com\">Google 2 </a> </string> you can also use this in an array in the strings file to make it actually work do this terms = findViewById(R.id.terms); terms.setMovementMethod(LinkMovementMethod.getInstance());

0

I answered this on Hackforums for you.

Store the links in a separate array make sure the indexes of the links line up with the location in the other array. So:

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 mLinks[index];
    }

}

and then

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);
    }
}
Aaron Blenkush
  • 3,034
  • 2
  • 28
  • 54
Jmrapp
  • 520
  • 4
  • 13