What you can do is to create a view programmatically and set it as content of your dialog.
//The Layouts of the dialog
Dialog confirmDialog = new Dialog(this);
LinearLayout btnLayout = new LinearLayout(this); //Contains OK & Cancel button
LinearLayout dialogLayout = new LinearLayout(this); //Container for the dialog
dialogLayout.setOrientation(LinearLayout.VERTICAL);
btnLayout.setOrientation(LinearLayout.HORIZONTAL);
btnLayout.setBackgroundColor(Color.LTGRAY);
//Create TextView and set clickable atributes
String definition = "Can click on each word on this to show a toast".trim();
TextView definitionView = new TextView(this);
definitionView.setMovementMethod(LinkMovementMethod.getInstance());
definitionView.setText(definition, TextView.BufferType.SPANNABLE);
Spannable spans = (Spannable) definitionView.getText();
BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
iterator.setText(definition);
int start = iterator.first();
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) {
String possibleWord = definition.substring(start, end);
if (Character.isLetterOrDigit(possibleWord.charAt(0))) {
ClickableSpan clickSpan = getClickableSpan(possibleWord);
spans.setSpan(clickSpan, start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
dialogLayout.addView(definitionView);
//Create Ok and Cancell Button
Button cancelBtn = new Button(this);
cancelBtn.setText(getString(R.string.detail_dialog_cancel_button_text));
cancelBtn.setBackgroundColor(Color.WHITE);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
confirmDialog.cancel();
}
});
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, getDp(60), 1f);
params.setMargins(0, getDp(1), 0, 0);
cancelBtn.setLayoutParams(params);
Button okBtn = new Button(this);
okBtn.setText(getString(R.string.detail_dialog_confirm_button_text));
okBtn.setBackgroundColor(Color.WHITE);
okBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
confirmDialog.cancel();
}
});
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, getDp(60), 1f);
params.setMargins(getDp(1), getDp(1), 0, 0);
okBtn.setLayoutParams(params);
btnLayout.addView(cancelBtn);
btnLayout.addView(okBtn);
dialogLayout.addView(btnLayout);
confirmDialog.setContentView(dialogLayout);
confirmDialog.setTitle("Click The Words");
confirmDialog.show();
If you have multiple TextViews then you can create a contentLayout(i.e. LinearLayout) containing the TextViews, and then add the the contentLayout to the dialoglayout.
The you need to implement the getClickableSpan()
method that handles the clickListener..
private ClickableSpan getClickableSpan(final String word) {
return new ClickableSpan() {
final String mWord;
{
mWord = word;
}
@Override
public void onClick(View widget) {
Log.d("tapped on:", mWord);
Toast.makeText(widget.getContext(), mWord, Toast.LENGTH_SHORT)
.show();
}
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
};
}
You can override the onClick()
method, and then call at method that opens the new activity