Adding gradient effect to TextView in a ListView generates NPE
Asked Answered
E

1

0

I've tried adding gradient effect to TextView. But it generates a NPE(I felt the id to was the culprit, but it wasn't so).

My Activity:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        TextView textView = (TextView)findViewById(R.id.label);        
        Shader textShader=new LinearGradient(0, 0, 0, 20,
                new int[]{Color.GREEN,Color.BLUE},
                new float[]{0, 1}, TileMode.CLAMP);
        textView.getPaint().setShader(textShader);


        // storing string resources into Array
        String[] menulist = getResources().getStringArray(R.array.menulist);      

        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, menulist));



    }

My XML:

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" >
</TextView>

I feel I'm missing something obvious.

If there's any other way to attain a list with a gradient color effect, feel free to suggest.

Elapse answered 16/1, 2013 at 15:22 Comment(0)
E
2

Try this :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    final Shader shader = new LinearGradient(0, 0, 0, 20,
            new int[]{Color.GREEN,Color.BLUE},
            new float[]{0, 1}, Shader.TileMode.CLAMP
    );
    // storing string resources into Array
    final String[] menulist = getResources().getStringArray(R.array.menulist);

    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, menulist) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final View view = super.getView(position, convertView, parent);
            final TextView label = (TextView) view.findViewById(R.id.label);

            label.getPaint().setShader(shader);
            return view;
        }
    });
}
Exacerbate answered 16/1, 2013 at 15:32 Comment(4)
Still getting a NPE at line : label.getPaint().setShader(shader);Elapse
And interestingly enough, my version was giving a NPE at textView.getPaint().setShader(textShader);Elapse
ooops, my bad =( It should be view.findViewById instead of findViewById. I've updated my answerExacerbate
It works! I'll upvote and maybe accept. But funnily enough, it doesn't meet my purpose. The text has a gradient, I wanted the background of the text to have a gradient. Sigh!Elapse

© 2022 - 2024 — McMap. All rights reserved.