An activity is the place where user interacts with your android app. For each screen of your app, there will be an activity.
Write your first activity
Select File - New - Class
from the menu
and fill in the details
Select the superclass as android.app.Activity.
Your empty activity is ready. But what do you display in this activity?
Views to be used in an activity can be written using java or xml file. Let us a simple TextView.
Let us write the code now.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Welcome to android");
tv.setTextColor(Color.BLUE);
setContentView(tv);
}
Now run the app on the emulator. Do you see your activity? Of course not. You have not called it.
To call this activity, either call it using an intent from your main activity or make this as the main activity.
When and how do you call this activity? May be on click of a button in our hello screen. So let us add a button to our first hello world activity.
Button b1 = new Button(this);
b1.setText("Click");//how innovative
b1.setOnClickListener(this);
this.addContentView(b1,new LayoutParams(LayoutParams.match_parent, LayoutParams.wrap_content));
Add these lines at the end of your earlier activity (MainActivity.java) onCreate method. When you see an error in Button, import android.widget.button. And add implements OnClickListener to the title of MainActivity class
Now IDE has added an empty OnClick method in the class. Complete that method.
Intent i = new Intent(this, Myactivity.class);
startActivity(i);
To start a new activity, we use intent. Second parameter to Intent constructor is the class name of new activity.
After adding these two lines to onClick method, you are asking the program to start new activity onclick of the button. Run the app now. What do you see?
Program crashed , right? OK, any activity to be used in the application must be specified in the android manifest file. As we did not add this activity in the android manifest file, we got error.
Let us add the line
<activity android:name="com.example.hello.Myactivity"></activity>
just before </application> in AndroidManifest.xml
Now you run the application again and click on the button. You will see your activity finally. :)
Layout files
It is not feasible to write code for complicated screen with multiple views. Hence you can define your screen layout in an xml file.
To create such a layout xml file,use
File - New - android xml file
Make sure the file is of the type .xml and it is in /res/layout folder. You will see a graphical layout. Drag and drop the required widgets in to the layout.
I have created myscreen.xml and added two textviews to the screen, second one center aligned. Here is how xml file looks like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView" />
</LinearLayout>
To use this screen for our activity, we should set the content view to the above xml file.
In the onCreate method, change setContentView(tv) to setContentView(R.layout.myscreen)
Accessing the views from xml file
If you want to access the widgets mentioned in xml file, you should access them using findViewById. For each resource used in the application, a unique resource id is created. Hence our first textview in the xml file has resource id of R.id.textView1 where textView1 is taken from android:id.
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Welcome");
TextView tv2 = (TextView)findViewById(R.id.textView2);
tv2.setText("to android");
tv1.setTextColor(Color.BLUE);
Write your first activity
Select File - New - Class
from the menu
and fill in the details
Select the superclass as android.app.Activity.
Your empty activity is ready. But what do you display in this activity?
Views to be used in an activity can be written using java or xml file. Let us a simple TextView.
Let us write the code now.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Welcome to android");
tv.setTextColor(Color.BLUE);
setContentView(tv);
}
Now run the app on the emulator. Do you see your activity? Of course not. You have not called it.
To call this activity, either call it using an intent from your main activity or make this as the main activity.
When and how do you call this activity? May be on click of a button in our hello screen. So let us add a button to our first hello world activity.
Button b1 = new Button(this);
b1.setText("Click");//how innovative
b1.setOnClickListener(this);
this.addContentView(b1,new LayoutParams(LayoutParams.match_parent, LayoutParams.wrap_content));
Add these lines at the end of your earlier activity (MainActivity.java) onCreate method. When you see an error in Button, import android.widget.button. And add implements OnClickListener to the title of MainActivity class
Now IDE has added an empty OnClick method in the class. Complete that method.
Intent i = new Intent(this, Myactivity.class);
startActivity(i);
To start a new activity, we use intent. Second parameter to Intent constructor is the class name of new activity.
After adding these two lines to onClick method, you are asking the program to start new activity onclick of the button. Run the app now. What do you see?
Program crashed , right? OK, any activity to be used in the application must be specified in the android manifest file. As we did not add this activity in the android manifest file, we got error.
Let us add the line
<activity android:name="com.example.hello.Myactivity"></activity>
just before </application> in AndroidManifest.xml
Now you run the application again and click on the button. You will see your activity finally. :)
Layout files
It is not feasible to write code for complicated screen with multiple views. Hence you can define your screen layout in an xml file.
To create such a layout xml file,use
File - New - android xml file
Make sure the file is of the type .xml and it is in /res/layout folder. You will see a graphical layout. Drag and drop the required widgets in to the layout.
I have created myscreen.xml and added two textviews to the screen, second one center aligned. Here is how xml file looks like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView" />
</LinearLayout>
To use this screen for our activity, we should set the content view to the above xml file.
In the onCreate method, change setContentView(tv) to setContentView(R.layout.myscreen)
Accessing the views from xml file
If you want to access the widgets mentioned in xml file, you should access them using findViewById. For each resource used in the application, a unique resource id is created. Hence our first textview in the xml file has resource id of R.id.textView1 where textView1 is taken from android:id.
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Welcome");
TextView tv2 = (TextView)findViewById(R.id.textView2);
tv2.setText("to android");
tv1.setTextColor(Color.BLUE);