0% found this document useful (0 votes)
36 views7 pages

Layout: Bermain Dengan Align /anchor Di Layar

The document discusses layouts in Android using XML and Java code. It shows how to create a RelativeLayout with buttons in different positions using anchor properties. It then demonstrates dynamically creating a LinearLayout with Java code to add views like TextView and Button. It also includes an example of an activity layout XML file with multiple buttons and a checkbox. The activity Java code sets onclick listeners for the buttons and oncheckchanged listener for the checkbox to display Toasts with click/check events.

Uploaded by

Hermawan Susilo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

Layout: Bermain Dengan Align /anchor Di Layar

The document discusses layouts in Android using XML and Java code. It shows how to create a RelativeLayout with buttons in different positions using anchor properties. It then demonstrates dynamically creating a LinearLayout with Java code to add views like TextView and Button. It also includes an example of an activity layout XML file with multiple buttons and a checkbox. The activity Java code sets onclick listeners for the buttons and oncheckchanged listener for the checkbox to display Toasts with click/check events.

Uploaded by

Hermawan Susilo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LAYOUT

BERMAIN DENGAN ALIGN /ANCHOR DI LAYAR


tools:context=".BermainAnchor" >

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Right"
android:layout_alignParentRight="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Right"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Left"
android:layout_alignParentBottom="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Center"
android:layout_centerInParent="true"/>
</RelativeLayout>

LAYOUT DENGAN BAHASA JAVA


package com.example.latihan2;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
// pakai code lebih cepat dari xml(sedikit), kelemahan code tidak ada preview design tampilannya
public class DinamicLayout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// untuk ukuran
LayoutParams params1 = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LayoutParams params2 = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
//jenis layout
LinearLayout mylayout = new LinearLayout(this);
// orientasi layout
mylayout.setOrientation(LinearLayout.VERTICAL);
// buat view view
TextView username = new TextView(this);

username.setText("username");
username.setLayoutParams(params2);
Button login = new Button(this);
login.setText("Login");
login.setLayoutParams(params2);

mylayout.addView(username);
mylayout.addView(login);
this.addContentView(mylayout, params1

);

LISTENER

File : activity_input.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".InputActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:text="Button" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="CheckBox" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="63dp"
android:text="Button1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="Button2" />
<Button

android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="Button3" />
</RelativeLayout>

File : InputActivity.java
package com.example.latihan2;
import
import
import
import
import
import
import
import

android.os.Bundle;
android.app.Activity;
android.view.Menu;
android.view.View;
android.widget.Button;
android.widget.CheckBox;
android.widget.CompoundButton;
android.widget.Toast;

public class InputActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
Button tombol = (Button)findViewById(R.id.button1);
CheckBox check = (CheckBox)findViewById(R.id.checkBox1);
tombol.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Button tombol = (Button)arg0;
if(tombol.getText()== "Berubah")
{

tombol.setText("Tombol");
}
else
{
tombol.setText("Berubah");
}
//tombol2.setVisibility(1);
Toast.makeText(InputActivity.this, "Anda menekan tombol",Toast.LENGTH_SHORT ).show();
}
});
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(InputActivity.this, "Nilai CheckBox : "+arg1,Toast.LENGTH_SHORT ).show();
}
});
Button tombol2 = (Button)findViewById(R.id.button2);
Button tombol3 = (Button)findViewById(R.id.button3);
Button tombol4 = (Button)findViewById(R.id.button4);
tombol2.setOnClickListener(klik);
tombol3.setOnClickListener(klik);
tombol4.setOnClickListener(klik);
}
View.OnClickListener klik = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Button tombol = (Button)arg0;
Toast.makeText(InputActivity.this, "Anda Menekan tombol : "+tombol.getText(),
Toast.LENGTH_SHORT).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.input, menu);
return true;
}
}

You might also like