a>
WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Android textview with android studio



TextView displays user to text and allow to edit.Textview is a text editor we can edit it

Attributes of TextView:


we are learn abot TextView in your xml file.

1. Id: Id is an attribute use for identify a text view. Simple code is given ;

<TextView
android:id="@+id/firstTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

2. gravity: The gravity attribute is a optional attribute use for control the alignment of the text center, top,left, right, bottom, center_vertical, center_horizontal etc.

Below is the example code with explanation included in which we set the center_horizontal gravity for text of a TextView.

<TextView
android:id="@+id/firstTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="WelookupsAndroid"
android:textSize="25sp"
android:gravity="center_horizontal"/> <!--center horizontal gravity-->

Gravity in TextView Android
3. text: text attribute is used for set the text in a text view. WE need to xml as well as in the java class.

Simple code we set the text “WelookupsAndroid” in a text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:text="WelookupsAndroid"/><!--Display Text as WelookupsAndroid-->

text attribute in TextView Android
In Java class:

simple code is given in which we set the text in a textview in java class.

TextView textView = (TextView)findViewById(R.id.textView);
textView.setText("WelookupsAndroid"); //set text for text view

4. textColor: textColor attribute is used for set the text color of a text view. Color value is in the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.

Below is the example code with explanation included in which we set the red color for the displayed text.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welookupsAndroid"
android:layout_centerInParent="true"
android:textSize="18sp"
android:textColor="#f00"/><!--red color for text view-->

textcolor in TextView
In Java class:

simple code is given in which we set the text color of a text view in java class.

TextView textView = (TextView)findViewById(R.id.textView);
textView.setTextColor(Color.RED); //set red color for text view

5. textSize: textSize attribute is used to set the size of text of a text view. We can set the text size in sp(scale independent pixel) or dp(density pixel).

simple code in which we set the sp size for the text of a text view.

<TextView
    android:id="@+id/simpleTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="WelookupsAndroid"
    android:layout_centerInParent="true"
    android:textSize="40sp" /><!--Set size-->

textsize attribute in TextView
In Java class:

simple code is given in which we set the text size of a text view in java class.

TextView textView = (TextView)findViewById(R.id.textView);
textView.setTextSize(20); //set 20sp size of text

6. textStyle: textStyle attribute is used to set the text style of a text view. The possible text styles are bold, italic and normal.  If we need to use two or more styles for a text view then “|” operator is used for that.

simple code in which we  set the bold and italic text styles for text.

<TextView
    android:id="@+id/simpleTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="WelookupsAndroid"
    android:layout_centerInParent="true"
    android:textSize="50sp"
    android:textStyle="bold|italic"/><!--bold and italic text style of text-->

textStyle in TextView
7. background: background attribute is used to set the background of a text view. We can set a color or a drawable in the background of a text view.

8. padding: padding attribute is used to set the padding from left, right, top or bottom. In above example code of background we also set the 10dp padding from all the side’s of text view.

simple code with explanation included in which we set the black color for the background, white color for the displayed text and set 50dp padding from all the side’s for text view.

<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WelookupsAndroid"
android:layout_centerInParent="true"
android:textSize="40sp"
android:padding="10dp"
android:textColor="#fff"
android:background="#000"/> <!--red color for background of text view-->

padding and background attribute in TextView
In Java class:

Below is the example code in which we set the background color of a text view programmatically means in java class.

TextView textView = (TextView)findViewById(R.id.textView);
textView.setBackgroundColor(Color.BLACK);//set background color