Sabtu, 05 April 2014

This Is Me!

Ayee...... lagi lagi dapet kerjaan yang harus dikerjain... tapi tak apa lah.. haha, kali ini kita akan bikin sesuatu yang sejenis form gitu, yang isinya data diri kita dan bisa langsung di tampilkan. Mau tau caranya? Oke, tidak usah bertele tele teletabis, langsung aja cuuusss.. ini dia caranya.. :D

Seperti biasa, buat dulu android application project kalian. :D
Setelah itu di activity_main.xml


kalian bisa menuliskan code ini :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"> 
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/id"
        android:layout_centerHorizontal="true" 
        android:paddingBottom="10dp"/>
   
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/id"
        android:text="@string/nama"
        android:id="@+id/text1"
        android:textSize="18sp"
        android:layout_marginTop="15dp"
        />
  
    <EditText
        android:id="@+id/nama"
        android:layout_below="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:layout_toRightOf="@+id/text1">
   
           <requestFocus />
     </EditText>
   
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:text="@string/email"
        android:id="@+id/text2"
        android:textSize="18sp"
        android:layout_marginTop="15dp"
        />

     <EditText
         android:id="@+id/email"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_toRightOf="@+id/text2"
         android:inputType="textEmailAddress"
         android:layout_below="@+id/nama"/>
    
     <TextView
         android:id="@+id/text3"
         android:text="@string/jenis"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/text2"
         android:layout_marginTop="10dp"
         android:textSize="18sp"/>
    
       <RadioGroup
            android:padding="5dp"
            android:id="@+id/group"
            android:layout_below="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
           
        <RadioButton
           android:id="@+id/co"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/cowok"
           android:textSize="18sp"/>
   
       <RadioButton
           android:id="@+id/ce"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/cewek"
           android:textSize="18sp"/>
     </RadioGroup>
   
        <TextView
        android:id="@+id/text4"
        android:text="@string/hoby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/group"
        android:layout_marginTop="7dp"
        android:textSize="18sp"
        />
       
    <CheckBox
        android:id="@+id/gambar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text4"
        android:layout_marginTop="5dp"
        android:textSize="18sp"
        android:text="@string/drawing"
        />
   
    <CheckBox
        android:id="@+id/game"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gambar"
        android:layout_marginTop="5dp"
        android:textSize="18sp"
        android:text="@string/game"
        />
   
    <CheckBox
        android:id="@+id/swim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/game"
        android:layout_marginTop="5dp"
        android:textSize="18sp"
        android:text="@string/swim"
        />

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/swim"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="@string/submit"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/submit"
        android:layout_marginTop="5dp"
        android:textSize="18sp" />
   
    <TextView
        android:id="@+id/e_mail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_marginTop="5dp"
        android:textSize="18sp"
        />
   
    <TextView
        android:id="@+id/hobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/e_mail"
        android:layout_marginTop="5dp"
        android:textSize="18sp"
        />
   
    <TextView
        android:id="@+id/jenisKelamin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hobby"
        android:layout_marginTop="5dp"
        android:textSize="18sp"
        />


</RelativeLayout>
</ScrollView>

Setelah sudah selesai, sekarang pindah di bagian MainActivity.java


  
Nah, pada bagian MainActivity.java ini, kalian bisa tambahkan code ini :
package com.example.formapplication;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener {

    EditText nama;
    EditText email;
    TextView name;
    TextView e_mail;
    TextView hobby;
    TextView jenisKelamin;
    Button submit;
    RadioGroup group;
    RadioButton co;
    RadioButton ce;
    CheckBox gambar;
    CheckBox game;
    CheckBox swim;
    String jenis;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        nama     = (EditText)findViewById(R.id.nama);
        email    = (EditText)findViewById(R.id.email);
        name    = (TextView)findViewById(R.id.name);
        e_mail    = (TextView)findViewById(R.id.e_mail);
        hobby    = (TextView)findViewById(R.id.hobby);
        jenisKelamin    = (TextView)findViewById(R.id.jenisKelamin);
        submit    = (Button)findViewById(R.id.submit);
        group    = (RadioGroup)findViewById(R.id.group);
        gambar    = (CheckBox)findViewById(R.id.gambar);
        game    = (CheckBox)findViewById(R.id.game);
        swim    = (CheckBox)findViewById(R.id.swim);
       
        submit.setOnClickListener(this);
        group.setOnCheckedChangeListener(this);
    }   
   
     @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 void onClick(View click){
         String hobi="";
         name.setText("Nama : "+nama.getText());
         e_mail.setText("Email : "+email.getText());
       
         if(gambar.isChecked())
         {
             hobi+="Melukis";
         }
         else if(game.isChecked())
         {
             hobi+="Main Game";
         }
         else
         {
             hobi+="Berenang";
         }
       
         hobby.setText("Hobi : "+hobi);
         jenisKelamin.setText(jenis);
     }
   
     @Override
     public void onCheckedChanged(RadioGroup group, int check) {
     // TODO Auto-generated method stub
     if(check==R.id.co)
     {
         jenis="Laki-Laki";
     }
     else
     {
         jenis="Perempuan";
     }
     }
}

Setelah itu, kalian simpan, terus cobain deh lewat AVD atau emulator yang kalian sukai :D
Dan ini adalah hasil dari punya saya..

 gambar di atas setelah di run tapi belum diisi :D

 Kalo gambar yang ini, udah di isi form nya.. :D

 Nah, ini hasil setelah di submit form nya.. :D

Gimana? gampang kan? dan seperti biasa, walau gampang tapi bikin saya puyeng juga... :D Semoga bisa membantu kalian biar nggak ikutan puyeng yaa... Selamat mencoba.. :D

Tidak ada komentar:

Posting Komentar