230 likes | 347 Views
Irina Presa irina.presa@gmail.com. Interfata grafica. Cuprins. Views/Layouts Focus Evenimente Resurse Meniuri Ferestre Dialog Notificari. Views. Ce sunt ? - blocurile de baza ale interfetei - interactioneaza cu utilizatorii prin evenimente Unde le definim ? - direct in cod
E N D
Irina Presa irina.presa@gmail.com Interfata grafica
Cuprins • Views/Layouts • Focus • Evenimente • Resurse • Meniuri • Ferestre Dialog • Notificari
Views • Ce sunt ? - blocurile de baza ale interfetei - interactioneaza cu utilizatorii prin evenimente • Unde le definim ? - direct in cod - intr-un fisier XML (resursa) • Ce definim ? - culori, dimensiuni, coordonate - obiecte grafice continute - handlere pentru evenimente
Custom Views • Extindem clasa View sau pornim de la un View existent - suprascriem metode/handlere de evenimente - preluam atribute din resurse XML, sau le definim dinamic in cod • Compound Control - grupam mai multe View-uri intr-un singur - definim layout container
Adapter View • Ce este ? - subclasa ViewGroup - View-urile copii sunt determinate de un Adapter ce aduna date de un anumit tip • Utilizare - pentru a afisa date intr-un layout - ex: Gallery, ListView, Spinner, etc • Responsabilitati - umple layoutul cu date - trateaza selectiile utilizatorilor (OnItemClickListener) • Exemplu utilizare - populeaza un spinner cu un vector de culori, sau o lista cu contacte din agenda
Layouts Organizare ierarhica, avand la baza un ViewGroup • AbsoluteLayout - coordonatele exacte ale copiilor • RelativeLayout - above, below, toLeftOf • FrameLayout - frame per copil (top left) • LinearLayout - orizontal/vertical Definire coordonate (LayoutParams) • Prin numere • Fill parent • Wrap content(getMeasuredWidth/getWidth)
Focus • OnFocusChangeListener • View().requestFocus • Control next focus ( nextFocusDown, nextFocusLeft nextFocusRight, nextFocusUp) <LinearLayout android:orientation="vertical" ..> <Button android:id="@+id/top" android:nextFocusUp="@+id/bottom" ... /> <Button android:id="@+id/bottom" android:nextFocusDown="@+id/top" ... /> </LinearLayout>
Evenimente Subiect - Button.setOnClickListener(OnClickListener listener) Observator - listener.onClick(View v)
Resurse • res/anim - animatii frame-by-frame definite in XML • res/drawable - imagini png si jpg • res/layout - reprezentari XML ale obiectelor View • res/values - XML pentru siruri de caractere, culori, stiluri, dimensiuni si vectori • res/xml - fisiere xml definite de utilizator • res/raw - fisiere arbitrare si necompilate ce pot fi adaugate
Resurse Referinte • Din cod (clasa R) : - resursa cu id-ul review_image (ImageView) findViewById(R.id.review_image); - fisierul review_detail.xml din folderul res/layout this.setContentView(R.layout.review_detail); • In XML : - atribuire id android:id="@+id/nume_id - referire alta resursa @id/nume_id - referire valoare externa @tip/nume_resursa (ex: @string/rating_label)
Resurse Valori externe • string.xml <string> name="menu_get_reviews">Get reviews</string> • styles.xml <style name="label"> <item name="android:textSize">18sp</item> <item name="android:textColor">#ffffff</item> </style> • arrays.xml <array name="cuisines"> <item>American</item> <item>Barbeque</item> <item>French</item> <array>
Resurse Exemplu definire layout + view-uri (review_detail.xml) <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:padding="10px" android.setVerticalScrollBarEnabled="true" > <ImageView android:id="@+id/review_image" android:layout_width="100px" android:layout_height="100px" android:layout_marginLeft="10px" android:layout_marginBottom="5px" />
Resurse <TextView android:id="@+id/name_detail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/review_image" android:layout_marginLeft="10px" android:layout_marginBottom="5px" style="@style/intro_blurb" /> <TextView android:id="@+id/rating_label_detail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name_detail" android:layout_marginLeft="10px" android:layout_marginBottom="5px" style="@style/label" android:text="@string/rating_label" />
Meniuri • Options Menu - Icon menu (max 6, suporta iconuri) - Expanded Menu • Context Menu - meniu dinamic ce poate aparea la un click prelungit pe un View • Submenu
Meniuri Cum le cream ? Best Practice : Definim meniul intr-o resursa XML si il integram in cod cu ajutorul unui MeniuInflater (Separam designul interfetei de codul aplicatiei) XML : menu, item, group <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" /> <item android:id="@+id/quit" android:icon="@drawable/ic_quit" android:title="@string/quit" /> </menu>
Meniuri Cum le integram ? public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.new_game: newGame(); return true; case R.id.quit: quit(); return true; } • addIntentOptions : adauga intenturi, pt a incarca activity-uri dinamic
Ferestre Dialog • AlertDialog - 0-3 butoane, lista de elemente selectabile • ProgressDialog - progress bar/wheel • DatePickerDialog • TimePickerDialog • Custom Dialog - AlertDialog.Builder + LayoutInflater
Notificari • A Toast Notification • A Status Bar Notification - remind-uri persistente, necesita raspunsul utilizatorului, icon in status bar - configurabil pt notificari (sunet, vibratii, lumini) • A Dialog Notification - notificari legate de Activity
Status Bar Notification • Utilizare - pentru a primi notificari de la un serviciu ce ruleaza in background • Creare - NotificationManager String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); - Instantiere Notification notification = new Notification(icon, tickerText, when); - Mesaj expandat + Intent notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); • Notificare mNotificationManager.notify(ID, notification); • Personalizare defaults, vibrate, sounds, ledARGB
Resurse [1] View-uri din Android API [2] API Samples Unlocking Android. A Developer's Guide [1] http://developer.android.com/guide/tutorials/views/index.html [2] http://developer.android.com/resources/samples/