Android components


Ivan Kocijan  -  ikocijan0@gmail.com
Milan  Jovanović  -  milan.jovanovic24@gmail.com

Content

  • Android Manifest
  • build.gradle file
  • Resources
  • Layout components
  • Layout
  • Intent
  • Activity

Main components


  • Java code
  • Resources
  • Android Manifest
  • But before all this say hello to your new best friend, build.gradle file


Android manifest


  • General application settings
  • What can your application do - use Internet, write to storage,  vibrate...
  • Contains list of Activities, Services, Receivers

  • Every activity needs to be declared in Android Manifest
  • Only one activity can be started when your application launches
 




Permissions

  • Your application needs to ask user for permissions in some cases
  • eg. User needs to allow your application to use the Internet
  • All permissions are declared in the Android Manifest

<uses-permission android:name="android.permission.INTERNET"/>


Java code


Everything you love and hate from java



Activity

  • Implements functionality for UI
  • Your class needs to extend Android Activity class
  • Almost all activities interact with the user
  • Activity class creates a window in which you place your UI
  • The entire lifecycle of an activity is defined by Activity methods
  • You  need to call  superclass method when implementing these methods in you activity

protected void onCreate(Bundle savedInstanceState);             protected void onStart();protected void onRestart();protected void onResume();protected void onPause();protected void onStop();protected void onDestroy();

Activity backstack


Activity example

Intent

  • Explicit Intent
 
 Intent i = new Intent (Context, Activity.class); startActivity(i);
  • Implicit intent
Intent i = new Intent (Intent.ACTION_VIEW);i.setData(Uri.parse("http://natjecanje.tvz.hr"));startActivity(i);

Starting new activity

 public class MainActivity extends Activity {

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

     Intent intent = new Intent(this, NewActivity.class);
     startActivity(intent);

    }

}

Android Manifest

<activity android:name=".NewActivity"/>

Resources

  • Images, strings, layouts, colors...
  • All resources are located in res/directory
  • Every resource has a generated id in R class
  • Resources can be universal for all devices
  • Resources can be different for different devices and can depend upon screen size, language...



Some resources

  • res/color -  colors
  • drawable - png., 9.png, jpg and gif files
  • layout - Application User Interface
  • menu - xml files for application menus
  • raw - audio files...

Layout

  • Defines the visual structure for a user interface
  • XML file located in res/layout
  • Contains UI elements - TextView, Button...
  • Each element in layout.xml file extends View class

www.itsolutions.eu

Some views

  • Button
  • Text View
  • Linear Layout
  • Relative Layout
  • ImageView
  • .....

Button

    <Button      android:id="@+id/button_id"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Button"/>

TextView

     <TextView
      android:text="@string/hello_world"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"                      android:textColor="#000000"      android:textSize="20sp"/>

Linear Layout


<LinearLayout   
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center">
</LinearLayout>


Linear layout - orientation

Linear layout - orientation

We lost two buttons :(


Use weight


How to bind layout with activity?





Practice - Button

Practice - Linear layout

Practice - Relative layout


Practice - TextView

Practice - ImageView

Practice - EditText

Practice - Activity

Practice - Intent




Homework


        Examples from this workshop    


    Read more

    Android components - Workshop 1 - part 2

    By Ivan Kocijan Koc

    Android components - Workshop 1 - part 2

    Resources, Layout components, Layout, Intent, Activity

    • 1,963