Showing posts with label action bar. Show all posts
Showing posts with label action bar. Show all posts

Sunday, 21 April 2013

Action Bar Sherlock : Action Bar with Custom Title View Example in Android

 

Add ActionBarSherlock Library Project to your Application Project, Here is a Link for this

activity_main.xml


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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>


custom_action_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#86898a"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="My Title"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>


MainActivity.java

package com.rajeshvijayakumar.customtitle;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;

public class MainActivity extends SherlockActivity {

    // Action bar
    private ActionBar mActionBar;
    private LayoutInflater mInflater;
    private View mCustomView;
    private TextView mTitleTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initActionBar();
    }

    private void initActionBar() {
        mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        mInflater = LayoutInflater.from(this);
        mCustomView = mInflater.inflate(R.layout.custom_action_layout, null);
        mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
        mTitleTextView.setText("My Own Title");
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);
        // mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.at_header_bg));
    }
}

Output :


  


Sunday, 20 January 2013

Action Bar Sherlock : Creating Action Menu Items in ICS Action Bar in Android

Add ActionBarSherlock Library Project to your Application Project, Here is a Link for this

MainActivity.java

package com.rajeshvijayakumar.actionmenuitem;

import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Menu;

public class MainActivity extends SherlockActivity {

    private MenuItem mGoItem;
    private MenuItem mClearItem;
   
    private static final int GO_ITEM_ID = 1;
    private static final int CLEAR_ITEM_ID = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        mGoItem = menu.add(0, GO_ITEM_ID, 0, null);
        mGoItem.setIcon(R.drawable.abs__ic_go).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_ALWAYS);
        mClearItem = menu.add(0, CLEAR_ITEM_ID, 0, null);
        mClearItem.setIcon(R.drawable.abs__ic_clear).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_ALWAYS);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {
        case GO_ITEM_ID :
            Toast.makeText(MainActivity.this, "You have Pressed 'Go' Menu Item", Toast.LENGTH_LONG).show();
            return true;
        case CLEAR_ITEM_ID :
            Toast.makeText(MainActivity.this, "You have Pressed 'Clear' Menu Item", Toast.LENGTH_LONG).show();
            return true;
        }
       
        return false;
    }   
}

Output :



Source Code : Download this Example with Library Here

Friday, 18 January 2013

ActionBarSherlock - How to Add ActionBar in your Android Project


To Add Common ActionBar to your entire Android Application follow the steps :

In Manifest File, do as shown below :



In Activity, do as  shown below :






To Add ActionBar for Particular Screen / Activity follow the steps :

In Manifest.xml










(or)