Download Android Action Bar Demo:Best Example
Action Bar in Android is used to provide navigation and to perform some actions. In this tutorial we are going to implement a Simple Action Bar in our Android Application and perform actions such as switching fragments , speech recognition. More Advanced implementation will be covered in another article. To support older version of Android use Android Support Library.Layout :
The Action Bar items should be added in the xml file and placed in menu folder. The default file is menu. Each item should be added with <item> tag. Each item has its own id. The “icon” specifies the icon which should be displayed in Action Bar. The “title” specifies the name which should be displayed in Action Bar. The “showAsAction” specifies the display property.showAsAction = “ifRoom” – Displays item in Action Bar if space is available.
showAsAction = “never” – Never displays item in Action bar
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_speech"
android:icon="@drawable/ic_action_microphone"
android:showAsAction="ifRoom"
android:title="Speech"/>
<item android:id="@+id/action_call"
android:icon="@drawable/ic_action_call"
android:showAsAction="ifRoom"
android:title="Call"/>
<item android:id="@+id/action_done"
android:icon="@drawable/ic_action_done"
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>
main_fragment.xml
<?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>
text_fragment.xml
<?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>
activity_main.xml
<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>
MainFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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;
}
}
TextFragment.java
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TextFragment extends Fragment {
TextView text;
@Override
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;
}
}
MainActivity.java
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment main = new MainFragment();
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.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();
}
}
Output:
Download Code:openload
Download Android Action Bar Demo:Best Example
Reviewed by Anonymous
on
January 09, 2016
Rating:
No comments: