Android-Logo

* Most of the material is taken from these youtube videos.
Official Documentation can be found here.

Keywords and Concepts


Options


Databases

Misc.


Examples


When button "about" (id) is clicked, show the page "aboutPage"
public class MainActivity extends AppCompatActivity {

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

        Button aboutBtn = (Button) findViewById(R.id.about);

        aboutBtn.setOnClickListener(new View.OnClickListener() {  //event handling
            @Override
            public void onClick(View v) {
                gotoAbout();					  //CallBack method
            }
        });
    }

    private void gotoAbout() {
        Intent intent = new Intent(this, aboutPage.class);
        startActivity(intent);
    }
}

An easier approach is to add android:onClick="clickBtn" to the button attributes in the xml file, and define the clickBtn in java code as follows:
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void clickBtn(View view) {
        Intent intent = new Intent(this, aboutPage.class);
    	startActivity(intent);
    }
}

To pass information along with transition to the new page: (suppose we have a text field (id: textField) in the main page where user enters his name. when he presses the button we want to go to about page and show his name in a TextView element (id: aboutPageText))

We need to extend clickBtn as follows
    public void clickBtn(View view) {
        Intent intent = new Intent(this, aboutPage.class);

        final EditText textField1 = (EditText) findViewById(R.id.textField); //referrence to input field
        String userMsg = textField1.getText().toString();
        intent.putExtra("Msg", userMsg);

    	startActivity(intent);
    }

and add the following to aboutPage.java in the onCreate method:
    Bundle comingInfo = getIntent().getExtras();
    if (comingInfo==null) {
	return;
    }
    String MsgContent = comingInfo.getString("Msg");
    final TextView newPageText = (TextView) findViewById(R.id.aboutPageText);
    newPageText.setText(MsgContent);