Pages

Intent Pada Android

Intent membantu untuk membuka halaman lain dalam Android, pada contoh kasus ini akan saya jelaskan menggunakan menu makanan dengan Android.



  1. Buka Eclipse
  2. Pada Eclipse File - New - Project - Android - Android Project - Next
  3. Kemudian pada jendela New Android Project isikan field berikut :
    1. Project Name : Menu Makanan Dengan Intent
    2. Build Target : Android 2.2
    3. Aplication name : Menu Makanan
    4. Package Name : jhohannespurba.blogspot.com
    5. Create Activity : MainActivity
    6. Kemudian klik Finish
  4. Buka main.xml (res - Layout - main.xml)
  5. Copy Source code berikut, lalu paste pada tab main.xml
  6. 1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <?xml version="1.0" encoding="utf-8"?>
    
    <AbsoluteLayout 
     android:id="@+id/AbsoluteLayout01"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     xmlns:android="http://schemas.android.com/apk/res/android">
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:text="Keluar"
      android:layout_y="300px" 
      android:id="@+id/btnKeluar"></Button>
     <Button android:layout_height="wrap_content" 
      android:layout_y="50px"
      android:layout_width="match_parent" 
      android:id="@+id/btnMenuUtama"
      android:text="Menu Utama"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="100px"
      android:id="@+id/btnIkan" 
      android:text="Ikan"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="150px"
      android:id="@+id/btnMie" 
      android:text="Mie"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="200px"
      android:id="@+id/btnNasi" 
      android:text="Nasi"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="250px"
      android:id="@+id/btnSayur" 
      android:text="Sayur"></Button>
     <TextView android:layout_y="10px" 
      android:textColor="#F32"
      android:textSize="20px" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:marqueeRepeatLimit="marquee_forever"
      android:text="Menu Utama"></TextView>
    </AbsoluteLayout>
    
  7. kemudian buat file xml baru, kemudian beri nama ikan.xml
  8. 1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    <?xml version="1.0" encoding="utf-8"?>
    
    <AbsoluteLayout android:id="@+id/AbsoluteLayout01"
     android:layout_width="fill_parent" android:layout_height="fill_parent"
     xmlns:android="http://schemas.android.com/apk/res/android">
     <Button android:layout_height="wrap_content" 
      android:layout_y="50px"
      android:layout_width="match_parent" 
      android:id="@+id/btnMenuUtama"
      android:text="Menu Utama"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="100px"
      android:id="@+id/btnIkan" 
      android:text="Ikan"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="150px"
      android:id="@+id/btnMie" 
      android:text="Mie"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="200px"
      android:id="@+id/btnNasi" 
      android:text="Nasi"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="250px"
      android:id="@+id/btnSayur" 
      android:text="Sayur"></Button>
     <TextView android:layout_y="10px" 
      android:textColor="#F32"
      android:textSize="20px" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:marqueeRepeatLimit="marquee_forever"
      android:text="Ikan"></TextView>
    </AbsoluteLayout>
    
  9. kemudian buat file xml baru, kemudian beri nama mie.xml
  10. 1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <?xml version="1.0" encoding="utf-8"?>
    
    <AbsoluteLayout android:id="@+id/AbsoluteLayout01"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     xmlns:android="http://schemas.android.com/apk/res/android">
     <Button 
      android:layout_height="wrap_content" 
      android:layout_y="50px"
      android:layout_width="match_parent" 
      android:id="@+id/btnMenuUtama"
      android:text="Menu Utama"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="100px"
      android:id="@+id/btnIkan" 
      android:text="Ikan"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="150px"
      android:id="@+id/btnMie" 
      android:text="Mie"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="200px"
      android:id="@+id/btnNasi" 
      android:text="Nasi"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="250px"
      android:id="@+id/btnSayur" 
      android:text="Sayur"></Button>
     <TextView android:layout_y="10px" 
      android:textColor="#F32"
      android:textSize="20px" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:marqueeRepeatLimit="marquee_forever"
      android:text="Mie"></TextView>
    </AbsoluteLayout>
    
  11. kemudian buat file xml baru, kemudian beri nama nasi.xml
  12. 1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    <?xml version="1.0" encoding="utf-8"?>
    
    <AbsoluteLayout android:id="@+id/AbsoluteLayout01"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     xmlns:android="http://schemas.android.com/apk/res/android">
     <Button android:layout_height="wrap_content" 
      android:layout_y="50px"
      android:layout_width="match_parent" 
      android:id="@+id/btnMenuUtama"
      android:text="Menu Utama"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="100px"
      android:id="@+id/btnIkan" 
      android:text="Ikan"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="150px"
      android:id="@+id/btnMie" 
      android:text="Mie"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="200px"
      android:id="@+id/btnNasi" 
      android:text="Nasi"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="250px"
      android:id="@+id/btnSayur" 
      android:text="Sayur"></Button>
     <TextView android:layout_y="10px" 
      android:textColor="#F32"
      android:textSize="20px" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:marqueeRepeatLimit="marquee_forever"
      android:text="Nasi"></TextView>
    </AbsoluteLayout>
    
  13. kemudian buat file xml baru, kemudian beri nama sayur.xml 
  14. 1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    <?xml version="1.0" encoding="utf-8"?>
    
    <AbsoluteLayout android:id="@+id/AbsoluteLayout01"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     xmlns:android="http://schemas.android.com/apk/res/android">
     <Button android:layout_height="wrap_content" 
      android:layout_y="50px"
      android:layout_width="match_parent" 
      android:id="@+id/btnMenuUtama"
      android:text="Menu Utama"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="100px"
      android:id="@+id/btnIkan" 
      android:text="Ikan"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="150px"
      android:id="@+id/btnMie" 
      android:text="Mie"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="200px"
      android:id="@+id/btnNasi" 
      android:text="Nasi"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:layout_y="250px"
      android:id="@+id/btnSayur" 
      android:text="Sayur"></Button>
     <Button android:layout_height="wrap_content"
      android:layout_width="match_parent" 
      android:text="Keluar"
      android:layout_y="300px" 
      android:id="@+id/btnKeluar"></Button>
     <TextView android:layout_y="10px" 
      android:textColor="#F32"
      android:textSize="20px" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:marqueeRepeatLimit="marquee_forever"
      android:text="Sayur"></TextView>
    </AbsoluteLayout>
    
  15. kemudian buka AndroidManifest.xml
  16. 1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="jhohannespurba.blogspot.com" android:versionCode="1"
     android:versionName="1.0">
     <application android:icon="@drawable/icon" 
      android:label="@string/app_name">
      <activity 
       android:name=".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>
    
      <activity android:name=".nasi" 
       android:label="@string/app_name">
       <intent-filter>
        <action android:name="android.intent.action.VIEW" />
       </intent-filter>
      </activity>
    
      <activity android:name=".MainActivity" 
       android:label="@string/app_name">
       <intent-filter>
        <action android:name="android.intent.action.VIEW" />
       </intent-filter>
      </activity>
    
      <activity android:name=".sayur" 
       android:label="@string/app_name">
       <intent-filter>
        <action android:name="android.intent.action.VIEW" />
       </intent-filter>
      </activity>
    
      <activity android:name=".mie" 
       android:label="@string/app_name">
       <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
      </activity>
    
      <activity android:name=".ikan" 
       android:label="@string/app_name">
       <intent-filter>
        <action android:name="android.intent.action.VIEW" />
       </intent-filter>
      </activity>
     </application>
     <uses-sdk android:minSdkVersion="8" />
    
    </manifest> 
    
  17. kemudian buka class MainActivity.java
  18. 1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    package jhohannespurba.blogspot.com;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
        Button ikan, mie, nasi, sayur, menuUtama, keluar;
     
     /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
    /*---------------------- Intent Menu utama------------------------*/   
            menuUtama = (Button) findViewById(R.id.btnMenuUtama);
            menuUtama.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-gen,erated method stub
        menuUtama(menuUtama); 
        
       }
      });
            
       
    /*------------------ Intent Ikan -------------------------*/
            ikan = (Button) findViewById(R.id.btnIkan);
            
            ikan.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        menuIkan(ikan);
       }
      });
     
    /*---------------------- Intent mie ------------------------*/   
            mie = (Button) findViewById(R.id.btnMie);
            mie.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        menuMie(mie);
       }
      });
        
    /*---------------------- Intent nasi ------------------------*/   
            nasi = (Button) findViewById(R.id.btnNasi);
            nasi.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        menuNasi(nasi);
       }
      });
            
    /*---------------------- Intent mie ------------------------*/   
            sayur = (Button) findViewById(R.id.btnSayur);
            sayur.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        menuSayur(sayur);
       }
      });
        
    /*---------------------- Intent mie ------------------------*/   
            keluar = (Button) findViewById(R.id.btnKeluar);
            keluar.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        setKeluar(keluar);
       }
      });
             
        }
        
        
     /*===============================================*/
        public void menuUtama(View view){
         Intent menuUtama = new Intent(this, MainActivity.class);
         startActivity(menuUtama);
        }
        
        
        public void menuIkan(View view){
         Intent ikan = new Intent(this,ikan.class);
         startActivity(ikan);
         
        }
        
        public void menuMie(View view){
         Intent mie = new Intent(this, mie.class);
         startActivity(mie);
        }
        
        
        public void menuNasi(View view){
         Intent nasi = new Intent(this,nasi.class);
         startActivity(nasi);
        }
     
        
        public void menuSayur(View view){
         Intent sayur = new Intent(this, sayur.class);
         startActivity(sayur);
        }
        
        public void setKeluar(View view){
         Intent keluar = new Intent();
         setResult(RESULT_OK,keluar);
         finish();
    }
    }
    
  19. kemudian buka class ikan.java
  20. 1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    package jhohannespurba.blogspot.com;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class ikan extends Activity{
      Button ikan, mie, nasi, sayur, menuUtama;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.ikan);
            
      /*---------------------- Intent Menu utama------------------------*/   
              menuUtama = (Button) findViewById(R.id.btnMenuUtama);
              menuUtama.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuUtama(menuUtama); 
          
         }
        });
              
         
      /*------------------ Intent Ikan -------------------------*/
              ikan = (Button) findViewById(R.id.btnIkan);
              
              ikan.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuIkan(ikan);
         }
        });
       
      /*---------------------- Intent mie ------------------------*/   
              mie = (Button) findViewById(R.id.btnMie);
              mie.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuMie(mie);
         }
        });
          
      /*---------------------- Intent nasi ------------------------*/   
              nasi = (Button) findViewById(R.id.btnNasi);
              nasi.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuNasi(nasi);
         }
        });
              
      /*---------------------- Intent mie ------------------------*/   
              sayur = (Button) findViewById(R.id.btnSayur);
              sayur.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuSayur(sayur);
         }
        });
          
          
          }     
          
       /*===============================================*/
          public void menuUtama(View view){
           Intent menuUtama = new Intent(this, MainActivity.class);
           startActivity(menuUtama);
          }
          
          
          public void menuIkan(View view){
           Intent ikan = new Intent(this,ikan.class);
           startActivity(ikan);
           
          }
          
          public void menuMie(View view){
           Intent mie = new Intent(this, mie.class);
           startActivity(mie);
          }
          
          
          public void menuNasi(View view){
           Intent nasi = new Intent(this,nasi.class);
           startActivity(nasi);
          }
       
          
          public void menuSayur(View view){
           Intent sayur = new Intent(this, sayur.class);
           startActivity(sayur);
          }
      }
    
  21. kemudian buka class mie.java
  22. 1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    package jhohannespurba.blogspot.com;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class mie extends Activity{
      Button ikan, mie, nasi, sayur, menuUtama;
    
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mie);
            
      /*---------------------- Intent Menu utama------------------------*/   
              menuUtama = (Button) findViewById(R.id.btnMenuUtama);
              menuUtama.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuUtama(menuUtama); 
          
         }
        });
              
         
      /*------------------ Intent Ikan -------------------------*/
              ikan = (Button) findViewById(R.id.btnIkan);
              
              ikan.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuIkan(ikan);
         }
        });
       
      /*---------------------- Intent mie ------------------------*/   
              mie = (Button) findViewById(R.id.btnMie);
              mie.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuMie(mie);
         }
        });
          
      /*---------------------- Intent nasi ------------------------*/   
              nasi = (Button) findViewById(R.id.btnNasi);
              nasi.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuNasi(nasi);
         }
        });
              
      /*---------------------- Intent mie ------------------------*/   
              sayur = (Button) findViewById(R.id.btnSayur);
              sayur.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuSayur(sayur);
         }
        });
          
          
          }
          
          
       /*===============================================*/
          public void menuUtama(View view){
           Intent menuUtama = new Intent(this, MainActivity.class);
           startActivity(menuUtama);
          }
          
          
          public void menuIkan(View view){
           Intent ikan = new Intent(this,ikan.class);
           startActivity(ikan);
           
          }
          
          public void menuMie(View view){
           Intent mie = new Intent(this, mie.class);
           startActivity(mie);
          }
          
          
          public void menuNasi(View view){
           Intent nasi = new Intent(this,nasi.class);
           startActivity(nasi);
          }
       
          
          public void menuSayur(View view){
           Intent sayur = new Intent(this, sayur.class);
           startActivity(sayur);
          }
      }
    
  23. kemudian buka class nasi.java
  24. 1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    package jhohannespurba.blogspot.com;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class nasi extends Activity{
      Button ikan, mie, nasi, sayur, menuUtama;
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
    
      
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.nasi);
            
      /*---------------------- Intent Menu utama------------------------*/   
              menuUtama = (Button) findViewById(R.id.btnMenuUtama);
              menuUtama.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuUtama(menuUtama); 
           
         }
        });
              
         
      /*------------------ Intent Ikan -------------------------*/
              ikan = (Button) findViewById(R.id.btnIkan);
              
              ikan.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuIkan(ikan);
         }
        });
       
      /*---------------------- Intent mie ------------------------*/   
              mie = (Button) findViewById(R.id.btnMie);
              mie.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuMie(mie);
         }
        });
          
      /*---------------------- Intent nasi ------------------------*/   
              nasi = (Button) findViewById(R.id.btnNasi);
              nasi.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuNasi(nasi);
         }
        });
              
      /*---------------------- Intent mie ------------------------*/   
              sayur = (Button) findViewById(R.id.btnSayur);
              sayur.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuSayur(sayur);
         }
        });
          
          
          }
          
          
       /*===============================================*/
          public void menuUtama(View view){
           Intent menuUtama = new Intent(this, MainActivity.class);
           startActivity(menuUtama);
          }
          
          
          public void menuIkan(View view){
           Intent ikan = new Intent(this,ikan.class);
           startActivity(ikan);
           
          }
          
          public void menuMie(View view){
           Intent mie = new Intent(this, mie.class);
           startActivity(mie);
          }
          
          
          public void menuNasi(View view){
           Intent nasi = new Intent(this,nasi.class);
           startActivity(nasi);
          }
       
          
          public void menuSayur(View view){
           Intent sayur = new Intent(this, sayur.class);
           startActivity(sayur);
          }
      }
    
  25. kemudian buka class sayur.java
  26. 1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    package jhohannespurba.blogspot.com;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class sayur extends Activity{
     Button ikan, mie, nasi, sayur, menuUtama, keluar;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
    
      
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.sayur);
            
      /*---------------------- Intent Menu utama------------------------*/   
              menuUtama = (Button) findViewById(R.id.btnMenuUtama);
              menuUtama.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuUtama(menuUtama); 
         }
        });
              
         
      /*------------------ Intent Ikan -------------------------*/
              ikan = (Button) findViewById(R.id.btnIkan);
              
              ikan.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuIkan(ikan);
         }
        });
       
      /*---------------------- Intent mie ------------------------*/   
              mie = (Button) findViewById(R.id.btnMie);
              mie.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuMie(mie);
         }
        });
          
      /*---------------------- Intent nasi ------------------------*/   
              nasi = (Button) findViewById(R.id.btnNasi);
              nasi.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuNasi(nasi);
         }
        });
              
      /*---------------------- Intent mie ------------------------*/   
              sayur = (Button) findViewById(R.id.btnSayur);
              sayur.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          menuSayur(sayur);
         }
        });
          
         /*---------------------- Intent mie ------------------------*/   
    
              keluar = (Button) findViewById(R.id.btnKeluar);
              keluar.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
          // TODO Auto-generated method stub
          setKeluar(keluar);
         }
        });
        }
    
          
       /*===============================================*/
          public void menuUtama(View view){
           Intent menuUtama = new Intent(this, MainActivity.class);
           startActivity(menuUtama);
          }
          
          
          public void menuIkan(View view){
           Intent ikan = new Intent(this,ikan.class);
           startActivity(ikan);
           
          }
          
          public void menuMie(View view){
           Intent mie = new Intent(this, mie.class);
           startActivity(mie);
          }
          
          
          public void menuNasi(View view){
           Intent nasi = new Intent(this,nasi.class);
           startActivity(nasi);
          }
       
          
          public void menuSayur(View view){
           Intent sayur = new Intent(this, sayur.class);
           startActivity(sayur);
          }
    
          public void setKeluar(View view){
           finish();
          }
    }
    
  27. kemudian jalankan, maka akan muncul hasil seperti berikut
     


      Jhohannes H Purba Coding Sederhana May 21, 2012

      14 comments:

      1. terima kasih banyak ilmunya sangat membantu tugas kuliah... saya. :)
        kang, saya mau bertanya kalo pake gambar di buttonnya itu gimna?? maaf saya baru belajar android dasarnya :)

        ReplyDelete
        Replies
        1. 1. copy dan paste gambar bertipe jpg,png dsb ke drawable- (drawable yang mana aja boleh)

          2. buka layout, dan tambahkan code berikut pada source button yang ingin dibuat gambar android:background="@drawable/gambar_button"

          3. hasil akhir dari button, kira-kira menjadi seperti berikut :
          < Button
          android:id="@+id/buttonKirim"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_x="wrap_content"
          android:layout_y="wrap_content"
          android:onClick="buttonClick"
          android:background="@drawable/gambar_button"
          android:text="Kirim" />
          4. Good Luck :)

          Delete
      2. Pingin coba yah gan?? moga berhasil ^^

        ReplyDelete
      3. kog ga bisa ya....saya sudah ikuti langkah2 diatas, tapi saat mau run, ada error........saya pakai eclipse juno

        ReplyDelete
        Replies
        1. coba lihat pada tutorial saya yang di sini gan, saya juga sudah sertakan source code.

          http://www.jhointegrity.com/2013/09/intent-sederhana-pada-android.html

          Delete
      4. What you're saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I'm sure you'll reach so many people with what you've got to say.

        ReplyDelete
      5. gan manifest.xml saya error "duplicate registration for activity" baris ke 23 ???
        apa ada yg kelewat atau gimana nih.....

        ReplyDelete
      6. After looking at a few of the blog articles on your site, I seriously appreciate your way of writing a blog.

        ReplyDelete
      7. kalo misalnya saya mau buat spinner menggunakan gambar bagaimana carannya?
        tolong dong min...

        ReplyDelete
      8. I really appreciate your professional approach. These are pieces of very useful information that will be of great use for me in future.

        ReplyDelete