Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dapm:laboratoare:02 [2018/03/08 15:17]
ioana_maria.culic
dapm:laboratoare:02 [2021/03/23 10:43] (current)
ioana_maria.culic [Lab 02 - Layouts]
Line 1: Line 1:
-====== Lab 2 ======+====== Lab 02 - Layouts ​======
 **LinearLayout**\\ **LinearLayout**\\
 LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the __android:​orientation__ attribute.\\ LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the __android:​orientation__ attribute.\\
Line 8: Line 8:
 \\ \\
 \\ \\
-**Fill_parent vs Wrap_content** \\+ 
 +** Example: ** 
 + 
 +<​code>​ 
 +<​LinearLayour xmlns:​android="​http://​schemas.android.com/​apk/​res/​android"​ 
 +   ​android:​layout_width="​fill_parent"​ 
 +   ​android:​layout_height="​fill_parent"​ 
 +   ​android:​orientation="​vertical">​ 
 +    
 +..... 
 +</​LinearLayout>​ 
 +</​code>​ 
 +**Fill_parent**(renamed **MATCH_PARENT** in API Level 8 and higher) ​vs **Wrap_content** \\
 - Setting the layout of a widget to __fill_parent__ will force it to expand to take up as much space as is available within the layout element it's been placed in. It's roughly equivalent of setting the dockstyle of a Windows Form Control to Fill.\\ - Setting the layout of a widget to __fill_parent__ will force it to expand to take up as much space as is available within the layout element it's been placed in. It's roughly equivalent of setting the dockstyle of a Windows Form Control to Fill.\\
 - Setting a View's size to __wrap_content__ will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown.\\ - Setting a View's size to __wrap_content__ will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown.\\
Line 15: Line 27:
 \\ \\
 \\ \\
 +**Difference Between OnClickListener vs OnClick:​**\\
 +__OnClickListener__ is what waits for someone to actually click, __onclick__ determines what happens when someone clicks\\
 +An OnClickListener enable you to separate the action/​behavior of the click event from the View that triggers the event. \\
 +Since OnClickListener is an interface, the class that implements it has flexibilities in determining the instance variables and methods that it needs in order to handle the event.\\
 +\\
 +Lately android added a xml attribute to views called android:​onclick,​ that can be used to handle clicks directly in the view's activity without need to implement any interface.\\
 +The onClick with function binding in XML Layout is a binding between onClick and the function that it will call. The function have to have one argument (the View) in order for onClick to function.
 +
 +//example :// \\
 +setOnClickListener Code Implementation:​\\
 +    Button btn = (Button) findViewById(R.id.mybutton);​
 +    btn.setOnClickListener(new View.OnClickListener() {
 +        @Override
 +        public void onClick(View v) {
 +        myFancyMethod(v);​
 +        }
 +    });
 +    // some more code
 +    public void myFancyMethod(View v) {
 +        // does something very interesting
 +    }
 +
 +XML Implementation:​\\
 +    <Button android:​id="​@+id/​mybutton"​
 +        android:​layout_width="​wrap_content"​
 +        android:​layout_height="​wrap_content"​
 +        android:​text="​Click me!"
 +        android:​onClick="​myFancyMethod"​ />
 +
 **LOGS** \\ **LOGS** \\
 System logs are the most important information that DDMS can provide. \\ System logs are the most important information that DDMS can provide. \\
Line 32: Line 73:
  
  
-====== Exercises ​======+====== Exercises ======
    
-__Ex 1.__  ​A) Make a new app. Transform the layout into a LinearLayout with orientation vertical.\\ +**Ex 1** A) Make a new app. Transform the layout into a LinearLayout with __orientation vertical__. \\ 
-B) Make two Buttons with width match_parent ​and height wrap_content.\\ +B) Make two Buttons with __width match_parent__ ​and __height wrap_content__.\\ 
-On click the first one must show messages using the Toast. \\+On click the first one must show messages using the __Toast__. \\
  
-For the first button use the **android:​onClick="​push"​** – with push function ​\\+For the first button use the **android:​onClick="​push"​** – with __push function__ ​\\
 For the second button use the **set on click listener.**\\ For the second button use the **set on click listener.**\\
  
-  ​Toast.makeText(MainActivity.this,"​You pushed the button from on create",​ Toast.LENGTH_LONG).show();​ +   Toast.makeText(MainActivity.this,"​You pushed the button from on create",​ Toast.LENGTH_LONG).show();​ 
-  Toast.makeText(this,​ "You pushed the button from push function",​ Toast.LENGTH_SHORT).show();​+   ​Toast.makeText(this,​ "You pushed the button from push function",​ Toast.LENGTH_SHORT).show();​
  
-__Ex 2.__  ​In onCreate function, write logs (Verbose, Debug, Info, Warning, Error).\\ +**Ex 2** In onCreate function, write logs (Verbose, Debug, Info, Warning, Error).
-  Logcat message format : Log.d(tag, message)+
 View them with Logcat (The Logcat window in Android Studio displays system messages, such as when a garbage collection occurs.\\ View them with Logcat (The Logcat window in Android Studio displays system messages, such as when a garbage collection occurs.\\
 It displays messages in real time and keeps a history so you can view older messages)\\ It displays messages in real time and keeps a history so you can view older messages)\\
 The **tag** of a system log message is a short string indicating the system component from which the message originates. The **tag** of a system log message is a short string indicating the system component from which the message originates.
 +   ​Logcat message format : Log.d(tag, message)
  
-__Ex 3.__ Do an activity overwritten by the following event features. Display a log message with the Event Tag in each of these features. The log message text must contain the name of the event.\\+**Ex 3** Do an activity overwritten by the following event features. Display a log message with the Event Tag in each of these features. The log message text must contain the name of the event.\\
 For example, in onCreate() you can display a text message called onCreate (). For example, in onCreate() you can display a text message called onCreate ().
  
Line 60: Line 101:
    ​•onStop    ​•onStop
    ​•onDestroy    ​•onDestroy
-    
-__Ex 4.__ Write Logs in all the functions above and turn off the app. View them in Logcat 
  
 +**Ex 4** Write Logs in all the functions above and turn off the app. View them in Logcat
dapm/laboratoare/02.1520515025.txt.gz · Last modified: 2018/03/08 15:17 by ioana_maria.culic
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0