Android Studio
Lab session
Layouts
● Relative Layouts
○ is a layout which arranges views/widgets/viewGroup according to the position
of other views/widgets/viewGroups i.e the new views are placed relative to the
already existing views
○ Most commonly used layout type
…cont’d
…cont’d
…cont’d
…cont’d
Android Activity Lifecycle – has 7 methods
● onCreate() – Called when the activity is first created
● onStart() – Called just after it’s creation or by restart method after onStop(). Here
Activity start becoming visible to user
● onResume() – Called when Activity is visible to user and user can interact with it
● onPause() – Called when Activity content is not visible because user resume previous
activity
● onStop() – Called when activity is not visible to user because some other activity takes
place of it
● onRestart() – Called when user comes on screen or resume the activity which was
stopped
● onDestroy – Called when Activity is not in background
onCreate()
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}
onStart(), onRestart()
protected void onStart() {
[Link]();
}
@Override
protected void onRestart() {
[Link]();
}
onPause(), onResume()
protected void onPause() {
[Link]();
protected void onResume() {
[Link]();
}
onStop(), onDestroy()
protected void onStop() {
[Link]();
protected void onDestroy() {
[Link]();
}
Toast Message
● A toast provides simple feedback about an operation in a small popup.
● It only fills the amount of space required for the message and the current activity
remains visible and interactive.
● Toasts automatically disappear after a timeout.
How to use a Toast
● Instantiate a Toast object (e.g Toast t1 ; )
● Use makeText() method with the following information:
○ Context
○ Text or String to be displayed
○ Time length
● Use show() to display the Toast message
● Example:
Context context=getApplicationContext();
CharSequence msg=“This is a Toast”;
int length=[Link];
Toast t=[Link](context,msg,length).show();
Adding Vector Asset
● Vector Assets in Android Studio helps to add material icons and import Scalable
Vector Graphics (SVG) and Adobe Photoshop Document files into your project
as vector drawable resources.
● Image scalability is the major advantage of using the vector drawable.
● There are many formats of vector asset files:
○ Ai
○ Svg
○ Pdf
○ Eps
● Follow the following steps to implement vector assets
Implement Vector Asset
● Step 1: Right-click on drawable > New > Vector Asset
● After clicking on Vector Asset a new dialog box is opened that is shown below.
● Step 2: Click on Clip Art and Search for the Icons and click ok button
● Step 3: Change the color of the icon
● Step 4: Click Next Step
● 5: Now Click on Finish Button
● Step 6: Icon is created in the drawable folder
● Now you can refer it to in your xml file
Image Asset
● Helps you generate your own app icons from material icons, custom
images, and text strings.
● Follow the following step to include Image asset
● Step 1: First click on drawable > New > Image Asset.
● Step 2: Then a new dialog box will appear. Here click on the Image option.
● Step 3: Then search for the image you are looking for. And then upload it.
● Step 4: Now Click on Next.
● Step 5: Click on Finish.