Scrollbars in TextView - android

I just didn't want to use ScrollView. So I have a textview with enabled vertical scrollbars.
<TextView
android:id="#+id/tv_service_ticketinfo_details"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:textColor="#color/black"
android:autoLink="web"
android:scrollbars="vertical"
android:text="empty"
android:background="#drawable/custom_shape_grey">
</TextView>
The problem is, that scrollbars are scrollable only for texts, which contain a web-links. For other texts I see a scrollbar, but can't scroll.
I can't explain it. And you?
UPD:
Another strange thing:
once I set the text with links, then I can replace it by another one without links and the textView stays scrollable

So I think the problem is that TextViews don't automatically scroll, just because you set android:scrollbars. You have to set the ScrollingMovementMethod.
However, when you use autoLink and links are found, the android framework will set the MovementMethod for you. That's why the behaviour's different.
There are two solutions that work for me.
After we set our text, force the movement method to one that supports links and scrolling.
final TextView output = (TextView) findViewById(R.id.output);
output.setText(content);
// ensure that text will scroll with or without linked text
output.setMovementMethod(LinkMovementMethod.getInstance());
Or set the movement method, assuming plaintext, before adding text to the TextView. If autoLink detects links, it will change the movement method itself.
final TextView output = (TextView) findViewById(R.id.t_output);
// ensure that text defaults to scrollable
output.setMovementMethod(ScrollingMovementMethod.getInstance());
output.setText(content);
(FYI: I'm using android:autoLink="all")

add this in your code(may be in onCreate)
//textView.setMovementMethod(ScrollingMovementMethod.getInstance());
tv_service_ticketinfo_details.setMovementMethod(ScrollingMovementMethod.getInstance());
and test.

To get a scroll bar at run time or from code, you may try with following solution:
xml:
<LinearLayout
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/tv_service_ticketinfo_details"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:textColor="#color/black"
android:autoLink="web"
android:scrollbars="vertical"
android:text="empty"
android:background="#drawable/custom_shape_grey">
</TextView>
</LinearLayout>
java:
mTextViewPort = (LinearLayout) findViewById(R.id.view1);
// Create a ScrollView instance
ScrollView mScrollView = new Scrollview(mContext);
// here mContext would be Activity's context. You may also choose
// mScrollView as a global variable.
mScrollView.setScrollBarStyle(SCROLLBARS_OUTSIDE_INSET);
mTextViewPort.addView(mScrollView,
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView mTextView = (TextView) findViewById(R.id.tv_service_ticketinfo_details);
mScrollView.addView(mTextView );
This may help to resolve this problem.

Related

Android how to create textview one over another

I am developing an Android application with a textview updated by one event and at the same place where the textview is present, I want 1 more textview so that other event can update this new textview. How do i achieve in having 1 textview on other
I'm assuming that you're asking how you could have two TextView components overlaying each other. There are a few way you could do this.
Frame Layout
Use a Frame Layout to determine the area in which the TextViews will occupy. Like this...
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Credit goes to https://stackoverflow.com/a/2634059/3769032
Create a Compound View
This is fairly in-depth for the type of question you are asking. CompoundViews are a collection of typical views, such as a TextView, that you can create if you plan on re-using the view frequently.
If you plan on overlaying the TextViews often, I recommend this. So check out this tutorial.
Use only one TextView
Having two overlayed textviews can become messy really quickly. If you have two pieces of text overlayed is becomes impossible to read. So since the content of your textview is based on an event. Use the same event listener in your java code to determine the content of your TextViews.
For example, in your on click listener you might have...
TextView tv1 = (TextView) findViewByID(R.id.tv1);
public void onClick(View view){
if (first_event_happened){
tv1.setText("One event happened");
} else if(second_event_happened){
tv1.setText("A different event happened");
}
}
These conditions might mean checking the type of view that was clicked, or checking its id (what I usually do). Please comment if things aren't clear. Some clarification on your question would be helpful too.
use relative layout and also you can set text on exiting textView like when event one triggered textView.setText(your text) and same when event two triggered textView.setText(your text)
There is no trick to this. Just put two TextViews in a RelativeLayout at the same position and they will draw overtop of one another. Like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first textview"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second textview"/>
</RelativeLayout>
You can make each one visible or invisible by using TextView.setVisibility(...) or you can set their text with TextView.setText(...).

Text in TextView sometimes is shown vertical. How to prevent it?

I have this layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutUsers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:textAppearance="?android:attr/textAppearanceSmall"
>
</LinearLayout>
I add a few textviews into "layoutUsers" layout programmatically:
TextView textView1=new TextView(this);
textView1.setText("some text");
layoutUsers.addView(textView1);
And I have the following performance:
How to prevent text from being vertical?
How to show all text in a few lines?
Every text has own color, so I can't combine all textviews into one.
I guess we should do something with layout, not with textviews inside it...
Any ideas please?
Thanks!
setLayout Params for TextView programatically
I used a few textViews in layout, because they had different color and font.
I've discovered that I can use only one textView with HTML text.
Like this
String SPACE="nbsp;"
info+=SPACE+"user=<B><FONT color=#666666> someName+</FONT></B>";
textInfo.setText(Html.fromHtml(info));

How to make a view as always on top?

I have a TextView and it is on an image. When I click the button, TextView's background color will change, but image won't disappear. For example:
My TextView at the beginning:
When I click a button:
If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using FrameLayout. you can write your layout as below.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
When you want to change the color behind the TextView then change the ImageView background as below...
ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);
If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.
Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (e.g. all centered).
You should use ImageView instead of TextView, the:
public void onClick(View v){
imageView.setBackgroundColor(Color.GREEN);
}
ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. if want set Text in this
android:drawableTop="#drawable/any_drawable"
android:text="#string/any_text"

Fit content of textview after change text size

I have text view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/item_table_light_grey"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
</LinearLayout>
When I change size of text using code
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setTextSize(adapter.getTextSize());
width and height of textView not changed though text size changed successfully.
What am I doing wrong?
Android does NOT refresh layout of views with "wrap_content" once it has been displayed.
So if you add a child view, or modify the content dynamically, you're screwed.
To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with "wrap_content". The code and instructions to use are available here:
https://github.com/ea167/android-layout-wrap-content-updater
Another solution is to set fixed values to layout_width and layout_height, add gravity="center" on the TextView, and remove the wrap_content
Hope it helps!
For anyone might need this now and in the future, I did a trick to solve this. If your TextVew is inside a LinearLayout, eg. with orientation="vertical", then put these:
android:layout_height="wrap_content"
android:layout_weight="1"
to your TextView will make its height updated when the text change.
I don't know what happened inside, but this worked for me.
Another solution to this issue would be to remove the TextView programatically and then add it again.
linearLayout.removeView(textView)
linearLayout.addView(textView)
I know it sound stupid but it works.
In my case calling invalidate didn't work, only this worked.
Depending on your implementation you need to take care of view index inside its parent

Android: How to create a Dialog with a Scrolling title?

Ok so I've read the Custom Dialog explanation on the And Dev website
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
It show's you how to make a custom dialog, but not how to customise the title!
Basically my title is too long and I want it to scroll (like textview) or better still have a 'marquee' effect i think it's called.
Or if I can't make it scroll, give it more space to wrap onto more lines!
Any ideas, I don't hold out much hope as it's not on android.dev :-(
You can make dialog title multiline:
TextView title = (TextView) dialog.findViewById(android.R.id.title);
title.setSingleLine(false);
Customizig window (and thus also dialog) titles can be done by requesting the window feature CUSTOM_TITLE, which must be done before setContentView.
So in your Dialog / Activity subclasses onCreate(), call the following:
super.onCreate(savedInstance);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // <- insert this
Then, after your setContentView, do this:
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); // <- insert this
The layout can generally contain anything you want.
For a marquee text control. e.g. do this:
layout/custom_title.xml:
<FrameLayout android:id="#+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView android:id="#+id/caption_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="This is a very very long text that will not fit into a caption regularly, so it will be displayed using marquee..."
android:lines="1"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
></TextView>
</FrameLayout>
Due to some constraints with the marquee feature, the text view has to be made focusable and it will only be scrolling when focused (which it initially should be).
I consider a combination of RuslanK's (for getting the TextView) Thorstenvv's (for making TextView scrollable) answer to be best practice.

Categories

Resources