Skip to content Skip to sidebar Skip to footer

Setting Color For Textview Dynamically Using Spinner?

I want to create one textview, the colour of that text is to be changed by the user by selecting colour from spinner object. The spinner object contains list of colours, that is op

Solution 1:

Try this way,hope this will help you to solve your problem.

activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"><TextViewandroid:id="@+id/textview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"/><Spinnerandroid:id="@+id/spinner"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"/></LinearLayout>

strings.xml

<arrayname="colorname"><item>Red</item><item>Yellow</item><item>Green</item><item>Blue</item><item>Pink</item></array><arrayname="colorcode"><item>#FF0000</item><item>#ffff00</item><item>#00ff00</item><item>#0000ff</item><item>#FF0080</item></array>

MainActivity.java

publicclassMainActivityextendsActivity {

    privateSpinner spinner;
    privateTextView textview;
    privateArrayList<String> colorNameList;
    privateArrayList<String> colorCodeList;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview = (TextView) findViewById(R.id.textview);
        spinner = (Spinner) findViewById(R.id.spinner);

        colorNameList = newArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.colorname)));
        colorCodeList = newArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.colorcode)));

        final ArrayAdapter<String> dataAdapter = newArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, colorNameList);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);

        spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
            @OverridepublicvoidonItemSelected(AdapterView<?> parent, View view, int position, long id) {
                textview.setTextColor(Color.parseColor(colorCodeList.get(position)));
            }

            @OverridepublicvoidonNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}

Solution 2:

YourSpinner.setOnItemSelectedListener(newOnItemSelectedListener() {
        @OverridepublicvoidonItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
        {
           YourTextView.setTextColor(getResources().getColor(R.color.some_color));
            // your code here

        }

        @OverridepublicvoidonNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

It would be nice if you've tried searching on Stack Site.

Solution 3:

publicclassMainActivityextendsActionBarActivity {

private TextView text;
private Spinner spin;
ArrayList al;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.textView1);
spin=(Spinner)findViewById(R.id.spinner1);
al=newArrayList();
al.add("RED");
al.add("BLUE");
ArrayAdapter adapter=newArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_activated_1,al);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(newOnItemSelectedListener() {

    @OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stubswitch(arg2)
        {
        case0:
            text.setText("srikanth");
            text.setTextColor(Color.RED);
            break;
        case1:
            text.setText("SRIkANTH BLUE");
            text.setTextColor(Color.BLUE);
            break;

        }

    }

    @OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});


}

}

I hope it helps you

Post a Comment for "Setting Color For Textview Dynamically Using Spinner?"