Activate Call action in Texview

1

I am modifying a project, I need to make the Text Number activate when I press it.

The call will be made from the activity LeadsFragment , here is the activity . starting where I need help

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.UUID;


/**
 * Vista para los leads del CRM
 */
public class LeadsFragment extends Fragment {
    private ListView mLeadsList;
    private LeadsAdapter mLeadsAdapter;





    public LeadsFragment() {
        // Required empty public constructor
    }

    public static LeadsFragment newInstance(/*parámetros*/) {
        LeadsFragment fragment = new LeadsFragment();
        // Setup parámetros
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            // Gets parámetros
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_leads, container, false);




        // Instancia del ListView.
        mLeadsList = (ListView) root.findViewById(R.id.leads_list);

        // Inicializar el adaptador con la fuente de datos.
        mLeadsAdapter = new LeadsAdapter(getActivity(),
                LeadsRepository.getInstance().getLeads());

        //Relacionando la lista con el adaptador
        mLeadsList.setAdapter(mLeadsAdapter);

        mLeadsList.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
                Intent callintent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + AyudaAQUI [position]));
                startActivity(callintent);
            }
        });
        setHasOptionsMenu(true);
        return root;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_leads_list, menu);
    }



}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Repositorio ficticio de leads
 */
public class LeadsRepository {
    private static LeadsRepository repository = new LeadsRepository();
    private HashMap<String, Lead> leads = new HashMap<>();

    public static LeadsRepository getInstance() {
        return repository;
    }

    private LeadsRepository() {
        saveLead(new Lead("Taxi Virtual", "La colonia", "123456789", R.drawable.lead_photo_1));
        saveLead(new Lead("Taxi Rubio Express", "la sur", "123456789", R.drawable.lead_photo_2));
        saveLead(new Lead("Sara Bonz", "Directora de Marketing", "123456789", R.drawable.lead_photo_3));
        saveLead(new Lead("Liliana Clarence", "Diseñadora de Producto", "123456789", R.drawable.lead_photo_4));
        saveLead(new Lead("Benito Peralta", "Supervisor de Ventas", "123456789", R.drawable.lead_photo_5));
        saveLead(new Lead("Juan Jaramillo", "CEO", "123456789", R.drawable.lead_photo_6));
        saveLead(new Lead("Christian Steps", "CTO", "123456789", R.drawable.lead_photo_7));
        saveLead(new Lead("Alexa Giraldo", "Lead Programmer", "123456789", R.drawable.lead_photo_8));
        saveLead(new Lead("Linda Murillo", "Directora de Marketing", "123456789", R.drawable.lead_photo_9));
        saveLead(new Lead("Lizeth Astrada", "CEO", "123456789", R.drawable.lead_photo_10));
    }

    private void saveLead(Lead lead) {
        leads.put(lead.getId(), lead);
    }

    public List<Lead> getLeads() {
        return new ArrayList<>(leads.values());
    }
}
import java.util.UUID;

/**
 * Entidad Lead
 */
public class Lead {
    private String mId;
    private String mName;
    private String mTitle;
    private String mNumber;
    private int mImage;

    public Lead(String name, String title, String number, int image) {
        mId = UUID.randomUUID().toString();
        mName = name;
        mTitle = title;
        mNumber = number;
        mImage = image;
    }

    public String getId() {
        return mId;
    }

    public void setId(String mId) {
        this.mId = mId;
    }

    public String getName() {
        return mName;
    }

    public void setName(String mName) {
        this.mName = mName;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        this.mTitle = title;
    }

    public String getNumber() {
        return mNumber;
    }

    public void setNumber(String mCompany) {
        this.mNumber = mNumber;
    }

    public int getImage() {
        return mImage;
    }

    public void setImage(int mImage) {
        this.mImage = mImage;
    }

    @Override
    public String toString() {
        return "Lead{" +
                "ID='" + mId + '\'' +
                ", Numero='" + mNumber + '\'' +
                ", Nombre='" + mName + '\'' +
                ", Cargo='" + mTitle + '\'' +
                '}';
    }
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Adaptador de leads
 */
public class LeadsAdapter extends ArrayAdapter<Lead> {
    public LeadsAdapter(Context context, List<Lead> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Obtener inflater.
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        ViewHolder holder;

        // ¿Ya se infló este view?
        if (null == convertView) {
            //Si no existe, entonces inflarlo con image_list_view.xml
            convertView = inflater.inflate(
                    R.layout.list_item_lead,
                    parent,
                    false);

            holder = new ViewHolder();
            holder.avatar = (ImageView) convertView.findViewById(R.id.iv_avatar);
            holder.name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.number = (TextView) convertView.findViewById(R.id.tv_number);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // Lead actual.
        Lead lead = getItem(position);

        // Setup.
        holder.name.setText(lead.getName());
        holder.title.setText(lead.getTitle());
        holder.number.setText(lead.getNumber());
        Glide.with(getContext()).load(lead.getImage()).into(holder.avatar);

        return convertView;
    }

    static class ViewHolder {
        ImageView avatar;
        TextView name;
        TextView title;
        TextView number;
    }
}
    
asked by Elihat Caceres 11.01.2017 в 22:34
source

3 answers

1

The simplest solution for not adding unnecessary code and permissions is to enable the operating system to automatically manipulate this content in the TextView that will contain the telephone numbers:

holder.number = (TextView) convertView.findViewById(R.id.tv_number);

add the property android: autoLink with the value phone :

  

android: autoLink Controls whether the links, such as directions   URLs and email addresses are automatically found   and they become links that can be clicked.

example:

<TextView
            android:id="@+id/tv_number"
            ...
            ..
            android:autoLink="phone"/>
    
answered by 12.01.2017 / 20:51
source
1

You just have to write the number as it is the format that I present:

  

--- > "+ 58 123-4567890"

and place autolink "Phone"

    
answered by 12.01.2017 в 19:39
0

You well put the following:

    mLeadsList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
            Intent callintent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + AyudaAQUI [position]));
            startActivity(callintent);
        }
    });

Here you must validate that the permission to call has been activated by the user and also retrieve the number of the position. I recommend using the following:

    mLeadsList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
            if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED){       //Permite llamar solo si esta habilitada
                Intent callintent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + mLeadsList.getNumber(position)));
                startActivity(callintent);
            }else{
                habilitarPermisosTelephone();
                //Toast.makeText(this, R.string.permiso_rechazado, Toast.LENGTH_LONG).show();
            }
        }
    });

where enabling permission asks the user if he authorizes the app to make calls

public void habilitarPermisosTelephone(){
    final String[] permissions = new String[]{Manifest.permission.CALL_PHONE};
    if (ActivityCompat.checkSelfPermission(fragmentActivity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, permissions, SOLICITUD_PERMISO_TELEFONO /*ACA DEFINE UN NUMERO CUALQUIERA QUE NO USES*/);
    }
}

Finally, make sure to add permission on your AndroidManifest

<uses-permission android:name="android.permission.CALL_PHONE"/>

Greetings

    
answered by 12.01.2017 в 14:04