13

I am very new to Android programming, and I have read everywhere and I can't seem to find any solution.

Basic problem is that I have a TextView in a widget and I would like the text to scroll when the text is longer than the TextView layout_width. This is my code in the layout_widget.xml

    <TextView android:id="@+id/fact" android:layout_width="200dp"
            android:text="Loading... More text to see if it spans or not and want more"
            android:singleLine="true" 
            android:ellipsize="marquee"
            android:marqueeRepeatLimit ="marquee_forever"
            android:scrollHorizontally="true"
            android:focusable="true"
            android:focusableInTouchMode="true" />

Now I read that I have to make the TextView to be on focus, which I have done. I have also read that you need to set the property setSelected(true), and this is where I am struggling to set. In my default Activity (in the AndroidManifest.xml) I have this following code.

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.widget_layout);
                findViewById(R.id.fact).setSelected(true);
                setContentView(R.layout.main);
            }

The part below is used to set the Content to the widget_layout.xml and then set the TextView property for setSelected to true

                setContentView(R.layout.widget_layout);
                findViewById(R.id.fact).setSelected(true);

I then return the ContentView back to main.xml

Now I am guessing this is wrong and this is not the way it should be done. But I am wondering if it can be done. I also read that if you can override the Framework, you can put your own properties in, for example ScrollView, is this right as well? Also I am on SDK Version 7.

I much appreciate the help I receive, thanks all!

Edit: By removing setContentView(R.layout.main); when launching the application via the app draw, the text does scroll, but the widget doesn't. Kind of leads me to that a widget cannot have a marquee??? Has anyone got a marquee working on a widget??

Edit2: Solved. This is how it is done

In the xml for the text view you need to have a tag This basically I think is the same as getSeleted(true);

So the code should be as followed:

    <TextView android:id="@+id/fact" android:layout_width="200dp"
            android:text="Loading... More text to see if it spans or not and want more"
            android:singleLine="true" 
            android:ellipsize="marquee"
            android:marqueeRepeatLimit ="marquee_forever"
            android:scrollHorizontally="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:duplicateParentState="true">
        <requestFocus android:focusable="true" android:focusableInTouchMode="true"
            android:duplicateParentState="true" />
    </TextView>
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Springy
  • 588
  • 5
  • 15

2 Answers2

32

Solved. This is how it is done

In the xml for the text view you need to have a tag This basically I think is the same as getSeleted(true);

So the code should be as followed:

<TextView android:id="@+id/fact" android:layout_width="200dp"
        android:text="Loading... More text to see if it spans or not and want more"
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:duplicateParentState="true">
    <requestFocus android:focusable="true" android:focusableInTouchMode="true"
        android:duplicateParentState="true" />
</TextView>
Springy
  • 588
  • 5
  • 15
0

You are calling setContentView twice:

setContentView(R.layout.widget_layout);
findViewById(R.id.fact).setSelected(true);
setContentView(R.layout.main);

Your activity can only have one layout. If widget_layout is the layout for your widget that includes the text view, then you don't want the second setContentView.

goto10
  • 4,370
  • 1
  • 22
  • 33
  • Yes I know. From how I understand (and probably wrong I might say), when you click on application on the app draw, this opens the Activity that is linked to the AndroidManifest. Now in this Activity I want it to use main.xml. The problem I have is that I need to use an Activity class where onCreate() is called where it is looking at the layout_widget.xml content. That's why i called the layout_widget first so i can set the property for layout_widget, and then revert it back to the content. I know it doesn't sound right but I don't know how to do it anyother way... – Springy Oct 23 '11 at 20:26
  • Hi, I worked out the issue. Posting. Editing on the post above on how this is achievable. – Springy Oct 29 '11 at 21:18