Saturday, 28 May 2016

Toolbar title in center

If you want your title text to be in center of your toolbar, you have two simple ways to do it.
First approach:-
Remember that the Toolbar is just a ViewGroup like others. So you can simply add Views into it.
In this case, you need a TextView inside a Toolbar at center position. So, simply define a layout for your toolbar containing a textview like this:

app_bar.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Some Fancy Title"
        android:gravity = "center"
        android:id="@+id/toolbar_title" />

</android.support.v7.widget.Toolbar>

Then just include this layout in activity's XML file like:

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

Then, in your Java file(Activity.java) access your toolbar and textview as follows:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);

Since the textview gravity is already set to "center", it will display the text in center of the toolbar.


Second approach:-
You can just use the below method and pass your toolbar variable as parameter to it to make your title centered.

public static void centerToolbarTitle(final Toolbar toolbar) {
        final CharSequence title = toolbar.getTitle();
        final ArrayList<View> outViews = new ArrayList<>(1);
        toolbar.findViewsWithText(outViews, title, View.FIND_VIEWS_WITH_TEXT);
        if (!outViews.isEmpty()) {
            final TextView titleView = (TextView) outViews.get(0);
            titleView.setGravity(Gravity.CENTER_HORIZONTAL);
            final Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) titleView.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.setMargins(0,0,60,0);
            toolbar.requestLayout();
        }
    }

Thas'it..!!! Enjoy!!


Friday, 20 May 2016

Disable background touch events on showing ProgressBar

When showing the progress bar loader while performing some operation, to disable your background controls or in other words if I say that, showing a progress bar spinner only with the properties of a Progress Dialog, here is the solution, how:-

In order to use Progress Bar, define it in the xml like this.

<ProgressBar                                                                          
   android:id="@+id/progressBar1"                                       
   style="?android:attr/progressBarStyleLarge"                     
   android:layout_width="wrap_content"                               
   android:layout_height="wrap_content"                              
   android:layout_centerHorizontal="true" />                        

After defining it in xml, get its reference in java file as follows−

private ProgressBar spinner;                                                  
spinner = (ProgressBar)findViewById(R.id.progressBar1); 


Now use the below method to show the spinner loader and disable user touch events on background like in the case of a progress dialog.

 void showLoading(){                                                                                                              
        spinner.setVisibility(View.VISIBLE);                                                                             
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);                           
    }                                                                                                                                           

In order to dismiss the spinner loader and enable the backgroung touch events, use the below method:-

 void dismissLoading(){                                                                                                               
        spinner.setVisibility(View.INVISIBLE);                                                                             
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    }                                                                                                                                                


Also like in the case of a Progress Dialog, if you want to add the feature of semi-transparent greyed display while your loading spinner is visible, you need to set the background of the linear layout to #80000000 and its visibilty to Gone in your xml file . Then programmatically set its visibility to Visible when required.

Enjoy..!! and Code to make it better..!!!



Monday, 16 May 2016

Difference between popBackStack() and replace() in Fragments


Using Fragments in your application is a very good practise that makes the application more responsive and transitive in the sense that it looks a bit fast. But sometimes it becomes a problem to manage these fragments. Now to differentiate the two main functions in fragments i.e popBackStack() and replace(), lets take two fragments, Fragment (A) and Fragment (B).

Now the replace() performs two things:-

  • Remove currently added fragment (A) from the container (C) you indicated.
  • Add new fragment (B) to the same container.

These 2 operations are what is saved as a Backstack record / transaction.
Note that fragment A remains in created state, but its view is destroyed.

Now popBackStack() reverses your last transaction that you've performed through replace().
In this case that would be 2 steps:

  • Remove fragment (B) from container (C).
  • Add fragment (A) to container C.

After this, fragment B becomes detached, and if you did not keep references to it, it will be garbage collected.

Now it is important to understand that you don't actually add Fragments to Backstack, you add FragmentTransactions. So when you think that you are doing "replace with Fragment B, and adding Fragment A to the back stack", then you actually add this whole operation to backstack - that is- remove A and add B.

Then, next step is popping of the transaction that contains this replacement. So you're not popping FragmentA, you're reversing the above transaction "remove A, add B", which when reversed is "remove B, add A".

So after this - there's no fragment (B) that the FragmentManager is aware of, so when you add it by replacing A with B at the above step, B needs to go through its early lifecycle methods - onAttach() and onCreate().

That's all, enjoy.!! and always code to make it better..!!

Friday, 6 May 2016

Eclipse Shortcuts for Android Studio Users

Many of you might be very familiar with working on eclipse and more comfortable with the shortcuts used in it. And now after swithcing to Android Studio you must be missing those shortcuts.
So here is the way to get your favourite eclipse shortcuts in Android Studio.
Go to:-
File -> Settings -> Keymap -> <Choose Eclipse from Keymaps dropdown>


To know more about shortcuts, go to this Link.

Other Programming shortcuts in Android Studio are as follows:
Reformat code -------------------------------------------CTRL + ALT + L
Optimize imports ---------------------------------------CTRL + ALT + O
Code Completion ---------------------------------------CTRL + SPACE
Issue quick fix -------------------------------------------ALT + ENTER
Surround code block -----------------------------------CTRL + ALT + T
Rename and Refractor ---------------------------------Shift + F6
Line Comment or Uncomment -----------------------CTRL + /
Block Comment or Uncomment ---------------------CTRL + SHIFT + /
Go to previous/next method --------------------------ALT + UP/DOWN
Show parameters for method ------------------------CTRL + P
Quick documentation lookup ------------------------CTRL + Q
Delete a line --------------------------------------------CTRL + Y
View declaration in layout ---------------------------CTRL + B

Happy coding & code to make it better..!!