Soni Kumar
Soni Kumar

Reputation: 121

How to change the style and color of edittext content on button click at run time

I am developing a cloth design android application. In which i want to change the style of text of edit text on required button click and same thing for text color.

My xml file is.......

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/mvieww"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <EditText
        android:layout_centerInParent="true"
        android:id="@+id/etext"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:textSize="15dp"
        android:layout_gravity="center"/>

    <ImageView
        android:id="@+id/icon"
        android:layout_centerInParent="true"
        android:layout_width="50dp"
        android:layout_height="50dp" />
</RelativeLayout>

please help me........

Upvotes: 0

Views: 1666

Answers (3)

Rustam
Rustam

Reputation: 6515

Try this it will update the style as well as color of your EditText text when you click yourButton

 yourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              yourEditText.setText(Html.fromHtml("<font color=red><b>"+yourEditText.getText().toString()+"</b></font>"));// set style and color  

            }
        });

Upvotes: 0

King of Masses
King of Masses

Reputation: 18775

I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is a template"
        style="@style/my_style" />


EditText myText = (EditText)getLayoutInflater().inflate(R.layout.tvtemplate, null);

Hope this helps :)

Upvotes: 0

Henry98
Henry98

Reputation: 571

To set the color, use :

 textView.setTextColor(Color.SOMECOLOR);

To set the font, use :

textView.setTypeFace(SOMEFONT);

If you don't actually know how to set it to the click of a button, then here's how you do it:

First, make a Button. I'll just assume you named it button. Set it via findViewById(R.whateverTheIdOfTheButtonIs)

Then, you will need to do :

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Set the text color or font here
            }
        });

Upvotes: 1

Related Questions