2012 in review

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

600 people reached the top of Mt. Everest in 2012. This blog got about 2,500 views in 2012. If every person who reached the top of Mt. Everest viewed this blog, it would have taken 4 years to get that many views.

Click here to see the complete report.

Advertisements

Styling Sherlock actionbar – Change actionbar color

Just few step to change the style of Sherlock actionbar. Here it goes like this

1)make your application theme something like shown below

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyCustomTheme" parent="Theme.Sherlock">
     <-- This one is for below android 3.x device-->
     <item name="actionBarStyle">@style/ActionBar</item>
     <-- This one is for android 3.x or greater device-->
     <item name="android:actionBarStyle">@style/ActionBar</item>
     <style>

</resources>

2) Define style for action bar

 <!-- style for actionbar(Equivalent to titlebar) -->
    <style name="ActionBar" parent="Widget.Sherlock.ActionBar">
        <item name="background">@drawable/actionbar_bg_top</item> -----------(1)
        <item name="android:background">@drawable/actionbar_bg_top</item>
       
        <item name="android:titleTextStyle">@style/customTextAppearance.Sherlock.Widget.ActionBar.Title</item> ----(2)
        <item name="titleTextStyle">@style/customTextAppearance.Sherlock.Widget.ActionBar.Title</item>
    </style>
<!-- define text style of Action bar Title -->
 <style name="customTextAppearance.Sherlock.Widget.ActionBar.Title" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
        <item name="android:textSize">14dp</item>
        <item name="android:textColor">#083647</item>
        <item name="android:shadowColor">#4ccfff</item>
        <item name="android:shadowDy">3</item>
    </style>

equation 1–> define the background color of action bar. @drawable/actionbar_bg_top may be in the form of nine patch image or color
equation 2 –>define the style of Text that appear as title in Sherlock actionbar

3)Use the theme on application in android manifest file


<application
       
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyCustomTheme" >
        <activity
            android:name=".YourMainActivtiy"
           
            android:configChanges="orientation"
            android:label="app" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application

That’s all for changing color of actionbar in Sherlock action bar
here is the affect
device-2013-02-15-143813

Enjoy android styling 🙂

Styling ActionBarSherlock – change icon of overflow menu button

I Needed to change the overflow menu button according to my application theme. so I spent few hours on how to style these things in Sherlock action bar.
So I am sharing my knowledge as it might be useful to someone in somewhere.

Just few steps

1)make your application theme something like shown below

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyCustomTheme" parent="Theme.Sherlock">
    <item name="actionOverflowButtonStyle">@style/customOverflowButtonStyle</item>
        <item name="android:actionOverflowButtonStyle">@style/customOverflowButtonStyle</item>
    </style>
 <!-- overflow button style -->
 <style name="customOverflowButtonStyle" parent="Widget.Sherlock.ActionButton.Overflow">
 <item name="android:src">@drawable/ic_your_menu_moreoverflow_with</item>
    </style>
</resources>

2)Use the theme on application in android manifest file


<application
       
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyCustomTheme" >
        <activity
            android:name=".YourMainActivtiy"
           
            android:configChanges="orientation"
            android:label="app" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application

That’s all for changing overflow menu icon in Sherlock action bar
Changes looks like
device-2013-02-15-143813
Enjoy android styling 🙂

Styling ActionBarSherlock – change icon of homeupIndicator

Changing home up indicator in action bar Sherlock is very easy. just few lines of code
Here is the step

1)make your application theme something like shown below

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyCustomTheme" parent="Theme.Sherlock">
    <item name="homeAsUpIndicator">@drawable/ic_your_new_home_up_indicator</item>
    <item name="android:homeAsUpIndicator">@drawable/ic_your_new_home_up_indicator</item>
    </style>
</resources>

2)Use the theme on application in android manifest file


<application
       
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyCustomTheme" >
        <activity
            android:name=".YourMainActivtiy"
           
            android:configChanges="orientation"
            android:label="app" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application

That’s all for changing home up icon in Sherlock action bar

hope, Someone will find my post helpful 🙂

Android HorizontalScrollView with center lock

During the development of News app , i needed a view which should slide horizontally and auto highlight the selected item in center of device screen. Something like Pulse app does in their news detail page where they have a quick access to news of browsing category. From user perceptive it was very handy feature so that user have quick access on current browsed category.
So i decide to use Android HorizontalScrollview with some modification.
Here is, how i implemented it on my news app.

1) create a custom java class extending HorizontalScrollView and define the function which center the current selected item. Something like this.


public class CenterLockHorizontalScrollview extends HorizontalScrollView {
	Context context;
	int prevIndex = 0;

	public CenterLockHorizontalScrollview(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		this.setSmoothScrollingEnabled(true);

	}

	public void setAdapter(Context context, CustomListAdapter mAdapter) {

		try {
			fillViewWithAdapter(mAdapter);
		} catch (ZeroChildException e) {

			e.printStackTrace();
		}
	}

	private void fillViewWithAdapter(CustomListAdapter mAdapter)
			throws ZeroChildException {
		if (getChildCount() == 0) {
			throw new ZeroChildException(
					"CenterLockHorizontalScrollView must have one child");
		}
		if (getChildCount() == 0 || mAdapter == null)
			return;

		ViewGroup parent = (ViewGroup) getChildAt(0);

		parent.removeAllViews();

		for (int i = 0; i < mAdapter.getCount(); i++) {
			parent.addView(mAdapter.getView(i, null, parent));
		}
	}

	public void setCenter(int index) {

		ViewGroup parent = (ViewGroup) getChildAt(0);

		View preView = parent.getChildAt(prevIndex);
		preView.setBackgroundColor(Color.parseColor("#64CBD8"));
		android.widget.LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.setMargins(5, 5, 5, 5);
		preView.setLayoutParams(lp);

		View view = parent.getChildAt(index);
		view.setBackgroundColor(Color.RED);

		int screenWidth = ((Activity) context).getWindowManager()
				.getDefaultDisplay().getWidth();

		int scrollX = (view.getLeft() - (screenWidth / 2))
				+ (view.getWidth() / 2);
		this.smoothScrollTo(scrollX, 0);
		prevIndex = index;
	}

}

2)Create xml file in res/layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.krish.horizontalscrollview.CenterLockHorizontalScrollview
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </com.krish.horizontalscrollview.CenterLockHorizontalScrollview>

    <LinearLayout
        android:id="@+id/bottomLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/scrollView"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/btnPrev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Prev" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Next" />
    </LinearLayout>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/bottomLayout"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:textColor="#FF0000"
        android:padding="@dimen/padding_medium" />

</RelativeLayout>



3)Use the CenterLockHorizontalScrollview in your activity as follows.

public class HorizontalScrollViewActivity extends Activity {
	CenterLockHorizontalScrollview centerLockHorizontalScrollview;
	CustomListAdapter customListAdapter;
	Button btnPrev, btnNext;
	int currIndex = 0;
	private TextView text;
	ArrayList<String> list = new ArrayList<String>() {

		{
			add("Manchester city");
			add("Manchester United");
			add("Chelsea");
			add("Liverpool");
			add("TottenHam");
			add("Everton");
			add("WestHam");
			add("Arsenal");
			add("West Broom");
			add("New Castle");
			add("Norich City");
			add("Swansea city");
			add("stroke city");

		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_horizontal_scroll_view);
		btnNext = (Button) findViewById(R.id.btnNext);
		btnPrev = (Button) findViewById(R.id.btnPrev);
		text=(TextView)findViewById(R.id.text);
		centerLockHorizontalScrollview = (CenterLockHorizontalScrollview) findViewById(R.id.scrollView);
		customListAdapter = new CustomListAdapter(this,
				R.layout.news_list_item, list);
		centerLockHorizontalScrollview.setAdapter(this, customListAdapter);
		btnNext.setOnClickListener(onClickListener);
		btnPrev.setOnClickListener(onClickListener);

	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v.getId() == R.id.btnPrev) {
				if (currIndex != 0) {
					currIndex--;
					centerLockHorizontalScrollview.setCenter(currIndex);
					text.setText(list.get(currIndex==0?0:currIndex-1));
				}
			} else if (v.getId() == R.id.btnNext) {

				if (currIndex < list.size()) {
					centerLockHorizontalScrollview.setCenter(currIndex);
					currIndex++;
					text.setText(list.get(currIndex-1));
				}
			}

		}
	};

}

4)Expected out

device-2012-12-09-234522

source with and demo project can also be found on my github account.

Android file explorer

I have to implement file browser in my on going project to pick single/multiple image file for which i have to do all those things browse to roots and check weather current browsing is file or directories etc…. so i made a simple library for browsing file which provides choice to pick single or multiple file.

Now if you have same scenario and thinking of “how to make file explorer in android”.Then, here i am with solution to implement file explorer with very few lines codes. just few lines of code.

Here are the steps you need to follow

1) Pull the library from github and make a project from it.

2) Then Simply call the intent from your project like this


Intent intent = new Intent(FilebrowserDemoActivity.this,
					FileBrowserActivity.class);
			intent.putExtra(FileBrowserActivity.BROWSE_MODE,
					FileBrowserActivity.SINGLE);
			startActivityForResult(intent, REQUEST_FILE_BROWSE);

Note: for picking multiple file send intent string extra

intent.putExtra(FileBrowserActivity.BROWSE_MODE,
					FileBrowserActivity.MULTIPLE);

instead of

intent.putExtra(FileBrowserActivity.BROWSE_MODE,
					FileBrowserActivity.SINGLE);

3) Define onActivityResult like this to get the selected file path


     @Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK
				& requestCode == REQUEST_FILE_BROWSE) {
			
			ArrayList<String> selectedImagePath = new ArrayList<String>();
			selectedImagePath = data
					.getStringArrayListExtra(FileBrowserActivity.RESULT);
			for (String imagePath : selectedImagePath) {
				Log.v(TAG, imagePath);
				
				
			}
			
		}

	};

Note : Don’t forget to add activity in android manifest file like this

<activity android:name="com.example.filebrowser.FileBrowserActivity"></activity>

Enough for implementing file browser in your project. if you got any problem while implementing then don worry,source with and demo project is in my github account.

Switching bottom Left radius and bottom Right corner issue in android.

During the development of Facebook Scheduler app , i face a amazing problem of switching bottom Left and bottom Right corner while using corners tag with in xml to make edge corner of drawable some what like rounded. This is a kind of bug in android 2.x platform which is already fixed in android 3.x platform. Now problem is How to cope up with this problem in android 2.x and android 3.x. (if I use correct convention for android 3.x then corner will not be as expected in android 2.x device and vice-versa).

Here is the procedure how i escape from this problem.
1) make separate drawable (lets say bottonPressedLeft.xml) for app running in android device 2.x.x. and place in the drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

     <gradient 
        android:angle="-270" 
        android:endColor="#F0C37E"
        android:startColor="#F0C37E"/>

    <corners 
        android:bottomLeftRadius="0dp" 
        android:bottomRightRadius="5dp" 
        android:topLeftRadius="5dp" 
        android:topRightRadius="0dp"></corners>

</shape>

Above drawable is for making topleft and bottomleft rounded with radius for running in android 2.x.x device.(bug)
Note location of xml file : res/drawable/bottonPressedLeft.xml

2)make another drawable with the same name for app running in android 3.x.x device and (make a folder with name res/drawable-v12 ) place in it.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

     <gradient 
        android:angle="-270" 
        android:endColor="#F0C37E"
        android:startColor="#F0C37E"/>

    <corners 
        android:bottomLeftRadius="0dp" 
        android:bottomRightRadius="5dp" 
        android:topLeftRadius="5dp" 
        android:topRightRadius="0dp"></corners>

</shape>

Above drawable is for making topleft and bottomleft rounded with radius for running in android 3.x.x device.(bug fixed case)
Note location of xml file : res/drawable-v12/bottonPressedLeft.xml.

If your app is running under android 2.x.x device then it will use the res/drawable/bottonPressedLeft.xml file and if your app is running under android 3.x.x device then will use res/drawable-v12/bottonPressedLeft.xml

Hope this helps someone somewhere in some point of time 🙂