XML, Views, Activity, Intents

A basic Introduction

Created by Saksham Agarwal / Piyush Bhopalka

Understanding Android Studio

XML and Views in Android

Getting reference from XML for TextView



TextView tv = (TextView)findViewById(R.id.my_text_view);

tv.setText("Hello text view");

					

Getting reference from XML for EditText



EditText editText = (EditText)findViewById(R.id.my_edit_text);

String s = editText.getText().toString();

          

Getting reference from XML for Button



Button b = (Button)findViewById(R.id.my_button);

b.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    /* Whatever you may want to do on Click */
  }
});
        

Make a simple Calculator

Two EditTexts For input

Four buttons, one each for basic arithmetic operations


Make a simple Calculator

After completion, check to see if you have done the same as here

Writing better code for Calculator

The same code can be written efficiently like this

Activity and Intents

Making a new Activity

Choose Empty Activity, not Blank Activity

Empty Activity

Calling the Activity that we just created



Intent i = new Intent(MainActivity.this, Activity2.class);

startActivity(i);
        

Attaching data to your intent from the calling activity



Intent i = new Intent(MainActivity.this, Activity2.class);

i.putExtra("my_key", "The text to be passed");

startActivity(i);

        

Receiving data at the callee activity


Intent i = getIntent();

String s = i.getStringExtra("my_key");
        

Intents

Explicit Intents - Intents within an app

Implicit Intents - Intents sent to another app on your device

Making Implicit Intents


String url = "http://www.facebook.com/groups/nitcdroid";

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

startActivity(i);
        

Providing a choice for choosing apps


String url = "http://www.facebook.com/groups/nitcdroid";

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

startActivity(Intent.createChooser(i, "Visit NITC-Droid using"));

        

PROJECT 2

Create a Contacts app which displays several contacts each having:

  • Contact image
  • Contact Name
  • Phone number

Optional: Clicking on the contact should open dialer with that number

Next slide contains a demo of how the app should look like

Demo of the Project

Your app should look something like this:

Contacts App Demo

THE END

- That's it for Week 1
- We just barely scratched through the tip of a few things. Self study is going to be the key.
- For deeper understanding, go through the Udacity Course or the Android Developer Website.
- Next week we will dive a little deeper into Views

Back to Home page