Tuesday, April 11, 2017

[MOBILE PROGRAMMING] WIDGET VIEW - Basic Button 2

Berikut ini adalah potongan kode definisi pebuatan button dalam bentuk XML:
 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
<ScrollView 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:gravity="center_horizontal"
    tools:context=".MainActivity">
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical">
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button1" />
 
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#f39c12"
                android:onClick="simpleButton"
                android:padding="10dp"
                android:text="@string/button2" />
 
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="#f39c12"
                android:drawableLeft="@android:drawable/sym_call_outgoing"
                android:onClick="leftIconButton"
                android:padding="10dp"
                android:text="@string/button3" />
 
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="#f39c12"
                android:drawableRight="@android:drawable/sym_call_outgoing"
                android:onClick="rightIconButton"
                android:padding="10dp"
                android:text="@string/button4" />
 
            <Button
                android:layout_width="300dp"
                android:layout_height="80dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/card_file"
                android:onClick="backgroundImageButton"
                android:padding="10dp"
                android:text="@string/button5" />
 
            <Button
                android:layout_width="250dp"
                android:layout_height="70dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/folder_default"
                android:onClick="borderButton"
                android:padding="10dp"
                android:text="@string/button6" />
 
 
        </LinearLayout>
   
</ScrollView>


Kemudian pada folder res/value/strings.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Button01</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="button1">Contoh Penerapan Widget Button Android</string>
    <string name="button2">Contoh Button Sederhana</string>
    <string name="button3">Button dengan Icon dikiri</string>
    <string name="button4">Button dengan icon dikanan</string>
    <string name="button5">Button dengan gambar latar</string>
    <string name="button6">Button dengan Border Color</string>
    
</resources>


Berikut ini adalah kode pemanggilan button menggunakan code Java:
 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
package com.example.button01;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {
	
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                  
    }
    
    @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;
    }
    
    
    public void simpleButton(View view){
    	Toast.makeText(getApplicationContext(), "Ini adalah button sederhana", Toast.LENGTH_LONG).show();
    }
    
    public void leftIconButton(View view) {
        Toast.makeText(getApplicationContext(), "Button dengan Icon dikiri", Toast.LENGTH_LONG).show();
    }
 
    public void rightIconButton(View view) {
        Toast.makeText(getApplicationContext(), "Button dengan Icon dikanan", Toast.LENGTH_LONG).show();
    }
 
    public void backgroundImageButton(View view) {
        Toast.makeText(getApplicationContext(), "Button dengan gambar latar", Toast.LENGTH_LONG).show();
    }
 
    public void borderButton(View view) {
        Toast.makeText(getApplicationContext(), "Button dengan Border Color", Toast.LENGTH_LONG).show();
    }
}


Hasil potongan kode di atas adalah sebagai berikut:

No comments:

Post a Comment