Activate marquee in a textview from code

3

Continuing with my new project, I intend to make that when they click on the corresponding textview start to make marquee , and when you press another one, stop doing it. p>

I've been looking for several hours and I only find ways to do it from the xml to be marquee all the time ... what could I do? I enclose the xml and the activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TabHost android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">

        <TabWidget android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs" />

        <FrameLayout android:layout_width="match_parent"
            android:layout_height="437dp"
            android:id="@android:id/tabcontent"
            android:layout_weight="0.62">

            <LinearLayout android:id="@+id/tab1"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="68dp"
                    android:background="#000000">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <LinearLayout
                            android:orientation="horizontal"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center"
                            android:id="@+id/layout1">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/txtl1b"
                                android:textStyle="bold"
                                android:textColor="#ffde07"
                                android:textSize="50dp"
                                android:singleLine="true"/>
                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="L1: "
                        android:id="@+id/textView6" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editl1b" />
                </LinearLayout>

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

                    <RadioGroup
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:gravity="center" >

                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Izqda."
                            android:id="@+id/rdizql1b"
                            android:checked="false"
                            android:onClick="centrarizql1" />

                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Centrado"
                            android:id="@+id/rdcenl1b"
                            android:checked="false"
                            android:onClick="centrarl1" />

                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Dcha."
                            android:id="@+id/rdderl1b"
                            android:checked="false"
                            android:onClick="centrarderl1" />

                    </RadioGroup>
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="40dp" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="MEMORIA Nº: "
                        android:id="@+id/textView9" />

                    <Spinner
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/spinner4"
                        android:entries="@array/memorias"/>

                </LinearLayout>

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center">

                    <Button
                        android:layout_width="204dp"
                        android:layout_height="wrap_content"
                        android:text="ENVIAR"
                        android:id="@+id/button2"
                        android:onClick="prueba" />

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Button"
                        android:id="@+id/button6"
                        android:layout_gravity="center_horizontal" />
                </LinearLayout>

            </LinearLayout>

            <LinearLayout android:id="@+id/tab2"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView android:id="@+id/textView2"
                    android:text="Contenido Tab 2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout android:id="@+id/tab3"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView android:id="@+id/textView3"
                    android:text="Contenido Tab 3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </FrameLayout>

    </LinearLayout>
</TabHost>
</LinearLayout>
package com.example.oftecnica2.tabsbt;

import android.content.res.Resources;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity{

    EditText editl1;
    TextView txtl1;
    RadioButton rdizql1;
    RadioButton rdderl1;
    RadioButton rdcenl1;
    LinearLayout layout1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/mifuente.ttf");

        editl1=(EditText)findViewById(R.id.editl1b);
        txtl1=(TextView)findViewById(R.id.txtl1b);
        txtl1.setTypeface(tf);

        rdizql1=(RadioButton)findViewById(R.id.rdizql1b);
        rdderl1=(RadioButton)findViewById(R.id.rdderl1b);
        rdcenl1=(RadioButton)findViewById(R.id.rdcenl1b);
        layout1=(LinearLayout)findViewById(R.id.layout1);
        editl1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                txtl1.setText(editl1.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        Resources res = getResources();
        TabHost tabs=(TabHost)findViewById(android.R.id.tabhost);
        tabs.setup();
        TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
        spec.setContent(R.id.tab1);
        spec.setIndicator("MESSAGES",
                res.getDrawable(android.R.drawable.btn_plus));
        tabs.addTab(spec);
        spec=tabs.newTabSpec("mitab2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("MEMORIES",
                res.getDrawable(android.R.drawable.ic_dialog_map));
        tabs.addTab(spec);
        spec=tabs.newTabSpec("mitab3");
        spec.setContent(R.id.tab3);
        spec.setIndicator("SETTINGS",
                res.getDrawable(android.R.drawable.ic_dialog_map));
        tabs.addTab(spec);
        tabs.setCurrentTab(0);
    }

    public void centrarizql1(View view){

        layout1.setGravity(Gravity.LEFT | Gravity.CENTER);
        System.out.println("XXXXXXXX");

    }
    public void centrarderl1(View view){
        layout1.setGravity(Gravity.RIGHT| Gravity.CENTER);
    }
    public void centrarl1(View view){
        layout1.setGravity(Gravity.CENTER);
    }

    public void prueba(View view){
        editl1.setSelected(false);
        txtl1.setSelected(true);
    }
}
    
asked by Sergio Cv 08.07.2016 в 13:20
source

2 answers

2

You can programmatically do it this way:

miTextView = (TextView) this.findViewById(R.id.textview);
miTextView.setSelected(true);
miTextView.setEllipsize(TruncateAt.MARQUEE);
miTextView.setSingleLine(true);

It is important miTextView.setSelected(true); since in this way the TextView gets the focus and forces the TextView to perform the marquee .

To do it from the layout simply add to your widget TextView the properties:

android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"    
android:lines="1"

To get something like this:

    
answered by 08.07.2016 в 13:49
1

By doing it programmatically I show you a complete example, I really do not know if it works, I did not try it. Try placing false to setSelected and setSingleLine to stop the marquee :

  public void iniciar(){
    txt1.setEllipsize(TruncateAt.MARQUEE);
    txt1.setText("Lorem ipsum... dolor Lorem... Ipsum dolor");
    txt1.setSelected(true);
    txt1.setSingleLine(true);
    }

    public void parar(){
    txt1.setEllipsize(null);
    txt1.setSelected(false);
    txt1.setSingleLine(false);
    }




botonIniciar.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       inciar();
    }
});

botonParar.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        parar();
    }
});
    
answered by 08.07.2016 в 16:27