Pages

Monday, 24 February 2014

Intents --- Navigation from one page to another page

It is used to navigation from one page to another page. For example if you want to click one button that will go to another page then we have to use the Onclick listener for the button and the intenet for navigation. Let us see how to code
  • First go to Res--->Layout--->activity main.xml page.
  • In that page drag and drop the button from the Palette to your page.
  • Then Double click that button. Then change the android:text="Button" />  line to this format      android:text="@string/button" />
  • Then go to String.xml in the values folder.In that String.xml file add the following line to match the button that you have previously added.
  •  <string name="button">Click Here</string> button (must be similar to third step)
  • Now Go to MainActivity.Java bu expanding the src folder. 
  1. Now Declare the button  by Button b; Then place the cursor over the Button lot of options will appear you need to choose the Import  'Button'(android.widget)
  2. Now type the onclickListener();  .then place the cursor and click the create the onclickListener() method appears.
  3. Inside the method Onclick listener type the following b=(Button)findViewById(R.id.button1);
            b.setOnClickListener(new OnClickListener() 
  4. Now go to the srcfolder and right click the src and click new---> other--->android activity.
  5. Now go to the Mainactivity.java and type Intent i=new Intent(MainActivity.this,MainActivity1.class);
                    startActivity(i);
  6. That's it.
  7. Sample code.
  8. import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MainActivity extends Activity {

        Button b;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
              OnClickListener();
        }

        private void OnClickListener() {
            // TODO Auto-generated method stub
            b=(Button)findViewById(R.id.button1);
            b.setOnClickListener(new OnClickListener() {
              
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent i=new Intent(MainActivity.this,MainActivity1.class);
                    startActivity(i);
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }
     
     

No comments:

Post a Comment