Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Action Bar in Android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 144
    Comment on it

    Here I have created Action Bar in android. In android Action Bar is used to provide navigation and perform some actions.
    In this blog I have implement a Action Bar in my Android project and perform actions such as switching fragment. Below example will described you how to make Action Bar in Android .

    Step(1)-Main.xml.Layout-

    <LinearLayout 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"
        tools:context=".MainActivity">
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
        </LinearLayout>
    

    Step(2)-main_Fragment.layout-

      <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40dp"
            android:text="Main Layout"/>
    </LinearLayout>
    

    Step(3)-text_fragment-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40dp"
            android:id="@+id/txt"/>
    </LinearLayout>
    

    Step(4)-menu_main.xml-

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:id="@+id/action_speech"
            android:icon="@drawable/ic_action_location"
            android:showAsAction="ifRoom"
            android:title="Speech"/>
        <item android:id="@+id/action_call"
            android:icon="@drawable/ic_action_refresh"
            android:showAsAction="ifRoom"
            android:title="Call"/>
        <item android:id="@+id/action_done"
            android:icon="@drawable/ic_action_settings"
            android:showAsAction="ifRoom"
            android:title="Done"/>
        <item android:id="@+id/action_contacts"
            android:showAsAction="never"
            android:title="Contacts"/>
        <item android:id="@+id/action_settings"
            android:showAsAction="never"
            android:title="Settings"/>
        <item android:id="@+id/action_status"
            android:showAsAction="never"
            android:title="Status" />
    </menu>
    

    Step(5)-MainActivity-

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Fragment main = new Fragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, main);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.addToBackStack(null);
            ft.commit();
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
    
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            int id = item.getItemId();
            switch (id) {
                case R.id.action_call:
                    Intent dialer= new Intent(Intent.ACTION_DIAL);
                    startActivity(dialer);
                    return true;
                case R.id.action_speech:
                    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    startActivityForResult(intent, 1234);
    
                    return true;
                case R.id.action_done:
    
                    Bundle args = new Bundle();
                    args.putString("Menu", "You pressed done button.");
                    Fragment detail = new TextFragment();
                    detail.setArguments(args);
                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
    
                    return true;
                case R.id.action_contacts:
                    Toast.makeText(getApplicationContext(), "Contacts Clicked", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.action_settings:
                    Toast.makeText(getApplicationContext(),"Settings Clicked",Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.action_status:
                    Toast.makeText(getApplicationContext(),"Status Clicked",Toast.LENGTH_SHORT).show();
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == 1234 && resultCode == RESULT_OK) {
                String voice_text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).get(0);
                Toast.makeText(getApplicationContext(),voice_text,Toast.LENGTH_LONG).show();
    
            }
        }
    }
    

    Step(6)-Create MainFragment class-

    public class MainFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.main_fragment, container, false);
    
            return view;
        }
    
    }
    

    Step(7)-Create TextFragment class-

    public class TextFragment extends Fragment {
       TextView text;
    
        public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.text_fragment, container, false);
            text= (TextView) view.findViewById(R.id.txt);
            String menu = getArguments().getString("Menu");
            text.setText(menu);
            return view;
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: