Search Tutorials

Loading...

Sunday, 15 September 2013

Set text, text color, image in Android using Java

This is first simple android application and in this application, you all 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 problem with the above code than please comment.

Related Tutorials:-

Print numbers from 1 to 100 in ScrollView

Change image when we click on buttons

Change image when we click on a single button

Take numbers from EditText and add them and display on TextView

Make Temperature conversion application

12 comments:

  1. i have one more doubt
    please explain me the sqlite database using in android select command

    ReplyDelete
    Replies
    1. Please see the list of android tutorials..you will find all projects.
      http://innosen.blogspot.in/p/android.html
      or see only SQLite tutorial: http://innosen.blogspot.com/2013/10/how-to-use-sqlite-database-in-android.html

      Delete
  2. hi sir can u post google map example in ur blog.
    thanks deepak

    ReplyDelete
    Replies
    1. yes I will post Google map example soon..

      Delete
  3. A very useful blog Thanks sir............
    I am a beginner in android programming
    Can u suggest some guidelines to become a good android developer???
    Thanks padmapriya

    ReplyDelete
    Replies
    1. Thanks for liking this blog...read all android post on this blog and if you have any doubt than comment.

      Understand every line and try other website post also.

      Delete
  4. hi sir, i have installed android 4.4.2 i wrote the above code but when i run, it gives "unfortunatly the TestProject has stopped" Please help sir i am new to android..............

    ReplyDelete
  5. it's good for beginners.But why don't you change the color of a layout with the help of creating id to linearlayout

    ReplyDelete
  6. hiii sir
    I am a beginner in android programming your blog is very helpful for me
    Can u suggest some guidelines to become a good android developer??

    ReplyDelete
  7. hello sir,
    post the list of softwares for developing android apps.

    ReplyDelete
  8. hello sir i am having one doubt about adding text on image like add text to photo app..i want only thing add text on image and it will resizable and rotatable.need your help please help me

    ReplyDelete
  9. It's really an amazing and nice blog,
    image uploader

    ReplyDelete

Back to Top