1

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>
  • did you try w custom toolbar instead i think that would have been better – Santanu Sur Mar 01 '18 at 18:09
  • just I am trying to put edittext in ActionBar. After that I am using it. İn the other hand , what is the custom toolbar ? – Hatay Berkay Işıkoğlu Mar 01 '18 at 18:11
  • exactly how are you trying to achieve this? did you create an additional xml layout, put a edittext in there and trying to place that xml as your action bar? I am finding it difficult to understand the technique which you've implementing because I don't see any connection of the code to the action bar itself. If you can give me an idea of what you are trying to achieve, I can try to point you in the right direction. – Nero Mar 01 '18 at 18:37
  • I want to put a AutoCompleteTextView in ActionBar. My purpose is I load to many words for gives me suggestion. (I said Edittext because there is to many resource ) By the way ; these codes are not mine. – Hatay Berkay Işıkoğlu Mar 01 '18 at 18:44

1 Answers1

0

You need a custom toolbar for sure.

First, let's create custom xml for toolbar.

toolbar.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: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>

then place this toolbar in your activity layout (includes fragment) activity_something.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">

        <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>

then in activity (written in Java), set as a custom toolbar, find views by Id and do all the stuff you need MyActivity.java

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.item_some_layout, data, this));

set this in your style themes.

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

app.gradle

minSdkVersion  16
targetSdkVersion  22
compileSdkVersion  27
buildToolsVersion  '27.0.3'
...
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation com.android.support:design:25.3.1
...

this should work.

tatocaster
  • 316
  • 3
  • 10