Tony Stark Tony Stark - 3 months ago 18
Android Question

Phone call Intent on a button click

I am trying to make a call to a specific number when a button is pressed on home screen. But I am getting an error. I am not able to implement the correct method. What am I doing wrong? Please help. Any elaborated answer would be very helpful.
Thing is

@override
is red, semicolon is also red. I am implementing it wrong I guess.

public class ActivityHome extends Fragment implements OnItemClickListener {
GridView gridview;
AdapaterGridView gridviewAdapter;
ArrayList<GridViewItem> data = new ArrayList<GridViewItem>();
Button btemergency;

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

View v = inflater.inflate(R.layout.fragment_list, container, false);

gridview = (GridView) v.findViewById(R.id.gridView1);
gridview.setOnItemClickListener(this);
btemergency = (Button) v.findViewById(R.id.btemergency);

Button call_button = (Button)findViewById(R.id.btemergency);
call_button.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view)
{ Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent); }

});

data.add(new GridViewItem(getResources().getString(R.string.menu_product), getResources().getDrawable(R.drawable.ic_product)));
data.add(new GridViewItem(getResources().getString(R.string.menu_cart), getResources().getDrawable(R.drawable.ic_cart)));


data.add(new GridViewItem(getResources().getString(R.string.menu_info), getResources().getDrawable(R.drawable.ic_info)));


data.add(new GridViewItem(getResources().getString(R.string.menu_contact), getResources().getDrawable(R.drawable.ic_contact)));

setDataAdapter();

return v;


}

// Set the Data Adapter
private void setDataAdapter() {
gridviewAdapter = new AdapaterGridView(getActivity(), R.layout.fragment_list_item, data);
gridview.setAdapter(gridviewAdapter);
}

@Override
public void onItemClick(final AdapterView<?> arg0, final View view, final int position, final long id) {
if (position==0){
startActivity(new Intent(getActivity(), ActivityCategoryList.class));
getActivity().overridePendingTransition (R.anim.open_next, R.anim.close_next);
}
else if (position==1){
startActivity(new Intent(getActivity(), ActivityCart.class));
getActivity().overridePendingTransition (R.anim.open_next, R.anim.close_next);


}
else if (position==2){
startActivity(new Intent(getActivity(), ActivityInformation.class));
getActivity().overridePendingTransition (R.anim.open_next, R.anim.close_next);


}
else {
startActivity(new Intent(getActivity(), ActivityContactUs.class));
getActivity().overridePendingTransition (R.anim.open_next, R.anim.close_next);
}

}}

Answer

You have extra braces at the end and where is the onClick method being overridden from? I assume you want to override from View.OnClickListener. Can you try following instead and see if it resolves the compile time error atleast.

public class ActivityHome extends Fragment implements OnItemClickListener{
    GridView gridview;
    AdapaterGridView gridviewAdapter;
    ArrayList<GridViewItem> data = new ArrayList<GridViewItem>();
    Button btemergency;

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

        View v = inflater.inflate(R.layout.fragment_list, container, false);

        gridview = (GridView) v.findViewById(R.id.gridView1);
        gridview.setOnItemClickListener(this);
        btemergency = (Button) v.findViewById(R.id.btemergency);

        Button call_button = (Button)v.findViewById(R.id.btemergency);
        call_button.setOnClickListener(new View.OnClickListener() {
              @Override 
              public void onClick(View view)
              { 
                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:123456789"));
                startActivity(callIntent); 
              }

          });
        data.add(new GridViewItem(getResources().getString(R.string.menu_product),  getResources().getDrawable(R.drawable.ic_product)));
        data.add(new GridViewItem(getResources().getString(R.string.menu_cart), getResources().getDrawable(R.drawable.ic_cart)));


        data.add(new GridViewItem(getResources().getString(R.string.menu_info), getResources().getDrawable(R.drawable.ic_info)));


        data.add(new GridViewItem(getResources().getString(R.string.menu_contact), getResources().getDrawable(R.drawable.ic_contact)));

        setDataAdapter();

        return v;


    }

    // Set the Data Adapter
    private void setDataAdapter() {
        gridviewAdapter = new AdapaterGridView(getActivity(), R.layout.fragment_list_item, data);
        gridview.setAdapter(gridviewAdapter);
    }

    @Override
    public void onItemClick(final AdapterView<?> arg0, final View view, final int position, final long id) {
        if (position == 0) {
            startActivity(new Intent(getActivity(), ActivityCategoryList.class));
            getActivity().overridePendingTransition(R.anim.open_next, R.anim.close_next);
        } else if (position == 1) {
            startActivity(new Intent(getActivity(), ActivityCart.class));
            getActivity().overridePendingTransition(R.anim.open_next, R.anim.close_next);


        } else if (position == 2) {
            startActivity(new Intent(getActivity(), ActivityInformation.class));
            getActivity().overridePendingTransition(R.anim.open_next, R.anim.close_next);


        } else {
            startActivity(new Intent(getActivity(), ActivityContactUs.class));
            getActivity().overridePendingTransition(R.anim.open_next, R.anim.close_next);
        }

    }

}
Comments