I have one information activity, that when user click on list item it is going to this activity(info). And I wanted add there number(in info activity). That when user will click on number in this activity(info), it will call. But now it is not working on click listener. When from list I am clicking on item to go to info activity but without opening info activity, it is going to call, after closing call dial, I can see info activity. How I can make it that it will start when user will click on textview, not when activity starts?
<code>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
String selected = getIntent().getStringExtra("selectedItem");
TextView txt = (TextView) findViewById(R.id.txthead);
txt.setText(selected);
String[] tels = { "1234", "5678" };
TextView tel = (TextView) findViewById(R.id.tel);
if (selected.equals("Istinye Park")) {
img.setImageResource(R.drawable.istnp);
tel.setText(tels[0]);
} else if (selected.equals("Kanyon Istanbul")) {
img.setImageResource(R.drawable.iconsd);
tel.setText(tels[1]);
final Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+" + tel.getText().toString().trim()));
startActivity(callIntent);
tel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(callIntent);
}
});
}
</code>
Move callIntent initialization and ststartActivityart code in tel click listener :
tel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+" + tel.getText().toString().trim()));
startActivity(callIntent);
}
});
Example :
private TextView txt;
private TextView tel;
private ImageView img;
private String selected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
txt = (TextView) findViewById(R.id.txthead);
tel = (TextView) findViewById(R.id.tel);
img = (ImageView) findViewById(R.id.img);
selected = getIntent().getStringExtra("selectedItem");
txt.setText(selected);
String[] tels = {"1234", "5678"};
if (selected.equals("Istinye Park")) {
img.setImageResource(R.drawable.istnp);
tel.setText(tels[0]);
} else if (selected.equals("Kanyon Istanbul")) {
img.setImageResource(R.drawable.iconsd);
tel.setText(tels[1]);
}
tel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+" + tel.getText().toString().trim()));
startActivity(callIntent);
}
});
}
Note : add call permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.CALL_PHONE"/>