Difference between revisions of "Android Studio: Cara Membuat Android Apps"

From OnnoWiki
Jump to navigation Jump to search
(New page: 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 ...)
 
Line 9: Line 9:
 
MainActivity.java
 
MainActivity.java
  
01 package com.newthinktank.helloagain.app;
+
package com.newthinktank.helloagain.app;
02
+
03 import android.os.Bundle;
+
import android.os.Bundle;
04 import android.support.v7.app.ActionBarActivity;
+
import android.support.v7.app.ActionBarActivity;
05 import android.view.Menu;
+
import android.view.Menu;
06 import android.view.MenuItem;
+
import android.view.MenuItem;
07 import android.view.View;
+
import android.view.View;
08 import android.widget.Button;
+
import android.widget.Button;
09 import android.widget.TextView;
+
import android.widget.TextView;
10
+
11 public class MainActivity extends ActionBarActivity {
+
public class MainActivity extends ActionBarActivity {
12
+
13     // onCreate is executed when the activity is created
+
    // onCreate is executed when the activity is created
14     @Override
+
    @Override
15     protected void onCreate(Bundle savedInstanceState) {
+
    protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
+
        super.onCreate(savedInstanceState);
17        
+
       
18         // Sets the file activity_main.xml as the user interface
+
        // Sets the file activity_main.xml as the user interface
19         setContentView(R.layout.activity_main);
+
        setContentView(R.layout.activity_main);
20
+
21         // To be able to edit the TextView with our code we have to create it and
+
        // To be able to edit the TextView with our code we have to create it and
22         // bind it to a TextView object. I need to use final because it will be
+
        // bind it to a TextView object. I need to use final because it will be
23         // used in the inner class below
+
        // used in the inner class below
24         final TextView firstTextView = (TextView) findViewById(R.id.textView);
+
        final TextView firstTextView = (TextView) findViewById(R.id.textView);
25
+
26         // I set up the Button just like I did the TextView
+
        // I set up the Button just like I did the TextView
27         Button firstButton = (Button) findViewById(R.id.firstButton);
+
        Button firstButton = (Button) findViewById(R.id.firstButton);
28
+
29         // This is how you make the Button change the text in the TextView when it is clicked
+
        // This is how you make the Button change the text in the TextView when it is clicked
30         firstButton.setOnClickListener(new View.OnClickListener() {
+
        firstButton.setOnClickListener(new View.OnClickListener() {
31             @Override
+
            @Override
32             public void onClick(View view) {
+
            public void onClick(View view) {
33
+
34                 firstTextView.setText("You Clicked");
+
                firstTextView.setText("You Clicked");
35
+
36             }
+
            }
37         });
+
        });
38     }
+
    }
39
+
40
+
41     @Override
+
    @Override
42     public boolean onCreateOptionsMenu(Menu menu) {
+
    public boolean onCreateOptionsMenu(Menu menu) {
43         // Inflate the menu; this adds items to the action bar if it is present.
+
        // Inflate the menu; this adds items to the action bar if it is present.
44         getMenuInflater().inflate(R.menu.main, menu);
+
        getMenuInflater().inflate(R.menu.main, menu);
45         return true;
+
        return true;
46     }
+
    }
47
+
48     @Override
+
    @Override
49     public boolean onOptionsItemSelected(MenuItem item) {
+
    public boolean onOptionsItemSelected(MenuItem item) {
50         // Handle action bar item clicks here. The action bar will
+
        // Handle action bar item clicks here. The action bar will
51         // automatically handle clicks on the Home/Up button, so long
+
        // automatically handle clicks on the Home/Up button, so long
52         // as you specify a parent activity in AndroidManifest.xml.
+
        // as you specify a parent activity in AndroidManifest.xml.
53         int id = item.getItemId();
+
        int id = item.getItemId();
54         if (id == R.id.action_settings) {
+
        if (id == R.id.action_settings) {
55             return true;
+
            return true;
56         }
+
        }
57         return super.onOptionsItemSelected(item);
+
        return super.onOptionsItemSelected(item);
58     }
+
    }
59 }
+
}
  
 
activity_main.xml
 
activity_main.xml
  
01 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02     xmlns:tools="http://schemas.android.com/tools"
+
    xmlns:tools="http://schemas.android.com/tools"
03     android:layout_width="match_parent"
+
    android:layout_width="match_parent"
04     android:layout_height="match_parent"
+
    android:layout_height="match_parent"
05     android:paddingLeft="@dimen/activity_horizontal_margin"
+
    android:paddingLeft="@dimen/activity_horizontal_margin"
06     android:paddingRight="@dimen/activity_horizontal_margin"
+
    android:paddingRight="@dimen/activity_horizontal_margin"
07     android:paddingTop="@dimen/activity_vertical_margin"
+
    android:paddingTop="@dimen/activity_vertical_margin"
08     android:paddingBottom="@dimen/activity_vertical_margin"
+
    android:paddingBottom="@dimen/activity_vertical_margin"
09     tools:context="com.newthinktank.helloagain.app.MainActivity">
+
    tools:context="com.newthinktank.helloagain.app.MainActivity">
10
+
11     <TextView
+
    <TextView
12         android:text="@string/hello_world"
+
        android:text="@string/hello_world"
13         android:textSize="40sp"
+
        android:textSize="40sp"
14         android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
16         android:layout_centerHorizontal="true"
+
        android:layout_centerHorizontal="true"
17         android:id="@+id/textView" />
+
        android:id="@+id/textView" />
18
+
19     <Button
+
    <Button
20         android:layout_width="wrap_content"
+
        android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
+
        android:layout_height="wrap_content"
22         android:text="@string/button_1_text"
+
        android:text="@string/button_1_text"
23         android:id="@+id/firstButton"
+
        android:id="@+id/firstButton"
24         android:layout_below="@+id/textView"
+
        android:layout_below="@+id/textView"
25         android:layout_centerHorizontal="true"
+
        android:layout_centerHorizontal="true"
26         android:layout_marginTop="52dp" />
+
        android:layout_marginTop="52dp" />
27
+
28 </RelativeLayout>
+
</RelativeLayout>
  
 
dimens.xml
 
dimens.xml
  
1 <resources>
+
<resources>
2     <!-- Default screen margins, per the Android Design guidelines. -->
+
    <!-- Default screen margins, per the Android Design guidelines. -->
3     <dimen name="activity_horizontal_margin">16dp</dimen>
+
    <dimen name="activity_horizontal_margin">16dp</dimen>
4     <dimen name="activity_vertical_margin">16dp</dimen>
+
    <dimen name="activity_vertical_margin">16dp</dimen>
5 </resources>
+
</resources>
  
 
strings.xml
 
strings.xml
  
01 <?xml version="1.0" encoding="utf-8"?>
+
<?xml version="1.0" encoding="utf-8"?>
02     <!-- We store all the text in the strings.xml file so it is easy to
+
    <!-- We store all the text in the strings.xml file so it is easy to
03     translate into other languages -->
+
    translate into other languages -->
04 <resources>
+
<resources>
05
+
06     <string name="app_name">HelloAgain</string>
+
    <string name="app_name">HelloAgain</string>
07     <string name="hello_world">Hello Again</string>
+
    <string name="hello_world">Hello Again</string>
08     <string name="action_settings">Settings</string>
+
    <string name="action_settings">Settings</string>
09     <string name="button_1_text">You Clicked</string>
+
    <string name="button_1_text">You Clicked</string>
10
+
11 </resources>
+
</resources>
  
 
AndroidManifest.xml
 
AndroidManifest.xml
  
01 <?xml version="1.0" encoding="utf-8"?>
+
<?xml version="1.0" encoding="utf-8"?>
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03     package="com.newthinktank.helloagain.app" >
+
    package="com.newthinktank.helloagain.app" >
04
+
05     <application
+
    <application
06         android:allowBackup="true"
+
        android:allowBackup="true"
07         android:icon="@drawable/ic_launcher"
+
        android:icon="@drawable/ic_launcher"
08         android:label="@string/app_name"
+
        android:label="@string/app_name"
09         android:theme="@style/AppTheme" >
+
        android:theme="@style/AppTheme" >
10         <activity
+
        <activity
11             android:name="com.newthinktank.helloagain.app.MainActivity"
+
            android:name="com.newthinktank.helloagain.app.MainActivity"
12             android:label="@string/app_name" >
+
            android:label="@string/app_name" >
13             <intent-filter>
+
            <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
+
                <action android:name="android.intent.action.MAIN" />
15
+
16                 <category android:name="android.intent.category.LAUNCHER" />
+
                <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
+
            </intent-filter>
18         </activity>
+
        </activity>
19     </application>
+
    </application>
20
+
21 </manifest>
+
</manifest>
  
 
28 Responses to “How to Make Android Apps”
 
28 Responses to “How to Make Android Apps”

Revision as of 18:19, 20 April 2015

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>

28 Responses to “How to Make Android Apps”

   Roni
   June 16, 2014 at 5:44 pm	
   when will your Android challenge start??
   I can’t wait to win Samsung Galaxy Note 3 :D
   Reply	
       Derek Banas
       June 25, 2014 at 12:27 pm	
       It just started yesterday. All the information is on my site
       Reply	
           alvin
           January 28, 2015 at 3:11 am	
           can you put a next and previous button for pagination on your site blog post so that we can easily go to next topic using this site? please. thank you
           Reply	
               Derek Banas
               February 3, 2015 at 11:01 am	
               I’ll see what I can do. I only avoided that because some people don’t like having to click to different pages.
               Reply	
   unknown
   June 21, 2014 at 1:10 am	
   great job on doing this tutorials keep it up :D
   Reply	
       Derek Banas
       June 25, 2014 at 12:21 pm	
       Thank you :)
       Reply	
   stephen
   July 13, 2014 at 12:08 am	
   can you help me with this error please and thanks
   Waiting for device.
   “/Applications/Android Studio.app/sdk/tools/emulator” -avd MonoForAndroid_API_8 -netspeed full -netdelay none
   emulator: ERROR: This AVD’s configuration is missing a kernel file!!
   Reply	
       Derek Banas
       July 17, 2014 at 8:29 am	
       In the SDK manager download the ARM EABI v7a System Image
       Reply	
   israel
   July 18, 2014 at 3:28 pm	
   Best tutorials given by the best teacher Dereck Banas
   Reply	
       Derek Banas
       July 21, 2014 at 6:05 pm	
       Thank you :) It is very kind of you to say that.
       Reply	
   Altiano
   July 23, 2014 at 8:06 am	
   i’m going to learn about android apps development, which one should i choose, the new android tutorial or the old one?
   Reply	
       Derek Banas
       July 25, 2014 at 6:38 pm	
       Probably the new one. I’m better at teaching Android now
       Reply	
   Ext
   July 26, 2014 at 10:43 am	
   This is my first time programming anything for android, but I have developed a lot for Java and different languages earlier. I followed your tutorial but I can not get it too work. First it does not like ActionBarActivity so I googled and added dependencies {
   compile ‘com.android.support:appcompat-v7:+’
   }
   too build.gradle but now it is complaing about Manifest merger failed : uses-sdk:minSdkVersion 15 cannot be smaller than version L declared in library com.android.support:appcompat-v7:21.0.0-rc1
   What am I doing wrong, is it supposed to be this hard? :D
   I have never actually experienced anything thid hard to do a simple hello world program :D
   Reply	
       Derek Banas
       July 28, 2014 at 8:49 am	
       Switch your target API to 19 and make sure you have all the proper files downloaded for 19 in the SDK manager and the errors will go away
       Reply	
   David
   August 1, 2014 at 12:32 pm	
   Derek, are you going to make a video on how to create a menu bar, for example, and add an icon to it? Is this too graphics intensice i.e. GL stuff or is it easier than it looks? Also would you be able to add an action behind that button?
   Reply	
       Derek Banas
       August 2, 2014 at 7:29 am	
       I’ll cover custom layouts, menu bars, etc. later. I already cover the action bar and options menu in part 4.
       Reply	
   Kevin Kesler
   October 2, 2014 at 5:51 pm	
   I think i need to learn the path to programming effectively for android. the app inventor has worked awesome and i have overcome all of the obstacles i set out to. what steps would you recommend?
   i have experience with computers mostly by necessity. i did basic in high school. a but of c in college but20 years ago. where should i start now and what should i not bypass? is java what i need to learn?
   a little insight could keep me from wasting vast amounts of time learning things that later will be irrelavant. thanks for your thought :)
   Reply	
       Derek Banas
       October 3, 2014 at 9:31 am	
       Yes parts 1 – 18, minus parts 8 and 10 is all you need from my Java tutorial. Then move on to my new Android tutorial and you’ll be ready to go.
       Reply	
   Will
   October 12, 2014 at 4:08 am	
   Thanks for your Tutorial.
   It`s great.
   cheers
   Reply	
       Derek Banas
       October 12, 2014 at 9:38 am	
       You’re very welcome :)
       Reply	
   sypi
   October 15, 2014 at 12:55 am	
   Thanks for the tutorials! Love the android ones!
   Reply	
       Derek Banas
       October 16, 2014 at 5:49 pm	
       Thank you :) More are coming in the next few days
       Reply	
   dre
   November 12, 2014 at 12:49 pm	
   way kool video. only downside is that it takes for ever to update the sdk manager.
   Reply	
       Derek Banas
       November 13, 2014 at 12:16 pm	
       Thank you :) That is odd that the SDK manager is so slow. I haven’t had that issue before.
       Reply	
   Derick
   November 12, 2014 at 9:15 pm	
   Hi there, I ran the code u given and i got an error.
   error:cannot find symbol variable main
   execution failed for task ‘:app.compileDebugJava
   compilation failed; see the computer compiler error output for details
   ///////////////////////////////////////
   @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;
   }
   /////////////////////////////////////////
   the ‘main’ in error is from the code above.
   may i know how do i resolve this? thank you.
   Reply	
       Derek Banas
       November 13, 2014 at 12:13 pm	
       Remove all of these from your file import android.R and clean the project
       Reply	
   Ashwin
   February 1, 2015 at 11:13 am	
   Really a huge fan of yours…! :)
   Reply	
       Derek Banas
       February 3, 2015 at 6:50 am	
       Thank you :) I do my best.
       Reply	

Leave a Reply

Your email address will not be published.

Name

Email

Website

Comment

You may use these HTML tags and attributes: <a href="" title="" rel="">

<acronym title="">
 

- See more at: http://www.newthinktank.com/2014/06/make-android-apps/#sthash.Yhlj67TE.dpuf





Referensi

* http://www.newthinktank.com/2014/06/make-android-apps/