简体   繁体   中英

Link not opening android studio

I'm new in android development and trying to open the link written in edit text by using open url button. I just pick the intent code from google only and The toast is showing the given link on click but the link is not opening. There is no action after clicking open url button. Url: http://www.google.com Here is my first fragment java code.

package com.example.implicitintent;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.example.implicitintent.databinding.FragmentFirstBinding;

public class FirstFragment extends Fragment {
Button button,email;
EditText url;
    private FragmentFirstBinding binding;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentFirstBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavHostFragment.findNavController(FirstFragment.this)
                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
            }
        });
        email=view.findViewById(R.id.email);
        url=view.findViewById(R.id.url);
        binding.email.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String urlText=url.getText().toString();
                String address[]={"abc@gmail.com","xyz@gmail.com"};
                Intent intent=new Intent(Intent.ACTION_SEND);
                intent.setType("*/*");
                intent.putExtra(Intent.EXTRA_EMAIL,address);
                intent.putExtra(Intent.EXTRA_SUBJECT,"My subject");
                intent.putExtra(Intent.EXTRA_TEXT,urlText);
                if(intent.resolveActivity(getActivity().getPackageManager())!=null){
                    startActivity(intent);
                }
            }
        });
        button=view.findViewById(R.id.button);
        url=view.findViewById(R.id.url);

        binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String urlText=url.getText().toString();
                Toast.makeText(getActivity(), urlText, Toast.LENGTH_SHORT).show();

               Intent intent =new Intent(Intent.ACTION_VIEW,Uri.parse(urlText));
               if(intent.resolveActivity(getActivity().getPackageManager())!=null){
                    startActivity(intent);
                }


            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}

without https:// or http:// link will not open but you can check enter url is valid or not

binding.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        String urlText = url.getText().toString();  // https://www.google.com/
        if (urlText.startsWith("https://") || urlText.startsWith("http://")) {
            Uri uri = Uri.parse(urlText);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }else{
            Toast.makeText(mContext, "Invalid Url", Toast.LENGTH_SHORT).show();
        }}
    });

you did not define url you have to assign value to the url variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM