I have an XML layout that defines a TextView box 50px x 320px who ID is TextView2.
I want to use the same TextView area to show an image sometimes.
I want to programatically be able to switch that area to be either TextView or ImageView.
<LinearLayout
android:layout_width="320dp"
android:layout_height="50dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.35"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Just add an ImageView below / next to your TextView and set the android:visibility="gone" on the one you don't want to show.
Also set an android:id="#+id/some_identifier" so that you can find the view in code and set the TextView to View.GONE and the ImageView to View.VISIBLE when you want to switch.
<LinearLayout
android:layout_width="320dp"
android:layout_height="50dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.35"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/image_view_1"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here you have two options :
You can set background of textview or set text whenever you want, based on certain conditions.
You can separatly take an ImageView nd when you want Image Set Visibility of imageView True and Textview GONE, Whereas When you want text then set Visibility of TextView True and ImageView GONE
Related
I have a textview with a drawableleft. As the textview is covering whole width of the screen so i want textview text and its drawable left to the center of the textview. At present only text is aligning to the center of it and drawable is always in the extreme left of it. So please suggest me something.
<TextView
android:layout_marginLeft="#dimen/_10sdp"
android:layout_width="0dp"
android:layout_height="#dimen/_40sdp"
android:layout_weight="5"
android:gravity="center"
android:drawableLeft="#drawable/ic_cloud"
android:paddingLeft="#dimen/_10sdp"
android:text="#string/up"
/>
You cannot achieve this using drawableleft. Use separate Layout with ImageView and TextView as shown below:
<RelativeLayout
//add necessary attributes
android:gravity="center_horizontal">
<ImageView
//Add your image here
/>
<TextView
android:layout_marginLeft="#dimen/_10sdp"
android:layout_width="0dp"
android:layout_height="#dimen/_40sdp"
android:layout_weight="5"
android:paddingLeft="#dimen/_10sdp"
android:text="#string/up" />
</RelativeLayout/>
Hope this helps
I am trying to have a TextView with an ImageView directly to its right. Right now the image partially overlaps the far right of the text, so if the text inputted is long, then the image is "over" the text. I have an ellipse setting on the text but I need the TextView to "stop" right at the left border of the ImageView.
TextView layout settings:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
ImageView layout settings:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
Try this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/iv"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"
.... />
<!-- background == #null if you don't want default button background -->
<ImageButton
android:id="#id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="3dp"
android:background="#null"
.... />
</RelativeLayout>
EDIT
See the comments in regards to why this works and the posted question doesn't. Also take a look at
Android Relative Layout alignParentRight and alignParentEnd
for a reference to end/right and start/left attributes.
I have a XML file with RelativeLayout, I also have Textview which has Gravity Center. In code I would like to get X coordinate of this Textview, where the TextView begins (length of TextView is not constant, it can change). I need a ImageView to be next to the TextView.
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="TextView" /> `
countryText = (TextView) findViewById(R.id.textView6);
countryText.getLeft()
getLeft() return 0;
Add android:layout_centerInParent="true" to textview and add android:layout_toLeftOf="#id/textView6" to imageview
Simply get rid of the ImageView and add a compound drawable to the left of your TextView.
This is also more efficient, because it helps flattening your layout hierarchy.
I.e.:
<TextView
android:id="#+id/textView6"
android:drawableLeft="#drawable/my_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="TextView"
/>
Official docs
in the below xml layout, i have two buttons at the same position and they will do their function based on the visibilty set. now i tried to place two textviews
below the buttons, i want the text views to be below both buttons so I used
android:layout_below="#id/actConnect2_btn_connect"
but at run time when the connect-button is visible the text view appears below it, and if pair-button is visible it overlap
how to display the textview below both buttons?
Note: i know that i can use android:layout:marginTop but i want to solve it without it
code:
<Button
android:id="#+id/actConnect2_btn_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_tv_label_devClass"
android:layout_centerInParent="true"
android:text="#string/str_pair"/>
<Button
android:id="#+id/actConnect2_btn_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/str_connect"
android:layout_below="#+id/actConnect2_tv_label_devClass"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/actConnect2_tv_label_uuids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_btn_connect"
android:text="Service's UUID: ">
</TextView>
<TextView
android:id="#+id/actConnect2_tv_uuids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_tv_label_uuids">
</TextView>
Put both buttons in a LinearLayout then put the textview below the LinearLayout
take both button in one layout
<RelativeLayout android:id="#+id/relativeButton"
android:layout_width="wrap_content"
android:layout_below="#id/actConnect2_tv_label_devClass"
android:layout_height="wrap_content">
<Button
android:id="#+id/actConnect2_btn_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/str_pair"/>
<Button
android:id="#+id/actConnect2_btn_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/str_connect"
android:layout_alignParentStart="true" />
</RelativeLayout>
and use this for your textview
android:layout_below="#id/relativeButton"
You have to place the buttons in a Layout (any type and arrange them accordingly)
Assign an ID to that layout.
Place the textView below that layout ID.
I have a some ui widgets including textview inside RelativeLayout which is clickable. My problem is textview text color does not change when relativelayout gets focus although I have set textview color property correctly.
Is there any easy way to cascade focus to childview inside relative layout, same as listview items.
<RelativeLayout
android:id="#+id/top_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/selector_white_blue"
>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:textSize="#dimen/text_size_med"
android:textColor="#color/black_text_white_focused"
android:textStyle="bold"
android:text="Movie Name"
/>
</RelativeLayout>
Specify android:duplicateParentState='true' on your TextView.
Programmatically by textView.setDuplicateParentStateEnabled(true);
for your
TextView textView= new TextView(this);