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:
- final EditText edittext = (EditText) actionBar.getCustomView().findViewById(R.id.edittext);
-
- 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.
- search.setOnEditorActionListener(new OnEditorActionListener()
- {
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
- {
- String a = search.getText().toString();
-
- Toast.makeText(MainActivity.this, a, Toast.LENGTH_LONG).show();
- return false;
- }
- });
Now you will set the CustomView display using the setDisplayOptions() method:
- bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
- ActionBar.DISPLAY_SHOW_HOME);
Step 2
Create an XML file and write this:
- <?xml version="1.0" encoding="utf-8"?>
-
- <EditText xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/edittext"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:inputType="textFilter" >
-
- </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:
- package com.customviewactionbar;
- import android.annotation.TargetApi;
- import android.app.ActionBar;
- import android.app.Activity;
- import android.os.Build;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.TextView.OnEditorActionListener;
- import android.widget.Toast;
- public class MainActivity extends Activity
- {
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ActionBar bar = getActionBar();
- bar.setCustomView(R.layout.activity_main);
- final EditText edittext = (EditText) bar.getCustomView().findViewById(R.id.edittext);
- edittext.setOnEditorActionListener(new OnEditorActionListener()
- {
- @Override
- public boolean onEditorAction(TextView text, int Id, KeyEvent e)
- {
- String a = edittext.getText().toString();
-
- Toast.makeText(MainActivity.this, a, Toast.LENGTH_LONG).show();
- return false;
- }
- });
- bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM ActionBar.DISPLAY_SHOW_HOME);
- }
- }