Laman

Kamis, 02 Oktober 2014

Membuat Aplikasi Sederhana "Life Cycle"


Kali ini saya akan membuat aplikasi dasar android, disini kita akan memahami dasar dari life cycle application android, disini saya menggunakan android studio.



1.  Buat Project baru "File > New Project..." (Isi nama aplikasi yang akan dibuat...)


2.  Tentukan minimum SDK (disini saya menggunakan minimum SDK 4.0.3 ICS)

3.  Pada MyActivity.xml 
<LinearLayout 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:orientation="vertical"
    tools:context=".my_activity"
    android:id="@+id/myLayout">
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Choose Color"/>
    <Button
        android:id="@+id/Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exit"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Please press exit if done"/>
</LinearLayout> 

 4.  pada MyActivity.java
package teguh.dx.latihan1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class my_activity extends Activity {
    private Context context;
    private Button btnExit;
    private EditText editTextColor;
    private TextView textView;
    private LinearLayout myLayout;
    private String PREFNAME = "teguhPref";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        editTextColor = (EditText) findViewById(R.id.editText);
        btnExit = (Button) findViewById(R.id.Button);
        textView = (TextView) findViewById(R.id.textView);
        myLayout = (LinearLayout) findViewById(R.id.myLayout);
        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        editTextColor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                String colorVal = editTextColor.getText().toString();
                int tempColor;
                if(colorVal.equals("red")) {
                    tempColor = Color.RED;
                } else if (colorVal.equals("green")) {
                    tempColor = Color.GREEN;
                } else if(colorVal.equals("blue")) {
                    tempColor = Color.BLUE;
                } else if(colorVal.equals("yellow")) {
                    tempColor = Color.YELLOW;
                } else {
                    tempColor = Color.WHITE;
                }
                myLayout.setBackgroundColor(tempColor);
            }
        });
        Toast.makeText(getApplicationContext(), "onCreate", Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(getApplicationContext(), "onDestroy", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_activity, 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);
    }
}
 5.  Hasil Setelah di RUN

 

 


Tidak ada komentar:

Posting Komentar