3

Although I know there are hundreds of posts in stackoverflow regarding this post still, I did not found my appropriate answer. So I am asking it as new question.

Problem:- I have a text in text view "You need cookies or banana to eat?" I want that if any one click on banana or cookies then only it should go to Kitchen screen. If clicked on other word it should not react.

My Try:- I am doing it from text view, android:clickable="true" but complete line is getting clickable. So I decided to break it into various text views, and used relative layout but again there was a problem that complete line was not coming in center of screen.When I hard coded it as android:layout_marginLeft="150dp" but then indentation disturbs for portrait and landscape. and obviously we should not hard code the values. Apart from that to give a clear view to user I need to do some formatting to banana and cookies like bold and underlined and some color. Although I used and tag for bold and underline, how to color the text.?

Lastly I should summarize my problems as

1) How to make some word in a complete string clickable (not url links)

2) How to do formatting of few text of complete string.

3) If relative layout is used how to keep the entire string in center.

If any other approach can b used they are also welcomed

Point to note that these all are not url links but internal screens. So please if any one could help. Any help will be appreciated. Thanks in advance.

Android
  • 3,828
  • 9
  • 46
  • 79

3 Answers3

1

Hello AddingKnowledge.

What u can do is to define your linear layout inside relative layout, that will provide u some flexibility. Apart from that style can be done through string.xml. Or you can do styling from style.xml and that is why style.xml is there.

If you want you can go to link as How to make a part of text clickable like button in Android? for some deeper knowledge. Or handle textview link click in my android app

Community
  • 1
  • 1
Android
  • 3,828
  • 9
  • 46
  • 79
0

To keep the text in center try with android:layout_centerInParent="true" dnt use android:layout_marginLeft="150dp"

and For Textview To be clickable

                tv.setText(Html
            .fromHtml("<a href='Banana'>Banana</a>"));
    tv.setMovementMethod(LinkMovementMethod.getInstance());

    // Performing action on TouchListener of ForgotPassword
    tvforgotpwd.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Intent Forgotpwdintent = new Intent();
            Forgotpwdintent.setClass(getApplicationContext(),
                    Kicthenscreen.class);
            startActivity(Forgotpwdintent);
            return true;

        }
    });

Look at this link it may usefull... http://coderzheaven.com/2011/05/textview-with-link-in-android/

The Xml file i tried with relative layout is ..

  <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/textView1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/textView2"
        android:text="TextView" />
     <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/textView3"
        android:text="TextView" />
</RelativeLayout>

and i wrote in java file like this..

         TextView tv=(TextView)findViewById(R.id.textView3);
    tv.setText(Html.fromHtml("<a href='Banana'>Banana</a>"));
    tv.setClickable(true);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    tv.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show();
            return true;
        }
    });

for me the layout aligned to center only..

user1213202
  • 1,305
  • 11
  • 23
  • thanx for quick reply, I ll trying your solution – Android Feb 29 '12 at 07:43
  • sorry not android:layout_centerInParent="true" use android:layout_centerHorizontal="true". – user1213202 Feb 29 '12 at 07:50
  • android:layout_centerInParent="true" When I am using this to my text view, its just takin my first textview to center thus disturbing the layout and same goes for android:layout_centerHorizontal="true" – Android Feb 29 '12 at 07:55
  • add that to ur layout not to the textview – user1213202 Feb 29 '12 at 08:25
  • sry that is applicable to linear layout and i m using relative layout for using toRightof tag – Android Feb 29 '12 at 08:48
  • thanx 4 reply but no ma'am I already quoted that I cant use android:layout_centerHorizontal="true" with relative layout.Its still left aligned. Although i using combination of both relative and linear still no success – Android Feb 29 '12 at 08:58
  • i added the what i tried with my example code.once check it.may be it will solve ur pblm. – user1213202 Feb 29 '12 at 09:14
0

Hi User AddingKnowledge,

I read your problem very carefully, If you ready to break the sentence in different set of texts then you can set the texts in the TextViews as I have done in below.You can check the below for more detail xml layout.For the TextView text_banana and text_cookies you can set the onclick listeners.

For any convenient please let me know .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You need " />

<TextView
android:id="@+id/text_cookies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cookies" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" or " />

<TextView
android:id="@+id/text_banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="banana" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" to eat?" />

</LinearLayout>

</RelativeLayout>
Om Narain Shukla
  • 361
  • 1
  • 4
  • 12