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

Aligning Buttons in Android Layouts

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)
6 views7 pages

Aligning Buttons in Android Layouts

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

LAYOUT

BERMAIN DENGAN ALIGN /ANCHOR DI LAYAR


tools:context=".BermainAnchor" >

<RelativeLayout
xmlns:android="[Link]
d"
xmlns:tools="[Link]
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 [Link].latihan2;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
// 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
[Link](savedInstanceState);
// untuk ukuran
LayoutParams params1 = new [Link](
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LayoutParams params2 = new [Link](
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
//jenis layout
LinearLayout mylayout = new LinearLayout(this);
// orientasi layout
[Link]([Link]);
// buat view view
TextView username = new TextView(this);

[Link]("username");
[Link](params2);
Button login = new Button(this);
[Link]("Login");
[Link](params2);

[Link](username);
[Link](login);
[Link](mylayout, params1

);

LISTENER

File : activity_input.xml
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
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 : [Link]
package [Link].latihan2;
import
import
import
import
import
import
import
import

[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];

public class InputActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_input);
Button tombol = (Button)findViewById([Link].button1);
CheckBox check = (CheckBox)findViewById([Link].checkBox1);
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Button tombol = (Button)arg0;
if([Link]()== "Berubah")
{

[Link]("Tombol");
}
else
{
[Link]("Berubah");
}
//[Link](1);
[Link]([Link], "Anda menekan tombol",Toast.LENGTH_SHORT ).show();
}
});
[Link](new [Link]() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
[Link]([Link], "Nilai CheckBox : "+arg1,Toast.LENGTH_SHORT ).show();
}
});
Button tombol2 = (Button)findViewById([Link].button2);
Button tombol3 = (Button)findViewById([Link].button3);
Button tombol4 = (Button)findViewById([Link].button4);
[Link](klik);
[Link](klik);
[Link](klik);
}
[Link] klik = new [Link]() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Button tombol = (Button)arg0;
[Link]([Link], "Anda Menekan tombol : "+[Link](),
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([Link], menu);
return true;
}
}

You might also like