Kamis, 12 Februari 2015

Materi Pertemuan 2

Layout Linear
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#aa0000"
            android:gravity="center_horizontal"
            android:text="Merah" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="#aaaa00"
            android:gravity="center_vertical"
            android:text="Kuning" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#00aa00"
            android:gravity="center_horizontal"
            android:text="Hijau" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Baris pertama"
            android:textSize="15pt" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="Baris kedua"
            android:textSize="15pt" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Baris ketiga"
            android:textSize="15pt" />
    </LinearLayout>

</LinearLayout>


Layout Relative
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top" >

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type here:" />

    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/label" />

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/entry"
        android:layout_marginLeft="10dip"
        android:text="OK" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/ok"
        android:layout_below="@+id/entry"
        android:layout_toLeftOf="@+id/ok"
        android:text="Cancel" />

</RelativeLayout>



Layout Table
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1" >

    <TableRow>

        <TextView
            android:layout_column="1"
            android:padding="3dip"
            android:text="Open..." />

        <TextView
            android:gravity="right"
            android:padding="3dip"
            android:text="Ctrl-O" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_column="1"
            android:padding="3dip"
            android:text="Save As..." />

        <TextView
            android:gravity="right"
            android:padding="3dip"
            android:text="Ctrl-Shift-S" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FF909090" />

    <TableRow>

        <TextView
            android:padding="3dip"
            android:text="X" />

        <TextView
            android:padding="3dip"
            android:text="Import..." />
    </TableRow>

    <TableRow>

        <TextView
            android:padding="3dip"
            android:text="X" />

        <TextView
            android:padding="3dip"
            android:text="Export..." />

        <TextView
            android:gravity="left"
            android:padding="3dip"
            android:text="Ctrl-E" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FF909090" />

</TableLayout>


Layout MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ini Main-Activity"
        android:textColor="#000000" />

    <Button
        android:id="@+id/ButtonLinear"
        android:layout_width="fill_parent"
        android:layout_height="55px"
        android:text="Linear Layout"
        android:textSize="18px" >
    </Button>
   
    <Button
        android:id="@+id/ButtonRelative"
        android:layout_width="fill_parent"
        android:layout_height="55px"
        android:text="Relative Layout"
        android:textSize="18px" >
    </Button>
   
    <Button
        android:id="@+id/ButtonTable"
        android:layout_width="fill_parent"
        android:layout_height="55px"
        android:text="Table Layout"
        android:textSize="18px" >
    </Button>

</LinearLayout>


Class Java untuk MainActivity
package com.example.pertemuan1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button linearLayout = (Button) findViewById(R.id.ButtonLinear);
linearLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View linear) {
Intent myIntent = new Intent(linear.getContext(),
LinearActivity.class);
startActivityForResult(myIntent, 0);
}
});
Button relativeLayout = (Button) findViewById(R.id.ButtonRelative);
relativeLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View relative) {
Intent myIntent = new Intent(relative.getContext(),
RelativeActivity.class);
startActivityForResult(myIntent, 0);
}
});
Button tableLayout = (Button) findViewById(R.id.ButtonTable);
tableLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View table) {
Intent myIntent = new Intent(table.getContext(),
TableActivity.class);
startActivityForResult(myIntent, 0);
}
});
}

@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;
}


}


1 komentar: