TextView color change on focus/press - android

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);

Related

how to display textview below button in relativeLayout

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.

Avoid TextView resizing if text is too long

I have a TextView that when it has a long text, it resizes to make the text fit inside, breaking the UI.
Is there a way to use a XML attribute to make the TextView not resizable?
I was thinking about using the TextView inside a ScrollView, is there other options?
For this to work, set specific width to textview (It will display textview without scroll)
android:layout_width=""
If you want to show scroll, add scrollview with specific size and inside scroll layout, add textview like
<ScrollView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/scrollView"
android:fillViewport="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
100dp is dummy size here.
Solution :
<TextView
android:elipsize="marquee"
</TextView>
check out this Answer for more options :
Elipsize Examples
this will slide the textView
Try this code:
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:singleLine="false"
android:text="Long multiline text"/>

Adjust TextView in Layout when TextView is empty

I have a basic ListView with two TextViews in the cells and I would like for the TextViews to center to the middle of the Layout when there isn't any text in the other TextView.
Here is my current XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:minHeight="80dp"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<TextView android:id="#+id/titleTextView"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:textSize="16sp"/>
<TextView android:id="#+id/detailsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_below="#id/titleTextView"
android:textSize="12sp"/>
</RelativeLayout>
I'm using a RelativeLayout because I am hoping to utilize it when I add more elements, but I want to figure this part out first. Switching to a LinearLayout didn't have any effect on adjusting the TextViews.
Will I have to adjust the views programmatically? If so, how should I handle that? Remove the one view from the layout (Which will require a LinearLayout, right)?
Simply make the TextView "gone" when it is empty, when a view is "gone" it has no effect on the layout it is in, thus the other TextView would center because of the gravity property you have added.
textView.setVisibility(View.GONE);

How to programatically change a TextView to an ImageView and back

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

Wrapping of multiline text in TextView - Android

I have a multiline TextView with a layout_width="wrap_content" property. The problem is when
the TextView actually becomes multiline then this property just doesn't work. How can I fix this behavior?
Here is illustration:
XML:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:focusable="false"
android:textSize="16.5dip"
android:padding="2dip"
android:lineSpacingMultiplier="1.1"
/>
It seems that you are using parent view (ViewGroup) where you have defined background for your box. You just need to define background for you TextView instead of ViewGroup.
So, do not use background in your ViewGroup, try to use it in the textview. For example:
<FrameLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:focusable="false"
android:textSize="16.5dip"
android:padding="2dip"
android:lineSpacingMultiplier="1.1"
android:background="#drawable/background_box"
/>
</FrameLayout>

Categories

Resources