AndroidManifest.xml
, which includes the essential information about your app to the Android system, information the system must have before it can run any of the app’s code,
MainActivity.java
, which is the main activity in Java,
NextActivity.java
, which is the activity of the next page in Java,
activity_main.xml
, which is the layout page of the MainActivity.java
,
activity_next.xml
, which is the layout page of the NextActivity.java
,
menu_main.xml
, which is the menu page of the MainActivity.java
,
menu_next.xml
, which is the menu page of the NextActivity.java
, and
strings.xml
, which is the file containing string values.
Start ⇒ All Programs ⇒ Android Studio ⇒ Android Studio
HelloWorld
,wenchen.example.com
,com.example.wenchen.helloworld
(given), andC:\Android-Studio-workspace\HelloWorld
.Phone and Tablet
and API 15: Android 4.0.3 (IceCreamSandwich)
Empty View Activity
Blank Activity
.
MainActivity
,activity_main
,MainActivity
, andmenu_main
.Note that Android is constantly revised and backward compatibility is an issue. This application works on API 15: Android 4.0.3 (IceCreamSandwich) correctly. If a newer version is used and minor compatibility errors show up, you should be able to fix them based on the error messages. One nice resource for Android debugging is Stack Overflow.
AndroidManifest.xml
.app
of the project HelloWorld
in the left pane.
AndroidManifest.xml
and complete it such as:
C:\Android-Studio-workspace\HelloWorld\app\src\main\AndroidManifest.xml |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.wenchen.helloworld" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.HelloWorld" > <activity android:name =".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name =".NextActivity" android:label="Next Page" /> </application> </manifest> |
MainActivity.java
.java\com\example\wenchen\helloworld\MainActivity.java
and a template will be given as follows:
C:\Android-Studio-workspace\HelloWorld\app\src\main\java\com\example\wenchen\helloworld\MainActivity.java |
package com.example.wenchen.helloworld; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); } @Override public boolean onCreateOptionsMenu( Menu menu ) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater( ).inflate( R.menu.menu_main, menu ); return true; } @Override public boolean onOptionsItemSelected( MenuItem item ) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId( ); // noinspection SimplifiableIfStatement if ( id == R.id.action_settings ) { return true; } return super.onOptionsItemSelected( item ); } } |
MainActivity.java
such as
C:\Android-Studio-workspace\HelloWorld\app\src\main\java\com\example\wenchen\helloworld\MainActivity.java |
package com.example.wenchen.helloworld; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); final EditText name = (EditText) findViewById( R.id.name ); final Button button = (Button) findViewById( R.id.next ); button.setOnClickListener( new View.OnClickListener( ) { public void onClick( View v ) { /** Here i calls a new screen. **/ Intent i = new Intent( MainActivity.this, NextActivity.class ); i.putExtra( "name", name.getText().toString( ) ); startActivity( i ); } } ); } @Override public boolean onCreateOptionsMenu( Menu menu ) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater( ).inflate( R.menu.menu_main, menu ); return true; } @Override public boolean onOptionsItemSelected( MenuItem item ) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId( ); // noinspection SimplifiableIfStatement if ( id == R.id.action_settings ) { return true; } else if ( id == R.id.next ) { Intent i = new Intent( MainActivity.this, NextActivity.class ); startActivity( i ); } return super.onOptionsItemSelected( item ); } } |
NextActivity.java
.NextActivity.java
, by right clicking the mouse:
com.example.wenchen.helloworld ⇒ New ⇒ Java Class
NextActivity.java
such as
C:\Android-Studio-workspace\HelloWorld\app\src\main\java\com\example\wenchen\helloworld\NextActivity.java |
package com.example.wenchen.helloworld; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class NextActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_next ); final TextView tvView = (TextView) findViewById( R.id.tvView ); Intent intent = getIntent( ); String name = intent.getStringExtra( "name" ); tvView.setText( "Welcome, " + name ); final Button button = (Button) findViewById( R.id.home ); button.setOnClickListener( new View.OnClickListener( ) { public void onClick( View v ) { Intent i = new Intent( NextActivity.this, MainActivity.class ); startActivity( i ); } } ); } @Override public boolean onCreateOptionsMenu( Menu menu ) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater( ).inflate( R.menu.menu_next, menu ); return true; } @Override public boolean onOptionsItemSelected( MenuItem item ) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId( ); // noinspection SimplifiableIfStatement if ( id == R.id.action_settings ) { return true; } else if ( id == R.id.home ) { Intent i = new Intent( NextActivity.this, MainActivity.class ); startActivity( i ); } return super.onOptionsItemSelected( item ); } } |
activity_main.xml
.activity_main.xml
and complete it such as:
C:\Android-Studio-workspace\HelloWorld\app\src\main\res\activity_main.xml |
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" android:weightSum = "1" android:gravity = "center_vertical|center_horizontal"> <TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textAppearance = "?android:attr/textAppearanceLarge" android:text = "Hello, your name:" android:layout_weight = "0.07" android:textStyle = "bold" /> <EditText android:layout_width = "216dp" android:layout_height = "wrap_content" android:id = "@+id/name" /> <Button android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Next Page" android:id = "@+id/next" /> </LinearLayout> |
activity_next.xml
.activity_next.xml
, by selecting the following options:
layout ⇒ New ⇒ XML ⇒ layout XML file
”
C:\Android-Studio-workspace\HelloWorld\app\src\main\res\activity_next.xml |
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" android:weightSum = "1" android:gravity = "center_vertical|center_horizontal"> <TextView android:layout_width = "287dp" android:layout_height = "wrap_content" android:textAppearance = "?android:attr/textAppearanceLarge" android:id = "@+id/tvView" android:textStyle = "bold" android:layout_weight = "0.10" android:gravity = "center_horizontal" /> <Button android:layout_width = "wrap_content" android:layout_height = "40dp" android:text = "Home Page" android:id = "@+id/home" /> </LinearLayout> |
menu_main.xml
.menu_main.xml
and complete it such as:
C:\Android-Studio-workspace\HelloWorld\app\src\main\res\menu_main.xml |
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never" /> <item android:id="@+id/next" android:title="Next Page" android:orderInCategory="100" app:showAsAction="never" /> </menu> |
menu_next.xml
.C:\Android-Studio-workspace\HelloWorld\app\src\main\res\menu_next.xml |
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".NextActivity"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never" /> <item android:id="@+id/home" android:title="Home Page" android:orderInCategory="100" app:showAsAction="never" /> </menu> |
strings.xml
.C:\Android-Studio-workspace\HelloWorld\app\src\main\res\values\strings.xml |
<resources> <string name="app_name">HelloWorld</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> </resources> |
Build ⇒ Rebuild Project
Tools ⇒ Android ⇒ AVD Manager
Run ⇒ Run 'app'