As an Android developer, you are used to import many third party libraries inside your Android project like Retrofit and Butterknife etc. and the sometimes the question comes into mind that how to develop my own libraries and distribute them publicly in community. We will learn how to write an android library and distribute it at some public repository like Maven and jcenter. Basically these are two standard servers supported by Android Studio to download libraries.
Make Android Library Module
In Android Studio, your project contain more than one modules and the main module is App Module where the project source exist. Similarly you can create multiple modules inside your project or import them that you have already created.
To create a new library module in your project, proceed as follows:
- Click File > New > New Module.
- In the Create New Module window that appears, click Android Library, then click Next.
- Give the library name and set the minimum sdk setting and click Finish
Select Android Library module
Enter library name as ‘ToastLibrary’ and minimum sdk level and click finish
After these steps you will see your library module inside project explorer. You can learn more about android library module from here. For our learning purpose we will build custom toast library for Android. This library contain the feature to update the toast position in screen like top, bottom and center and also user will be able to add icon inside toast and change the color of message text and toast background.
Time to write Custom Toast
Now we have created Android Library project but it is empty now. No java files and xml layouts exist inside the module. First of all we need one xml layout for toast layout. For this right click on ‘res’ folder and click new then click Android resource file and name it toast_layout.xml like below
Then, write the following code for layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:background="#a6504f4f" android:id="@+id/toast_bg" android:layout_height="wrap_content"> <ImageView android:id="@+id/toast_img_icon" android:layout_width="45dp" android:layout_height="45dp" android:layout_margin="5dp" /> <TextView android:id="@+id/toast_text_message" android:layout_width="0dp" android:layout_weight="1" android:layout_gravity="center_vertical" android:maxLines="3" android:layout_margin="5dp" android:text="" android:layout_height="wrap_content" /> </LinearLayout> |
Now, add one java file and write the custom toast class source inside. It is just wrapper on android toast. Right click on java folder then click new then Java Class and name it CustomToast. After that write the custom toast java code inside like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
public class CustomToast { private Context mContext; private LayoutInflater mLayoutInflater; private String mMessaage; private int mResourceId; private String mBackgroundColorCode = "#a6504f4f"; private String mTextColorCode = "#ffffff"; private Toast mToast; private int mGravity = Gravity.BOTTOM; public CustomToast(@NonNull Context context){ mContext = context; mLayoutInflater = LayoutInflater.from(mContext); } /** * Set the message to show in Toast * @param message */ public void setMessage(@NonNull String message){ mMessaage = message; } /** * Set the icon resource from drawable to show * @param iconResource */ public void setIconResource(@NonNull int iconResource){ mResourceId = iconResource; } /** * set the gravity of toast by default it show at bottom of screen * @param gravity */ public void setGravity(int gravity){ mGravity = gravity; } /** * set background color code i.e #a6504f4f */ public void setBackgroundColorCode(String backgroundColorCode){ mBackgroundColorCode = backgroundColorCode; } /** * set background color code i.e #a6504f4f */ public void setTextColorCode(String textColorCode){ mTextColorCode = textColorCode; } /** * show the toast messsage */ public void show(){ generateToast(); } private void generateToast(){ mToast = new Toast(mContext); mToast.setDuration(Toast.LENGTH_LONG); View view = mLayoutInflater.inflate(R.layout.toast_layout,null); TextView text = (TextView) view.findViewById(R.id.toast_text_message); ImageView icon = (ImageView) view.findViewById(R.id.toast_img_icon); LinearLayout background = (LinearLayout) view.findViewById(R.id.toast_bg); if(mMessaage == null){ throw new NullPointerException("Please set the toast message by using setMessage() method"); } text.setText(mMessaage); if(mResourceId == 0){ throw new NullPointerException("Please use the valid id of icon resource"); } icon.setImageResource(mResourceId); background.setBackgroundColor(Color.parseColor(mBackgroundColorCode)); text.setTextColor(Color.parseColor(mTextColorCode)); mToast.setGravity(mGravity,0,0); mToast.setView(view); mToast.show(); } } |
Now our custom toast is ready to use inside app module.
Import module inside app module
Right now, we have created Android Library module and it is time to check this library inside our app module.
Right click on app module > click on Open module setting > Dependencies > then click at Module dependency.
Your app module build.gradle file now contain the dependency of library module compiled project(‘:toastlibrary’)
So now we have dependency of CustomToast module inside our app module. Add the following code inside method where you want to show custom toast message. I have placed the code inside the click event of button.
1 2 3 4 5 6 7 8 9 10 11 |
btnShowToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CustomToast customToast = new CustomToast(MainActivity.this); customToast.setGravity(Gravity.BOTTOM); customToast.setMessage("Now Internet connection please try again"); customToast.setIconResource(R.drawable.ic_action_name); customToast.setBackgroundColorCode("#000000"); customToast.show(); } }); |
Custom Toast is showing with black background and icon inside. Now our library is ready to distribute at repository. We will published or distribute our library in the next part of this post.