Check out my latest video Tutorial on YouTube!
In this video I show you how to:
- Create an Android Studio project
- Create an Android Virtual Device (AVD)
- Run the Hello World program
- Add custom behavior to the program
- Add a string resource to strings.xml
Below is the main source code for the project.
MainActivity.java:
package com.makotogo.mobile.helloworld; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Add button Button helloButton = (Button)findViewById(R.id.helloButton); helloButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView textView = (TextView)findViewById(R.id.textView); textView.setText(R.string.seriouslyHelloWorld); } }); } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.makotogo.mobile.helloworld.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/helloWorld" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignRight="@+id/helloButton" android:layout_alignEnd="@+id/helloButton"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me!" android:id="@+id/helloButton" android:layout_marginTop="76dp" android:layout_below="@+id/textView" android:layout_centerHorizontal="true"/> </RelativeLayout>
strings.xml:
<resources> <string name="app_name">HelloWorld</string> <string name="helloWorld">Hello, World!</string> <string name="seriouslyHelloWorld">Seriously, HELLO, WORLD!!</string> </resources>
I hope you enjoy the video!
–jsp