2

i want add Button in middle of text in TextView, like when i see a phone number in text, replace it with a Button in TextView, and set background of Button to a contact picture, but how can i add Button to TextView ? is it possible ?

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

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
4
  • A textview is for text, not buttons Commented Oct 27, 2014 at 9:04
  • 2
    Better to hide or show the view.Suppose, if any case you don't want button then progromatically setVisibility(View.gone) and add the textview already and hide the visibility to gone and whenever needed visible the needed view Commented Oct 27, 2014 at 9:05
  • thanks @HarshParikh, see my comment in vishal answer Commented Oct 27, 2014 at 9:18
  • better to play with visibility, take imageview or imagebutton for contact picture in a relative layout. Commented Oct 27, 2014 at 9:51

3 Answers 3

2

You are talking about placing a widget(or view) inside a widget. This is not possible. You can only add widgets inside layouts. However, you can position a widget on top of another widget if you will use a relative layout.

Refer : developer.android.com/guide/topics/ui/declaring-layout.html

Sign up to request clarification or add additional context in comments.

4 Comments

thanks @vishal but i don't know where it might be place, is it possible to catch the position of phone number in a text and set it as margin to another layout ?
May be place button to the right of the TextView (or somewhere else) and use an ImageButton. Whenever the TextView finds a phone number, the image of the ImageButton is replaced. Simple If-then-else would do this, i hope.
I didn't get this part "is it possible to catch the position of phone number in a text and set it as margin to another layout ?"
ok i accept your answer for possibility of placing a widget inside a TextView
2

I think you dont need button in textview.Its a combination of views means viewgroup is sufficient relativelayout will do the job

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/contactpicture"
    android:text="phonenumber"
    android:visibility="invisible" />

depending on the text change the visiblity of TextView and button. Hope it helps you.Thanks

Comments

1

Basically you cant place any Button inside a TextView. One approach to this sort of problems is to programmatically partition the text into different parts and i.e add a Button between TextViews whenever you meet a number. So lets say you have a text: "The number 123456 is my phone number", you would need to split into three parts: "The number", "123456", " is my phone number" and create the widgets accordingly i.e <TextView/> <Button/> <TextView/>

Comments

Your Answer

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