Friday, 13 January 2017

Android resources

Android Resources are managed in a folder named as res under the application package.In this folder we can manage our external or internal resources that are used by android system whenever these are required and resources can be accessed by class represented by R keyword.Resources can be layout files,strings,images,color,drawable files (used for designing of views or to store images ),icon images etc.This can be easily understood by the following image -

Some of the resources folders are not available default but can be created whenever is used like animation,raw etc.Android Resources folder are explained below -

1
anim/
XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim class.
2
color/
XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class.
3
drawable/
Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists, shapes, animation drawable. They are saved in res/drawable/ and accessed from the R.drawable class.
4
layout/
XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout class.
5
menu/
XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. They are saved in res/menu/ and accessed from the R.menu class.
6
raw/
Arbitrary files to save in their raw form. You need to call Resources.openRawResource() with the resource ID, which is R.raw.filename to open such raw files.
7
values/
XML files that contain simple values, such as strings, integers, and colors. For example, here are some filename conventions for resources you can create in this directory −
  • arrays.xml for resource arrays, and accessed from the R.array class.
  • integers.xml for resource integers, and accessed from the R.integer class.
  • bools.xml for resource boolean, and accessed from the R.bool class.
  • colors.xml for color values, and accessed from the R.color class.
  • dimens.xml for dimension values, and accessed from the R.dimen class.
  • strings.xml for string values, and accessed from the R.string class.
  • styles.xml for styles, and accessed from the R.style class.
8
xml/
Arbitrary XML files that can be read at runtime by calling Resources.getXML(). You can save various configuration files here which will be used at run time.
 You should always provide the alternative resources to support all devices configurations.There are five type of different sizes in android for which we can provide the resources by default , for other than these size we have to create the folder with specific dimensions.Here are five type of drawable folder and icon size as shown below-
 1.mdpi (48*48 )
 2. hdpi (72*72)
 3.xhdpi(96*96)
 4.xxhdpi(144*144)
 5.xxxhdpi (192*192)

We have to copy our images in to drawable folder to access them otherwise it will not be available to us.All strings to be defined in strings folder under the values,all the xml file for layout of activities and fragment must be defined under layout folder,all the menu that are being used in our application must be defined under the menu folder, all the launcher icons must be copied to mipmap folder and strings,colors,arrays etc must be created under values folder in respective xml files.

By default there is no folder in drawable to copy images to support different above screen sizes we have to create these folder under drawable folder like drawable-hdpi ,drawable-mdpi and same for the other screen sizes.If we need to define the images for the specific screen size then we have to name the folder as drawable-sw720dp then we need to copy all the images in to this folder, after copying the files in to this folder by providing the name of image android automatically fetch the image from the resources folder as shown below-


How to access the Android Resources

When we create the project in android studio then automatically a file is created where all the id's of resources ,views and view groups are stored this file is named as R.java. In this file all id's are stored in an hexadecimal format it is advised that you should never change anything in to this file otherwise you may not be able to generate the id again and it will give an error while compiling the code.In the image below it is shown that where you can locate the R.java file in android studio.


In the image below it is shown that how an id is stored in R.java file.



In below image it is explained that how you can provide the id to views you define in your android layout file.Now by using this id you can find your view in activity class.


Now in the image below it is shown that how you can find the views in an android activity by writing an view id.

In above image this is shown that we have some views that are of button type and whenever we call the the method findViewById(viewId) (it take one parameter that is id of view we need to find from the resource file) will return an object then we need to type cast the object with the appropriate class whether it is textview,imageview or anything else.

In below images it is shown that how you can create strings,color,styles etc. under the values folder.


Android Colors Resource-


Android Dimen Resource-


Android Strings Resource-


 Android Styles Resource-


In below image it is shown that where you need to copy your launcher icon for you android application and it is also shown that image for different sizes is also copied with the same name because with same name android system will automatically load the image with respective device screen density. This is the same case with drawable images you have to provide the same name for all images for every type of drawable i.e. mdpi,hdpi etc. difference will be only in image size then android system will automatically find the respective image from drawable folder and will show in the view.



Thursday, 12 January 2017

Android Toast

Android Toast are used to show some messages for a small period of time. After showing information Toast disappear automatically.An android toast provide a kind of feedback for any action done by user.While showing an toast on the screen activity remain interactive and a toast does not take too much space on the screen. We can place toast on screen where we want like in center,bottom or top.

Android Toast Basics

Toast is class already defined in android system and we use its methods to show a popup message on the screen named as makeText(). This method takes three parameters- Application or activity context, a text message and duration for which toast will be shown on the screen. This method will return an toast object then by using show() method you can show the toast.

Example- 

Toast.maketext(getapplicationcontext,"message to show",Toast.LENGTH_SHORT).show();


This is enough to show an toast to user.Here duration can be of two types-
1. Length_Short
2. Length_Long

You can also give duration custom in form of milliseconds like 2000 will show toast for 2 seconds.

Setting Gravity to Android Toast

By default position of an Android Toast is near to Bottom of Android Device Screen, centered horizontally. You can change the position of toast using setGravity(int,int,int) method.
It takes three parameters one is a Gravity constant, an x-position offset, an y-position offset.You can see the Gravity constant on this link  Android Gravity Constant.

Example- 

toast.setGravity(Gravity.TOP|Gravity.LEFT,0,0)

It will show the toast on the top left side of android device screen. If you want to nudge the position to the right, increase the value of the second parameter.To nudge it down, increase the value of the last parameter. 

Creating Custom Toast

If you do not want this simple toast to show you can also create your own custom Toast messages.To do this you need to simple thing just define an layout either in any XML format or programmatically.
And just pass the root view to the method setView(rootView).

Example - 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/custom_toast_container"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="8dp"
              android:background="#DAAA"
              >
    <ImageView android:src="@drawable/droid"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="8dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
</LinearLayout>
Notice that the ID of the LinearLayout element is "custom_toast_container". You must use this ID and the ID of the XML layout file "custom_toast" to inflate the layout, as shown here:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                (ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
First, retrieve the LayoutInflater with getLayoutInflater() (or getSystemService()), and then inflate the layout from XML using inflate(int, ViewGroup). The first parameter is the layout resource ID and the second is the root View. You can use this inflated layout to find more View objects in the layout, so now capture and define the content for the ImageView and TextView elements. Finally, create a new Toast with Toast(Context) and set some properties of the toast, such as the gravity and duration. Then call setView(View) and pass it the inflated layout. You can now display the toast with your custom layout by calling show().