0

I have two buttons in my MainActivity, these buttons have OnLongClickListener, and they both start another activity called Pop.class. So everything works fine. What I want is to send a url that correspond to the button in the next activity.

So like this when the user press the button in the MainActivity activity, he is bring by the application on the Pop activity and when clicking on the button in the Pop activity he will be redirected to a YouTube video on the YouTube app that correspond to the url that has been "declared" by the previous button (here b26 or b27).

The problem is that I don't know how to make my string a true URL for the Uri.parse() method. Also I've used a string because I wanted to put different url in each button.

For this I have used putExtra and getExtra but I don't understand why string url is not working whit the b27 button so I didn't modify the b26 code. :(

This is why I'm asking you some help

This is the MainActivity activity :

Button b26 = (Button) findViewById(R.id.b26);
    b26.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    if (SoundBuildingOnFiire.isPlaying()) {
                        SoundBuildingOnFiire.seekTo(0);
                    } else {SoundBuildingOnFiire.start();}}});
    b26.setOnLongClickListener(
            new Button.OnLongClickListener() {
                public boolean onLongClick(View v) {
                Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                    vibrator.vibrate(1000);
                    startActivity(new Intent(MainActivity.this, Pop.class));
                    return true;}});


    Button b27 = (Button) findViewById(R.id.b27);
    b27.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    if (SoundJackson.isPlaying()) {
                        SoundJackson.seekTo(0);
                    } else {SoundJackson.start();}}});
    b27.setOnLongClickListener(
            new Button.OnLongClickListener() {
                public boolean onLongClick(View v) {
                Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                    vibrator.vibrate(1000);
                    startActivity(new Intent(MainActivity.this, Pop.class));
                    getIntent().putExtra("SoundName", "https://www.youtube.com/");
                    return true;}});
}

This is the Pop activity :

public class Pop extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popup);

    final String SoundName = getIntent().getStringExtra("SoundName");

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;
    getWindow().setLayout((int) (width * .8), (int) (height * .4));

    TextView txtLabel = (TextView)findViewById(R.id.textView);
    txtLabel.setText("En savoir plus sur ce Son");

    Button button = (Button)findViewById(R.id.youtube);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
                Intent viewIntent =
                        new Intent("android.intent.action.VIEW",
                                Uri.parse(SoundName));
                startActivity(viewIntent);
        }
    });
}

}

Thanks for your time

Jack Wilsdon
  • 6,706
  • 11
  • 44
  • 87
Thomas Oumar
  • 85
  • 1
  • 2
  • 8
  • Possible duplicate of [How to send an object from one Android Activity to another using Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – David Rawson Feb 05 '17 at 00:34

2 Answers2

0

It happens because of you doesn't pass URL to POP activity, and it is null everytime.

change

startActivity(new Intent(MainActivity.this, Pop.class));
getIntent().putExtra("SoundName", "https://www.youtube.com/");

to

Intent intent = new Intent(MainActivity.this, Pop.class);
intent.putExtra("SoundName", "https://www.youtube.com/");
startActivity(intent);
pitfall
  • 166
  • 1
  • 4
0

Apply the following changes to your code and it should work

In Main Activity

 b27.setOnLongClickListener(
        new Button.OnLongClickListener() {
            public boolean onLongClick(View v) {
            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(1000);
                String url="https://youtube.com";
                Intent intent= new Intent(MainActivity.this,Pop.class);
                intent.putExtra("SoundName", url);
                startActivity(intent);
                return true;}});

And in your Pop Activity

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
            Intent viewIntent =getIntent();
               String url=viewIntent.getStringExtra("SoundName");
               Uri uri = Uri.parse(url);
               Intent intent = new Intent(Intent.ACTION_VIEW, uri);
               startActivity(intent);
    }
});
mcprilla79
  • 99
  • 1
  • 11