Learn CustomView in Action Bar in Android

Introduction

 
This article explains about CustomView in the Action Bar.
 
In this article, you can add the EditText to the ActionBar. Inside the EditText, if you type any value and on click then you can show that value as a notification in your Activity.
 
For this, you will use an EditText in your XML file. First, you will get an object of ActiuoBar by calling the getActionBar() method. After getting the object of Action you will cal the setCustomVIew (int resid) method to the set EditText in the Action Bar. Now you will get the id of the CustomView EditText with this:
  1. final EditText edittext = (EditText) actionBar.getCustomView().findViewById(R.id.edittext);  
  2.   
  3. getCustomView(): It returns a CustomView 
Now you need to show the data as a Toast on the Activity so you will set the EditText on setOnEditorActionListener(). When you click anywhere on the screen notification will appear.
  1. search.setOnEditorActionListener(new OnEditorActionListener()   
  2. {  
  3.  @Override  
  4.  public boolean onEditorAction(TextView v, int actionId, KeyEvent event)   
  5.  {  
  6.   String a = search.getText().toString();  
  7.   //textview.setText(a);  
  8.   Toast.makeText(MainActivity.this, a, Toast.LENGTH_LONG).show();  
  9.   return false;  
  10.  }  
  11. }); 
Now you will set the CustomView display using the setDisplayOptions() method:
  1. bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM  
  2. ActionBar.DISPLAY_SHOW_HOME); 
Step 1
 
Clipboard02.jpg
 
Step 2
 
Create an XML file and write this:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    
  3. <EditText xmlns:android="http://schemas.android.com/apk/res/android"  
  4.           android:id="@+id/edittext"  
  5.           android:layout_width="match_parent"  
  6.           android:layout_height="match_parent"  
  7.           android:inputType="textFilter" >  
  8.    
  9. </EditText> 
Step 3
 
For this, you will use an Editext in your XML file. First, you will get an ActiuoBar object by calling the getActionBar() method. After getting the object of Action you will call the setCustomVIew(int resid) method to set the EditText in the Action Bar. Now you will get the id of CustomView EditText.
 
Now you need to show the data as a Toast on the Activity so you will set the EditText on setOnEditorActionListener(). When you click anywhere on the screen notification will appear.
 
Create a Java file and write this:
  1. package com.customviewactionbar;  
  2. import android.annotation.TargetApi;  
  3. import android.app.ActionBar;  
  4. import android.app.Activity;  
  5. import android.os.Build;  
  6. import android.os.Bundle;  
  7. import android.view.KeyEvent;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10. import android.widget.TextView.OnEditorActionListener;  
  11. import android.widget.Toast;  
  12. public class MainActivity extends Activity   
  13. {  
  14.  @TargetApi(Build.VERSION_CODES.HONEYCOMB)  
  15.  @Override  
  16.  protected void onCreate(Bundle savedInstanceState)   
  17.  {  
  18.   super.onCreate(savedInstanceState);  
  19.   setContentView(R.layout.activity_main);  
  20.   ActionBar bar = getActionBar();  
  21.   bar.setCustomView(R.layout.activity_main);  
  22.   final EditText edittext = (EditText) bar.getCustomView().findViewById(R.id.edittext);  
  23.   edittext.setOnEditorActionListener(new OnEditorActionListener()   
  24.   {  
  25.    @Override  
  26.    public boolean onEditorAction(TextView text, int Id, KeyEvent e)   
  27.     {  
  28.     String a = edittext.getText().toString();  
  29.     // textview.setText(a);  
  30.     Toast.makeText(MainActivity.this, a, Toast.LENGTH_LONG).show();  
  31.     return false;  
  32.    }  
  33.   });  
  34.   bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM ActionBar.DISPLAY_SHOW_HOME);  
  35.  }  
Step 4
 
Clipboard04.jpg
 
Step 5
 
Clipboard06.jpg


Similar Articles