This shows you the differences between two versions of the page.
mdad:laboratoare:03 [2020/10/07 13:55] 127.0.0.1 external edit |
mdad:laboratoare:03 [2020/11/18 17:24] (current) ioana_maria.culic |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Laboratorul 03. ===== | + | ====== Lab 03 - Android Interfaces====== |
- | **Intents** \\ | + | **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.\\ | |
- | An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases:\\ | + | LinearLayout has one goal: lay out children in a single row or column (depending if the android:orientation is horizontal or vertical).\\ |
- | - Starting an activity\\ | + | Other Layouts: RelativeLayout, FrameLayout, GridLayout, CoordinatorLayout etc\\ |
- | - Starting a service\\ | + | |
- | - Delivering a broadcast\\ | + | |
\\ | \\ | ||
- | An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.\\ | + | {{:dapm:laboratoare:linearlayout_vs_relativelayout.png?600|}} |
\\ | \\ | ||
- | {{:mdad:laboratoare:lab3_intent.png?500|}}\\ | ||
- | How an implicit intent is delivered through the system to start another activity:\\ | ||
- | [1] Activity A creates an Intent with an action description and passes it to startActivity().[2] The Android System searches all apps for an intent filter that matches the intent. When a match is found, [3] the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent. \\ | ||
\\ | \\ | ||
- | Example to start an activity\\ | + | **Fill_parent**(renamed **MATCH_PARENT** in API Level 8 and higher) vs **Wrap_content** \\ |
- | Intent intent= new Intent(this, SecondClass.class); | + | - Setting the layout of a widget to __fill_parent__ will force it to expand to take up as much space as available within the layout element it's been placed in. It's the rough equivalent to setting the dockstyle of a Windows Form Control to Fill.\\ |
- | startActivity(intent); | + | - 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.\\ |
\\ | \\ | ||
- | **Extras**\\ | + | {{:dapm:laboratoare:vertical_vs_horizontal_linearlayout.png?700|}} |
- | Key-value pairs that carry additional information required to accomplish the requested action. Just as some actions use particular kinds of data URIs, some actions also use particular extras.\\ | + | |
\\ | \\ | ||
- | Example: send a string to the secondActivity\\ | ||
- | //In the firstActivity | ||
- | Intent i = new Intent(FirstActivity.this, SecondActivity.class); | ||
- | String strName = "Lab3 is great!"; | ||
- | i.putExtra("STRING_I_NEED", strName); | ||
- | |||
- | //In the secondActivity | ||
- | public void onCreate(Bundle savedInstanceState) { | ||
- | super.onCreate(savedInstanceState); | ||
- | setContentView(R.layout.secondactivity); | ||
- | | ||
- | Intent intent = getIntent(); | ||
- | Bundle extras = intent.getExtras(); | ||
- | if(extras != null) { | ||
- | String getString= extras.getString("STRING_I_NEED"); | ||
- | } | ||
- | } | ||
- | |||
- | |||
- | You can add extra data with various putExtra() methods, each accepting two parameters: the key name and the value. You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras(). | ||
\\ | \\ | ||
+ | **Difference Between OnClickListener vs OnClick:**\\ | ||
+ | __OnClickListener__ is what waits for someone to actually click on the element while __onclick__ determines what happens after the click\\ | ||
+ | An OnClickListener enables 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.\\ | ||
\\ | \\ | ||
- | When you use an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the **manifest file** of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. | + | 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 the need to implement any interface.\\ |
+ | The onClick function binding in XML Layout is a binding between onClick and the function that it calls. The function needs to have one argument (the View) in order for onClick to function. | ||
- | __Activity example from a manifest file__\\ | + | //example :// \\ |
- | <activity android:name="MainActivity"> | + | setOnClickListener Code Implementation:\\ |
- | <!-- This activity is the main entry, should appear in app launcher --> | + | Button btn = (Button) findViewById(R.id.mybutton); |
- | <intent-filter> | + | btn.setOnClickListener(new View.OnClickListener() { |
- | <action android:name="android.intent.action.MAIN" /> | + | @Override |
- | <category android:name="android.intent.category.LAUNCHER" /> | + | public void onClick(View v) { |
- | </intent-filter> | + | myFancyMethod(v); |
- | </activity> | + | } |
- | + | }); | |
- | <activity android:name="ShareActivity"> | + | // some more code |
- | <!-- For example, here's an activity declaration with an intent filter to receive an ACTION_SEND intent when the data type is text: --> | + | public void myFancyMethod(View v) { |
- | <intent-filter> | + | // does something very interesting |
- | <action android:name="android.intent.action.SEND"/> | + | } |
- | <category android:name="android.intent.category.DEFAULT"/> | + | |
- | <data android:mimeType="text/plain"/> | + | XML Implementation:\\ |
- | </intent-filter> | + | <Button android:id="@+id/mybutton" |
- | </activity> | + | android:layout_width="wrap_content" |
- | + | android:layout_height="wrap_content" | |
- | ====== Exercises : ====== | + | android:text="Click me!" |
+ | android:onClick="myFancyMethod" /> | ||
+ | |||
+ | **LOGS** \\ | ||
+ | System logs are the most important information that DDMS can provide. \\ | ||
+ | System logs describe all the actions performed by the device, the exceptions that occurred, and the information required for troubleshooting. The logs will appear in the panel named LogCat (do not confuse with Console). Each message in this list is accompanied by the following data (each one per column):\\ | ||
+ | |||
+ | - Type of message\\ | ||
+ | * **I** - Information - informative message\\ | ||
+ | * **D** - Debug - useful debugging message\\ | ||
+ | * **W** - Warning - warning message (exceptions that are not very important and do not have an important impact on the component appear as such messages)\\ | ||
+ | * **E** - Error - error message (exceptions that break a component appear as such messages)\\ | ||
+ | * **V** - Verbose - additional information displayed by the programs (generally on request, they are used to detect problems generally resulting from misconfiguration of programs)\\ | ||
+ | |||
+ | - Time - Date and time at which the message was written\\ | ||
+ | - PID - The ID of the process that generated the message\\ | ||
+ | - Tag - Label or category of message (useful for filters)\\ | ||
+ | - Message - the actual message (text)\\ | ||
+ | |||
+ | |||
+ | ====== Exercises ====== | ||
- | **Ex 1 ** | + | **Ex 1** A) Create a new app. Transform the layout into a LinearLayout with __orientation vertical__. \\ |
- | Make a new project with 2 activities (MainActivity and FindTheNumberActivity. | + | B) Add two Buttons with __width match_parent__ and __height wrap_content__.\\ |
- | Add the seccondActivity (FindTheNumberActivity) in manifest file\\ | + | On click the first one should show a message using the __Toast__. \\ |
- | \\ | + | |
- | **Ex 2 ** | + | |
- | MainActivity will be a login Page like in the picture.\\ | + | |
- | Add an action to the button: when clicked, it will check the username and the password field, and if it's __correct__, it will redirect you to the secondActivity (FindTheNumberActivity). \\ | + | |
- | Make the username and the password be the string __student__.\\ | + | |
- | **Attention !** \\ | + | |
- | You get the text from an editText with "getText()" method, and you convert it to string with "toString()" method.\\ | + | |
- | The password and the username field are editText. And the text from the password field will have to be hidden (add android:inputType="textPassword")\\ | + | |
- | {{:mdad:laboratoare:lab3_login.png?255|}} | + | |
- | **Ex 3 ** | + | For the first button use the **android:onClick="push"** – with __push function__ \\ |
- | Make the mini-game like in the picture. If the number is lower, show a toast with the message "Lower", and if it is higher with the message "Higher". If the number is correct, reset the number and show another toast with "You won! Try again" message.\\ | + | For the second button use the **set on click listener.**\\ |
- | {{:mdad:laboratoare:lab3_findthenumber.png?250|}} | + | |
- | \\ | + | Toast.makeText(MainActivity.this,"You pushed the button from on create", Toast.LENGTH_LONG).show(); |
- | **Ex 4 ** | + | Toast.makeText(this, "You pushed the button from push function", Toast.LENGTH_SHORT).show(); |
- | In the FindTheNumberActivity, when the button is cliked, change it's color (red<->blue) | + | |
+ | **Ex 2** In the onCreate function, write logs (Verbose, Debug, Info, Warning, Error). | ||
+ | View them using 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)\\ | ||
+ | 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** Create 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 (). | ||
+ | |||
+ | •onCreate | ||
+ | •onRestart | ||
+ | •onStart | ||
+ | •onResume | ||
+ | •onPause | ||
+ | •onStop | ||
+ | •onDestroy | ||
+ | |||
+ | **Ex 4** Write Logs in all the functions above and turn off the app. View them in Logcat |