0

I have a dialog popping up with a title and a close button. This is the XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:maxLines="1"
        <!--- android:singleLine="true" <-- Do not comment about this, it is deprecated --->
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        />

    <ImageView
        android:id="@+id/backburger"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/ic_close"
        android:foreground="?android:attr/selectableItemBackground"
        />

</LinearLayout>

Like you can see the TextView has all the things it needs for a marquee, the singleLine options is deprecated, so I can't use it. I do call setSelected(true) in my Java. There are lots of questions about a marquee, but none with layout_weight and from '17.

This is the preview of the XML:

Output

Roy Berris
  • 1,502
  • 1
  • 17
  • 40
  • Check my answer here https://stackoverflow.com/a/46360549/5381331. I have tested it with layout_weight so this problem is not relevant to `android:layout_weight`. Also see https://stackoverflow.com/a/12005799/5381331 – Linh Sep 22 '17 at 08:52

2 Answers2

1

Try this one: In your Xml:

<TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
         android:textSize="20sp"
        android:textColor="@android:color/black" />

and your Code:

tv = (TextView) this.findViewById(R.id.text_title);  
tv.setSelected(true);

Hope its help:)

0
public void setticker(String text, TextView view) {
    if (text != "") {


        view.setText(text);
        //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);
    }
}
Dhan Shah
  • 9
  • 1
  • So this will replace the marquee with a custom animator? Only one problem, I have a `MaterialDialog` which the `TextView` will be in, this means that your `float screenWidth` will be too wide. – Roy Berris Sep 22 '17 at 08:37
  • yes it will be too wide.Instead of screen width try calculating it with dialog width – Dhan Shah Sep 22 '17 at 10:09