Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Development

Android Development

Wingnity Team

November 10, 2014
Tweet

Transcript

  1. Agenda • Views • ViewGroups • Layouts • Using Styles

    as themes • Applying simple Style • Intents and Bundles • Sharing data between Activities
  2. Views • View is the base class for all UI

    components. View Buttons Check boxes Layouts Spinners Text Views Edit Texts Others
  3. View Groups • Can contains other view groups or view

    • Base class for layouts and view containers • Just to make it simple: – Layout = ViewGroup – Widget = View
  4. Layout • Layout defines the structure for the user interface

    • Holds all the elements that appear to the user • Elements are instantiated at runtime • Some useful layouts: – Linear Layout – Relative Layout – Table Layout – GridView – ListView
  5. Weight & WeightSum in Linear Layout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0" // yellow /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f00" // red /> </LinearLayout>
  6. Equal Weight <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout

    android:layout_width="match_parent" android:layout_height="match_parent“ android:layout_weight="1" android:background="#ff0" // yellow /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent“ android:layout_weight="1" android:background="#f00" // red /> </LinearLayout>
  7. Changing Weight <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height=“match_parent" android:orientation="vertical" > <LinearLayout

    android:layout_width="match_parent" android:layout_height="match_parent“ android:layout_weight=“2" android:background="#ff0" // yellow /> <LinearLayout android:layout_width="match_parent" android:layout_height=“match_parent“ android:layout_weight="1" android:background="#f00" // red /> </LinearLayout>
  8. 0dp…? <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height=“match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent"

    android:layout_height="0dp“ android:layout_weight=“2" android:background="#ff0" // yellow /> <LinearLayout android:layout_width="match_parent" android:layout_height=“0dp“ android:layout_weight="1" android:background="#f00" // red /> </LinearLayout>
  9. WeightSum <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height=“match_parent" android:orientation="vertical“ android:weightSum="6" > <LinearLayout

    android:layout_width="match_parent" android:layout_height="0dp“ android:layout_weight=“2" android:background="#ff0" // yellow /> <LinearLayout android:layout_width="match_parent" android:layout_height=“0dp“ android:layout_weight="1" android:background="#f00" // red /> </LinearLayout>
  10. Relative Layout • Arranges its children relative to other views

    • Suggested to use for simple layouts • ex- Login screen
  11. Table Layout • Arranges its children in rows. • Where

    to use? • Ans: When your layout requires more Linear Layouts use Table Layout
  12. Explanation 1. Button btn = (Button)findViewById(R.id.button1); • Creating a new

    object of type Button. • A method to find views in xml files • (Button) means – Casting the view into button type.
  13. Intents • Intent – Intention to do some action. •

    Intents are generally used to launch new activities. Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent);
  14. Bundle • A mapping from String values to various Parcelable

    types. • Bundles are generally used to hold the values received from a different activity. • Sample code: Bundle bundle = getIntent().getExtras(); String data = bundle.getString("key");
  15. Sharing data between Activities MainActivity: Intent intent = new Intent(getApplicationContext(),

    SecondActivity.class); intent.putExtra("name", etName.getText().toString()); startActivity(intent); SecondActivity: Bundle bundle = getIntent().getExtras(); if (bundle != null) { TextView tvHello = (TextView) findViewById(R.id.tv_hello); String data = bundle.getString("name"); tvHello.setText("Hi " + data); }
  16. List Views • Shows a list of scrollable items •

    Adapter takes care of items insertion and deletion. Sample code: ListView list = (ListView) findViewById(R.id.listView); String[] objects = { "Apple", "Blueberry", "Fig", "Grapes", "Lemon", "Lime", "Orange", "Peach", "Pear", "Strawberry", "Cherry" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>( getApplicationContext(), android.R.layout.simple_list_item_1, objects); list.setAdapter(adapter);
  17. Grid Views • Similar to ListView • It can show

    a list of scrollable items in two dimension. Sample code: GridView list = (GridView) findViewById(R.id.GridView1); String[] objects = { "Apple", "Blueberry", "Fig", "Grapes", "Lemon", "Lime", "Orange", "Peach", "Pear", "Strawberry", "Cherry" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>( getApplicationContext(), android.R.layout.simple_list_item_1, objects); list.setAdapter(adapter);
  18. Styles in Android <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dp" android:padding="4dp" android:textColor="#FF0" android:text="@string/hello_world"

    /> • Style is a bunch of properties that can change the look and feel of Android Views. • You can specify properties like height, width, margin, padding, color, background and many other properties. <TextView style="@style/MyDefaultText" android:text="@string/hello_world" />
  19. How to Create and Apply Styles <?xml version="1.0" encoding="utf-8"?> <resources>

    <style name="MyDefaultText" parent="@android:style/TextAppearance.Medium"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_margin">4dp</item> <item name="android:padding">4dp</item> <item name="android:textColor">#FF0</item> </style> </resources> • You can define multiples styles inside your project/res/values/styles.xml.
  20. Q&A