Marquee text in Android
Here is an example:
public class TextViewMarquee extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.mywidget);
tv.setSelected(true); // Set focus to the textview
}
}
The xml file with the textview:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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" />
</RelativeLayout>
Textview marquee programmatically
Try this.
final RelativeLayout relativeLayout = new RelativeLayout(this);
final RelativeLayout relativeLayoutbotombar = new RelativeLayout(this);
textView = new TextView(this);
textView.setId(1);
RelativeLayout.LayoutParams relativlayparamter = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams relativlaybottombar = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutbotombar.setLayoutParams(relativlaybottombar);
textView.setText("text text text text text, with a long ");
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setSelected(true);
textView.setSingleLine(true);
relativeLayout.addView(relativeLayoutbotombar);
relativeLayoutbotombar.addView(textView);
setContentView(relativeLayout, relativlayparamter);
Another option:
TextView textView = (TextView) this.findViewById(R.id.xxx);
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("Text text text text");
textView.setSelected(true);
textView.setSingleLine(true);
TextView Marquee not working
working now :)
Code attached below
<TextView
android:text="START | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | END"
android:id="@+id/MarqueeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true">
Edit (on behalf of Adil Hussain):
textView.setSelected(true)
needs to be set in code behind for this to work.
Android TextView Marquee programmatically
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:marqueeRepeatLimit="marquee_forever"
android:text=""/>
TextView textView = (TextView)findViewById(R.id.TextView03);
textView.setText(";lkjdf;alkdsfjoiawejrokajdsl;fkadmnsflkamndvklahoreiuaweifjakldsmflkhfgoueyhtoaijpkjdflkahndsofuhyoeia");
textView.setHorizontallyScrolling(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine();
textView.setSelected(true);
Marque text not moving if texts are less in android
You should call TextView.isSelected = true to start the text moving.
Edit: Working solution
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="@android:color/white" />
Extension function
fun TextView.setMovingText(text: String) = this.post {
val textWidth = this.paint.measureText(text)
val spaceWidth = this.paint.measureText(" ")
val requiredAdditionalSpace = ((this.width - textWidth) / spaceWidth).toInt()
this.text = StringBuilder(text).apply {
for (i in 0..requiredAdditionalSpace) {
append(" ")
}
}
this.isSelected = true
}
How to use
textView.setMovingText("Hey")
I just wanted to show a working solution. Also you can (should) modify for better performance and check for negative values.
How to create a marquee effect for a text of smaller length not exceeding the screen size in Android?
I used simple light weight of ticker like animation which I developed in my early Android days.
below is complete code.
Hope this helps.
Works for all API levels of Android
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setticker("Hello", this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void setticker(String text, Context contx) {
if (text != "") {
LinearLayout parent_layout = (LinearLayout) ((Activity) contx)
.findViewById(R.id.ticker_area);
TextView view = new TextView(contx);
view.setText(text);
view.setTextColor(Color.BLACK);
view.setTextSize(25.0F);
Context context = view.getContext(); // gets the context of the view
// measures the unconstrained size of the view
// before it is drawn in the layout
view.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
// takes the unconstrained width of the view
float width = view.getMeasuredWidth();
float height = view.getMeasuredHeight();
// gets the screen width
float screenWidth = ((Activity) context).getWindowManager()
.getDefaultDisplay().getWidth();
view.setLayoutParams(new LinearLayout.LayoutParams((int) width,
(int) height, 1f));
System.out.println("width and screenwidth are" + width + "/"
+ screenWidth + "///" + view.getMeasuredWidth());
// performs the calculation
float toXDelta = width - (screenWidth - 0);
// sets toXDelta to -300 if the text width is smaller that the
// screen size
if (toXDelta < 0) {
toXDelta = 0 - screenWidth;// -300;
} else {
toXDelta = 0 - screenWidth - toXDelta;// -300 - toXDelta;
}
// Animation parameters
Animation mAnimation = new TranslateAnimation(screenWidth,
toXDelta, 0, 0);
mAnimation.setDuration(15000);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
view.setAnimation(mAnimation);
parent_layout.addView(view);
}
}
}
in activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/ticker_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#9CB1DD"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
Related Topics
Android Lollipop Change Navigation Bar Color
Android Asynctask Example and Explanation
Android Asynctask Threads Limits
How to Set Background Color of a View
Recyclerview Horizontal Scroll Snap in Center
Google Play Services Library Update and Missing Symbol @Integer/Google_Play_Services_Version
How to Display Inline Images from HTML in an Android Textview
How to Set a Reminder in Android
Storing R.Drawable Ids in Xml Array
Asynctask VS Thread in Android
Broadcastreceiver Not Receiving Boot_Completed
Android: How to Check If Activity Is Running
How to Startforeground() Without Showing Notification
Android 4.3 Menu Item Showasaction="Always" Ignored
Force Application to Restart on First Activity