Run TextView marquee after some time - android

How can I run a text marquee in the TextView with delay before start?
At this moment I use the next code to start:
mTVTitle.postDelayed(new Runnable() {
#Override
public void run() {
mTVTitle.setFocusableInTouchMode(true);
mTVTitle.invalidate();
}
}, 1000);
TextView xml:
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="2"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAppearance="?attr/titleTextAppearance"
android:textColor="#color/white"/>
But it doesn't work although if I set this property in xml then all right. How to fix it to I can start a marquee programmatically?

As mentioned here in order to activate textview marquee you have to add this :
mTVTitle.setSelected(true);
As you want to start the marquee with a delay you have to put this inside your run() like this
mTVTitle.postDelayed(new Runnable() {
#Override
public void run() {
mTVTitle.setSelected(true);
}
}, 1000);

Related

Android Textview marquee not working for first time

I have Textview it is intially hidden when activity loads.. When click on button it is shown..
But first time when textview is showing marquee doesnot work.. Unless it is working fine.. If screen get locked after unlocked it started working fine..
I am setting string as text in code and have also used setselected(true) in code..
<TextView
android:id="#+id/txtInfo"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/linearTtitle"
android:background="#color/md_grey_300"
android:ellipsize="marquee"
android:freezesText="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text=""
android:textColor="#color/md_black_1000"
android:textSize="16sp" />`
When you setSelected true to your textview at that time textview is not in a position to perform command so you can do it inside view.post so when it is active it will perform the operation.
Try this code inside your button
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tv.setVisibility(View.VISIBLE);
tv.post(new Runnable() {
#Override
public void run() {
tv.setSelected(true);
}
});
}
});

Android Marquee Wait duration

How to make a Marquee TextView wait for a specific time until it starts to scroll horizontally?
Because when I open an Activity it starts straight to scroll. So you have to wait until its back on startposition to read it.
In the XML I simply added TextView like this:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!, Hello World!, Hello World!, Hello World!, Hello World!, Hello World!, Hello World!, Hello World!"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:maxLines="1"
android:scrollbars="none" />
Then in code (in Activity, but can be anywhere):
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView.setSingleLine(false);
textView.setMaxLines(1);
}
#Override
protected void onResume() {
super.onResume();
textView.postDelayed(new Runnable() {
#Override
public void run() {
textView.setSingleLine(true);
}
}, 3000);
}
As mentioned in this answer in order to activate textview marquee you have to add this :
tv.setSelected(true);
As you want to start the marquee with a delay you have to put this inside your run() like this
tv.postDelayed(new Runnable() {
#Override
public void run() {
tv.setSelected(true);
}
}, 1000);
this will delay it
<TextView
android:id="#+id/testing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
android:layout_below="#id/toolbar"
/>
Then, in activity just set using setSelection to true. I already tested it and its working
testing.postDelayed(new Runnable() {
#Override
public void run() {
testing.setMaxLines(1);
testing.setEllipsize(TextUtils.TruncateAt.MARQUEE);
testing.setMarqueeRepeatLimit(10000);
testing.setSelected(true);
}
}, 3000);
All you need to do is delay the focus of textview so that it starts it marquee after a certain period of time. The following code starts rolling the marquee after 2 seconds.
yourTextview.postDelayed(new Runnable() {
#Override
public void run() {
yourTextview.setSelected(true);
}
}, 2000);
P.S : You first need to post what you have tried so far.

Android ScrollView scrolling

I'm working on a project where I have a ScrollView which contains a linearlayout.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9e9e9e"
android:layout_weight="2"
android:id="#+id/scrollview_llChatContainer"
android:layout_above="#+id/relativeLayout">
<LinearLayout
android:id="#+id/llChatContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:background="#cccccc">
</LinearLayout>
</ScrollView>
I'm inflating this linearlayout in my code. The last result looks more like a listView. Here is how I'm inflating the LinearLayout.
public void populateMessages(List<Message> messagesList){
inflater.inflate(R.layout.inc_message_layout, llChatContainer, false);
imageLoader.displayImage(message.imageuser, (ImageView)messageLayout.findViewById(R.id.imageView));
}
if(!message.image.equalsIgnoreCase("")){
imageLoader.displayImage(message.image, (ImageView)messageLayout.findViewById(R.id.rivMsgImageN));
}
TextView tv = (TextView)messageLayout.findViewById(R.id.tvMsgTextN);
tv.setText(message.text);
llChatContainer.addView(messageLayout);
}
}
Now when the list is populated, I want the scrollView to scroll to the bottom of the LinearLayout. I have tried two of the ways I found.
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
}
});
scroller.post(new Runnable() {
public void run() {
scroller.scrollTo(0,scroller.getBottom());
}
});
None of the above mentioned solution is working for me. Linearlayout gets populated as expected but the scrollview doesn't even scroll a bit.
Is there any other work-around to get this done?
Thanks.
this is caused by time problem , try to scroll with a little delay , use this code
scroller.postDelayed(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
}
}, 10L);
Use scroller.scrollTo(0,linearlayout.getBottom()); or scroller.scrollTo(0,linearlayout.getHeight()); after populateMessages() called. Child of the ScrollView is changing it's value not the ScrollView itself.

Why HorizontalScrollView not scroll in HTCDesire 2.2 but only on simulator?

I am using a HorizentalScrollView in a view. I add TextView at runtime. I use horView.smoothScrollTo(x,y); It works fine for simulator. But it doesn't scroll at HTCDesire 2.2 ? any idea? here my code.
horView.smoothScrollTo(num, 0);
<HorizontalScrollView
android:id="#+id/cate_head"
android:scrollbars="none"
android:foregroundGravity="fill_horizontal"
android:layout_width="fill_parent"
android:paddingTop="6dip"
android:background="#drawable/slider_background"
android:focusable="true"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/cate_head_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
</LinearLayout>
</HorizontalScrollView>
At the end I found solution. I was calling smoothScrollTo(x,y) at the end of on create.(where else I can put that?) problem was that at the time of initializing there was not size or length found(But it should not be, because I have put all the data in that).
So call postDealy() with a delay of 50 sec. It works for me. Here is that. I put it at the end of onCreate();
May b anyone else have a batter solution...
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
horView.smoothScrollTo((scrollAmount), 0);
}
}, 50);
You can use View.post to avoid depending on timing.
horView.post(new Runnable() {
public void run() {
horView.smoothScrollTo((scrollAmount), 0);
}
});
This gets executed right after the view has been attached to the screen.

How to click or tap on a TextView text

I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.
I keep thinking about button listeners and anonymous method listener calls, but it just does not seem to apply to TextView.
Can someone point me at some code snippet to show how clicking or tapping on a piece of text in a TextView runs a method?
You can set the click handler in xml with these attribute:
android:onClick="onClick"
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
...
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
This may not be quite what you are looking for but this is what worked for what I'm doing. All of this is after my onCreate:
boilingpointK = (TextView) findViewById(R.id.boilingpointK);
boilingpointK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ("Boiling Point K".equals(boilingpointK.getText().toString()))
boilingpointK.setText("2792");
else if ("2792".equals(boilingpointK.getText().toString()))
boilingpointK.setText("Boiling Point K");
}
});
OK I have answered my own question (but is it the best way?)
This is how to run a method when you click or tap on some text in a TextView:
package com.textviewy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class TextyView extends Activity implements OnClickListener {
TextView t ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView)findViewById(R.id.TextView01);
t.setOnClickListener(this);
}
public void onClick(View arg0) {
t.setText("My text on click");
}
}
and my main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="#+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<ListView android:id="#+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
<LinearLayout android:id="#+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<TextView android:text="This is my first text"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="28dip"
android:editable = "true"
android:clickable="true"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
from inside an activity that calls a layout and a textview, this click listener works:
setContentView(R.layout.your_layout);
TextView tvGmail = (TextView) findViewById(R.id.tvGmail);
String TAG = "yourLogCatTag";
tvGmail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View viewIn) {
try {
Log.d(TAG,"GMAIL account selected");
} catch (Exception except) {
Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage());
}
}
});
the text view is declared like this (default wizard):
<TextView
android:id="#+id/tvGmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/menu_id_google"
android:textSize="30sp" />
and in the strings.xml file
<string name="menu_id_google">Google ID (Gmail)</string>
Although you can resolve the problem by setting the listener to textview, it's recommended not to. You should use flat button as it is a subclass of Button and it provides many attributes which TextView doesn't.
To use flat button, add style="?android:attr/borderlessButtonStyle" attribute -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?android:attr/borderlessButtonStyle"/>
in textView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:onClick="onClick"
android:clickable="true"
You must also implement View.OnClickListener and in On Click method can use intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
I tested this solution works fine.
To click on a piece of the text (not the whole TextView), you can use Html or Linkify (both create links that open urls, though, not a callback in the app).
Linkify
Use a string resource like:
<string name="links">Here is a link: http://www.stackoverflow.com</string>
Then in a textview:
TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);
Html
Using Html.fromHtml:
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
Then in your textview:
textView.setText(Html.fromHtml(getString(R.string.html)));
You can use TextWatcher for TextView, is more flexible than ClickLinstener (not best or worse, only more one way).
holder.bt_foo_ex.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// code during!
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// code before!
}
#Override
public void afterTextChanged(Editable s) {
// code after!
}
});

Categories

Resources