Ashwin Shirva
Ashwin Shirva

Reputation: 1441

TextView stops scrolling horizontally when EditText gets focus in Android

Horizontal scrolling problem

<TextView
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView"
        android:scrollHorizontally="true"
        android:text="@string/adtext"
        android:textColor="#000000"
        android:singleLine="true"/>

I have a single line text view which scrolls horizontally in the screen. And i have a edit text view above it. My horizontal scroll is working fine and i have no problems with it.
But my problem is whenever i press on the edit text (i.e when edit text gets the focus), my text view stops moving horizontally until i restart my activity !!!
What can i do? Please help.
Thanks in advance.

Upvotes: 3

Views: 1270

Answers (3)

AndroidHacker
AndroidHacker

Reputation: 3596

Use HorizontalScroll view instead. This will not be affected by your focus on EditText.

<HorizontalScrollView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    android:id="@+id/hrz_scroll"
    android:layout_weight="1" >

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="your text view" />

</HorizontalScrollView>

If focus is your main concern on TextView, then try:

textview[i].requestFocus();

Upvotes: 1

user3019580
user3019580

Reputation: 11

Only one of these is needed to make the TextView a single line. (android:scrollHorizontally or android:singleLine). The attribute singleLine is deprecated, so scrollHorizontally is preferred.

There are four ways (that I know of) to set focus to a TextView. One of them is using requestFocus() in Java. If you use this option to set focus, then you would need android:focusable = "true" and android:focusableInTouchMode = "true". The other three options should not include these two attributes.

The two examples below use either scrollHorizontally or singleLine, with the 4 ways to set focus to the TextView.

Make a Scrolling Marquee, using scrollHorizontally, in a TextView

Deprecated: Make a Scrolling Marquee, using singleLine, in a TextView

Upvotes: 1

Rahul Gupta
Rahul Gupta

Reputation: 5295

Here do this. When i say it is working, i am not lying. Now there is no support of uploading a video here otherwise i would have shown you. See my full code below :-

MainActivity.java

package com.example.test2;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText edit = (EditText) findViewById(R.id.edit);
        //edit.clearFocus();

        //Now, in your MainActivity.class, add the following code:
            TextView txt = (TextView)findViewById(R.id.textview1);
            txt.setSelected(true);
    }

    @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;
    }

}

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:layout_width="350dp"
        android:layout_height="70dp"
        android:id="@+id/edit"
        android:layout_alignParentTop="true" 
        android:background="@android:color/background_dark"
        android:textColor="@android:color/white"/>

    <TextView
        android:id="@+id/textview1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="hdfhghdfhudjfhudjfhudjfbhdjkfvbjfdkvbjfdkbvhfjkdbvhjkfdbvhdfjkvbnfdkhjbv"
        android:textColor="#000000"
        android:textSize="30dp" />

</RelativeLayout>

Upvotes: 2

Related Questions