Android Studio: Cara Membuat Android Apps
Jump to navigation
Jump to search
In this video I start my new Android tutorial. The last Android tutorial I made is still very popular, but I’m going to try and improve on it here.
If you are a beginner to Android and don’t know Java you may prefer my Android tutorial for beginners. I’ll be using Android Studio in this tutorial and I show how to install Android Studio here. All of the code follows the tutorial below.
If you like videos like this, it helps my search ranking if you share it on Google Plus with a click here
Code from the Video
MainActivity.java
package com.newthinktank.helloagain.app; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends ActionBarActivity { // onCreate is executed when the activity is created @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Sets the file activity_main.xml as the user interface setContentView(R.layout.activity_main);
// To be able to edit the TextView with our code we have to create it and // bind it to a TextView object. I need to use final because it will be // used in the inner class below final TextView firstTextView = (TextView) findViewById(R.id.textView); // I set up the Button just like I did the TextView Button firstButton = (Button) findViewById(R.id.firstButton); // This is how you make the Button change the text in the TextView when it is clicked firstButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { firstTextView.setText("You Clicked"); } }); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.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(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
activity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.newthinktank.helloagain.app.MainActivity"> <TextView android:text="@string/hello_world" android:textSize="40sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_1_text" android:id="@+id/firstButton" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="52dp" /> </RelativeLayout>
dimens.xml
<resources> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HelloAgain</string> <string name="hello_world">Hello Again</string> <string name="action_settings">Settings</string> <string name="button_1_text">You Clicked</string> </resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.newthinktank.helloagain.app" >
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity
android:name="com.newthinktank.helloagain.app.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> </application> </manifest>