How to make vertical scrolling textview's in Android - android

I have been looking for a post to create vertical scrolling multiple textviews. similar to one shown here http://vertical-scroller.vbarsan.com/
But all post are related to creating horizontal scrolling (Marquee) textview.

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"/>
I hope this helps

Have main layout as ScrollView ,Create vertical LinearLayout as child of ScrollView and create (number of textview you required ) textViews inside LinearLayout.
you should change android:layout_height
as per your code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FF4081"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="1st Line !" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="2nd line !"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="3rd line !" />
</LinearLayout>
</ScrollView>

You have 2 choice for that:
1) Use Native way: using thread, put 10 spaces before any text and in each second remove left side space and increase right space and so on, until it's limits. Hope you understood. Predefined TextView's below properties also uses this method.
2) Use predefined control TextView with below properties:
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"

use this example code below it will do a marquee like html
<TextView
android:id="#+id/mywidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:textColor="#ff4500"
android:text="Simple application that shows how to use marquee, with a long text" />

Related

Top margin added in android to button when it's text has multiple lines [duplicate]

I'm trying to figure out why a two-line button in my application is being shifted a couple of pixels lower than the other buttons:
This does not happen if I shorten the text on the third button until it fits on one line, which tells me it has something to do with the line break. Adding android:layout_gravity="top" to the button's layout doesn't seem to help. Any ideas what might be causing this one?
Edit: Here's the layout XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="10dip"
android:layout_width="wrap_content">
<TextView android:id="#+id/error_text"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:text="Place holder"
android:layout_width="wrap_content"
android:textSize="17dip"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:padding="10dip"
android:layout_width="wrap_content">
<Button android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/black"
android:textStyle="bold"/>
<Button android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:text="#string/cancel_login"
android:textColor="#color/black"
android:textStyle="bold"
android:visibility="gone"/>
<Button android:id="#+id/third_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:textColor="#color/black"
android:textStyle="bold"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
A horizontal LinearLayout aligns the baselines of all its child controls by default. So the first line of text in your multi-line button is vertically aligned with the single line of text in the other buttons.
To disable this behaviour, set android:baselineAligned="false" on the LinearLayout.
Having had a look at your layout (on a device), I am not sure why it exhibits this strange behaviour. When debugging layouts I like to put background colours on Views so you can see more clearly the space they are taking up. If we remove all the padding, we see that the buttons simply don't sit on the same top line. If we apply android:gravity="center_vertical" to the containing LinearLayout we see that the first two buttons are centered but the last one sits snugly with the top edge.
One solution to this is just to rewrite the inner container using a RelativeLayout:
<RelativeLayout
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_width="wrap_content">
<Button android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/black"
android:textStyle="bold"/>
<Button android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_toRightOf="#id/ok_button"
android:text="#string/cancel_login"
android:textColor="#color/black"
android:textStyle="bold"
android:visibility="gone"/>
<Button android:id="#+id/third_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_toRightOf="#id/cancel_button"
android:textColor="#color/black"
android:textStyle="bold"
android:visibility="gone"/>
</RelativeLayout>
From my testing, by using a RelativeLayout you can get all of the buttons to sit on the same top edge.
I ran your code with the following change and it worked fine:
Change the android:gravity in the horizontal linear layout from "center_horizontal" to "center".
I have add the following lines to your third button in XML and make it's height to "fill_parent" :
android:paddingTop="4dp"
android:layout_height="fill_parent"
For me, 4dp worked fine you can check if you need more or less.
Make these changes and your button will be fine, like its fellows :-)
write android:layout_height="fill_parent" for all three buttons

Scrolling textview is not scrolling, in Android.

I'm making my text scrolling as the post here.
I think I saw it was scrolling first time in another layout even without any java code.
But now, it's not scrolling anymore. No idea what's wrong with this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<LinearLayout
android:id="#+id/scrollingTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#0792B5"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/scrollingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:maxLines="1"
android:padding="5dp"
android:scrollHorizontally="true"
android:text="The culture of India is the way of living of people of India."
android:textColor="#FFFFFF"
android:textSize="16sp" />
</LinearLayout>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/scrollingTextLayout"
android:background="#android:color/darker_gray" />
</RelativeLayout>
just put these lines in your xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:scrollHorizontally="true"
And in your code:
textView.setSelected(true);
It's now scrolling. I just changed maxLines="1" to singleLine="true" , you know it's a little weird. I changed singleLine to maxLines because the IDE suggested me to use maxLines instead of singleLine since it's deprecated already.
Add this code in Activity
TexView tv=findViewbyId(R.id.scrollingText);
tv.setSelected(true);

RelativeLayout align Textview and Edittext

I want to use a RelativeLayout to align horizontally a textview and after that an edittext and then again a textview and then an edittext. I know that you can do that with a LinearLayout, but I want to accomplish that with a relativeLayout.
My Code is the following:
<?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="wrap_content">
<TextView
android:id="#+id/userNameLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/userNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/userNameLbl"
android:minWidth="200dp"
android:text="Sample Text" />
<TextView
android:id="#+id/pwdLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_toRightOf="#+id/userNameText"
android:layout_toEndOf="#+id/userNameText" />
<EditText
android:id="#+id/userNameText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/pwdLbl"
android:minWidth="200dp"
android:text="Sample Text" />
</RelativeLayout>
But it doesn't work. All the controls (edittexts and textviews) are put on each other! What is wrong with my code? I have used layout_toRightOf to put them next to each other.
And because it was mentioned, there is enough place for the controls.
Here is also a picture of the designer, how it is looking like:
I copied your code just to see the exact problem, but everything looked fine. However The last EditText was cramped on the right side due to space-issues.
Are you sure you have enough space so that everything can fit?
With too little space given it might be possible that the views get crammed onto each other.
When you look closer, in android:layout_toRightOf and android:layout_toEndOf attributes you are using #+id/... values. Delete the + in order to refer to an existing resource item.
Ok, the problem seems to be in Xamarin Studio itself which is not rendering the content of the layout correctly. Because when I run this layout on a device the controllers are put next to each other and there is not such a problem, but the designer show it completely different.
Try this code it will align as you need:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:id="#+id/userNameLbl"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:text="Username:" />
<EditText
android:id="#+id/userNameText"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Sample Text" />
<TextView
android:id="#+id/pwdLbl"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="#+id/userNameText2"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Sample Text" />
</LinearLayout>
Comment below if you need any further info

Unable to horizontally center a TextView

I have a TextView inside a LinearLayout and i want to align it in center of the page but its not happening, i have tried the following:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#80000000"
android:gravity="center"
android:padding="10dp"
android:text="User Name"
android:textColor="#FFFFFF"
android:textSize="12sp" />
</LinearLayout>
Change the TextView layout_width to "match_parent", the issue is that "gravity" works only for the child of the View in this case the "text" it self, hence if you specify the object width as just to wrap its content, it means there's no space to center to, filling the whole parent and using "gravity" center would do the trick.
The other thing you can do is changing the "android:gravity" property to "android:layout_gravity" and center horizontal, this way you are telling the TextView itself to move to the center...
As best practice always try to use RelativeLayouts and avoid LinearLayouts, they provide a better way to manipulate Views position and are "device size fragmentation" friendly, RelativeLayout have plenty of methods to position views on the most common positions including center, top, bottom etc...
Hope this Helps.
Regards!
Try changing the layout. Linear Layout is meant for displaying ui components in rows. If you want to center your textview using you layout, I suggest changing the layout to Relative Layout.
First of all you cant set the property android:layout_centerHorizontal="true" for a LinearLayout. To make it centered you need to make layout height and width to match_parent like below,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/userName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="#80000000"
android:gravity="center"
android:padding="10dp"
android:text="User Name"
android:textColor="#FFFFFF"
android:textSize="12sp" />
</LinearLayout>
Try using:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center" >
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#80000000"
android:gravity="center"
android:padding="10dp"
android:text="User Name"
android:textColor="#FFFFFF"
android:textSize="12sp" />
</LinearLayout>

TextView cuts off text when it is long enough

I have strange problem with TextView, it cuts off part of the text at the end. My layout looks like
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top|center_horizontal"
android:gravity="top|center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top|center_horizontal"
android:gravity="top|center_horizontal"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginBottom="5dp">
<Button
android:id="#+id/btnPreviousQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selector_arrow_left"
android:clickable="true"
android:visibility="gone" >
</Button>
<TextView
android:id="#+id/txtQuestion"
style="#style/question_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top|center_horizontal"
android:layout_weight="1"
/>
<Button
android:id="#+id/btnNextQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/selector_arrow_right"
android:clickable="true" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="horizontal" >
<WebView
android:id="#+id/wvMultimediaQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginLeft="50dp"
android:layout_marginRight="55dp"
android:layout_weight="1"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
and txtQuestion cuts off text when it is long enough. What is wrong, does anybody know ?
Make use of these attributes
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
will append "..." at the end. But this will not solve problem in honeycomb tab
So for honeycomb tablet add the following atttibute also
android:singleLine="true"
On the other hand if you require marquee effect
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true
Yes there are some attributes you need to set, but first let us know what type of textview do you want exactly, single line or multi line?
There are some attributes you can take care of:
android:singleLine="true" // or false
android:ellipsize="marquee" // must check
android:lines="1"
Set below properties in layout file. It works fine for me
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
If you expect differing lengths of text for your view or you're going to change the textSize of different instances of your TextView then you should use View.addOnLayoutChangeListener().
See an example here: https://stackoverflow.com/a/44069734/1402087
Set the layout_gravity on the TextView to fill
You need to set
android:minLines="2"
For me it I realised I was using the layout view of a different device to the one I was testing with. When I corrected this, I could see the textlayout was way too wide so I used this setting to reign it in:
android:layout_width="330dp"
Tried all the solutions provided here but none of these helps, finally got the solution using android:layout_width="0dp" instead of android:layout_width="fill_parent" in textview.

Categories

Resources