Android form to another form

Navigation from one form to another form is very important in android application because user usually navigate from main form to sub form by clicking a button, list box, and other controls. The android application in this tutorial is an example on how to navigate from main form to another form by clicking a button. There are more users who want to buy mobile devices like smart phones, iPhones and cell phones than buying a personal computer or laptop because those mobile devices are portable, handy and powerful. It can be used to access the Internet, shop online and access to social media like twitter and Facebook.

To begin our tutorials we need to write the following code for the activity_main.xml. This will serve as the main form of our android application. The code for activity_main.xml is listed below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/button1"
        android:layout_width="116dp"
        android:layout_height="wrap_content"
        android:text="Goto Next Form" />
</LinearLayout>

The main form of our android apps will look like this:

android form navigation

After we have done with the design of the main form for our android apps we need to have a Java code that will contain the instruction set or code on what the application will do when the user will click the button. The code MainActivity.java is listed below:
package com.kid.navigation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view) {
                Intent i = new Intent(getBaseContext(),insert.class);
                startActivity(i);
            }
        });

    }
}

After we have done with design and code for our main form we need to create another xml file for the interface or design of our sub form. In the left part of your android studio screen right click on layout then choose New Layout Resource File type insert in the box for the name of our xml file then replace the existing code with the code listed below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="276dp"
        android:layout_height="wrap_content"
        android:text="Goto Main Form" />
</LinearLayout>

The second form of our android apps will look like this:

button navigation android

After creating the design of our second form we need to create a Java code for the second form. IN the left part of your android studio right click on MainActivity then select New Java class and type insert as the name of Java class. The code for insert.java is listed below:

package com.kid.navigation;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class insert extends Activity
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.insert);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {

                setContentView(R.layout.activity_main);

            }
        });
    }
}

Finally we need to modify the AndroidManifest.xml file by adding the code listed below. Add the line of code above the </application>

<activity
            android:label="Form"
            android:name=".insert" >
            <intent-filter >
                <action android:name="q.w.e.qwe" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
</activity>