Tuesday, 23 August 2016

Android Material design Notifications

Android Notifications are meant to inform the user about any useful information , which the user can see without open an application.

In other words , we can say that Notifications are short cut method to inform user in an easiest and attractive way.

Now a days there are many anti-access options are available for Android Notifications.

For example :
  1. In device settings , we can restrict the notification content to show when the device is locked.
  2. We can also restrict the device to show the Notifications when device is locked.
There are several things which comes into the Developer mind that
  1. How to create the Notification?
  2. How to show the large text in the Notifications?
  3. How to show some custom images in the Notifications?
  4. How to open a page when user tap on the Notification?
  5. How to clear the Notifications?   and many more......
So today we will dirty our hands with Android Notifications and i hope you will enjoy the session.

Prerequisite :
1. I am always encourage developers to use the Android studio , as this is an very intensive tool for programming and very proactive.

2. Use the design library to use the NotificationCombat class
Please add the following text in your Module's .gradle file under dependencies as follows.

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:23.4.0' 
compile 'com.android.support:design:23.4.0' 
}

After adding above design library , your code environment is ready.

Sample code Snippet :





and for more please download the code.
Happy Coding.....




Sunday, 14 August 2016

Material design Tabs with Viewpager

Tabs are very powerful component of Android , which helps us to show the data in a segregated and arranged manner.

As we have seen that the most of the apps now focusing on the Tabs and View Pager components to make their app more user friendly.

Now a days users want more data on single page or we can say more data in less navigation. So Tabs and View Pager fulfilled the criteria and helps developers to show collective data in a segregated manner to user.

Don't you think its interesting and if you are an Android developer ans still you didn't dirty your hands with Tabs and View Pager, then lets start. So today you will become expert in developing tabs under View pager.

So now let's start learning it.

Prerequisite :
1. I am always encourage developers to use the Android studio , as this is an very intensive tool for programming and very proactive.

2. Use the design library to use the View pager and the Tab layout.

Please add the following text in your Module .gradle file under dependencies as follows.

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:23.4.0' 
compile 'com.android.support:design:23.4.0' 
}

After adding above design library , your code environment is ready and we will now start coding.

The coding is pretty simple as i will explain now.

1. Firstly we need to initialize the View pager
mPager = (ViewPager)findViewById(R.id.viewpager);

2. And then initialize the tabLayout
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);


3. Now add the required listener, which helps us to track the tabs which are getting scrolled.
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

4. Now you should add the fragments to the viewpager ad follows. The number of fragments you add, the number of tabs will be generated.
private void initViewPager(ViewPager mPager) { 
   FragmentManager fm = getSupportFragmentManager(); 
   ViewPagerAdapter adapter = new ViewPagerAdapter(fm); 
   adapter.addFragment(new FirstTabFragment(),getString(R.string.first_tab));  adapter.addFragment(new SecTabFragment(), getString(R.string.second_tab));        mPager.setAdapter(adapter); 

where below line adding the fragments to the view pager

adapter.addFragment(new FirstTabFragment(),getString(R.string.first_tab)); 
adapter.addFragment(new SecTabFragment(), getString(R.string.second_tab));

5. Now add the tabLayout to the View pager

tabLayout.setupWithViewPager(mPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
 @Override public void onTabSelected(TabLayout.Tab tab) { 
  // setting the action bar title with the selected tab 
   setActionBarTitle(tab.getText().toString().trim()); 
   setSelectedTab(tab.getPosition()); 
  }
 @Override public void onTabUnselected(TabLayout.Tab tab) { } 
 @Override public void onTabReselected(TabLayout.Tab tab) { } 
});

The initialization of the view pager includes the initialization of the ViewPager Adapter as well.
class ViewPagerAdapter extends FragmentStatePagerAdapter { 
  private final List mFragmentList = new ArrayList<>(); 
  private final List mFragmentTitleList = new ArrayList<>(); 

  public ViewPagerAdapter(FragmentManager manager) { 
    super(manager); 
  } 

@Override public Fragment getItem(int position) { 
  return mFragmentList.get(position); 
 } 

 @Override public int getCount() { 
     return mFragmentList.size(); 
  } 

  public void addFragment(Fragment fragment, String title) { 
    mFragmentList.add(fragment); mFragmentTitleList.add(title); 
  } 

@Override public CharSequence getPageTitle(int position) { 
   return mFragmentTitleList.get(position); 
  } 
}

Screenhots:

Sunday, 7 August 2016

Custom SnackBar in Android

In ancient days , we were talking about the Toast. Using Toast we were showing the messages , useful information to the user.
But now a days, Toast has been replaced by the SnackBar, a powerful feature using which we can display messages to the user in an unique and attractive style.
Snackbar comes with a default animation from bottom to up and hide from top to bottom.
Like Toast , we can customize the Snackbar as well, which we will disucss now.
Note :
To access the SnackBar , The design library should be added in Android Studio project.
Steps to add the Design Library:
1. In Android Studio, Open you Module.
2. Now open the .gradle file.
3. and then add the design library under dependencies section like below.
Have a look at a design library in Bold
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
}
Snack Bar in Fragment:
When we showing the Snack bar in Fragment, many of us facing an issue as highlighted in below image.

Screenshot_20160807-014106

This situation arises when we are displaying the Snack Bar as below.
Snackbar.make(view.getRootView(), "This is a wrong SnackBar", Snackbar.LENGTH_LONG).show();
And to overcome this problem , the one should use the root layout id in place of view.getRootView() as follows
Snackbar.make(mView.findViewById(R.id.top_layout), "This is a correct SnackBar", Snackbar.LENGTH_LONG).show();
where R.id.top_layout is the parent element of your layout.

Custom Action Bar

In Android applications, we have seen that the Action Bar plays a major role. There are many vital options that we can provide using the Action Bar and moreover this is customizable.
We can provide any User Interface to action bar and make at attractive and user-friendly.
So today we will learn about the customization of the Action Bar.

Prerequisites :
I strongly recommend that you should use the Android Studio,as this is a great tool for developing the Android applications.As for most of the developers its a night mare, But i hope  you should overcome it.
Now we shall begin.Start by creating an Activity which extends AppCompatActivity.

public class MainActivity extends AppCompatActivity{
ActionBar mActionBar = getSupportActionBar();
mActionBar.setElevation(0); // used to remove the shadow under Action Bar
mActionBar.setCustomView(R.layout.custom_action_bar);
}
Where 'custom_action_bar.xml'  is the custom layout for the Action Bar.In this XML file , you can create the User Interface according to the requirement.
Output :
action_bar



Thursday, 23 January 2014

Layouts are the core part of the application. The application GUI design should be in such a way that the end user can easy to understand and he can navigate the app smoothly.

Relative layout:

A Relative Layout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested Linear Layout groups, you may be able to replace them with a single Relative Layout.

In Relative Layout we have a lot of options to align the view with respect to another views.
e,g  alignParentLeft , alignParentBottom , alignParentTop , layout_albove , layout_below , layout_alignBaseline , layout_toRightOf , layout_toLeftOf etc. 

Frame Layout:

Frame Layout is designed to block out an area on the screen to display a single item. Generally, Frame Layout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a Frame Layout and control their position within the Frame Layout by assigning gravity to each child, using the android: layout_ gravity attribute.

Child views are drawn in a stack, with the most recently added child on top. The size of the Frame Layout is the size of its largest child (plus padding), visible or not (if the Frame Layout’s parent permits).



Linear layout:

Using nested instances of Linear Layout can lead to an excessively deep view hierarchy. Furthermore, nesting several instances of Linear Layout that use the layout _weight parameter can be especially expensive as each child needs to be measured twice.



Hierarchy Viewer:

Hierarchy Viewer works by allowing you to select running processes on a connected device or emulator, and then display the layout tree. The traffic lights on each block represent its
1.       Measure,
2.        Render and
3.       Draw performance, helping you identify potential issues.
I shall explain the below example to optimize the layout and showing you how the performance will increase by using the appropriate layout.


To draw this above layout, if we use the Linear Layout, the time to measure, render and Draw the layout is and the Hierarchy viewer view is as follows.



 


and if we use the Relative layout , the time to measure, render and Draw the layout is and the Hierarchy viewer view is as follows.



Lint:

It is always good practice to run the Lint tool on your layout files to search for possible view hierarchy optimizations. Lint has replaced the Layout opt tool and has much greater functionality.

1.       Use compound drawables - A Linear Layout which contains an Image View and a Text View can be more efficiently handled as a compound draw able.
2.       Merge root frame - If a Frame Layout is the root of a layout and does not provide background or padding etc, it can be replaced with a merge tag which is slightly more efficient.
3.       A layout that has no children or no background can often be removed.
4.       Deep layouts - Layouts with too much nesting are bad for performance. Consider using flatter layouts such as Relative Layout or Grid Layout to improve performance. 

Another benefit of Lint is that it is integrated into the Android Development Tools for Eclipse (ADT 16+). Lint automatically runs whenever you export an APK, edit and save an XML file or use the Layout Editor. To manually force Lint to run press the Lint button in the Eclipse toolbar.

More Optimization Techniques for draw Layouts:

To efficiently re-use complete layouts, you can use the<include/> and <merge/> tags to embed another layout inside the current layout.

Include<> : 
Inside the layout to which you want to add the re-usable component, add the <include/> tag

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" 

    android:layout_width=”match_parent”

    android:layout_height=”match_parent”

    android:background="@color/app_bg"

    android:gravity="center_horizontal">



    <include layout="@layout/title"/>



    <TextView android:layout_width=”match_parent”

              android:layout_height="wrap_content"

              android:text="@string/hello"

              android:padding="10dp" />



    ...



</LinearLayout>

And title.xml is as follows
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width=”match_parent”
   
android:layout_height="wrap_content"
   
android:background="@color/titlebar_bg">

   
<ImageView android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:src="@drawable/gafricalogo" />
</FrameLayout>

You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the <include/> tag. For example:

<include android:id=”@+id/news_title”

         android:layout_width=”match_parent”

         android:layout_height=”match_parent”

         layout=”@layout/title”/>


Merge<>:

The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another. For example, if your main layout is a vertical Linear Layout in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another Linear Layout as the root for the re-usable layout would result in a vertical Linear Layout inside a vertical Linear Layout. The nested Linear Layout serves no real purpose other than to slow down your UI performance.

To avoid including such a redundant view group, you can instead use the <merge> element as the root view for the re-usable layout. For example:

<merge xmlns:android="http://schemas.android.com/apk/res/android">



    <Button

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content"

        android:text="@string/add"/>



    <Button

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content"

        android:text="@string/delete"/>



</merge>

A very basic example of using the include and the merge
this below example will clear your doubts if remaining in merge and Include tag concept

layout1.xml:
<FrameLayout>
   <include layout="@layout/layout2"/>
</FrameLayout>

layout2.xml:
<FrameLayout>
   <TextView />
</FrameLayout>
which is functionally equivalent to this single layout:
<FrameLayout>
   <FrameLayout>
      <TextView />
   </FrameLayout>
</FrameLayout>
That Frame Layout in layout2.xml may not be useful. <merge/> helps get rid of it. Here's what it looks like using merge (layout1.xml doesn't change):

layout2.xml:
<merge>
   <TextView />
</merge>
This is functionally equivalent to this layout:
<FrameLayout>
   <TextView />
</FrameLayout>

Table Layout:

Table layout is a layout that arranges its children into rows and columns.  A Table Layout consists of a number of Table Row objects, each defining a row (actually, you can have other children, which will be explained below). Table Layout containers do not display border lines for their rows, columns, or cells. Each row has zero or more cells; each cell can hold one View object. The width of a column is defined by the row with the widest cell in that column. Cells must be added to a row in increasing column order, both in code and XML. Column numbers are zero-based. If you don't specify a column number for a child cell, it will auto increment to the next available column. If you skip a column number, it will be considered an empty cell in that row. See the Table Layout examples in Api Demos for examples of creating tables in XML.