1

So what I wanted to do was change the activity when clicking a certain word inside an Alert Dialog.

The text is taken from a JSON file, and it displays fine. I just wanted to be able to click a word from inside the Alert Dialog and then go to a different activity from there. Any ideas on how I can get this done?

  • any code to show for it – 1baga Jul 19 '14 at 22:18
  • Should I put all the JSON Parsing as well, or just the Alert Dialog? Everything works fine, I just want to know how to make some of the text inside the Alert Dialog clickable. – user3717258 Jul 19 '14 at 22:29
  • Check this post: http://stackoverflow.com/questions/8612652/select-a-word-on-a-tap-in-textview-edittext – Mike M. Jul 19 '14 at 22:30

2 Answers2

0

What you need to do is to set a clicklistener for the TextView(containing the text)

You can do something like this:

public void onCreate(Bundle savedInstanceState){
    ...

    TextView text = new TextView(this);//or findViewById(R.id.textView)
    text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onTextClicked(view);
                }
            });
    ....
}

public void onTextClick(View v) {
    Intent i = new Intent(this, newIntent.class);
    startActivity(i);
}

You can addapt the onTextClick method to accepts the parameters you need.

Ibrahim Yildirim
  • 2,731
  • 2
  • 19
  • 31
  • I'm afraid this is for the whole TextView not specific words. – Simas Jul 19 '14 at 22:24
  • I was just about to say that. For example, if what's in the Alert Dialog box is "Hello World, my name is Something," I would want just the world "Something" to be clickable (possibly bold), starting a new activity. – user3717258 Jul 19 '14 at 22:28
  • Oh i see, i didn't read the entire question. I will admit another answer where you create a TextView in code and and add it in a view which will be the content of the dialog.. – Ibrahim Yildirim Jul 20 '14 at 00:54
0

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

Ibrahim Yildirim
  • 2,731
  • 2
  • 19
  • 31