Android Studio: Cara Membuat Android Apps 2

From OnnoWiki
Jump to navigation Jump to search

How to Make Android Apps 2In this part of my how to make Android apps tutorial I will cover the following topics : strings.xml, Linear Layouts, Toasts, Event Handling, Excepting User Input, Translation, EditText, Button and a whole bunch of other topics.

If you missed part 1 of this series, or if you need to install Android Studio I have you covered with these links. For translation you’ll need the ISO 639-1 Codes as well. All of the code from the video can be found below.

If you like videos like this, it helps me if you share on Google Plus with a click here

Code From the Video

MainActivity.java

package com.newthinktank.messingaround.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.EditText;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
 
    // 17. Create the components here that you want to use in your code
 
    private Button answerYesButton, answerNoButton;
    private EditText usersNameEditText;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 18. Initialize all of your components in the code here
 
        answerYesButton = (Button) findViewById(R.id.answer_yes_button);
        answerNoButton = (Button) findViewById(R.id.answer_no_button);
        usersNameEditText = (EditText) findViewById(R.id.users_name_edit_text);
    }
 
 
    @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

41 public boolean onOptionsItemSelected(MenuItem item) { 42 // Handle action bar item clicks here. The action bar will 43 // automatically handle clicks on the Home/Up button, so long 44 // as you specify a parent activity in AndroidManifest.xml. 45 int id = item.getItemId(); 46 if (id == R.id.action_settings) { 47 return true; 48 } 49 return super.onOptionsItemSelected(item); 50 } 51 52 public void onYesButtonClick(View view) { 53 54 String usersName = String.valueOf(usersNameEditText.getText()); 55 56 String yourYesResponse = "That is great " + usersName; 57 58 // 19. We display a short popup or Toast when the user clicks 59 // LENGTH_SHORT or LENGTH_LONG defines how long the toast shows 60 61 Toast.makeText(this, yourYesResponse, Toast.LENGTH_SHORT).show(); 62 } 63 64 public void onNoButtonClick(View view) { 65 66 String usersName = String.valueOf(usersNameEditText.getText()); 67 68 String yourNoResponse = "To bad " + usersName; 69 70 Toast.makeText(this, yourNoResponse, Toast.LENGTH_SHORT).show(); 71 72 } 73 }

activity_main.xml

001 002 003 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 004 xmlns:tools="http://schemas.android.com/tools" 005 android:orientation="vertical" 006 android:layout_width="match_parent" 007 android:layout_height="match_parent" 008 android:paddingLeft="@dimen/activity_horizontal_margin" 009 android:paddingRight="@dimen/activity_horizontal_margin" 010 android:paddingTop="@dimen/activity_vertical_margin" 011 android:paddingBottom="@dimen/activity_vertical_margin" 012 tools:context="com.newthinktank.messingaround.app.MainActivity" 013 android:gravity="center|top"> 014 015 017 018 022 023 030 031 047 048 050 051 053 054 057 058 060 061 064 065 083 084 096 097 099 100 107 108 109 110 111 112 139 140 143 144 154 155 160 161 <TextView 162 android:id="@+id/happy_question" 163 android:layout_width="wrap_content" 164 android:layout_height="wrap_content" 165 android:text="@string/happy_question" 166 android:textSize="20sp"/> 167 168 <LinearLayout 169 android:layout_width="wrap_content" 170 android:layout_height="wrap_content" 171 android:orientation="horizontal"> 172 173 178 179 <Button 180 android:id="@+id/answer_yes_button" 181 android:layout_width="wrap_content" 182 android:layout_height="wrap_content" 183 android:text="@string/answer_yes" 184 android:hint="@string/hint_pick_me" 185 android:onClick="onYesButtonClick"/> 186 187 <Button 188 android:id="@+id/answer_no_button" 189 android:layout_width="wrap_content" 190 android:layout_height="wrap_content" 191 android:text="@string/answer_no" 192 android:hint="@string/hint_no_pick_me" 193 android:onClick="onNoButtonClick"/> 194 195 </LinearLayout> 196 197 <LinearLayout 198 android:layout_width="wrap_content" 199 android:layout_height="wrap_content" 200 android:orientation="horizontal"> 201 202 <EditText 203 android:id="@+id/users_name_edit_text" 204 android:layout_width="200dp" 205 android:layout_height="50dp" 206 android:hint="@string/users_name_edit_text" 207 android:inputType="textPersonName"/> 208 209 </LinearLayout> 210 211 </LinearLayout>

strings.xml Default

01 <?xml version="1.0" encoding="utf-8"?> 02 <resources> 03 04 <string name="app_name">MessingAround</string> 05 <string name="hello_world">Hello world!</string> 06 <string name="action_settings">Settings</string> 07 <string name="users_name_edit_text">Your Name</string> 08 <string name="happy_question">Are You Happy?</string> 09 <string name="answer_yes_button">Yes</string> 10 <string name="answer_no_button">No</string> 11 12 </resources>

strings.xml Spanish

01 <?xml version="1.0" encoding="utf-8"?> 02 <resources> 03 04 <string name="app_name">Perder el tiempo</string> 05 <string name="hello_world">¡Hola, mundo</string> 06 <string name="action_settings">Configuración</string> 07 <string name="users_name_edit_text">Su nombre</string> 08 <string name="happy_question">¿Eres feliz?</string> 09 <string name="answer_yes">Sí</string> 10 <string name="answer_no">No</string> 11 <string name="hint_pick_me">Llevarnos</string> 12 <string name="hint_no_pick_me">No, me recogiera</string> 13 14 </resources> - See more at: http://www.newthinktank.com/2014/06/make-android-apps-2/#sthash.KFPwFMDe.dpuf