Sunday, July 22, 2012

Make a New Line , Space and Displaying text with different color



Click Here to download source code

Package Name     :    selva.textbox

Project Name       :    TextBox

Version                :     1.5 ( Supports 1.5 and above versions)



main.xml


 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />
  
     <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />
  

</LinearLayout>



 
TextBoxActivity.java


package selva.textbox;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextBoxActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button ok=(Button) findViewById(R.id.button1);
        final TextView tv=(TextView) findViewById(R.id.textView1);
       
        Button ok1=(Button) findViewById(R.id.button2);
        final TextView tv1=(TextView) findViewById(R.id.textView2);
       
        ok.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String str="This is first line in textview1\nThis is\tsecond line in textview1";
                tv.setText(str);
               
            }
        });
       
       
ok1.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String str1 = "<font color=#00cc00>First line is green color in textview2</font> <br />" +
                        " <font color=#ff0000>Second line is red color in textview2</font>";

                tv1.setText(Html.fromHtml(str1));
               
            }
        });
       
    }
}


OUTPUT :










  click "click Me" buttons











Click Here to download source code



No comments:

Post a Comment