Sunday, October 6, 2013

How to call another activity from a Button ?

To call another activity from a button or a menu-like component, proceed as follows:

1. Calling XML file (activity_serve_motion.xml)
Add   android:onClick="ServeMotionStaggered"
where ServeMotionStaggered is the next activity XML

<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ServeMotionStaggered"
        android:text="Staggered" />

2. Add a method to call ServeMotionStaggeredActivity in Java file (ServeMotionActivity.java)

       public void ServeMotionStaggered(View view) {
     // Do something in response to button
Intent intent = new Intent(this, ServeMotionStaggeredActivity.class);
startActivity(intent);
}

3. Create ServeMotionStaggeredActivity as follows:-
a) Put your cursor at src
b) Right-click and select Others
c) Select Andorid Activity
d) Select Blank Activity
e) Change Activity name to ServeMotionStaggeredActivity
f) Click Finish

4. Run your apps

5. Click your button and the new Tab displaying 'Hello' shoudl display

No comments:

Post a Comment