Friday, April 4, 2014

How to set text text color image in Android using Java

This is first simple android application and in this application, you will learn how to set text, text color and image using Java. Create new project and open XML file and drag linear layout and take text view and image view from widgets and drop into linear layout. Give id textview1 to text view and imageview1 to image view. To change background color of layout use this code in XML:  android:background=”Color_Value” in linear layout tag. Android supports RGB, ARGB, RRGGBB, AARRGGBB color code. The code of android XML file is given below:


How to set text, text color, image in Android using Java



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="#004444" >
<TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="30sp"
  android:textColor="#000"
  android:text="saturday"
/>
<ImageView
  android:id="@+id/imageView1"
  android:layout_width="200dp"
  android:layout_height="200dp"

/>
</LinearLayout>



Now open your Java file and initialize text view and image view object.
To set text on text view use: textview_object.setText(“Any String”);
To set color on text use: textview_object.setTextColor(Color_name);
To set image first drop any image to any drawable folder and use this code to set image on image view: imageview_object.setImageResource(R.drawable.image_name);
The code of android Java file is given below:


package com.example.checkblogapp; //your package name


import android.os.Bundle;

import android.widget.ImageView;
import android.widget.TextView; 
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //set layout
  setContentView(R.layout.activity_main);
  //initialize text view object
  TextView tv=(TextView)findViewById(R.id.textView1);
  //set text color
  tv.setTextColor(Color.RED);
  //set text
  tv.setText("This is my first app");
  //initialize image view object
  ImageView im=(ImageView)findViewById(R.id.imageView1);
  //set image resource
  im.setImageResource(R.drawable.myimage);
  }
}



Now runs your project and if have any doubt please comment. For more related to Android tutorials see List of Android Tutorials. Thanks... :)

rerea

About rerea

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :