5

I'm trying to remove the left space before the search view of Android, without success. I tried every answer I found on Internet but didn't work. I'm using AppCompat themes for this activity. I'm targeting KitKat api (19).

Can someone help me please? enter image description here

EDIT

Layout of activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MainLayout"
android:background="#6ED19E">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/BackgroundLayout"
    android:background="#6ED19E">
    <ImageView
        android:src="@drawable/bgpattern"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/PatternImageView"
        android:scaleType="centerCrop" />
    <TextView
        android:text="Já estamos em muitas cidades\nEstamos na sua?"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ManyCityLabel"
        android:textColor="#ffffff"
        android:textSize="25dp"
        android:width="20dp"
        android:gravity="center"
        android:textStyle="normal" />
</RelativeLayout>
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/LocationsListView"
    android:background="#ffffff"
    android:visibility="invisible" />

Menu

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
  <item android:id="@+id/LocationSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:showAsAction="always"
        tools:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

The iconified stuff I did programatically because when I change the xml didn't work

EDIT If I set the background color of SearchView

enter image description here

The search doesn't fit the entire space. Why?

4 Answers4

1

make a custom search bar in the activity layout and set its height and width according to you

Aashutosh Kumar
  • 183
  • 4
  • 12
  • BhalchandraSW provided a nice example of this... I'll give ya kudos for the succinct and pertinent suggestion though, b/c your are right (either do this or modify the builtin SearchView http://stackoverflow.com/questions/11527636/styling-a-searchview-in-android-action-bar)? – samus Mar 28 '17 at 15:15
1

Get output like this:

You can do following:

Hide app bar in onCreate() like so:

// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.hide();
}

In the layout file set background to green (or your colour), add a SearchView and set background for SearchView to white like so:

<android.support.constraint.ConstraintLayout 
    ...
    android:background="#00FF00"
    ...>

    <android.support.v7.widget.SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF" />
    ...
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    ....
</android.support.constraint.ConstraintLayout>

I am pretty sure that your other search interface code shall work. You only need to redirect your event calls.

BhalchandraSW
  • 714
  • 5
  • 13
0
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/FullWidthSearchBar"
    android:background="#DD2277"
    android:orientation="horizontal">
    <item android:id="@+id/LocationSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:showAsAction="always"
        tools:actionViewClass="android.support.v7.widget.SearchView"/>
     <view android:layout_width="100dp"
        android:layout_height="wrap_content"/>
  </LinearLayout>
</menu>
samus
  • 6,102
  • 6
  • 31
  • 69
0
  1. Get a reference/handle to the SearchView
  2. Then a ref to the Title's view with getChildAt (need to experiment, start at 0)
  3. Set TextView's visibility to gone.
View sv = findViewById(R.Id.SearchView);
View tv = sv.getChildAt(n);
tv.setVisibility(GONE);
samus
  • 6,102
  • 6
  • 31
  • 69