First of all , I know that There are too many answer here.But I have tried probably all of them. Here is links :
How to display custom view in ActionBar?
How to make a Custom View(EditText) on the actionbar automatically selected?
Some others web-sites :
http://wptrafficanalyzer.in/blog/adding-custom-action-view-to-action-bar-in-android/
http://www.sanfoundry.com/java-android-program-add-custom-view-actionbar/
I want to make a custom action bar which has a Edittext. Here is my code. But all my tried is fail. Emuletor or real phone crushing all my running.How can I fix it ?
My main_activty :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> data = new ArrayList<>();
data.add("hi");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
actionBarCustomView = findViewById(R.id.actionbar_in_toolbar); //wrapper because layout is included
autocompleteTextView = actionBarCustomView.findViewById(R.id.actionbar_search);
autocompleteTextView.clearFocus();
autocompleteTextView.setAdapter(new AutocompleteListViewAdapter(this, R.layout.activity_main, data, this));
}
}
my layout_part_of_design : Missing classes
The following classes could not be found: - android.support.design.widget.AppBarLayout (Fix Build Path, Edit XML, Create Class) - android.support.design.widget.CoordinatorLayout (Fix Build Path, Edit XML, Create Class) Tip: Try to build the project. Tip: Try to refresh the layout.
There is not any code or different something.I am using 21 API.
and Render problem :
Couldn't resolve resource @color/primary Tip: Try to refresh the layout.
Activity_main xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingBottom="10dp"
android:paddingEnd="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp">
<AutoCompleteTextView
android:id="@+id/actionbar_search"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:hint="Some Hint"
android:background="@color/primary_dark"
android:drawableLeft="@drawable/ic_search_white_24dp"
android:drawableStart="@drawable/ic_search_white_24dp"
android:gravity="center_vertical"
android:padding="5dp"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="@dimen/bigger_text_size_14sp" />
</RelativeLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<include
android:id="@+id/actionbar_in_toolbar"
layout="@layout/toolbar.xml" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:background="@android:color/white"
tools:ignore="MergeRootFrame" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>