Monday, 26 December 2016

Custom Checkbox in Android

 
Custom Checkbox in Android

It's a very common practice to use a checkbox in our app. We all know how it works. Right ??
What if we need a customize the checkbox ? Well, by saying customize a checkbox, I actually mean to set a customize background for the checkbox. Means, if a user click over an unchecked checkbox the checked checkbox should appear as a background instead of the default ticked checbox provided by android. Similar thing should be happened in the later case.

Soon or later you will need to learn this feature in order to implement for your app. So better learn it today itself. So let’s start....

1. Create an xml file (say myselector.xml) and put it inside the drawable folder(you can put it to some other location as well).

Copy and paste this ....to your newly created xml file.

checkbox_selector.xml(My xml file name)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:state_focused="true"
          android:drawable="@drawable/check" />
    <item android:state_checked="false"
          android:state_focused="true"
          android:drawable="@drawable/uncheck" />
    <item android:state_checked="false"
          android:drawable="@drawable/uncheck" />
    <item android:state_checked="true"
          android:drawable="@drawable/check" />
</selector>
 
Remember to replace the
android:drawable="@drawable/check" ....with your “check” image/icon
android:drawable="@drawable/uncheck" ....with your “uncheck” image/icon



2. Include “checkbox” tag in Your Activity’s xml file 

<RelativeLayout  ..............................(you can use any layout of your choice)
    android:id="@+id/checkbox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
         android:id="@+id/checkBox4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
            android:background="@drawable/checkbox_selector"  
             (use your file name here)
 
         android:button="@drawable/uncheck">  
         (Uncheck has been set but you can change it)
    
</RelativeLayout>
 
If you didn’t get any error till now....You are Done !! Compile and Run your project. You should not face any difficulty in using this feature. In case you do...leave me a message below.

The same concept should be applied for the radio button as well. I have not tried with the radio buttons though. But I guess there shouldn’t be any issue. Anyways....enjoy programming !!

Sunday, 18 December 2016

UI designing in Android

Hello Everyone,

If you are an android developer then you must be aware of it’s UI designing part. It’s nothing but to design the layouts (i.e XML files). It’s the part which I used to skip all the time. I never took this seriously until last couple of days. You must be thinking what the hell happened with me ? Well, I was assigned an android project last week. I have to work upon this project from scratch. In my earlier projects I was not bothered about this designing and all things so I could concentrate only on the logical part. Unfortunately, it is not the case this time.

Since I was left with no other choice but to learn the UI design. So finally, I started learning it. So I thought sharing my learnings with you guys will be a good idea. I am  thankful to Nishant(Sir) who helped me to learn this XML things with an ease. He is a champ, and I just got lucky to work with him.

Let’s start from one of the most basic concepts, such as :

1.  What’s the difference between margin and padding ?
       Padding is for inside/within components. Eg. TextView , Button, EditText etc.
  Eg. space between the Text and Border
       Margin is to be applied for the on-outside of the components.
  Eg. space between left edge of the screen and border of your component 

Also, 
Margin is that space between the edge of an element's box and the edge of the complete box, such as the margin of a letter

It means, if we need all the four (left, right, top, and bottom) margins for any view. Then we don't need to specify all the four margins. We can use just "margin" which means margins from all the corners.  

2.  What is the difference between Linear, Relative and Frame layouts ??

LinearLayout means you can align views one by one (vertically/ horizontally).
RelativeLayout means based on relation of views from its parents and other views.
FrameLayout to load child one above another, like cards inside a frame, we can place one above another or anywhere inside the frame.
 So before picking up any layout for your design, observe the screen and you will get an idea to select the suitable layout for your screen. 


One last thing, by default, the text within any Button is set to upper-case. If you wanna change is to lower case. All you have to do is to include this line anywhere into your Button tag.


<Button    
       android:textAllCaps="false"    
  />
  
Will share some more in my coming blogs. If you wanna more clarity about anything mentioned in my blog, feel free to mail me or comment below.

Take Care and have a good day !!



Friday, 9 December 2016

Shared Preferences


Most Android apps need to save data about the application state so that users progress is not lost. SharedPreferences API in android lets us save data in key-value sets. When there is a need to save a small collection of key-values we should use Android SharedPreferences.
A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.

Step 1:
To use android shared preference we have to call method getSharedPreferences() that returns a SharedPreference instance poiting to the file that contains the values of preferences. The first parameter is the key and the second parameter is the MODE as shown below,

shared preferences = getSharedPreferences(mypreference,Context.MODE_PRIVATE);

Step 2 : Save object to shared preferences
We can save data to shared preferences using SharedPreferences.Editor class, for which we need to use edit method of the shared preference.

Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Email, e);
editor.commit();


Step 3 : Get data from shared preference
shared preferences.getString() method should be used to get the values for corresponding keys as shown below

if (sharedpreferences.contains(Name)) {
sharedpreferences.getString(Name, "");
}
if (sharedpreferences.contains(Email)) {
sharedpreferences.getString(Email, "");

}

So basically using these get and put methods in shared preference we can perform this save and retrieval functionality as we wish. S
haredPreferences are not the only way to store data. SQLite, AccountManager are some of the alternatives. 
 
But which one to use and when ??
Well, If you want to store some user-preference data - SharedPreferences may be a good choice.
  • If you want to store authentication data, like your user's auth-tokens - don't use SharedPreferences and take a look at AccountManager instead.
  • If you want to store business data e.g. multiple business entities, that keep some relations to each other, you want to be able to query on them and/or modify it - I'd recommend you to use Realm. Alternative is to use SQLite but in my very subjective opinion, Realm is much easier to start with.
  • If you just want to cache some JSON-based responses - take a look at caching mechanisms that your HTTP client may offer you. OkHttp for instance, has a pretty good support for that.
Speaking of loading time - SharedPreferences is pretty fast in general, but it really depends on how you use it. If you store big JSON structs in it, then read it fully just to find some specific object based on id obviously it'll take more time than using a real database for that.
Have in mind that all solutions I proposed (AccountManager, SharedPreferences, and SQLite/Realm) can perfectly work with each other in one app. Just make sure to choose the proper tool for a given problem.