Reputation: 145
How can I intent a web page when one of those three item is checked? I suppose it needs to have ID and then somehow make if statement to determinate which one is chosen. But how and where can it be implemented? Some ideas? :)
public class MainActivity extends Activity {
CharSequence[] items = {"First web site", "Second web site", "Third web site"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dialog(View v){
showDialog(0);
}
public void dialog1(View v){
Toast.makeText(getBaseContext(), "No function.", Toast.LENGTH_LONG).show();
}
public void dialog2(View v){
Toast.makeText(getBaseContext(), "No function.", Toast.LENGTH_LONG).show();
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.mipmap.ic_launcher)
.setTitle("Dialog with some text...")
.setSingleChoiceItems(items, 0, null)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getBaseContext(), "OK pressed!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getBaseContext(), "Cancel pressed!", Toast.LENGTH_SHORT).show();
}
}).create();
}
return null;
}
}
Upvotes: 0
Views: 140
Reputation: 440
I think this is what you are looking for
final CharSequence[] items = {"web1", "web2", "web3"};
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Title");
builder.setSingleChoiceItems(items,0,null);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
ListView lw = ((AlertDialog)dialog).getListView();
Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());
Toast.makeText(getApplicationContext(),
((String) checkedItem)
,Toast.LENGTH_SHORT).show();
}
});
builder.show();
`
Upvotes: 1
Reputation: 6114
You can launch a web page using Intent
with this method:
private void launchWebPage(String url)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
And call this method from onClickListener()
of any Button
with appropriate URL
.
Upvotes: 1