0

i am trying to add a phone button to a city guide app, where there should be a phone icon in each item that when clicked will redirect me to the dialer app with the phone number already be written.

this is the main activity code

public class Resto extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resto);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ListView lv;
    lv=(ListView) findViewById(R.id.listview);
    String[] names= {"L'olivo verde","Lounge36","Punto Alto","Sa7se7", "Maison Jabbour", "Pizza Napoli", "Capri","Classic burger joint","kaakunada","Ichiban Express","Ikura Sushi","Machos" };
    int[] imageids = {R.drawable.lolivo,R.drawable.lounge,R.drawable.punto,R.drawable.sahseh, R.drawable.jabbour, R.drawable.napoli, R.drawable.capri,R.drawable.cbj, R.drawable.kaak, R.drawable.ichiban, R.drawable.ikura,R.drawable.machos, R.drawable.gate};

    lv.setAdapter(new CustomAdapter(this,names,imageids));



    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}
}

and this is the customAdapter code

public class CustomAdapter extends BaseAdapter{

String[] result;
Context context; 
int[] image ID; 
LayoutInflater inflater=null;

public CustomAdapter(Context c, String[] names, int[] imageids)
{
    result = names;
    context= c;
    imageID = imageids;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return result.length;
}
@Override
public Object getItem(int i) {
    return null;
}
@Override
public long getItemId(int i) {
    return 0;
}

public class Holder {
    TextView tv;
    ImageView img;

}

public View getView(int i, View view, ViewGroup viewGroup) {
    // i is the position. starts at 0 and keeps on incremeting till the end of the data

    Holder holder= new Holder();
    final View rowView;
    rowView = inflater.inflate(R.layout.restos,null);
    holder.tv = (TextView) rowView.findViewById(R.id.textView);
    holder.img= (ImageView) rowView.findViewById(R.id.imageView);
   holder.img = (ImageView) view.findViewById(R.id.imageclick);
    holder.tv.setText(result[i]);
    holder.img.setImageResource(imageID[i]);
    rowView.setTag(i);
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "position " + rowView.getTag(), Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}
}
Kostas Drak
  • 3,222
  • 6
  • 28
  • 60
  • You have to create custom listview for this purpose. check this https://stackoverflow.com/questions/17525886/listview-with-add-and-delete-buttons-in-each-row-in-android – Sahdeep Singh Jan 06 '18 at 12:55
  • Welcome to SO. Please finish the tour and enjoy SO ;-) – ZF007 Jan 06 '18 at 12:55

1 Answers1

0

First , you have to create a custom listview as mentioned by @Sahdeep Singh in the comment. Then, you have to put this code in the item onclick method:

listview.setOnItemClickListener(new OnItemClickListener(){   
    @Override
    public void onItemClick(AdapterView<?>adapter,View v, int position){
        ItemClicked item = adapter.getItemAtPosition(position);

        Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:0123456789"));
    startActivity(intent);
    }
});

For details, please check this answer. Hope this helps.

Imtiaz Abir
  • 731
  • 7
  • 12