In previous post we learned to How to Create New Activity to your android application. So we want to go to our new activity to see how it looks like! How to do it? Its just a simple piece of code.So just relax.Let us do it with in few steps..
We are going to do it using a 'Button' widget.See more about button widget here
STEP 1:
Go to your Activity's (Here 'MainActivity.java') layout (Here 'activity_main.xml' )
res > layout > activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
STEP 2:
Let us add a 'Button' below the TextView with an android:OnClick="onclick"
(you can assign any onclick value nstead of "onclick")
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="Next Activity"/>
</RelativeLayout>
You can now see your button is created with a text on it "NEXT ACTIVITY".Now we want to make that button to move to our new activity on clicking that button.
STEP 3:
Now go to your MainActivity
src > main>java >MainActivity.
Add a small piece of code in MainActivity for Make app to go to next activity on clicking our newly added button
Let us add a new method with name of method as the onClick value assigned.This is very important, as this method is called when button is clicked.So now we just have to insert code to tell the app to move to next activity.We are doing this using Intent
public void onclick(View v)
{
Intent intent = new Intent(this, MainActivity2.class);
this.startActivity(intent);
}
So our MainActivity.java file will now look like this;
package cooltechiz.blogspot.com.helloworld;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onclick(View v)
{
Intent intent = new Intent(this, MainActivity2.class);
this.startActivity(intent);
}
}
Click 'RUN' icon and launch the application.Then click on Button.It will move to our newly created Activity.
So we have finished some basic lessons of android programming.Hope you have enjoyed it..
No comments:
Post a Comment