Kamis, Maret 15, 2012

Cara Pindah Kelas di Android (2.2 - Froyo)



di main.xml -> res/layout


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Here!"
/>


di main.java -> activity ke-1 :

public class main extends Activity implements android.view.View.OnClickListener{
/** Called when the activity is first created. */
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button1);

button.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==button){
Intent i = new Intent(getApplicationContext(),menu.class);
startActivity(i);
}
}

buat "xml file" & "java class" baru, dengan nama menu.xml dan menu.java.

di menu.xml -> res/layout


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Moved Class Success!" />


di menu.java -> activity ke-2 :

public class menu extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
}
}

hasilnya seperti ini :