0

I'm trying to develop an app that consists of multiple arrays and textviews and I've run into a problem.

I have 3 textviews and 2 arrays.

One of my arrays contains sentences. My first question is how can I highlight specific words in each sentence? Ex: "This is my first array item" I need to highlight a word in the string so that when it is displayed in textview1, it will appear like this... "This is my first array item"

My other array contains words. They are displayed in textview2 and should also be highlighted. My next question is how can I replace the current highlighted word in textview1 with the new highlighted word from textview2, and also have a 3rd textview displaying the new sentence? Ex: If the chosen item from my sentence array is "This is a test" (displayed in textview1) And the item from my word array is "website" (displayed in textview2) the result would be "This is a website" (displayed in textview3)

Here is my code so far

import java.util.Random;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;



public class Class1 extends Activity implements OnClickListener
    {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class1);

        Button btn_sentence = (Button) findViewById(R.id.btn_sentence );
        Button btn_word = (Button) findViewById(R.id.btn_word );
        Button btn_newsentence = (Button) findViewById(R.id.btn_newsentence );

        final Random ran = new Random();
        final Resources res = getResources();
        final TextView text1 = (TextView) findViewById(R.id.sentencetext);
        final TextView text2 = (TextView) findViewById(R.id.wordtext);
        final TextView text3 = (TextView) findViewById(R.id.newsentencetext);

        btn_sentence.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                String[] NewText = null;
                NewText = res.getStringArray(R.array.sentences);
                String strRandom = NewText[ran.nextInt(NewText.length)];
                text1.setText(strRandom);

            }
            });
        btn_word.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                String[] NewText2 = null;
                NewText2 = res.getStringArray(R.array.words);
                String strRandom2 = NewText2[ran.nextInt(NewText2.length)];
                text2.setText(strRandom2);

            }
            });
        btn_newsentence.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                **text3.setText(" " + (text1.getText()) + " " + (text2.getText()) + " ");**

            }
            });

    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }}

The bold text is as close as I've gotten, but all that really does is combine both TexttView1 and textview2, and display it in textview3... which isn't exactly what I want.

Help would be much appreciated ~TylerNormal

3
  • Homework ?? What did you try? please add a code Commented Sep 29, 2014 at 10:55
  • Not sure if my new edit helps but that's my code so far Commented Sep 29, 2014 at 11:17
  • See my code-snippet below. It contains what you need, with proper adjustment and a bit of thinking applied, of course :) Commented Sep 29, 2014 at 11:33

1 Answer 1

1

You can use simple HTML-tags, wrap your words you want to highlight into those using this:

TextView text = findViewByID(R.id.myTextView);
String test = "This is my first array item";
// example - the word in brackets is to be replaced and captured
// it is just an example to show how you can use it later with more
// complex cases
test = test.replaceAll("(first)", "<b>$1</b>");
text.setText(Html.fromHtml(test));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks man, literally my third day of doing coding haha. But if I do what you suggested, wouldn't I have to type in this snippet for every element? I plan on having hundreds of items in both my arrays
Only if you use it literally, which of course you shouldn't. You should adjust it, according to what you really need to highlight. Make a list of words you need to highlight, put them into an array, iterate over that array with the replace-call for every entered string, and have that string from that search-array replaced. The replace-line would transform into something like test = test.replaceAll("("+search[i]+")", "<b>$1</b>"); and it would of course be inside a for-loop.
If this really is only your third day into programming...I strongly advise of getting a book about the fundamentals of programming, then about object oriented programming, probably in java, and then you should turn to android development, instead of jumping directly and blindly into developing an app...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.