Thursday, April 3, 2014

Android Sherlock ActionBar and CustomTheme Example



Android Sherlock ActionBar and CustomTheme Example
This article about how to add action views in action bar and  change action bar colors , Custom Themes

Add action view into ActionBar.
XML code
Menu.xml
Add the action  items here
Menu Item I have added Search Button and Setting Navigation
<menuxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.vijay.sherlockactionbar.MainActivity">

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_vj_blog"
android:orderInCategory="100"
android:title="@string/action_vj_blog"
app:showAsAction="never"/>
<item
android:id="@+id/action_recahrge"
android:orderInCategory="100"
android:title="@string/action_recharge"
app:showAsAction="never"/>
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_search_category_default"
android:title="Search....."
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>

</menu>






Activity Code
Create the option menu and set the listener for SearchView
@Override
       publicbooleanonCreateOptionsMenu(Menu menu) {

              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              MenuItemmenuItem = menu.findItem(R.id.action_search);
              SearchViewsearchView = (SearchView) MenuItemCompat
                           .getActionView(menuItem);

              returnsuper.onCreateOptionsMenu(menu);
       }

@Override
       publicbooleanonOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     Intent in = newIntent(MainActivity.this, SecodActivity.class);
                     startActivity(in);

                     returntrue;
              } elseif (id == R.id.action_vj_blog) {
                     returntrue;
              } elseif (id == R.id.action_recahrge) {
                     Toast.makeText(MainActivity.this, "Recharge", Toast.LENGTH_LONG)
                                  .show();
                     returntrue;
              }
              returnsuper.onOptionsItemSelected(item);
       }


Set the ActionBar background Color
       ActionBaractionBar = getSupportActionBar();
              actionBar.setBackgroundDrawable(newColorDrawable(Color
                           .parseColor("#FF8800")));



Enable Home Navigation
Suppose the your are in last page of  ur application you want to back to home page.
Now you can enable home navigation in action ba with app icon.
ActionBaractionBar = getSupportActionBar();
       actionBar.setDisplayHomeAsUpEnabled(true);
       actionBar.setBackgroundDrawable(newColorDrawable(Color.parseColor("#FF4444")));

Manifest  you need to declare Home activity
<activity
android:name="com.vijay.sherlockactionbar.SecodActivity"
android:label="@string/app_name"
android:parentActivityName="com.vijay.sherlockactionbar.MainActivity">
<meta-data
android:name="android:support:PARENT_ACTIVITY"
android:value="com.vijay.sherlockactionbar.MainActivity"/>
</activity>

Manifest.xml
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.vijay.sherlockactionbar"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.vijay.sherlockactionbar.MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.vijay.sherlockactionbar.SecodActivity"
android:label="@string/app_name"
android:parentActivityName="com.vijay.sherlockactionbar.MainActivity">
<meta-data
android:name="android:support:PARENT_ACTIVITY"
android:value="com.vijay.sherlockactionbar.MainActivity"/>
</activity>
</application>

</manifest>
packagecom.vijay.sherlockactionbar;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SearchView;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
importandroid.content.Intent;
importandroid.graphics.Color;
importandroid.graphics.drawable.ColorDrawable;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.ViewGroup;
importandroid.widget.Button;
importandroid.widget.Toast;
importandroid.os.Build;

publicclassMainActivityextendsActionBarActivity {

       @Override
       protectedvoidonCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              if (savedInstanceState == null) {
                     getSupportFragmentManager().beginTransaction()
                                  .add(R.id.container, newPlaceholderFragment()).commit();
              }

              ActionBaractionBar = getSupportActionBar();
              actionBar.setBackgroundDrawable(newColorDrawable(Color
                           .parseColor("#FF8800")));

              Button secondActivity = (Button) findViewById(R.id.second_activity);
              secondActivity.setOnClickListener(newOnClickListener() {

                     @Override
                     publicvoidonClick(View arg0) {
                           Intent in = newIntent(MainActivity.this, SecodActivity.class);
                           startActivity(in);
                     }
              });
       }

       @Override
       publicbooleanonCreateOptionsMenu(Menu menu) {

              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              MenuItemmenuItem = menu.findItem(R.id.action_search);
              SearchViewsearchView = (SearchView) MenuItemCompat
                           .getActionView(menuItem);

              returnsuper.onCreateOptionsMenu(menu);
       }

       @Override
       publicbooleanonOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     Intent in = newIntent(MainActivity.this, SecodActivity.class);
                     startActivity(in);

                     returntrue;
              } elseif (id == R.id.action_vj_blog) {
                     returntrue;
              } elseif (id == R.id.action_recahrge) {
                     Toast.makeText(MainActivity.this, "Recharge", Toast.LENGTH_LONG)
                                  .show();
                     returntrue;
              }
              returnsuper.onOptionsItemSelected(item);
       }

       /**
        * A placeholder fragment containing a simple view.
        */
       publicstaticclassPlaceholderFragmentextends Fragment {

              publicPlaceholderFragment() {
              }

              @Override
              public View onCreateView(LayoutInflaterinflater, ViewGroup container,
                           Bundle savedInstanceState) {
                     View rootView = inflater.inflate(R.layout.fragment_main, container,
                                  false);
                     returnrootView;
              }
       }

}

SecondActivity




packagecom.vijay.sherlockactionbar;

importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.graphics.drawable.ColorDrawable;
importandroid.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

publicclassSecodActivityextendsActionBarActivity{
       @Override
       protectedvoidonCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              ActionBaractionBar = getSupportActionBar();
       actionBar.setDisplayHomeAsUpEnabled(true);
       actionBar.setBackgroundDrawable(newColorDrawable(Color.parseColor("#FF4444")));
       }

}


Check out this may be help you

Related Posts Plugin for WordPress, Blogger...