Put SearchView on the right of back button icon - android

So I am implementing a SearchView and right now I have two items in the layout. The back button and the search view.
I would like to know how can I make sure the back button appears on the left side of the screen before the search view, instead of the opposite.
This is the layout file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/back_btn"
android:title="#string/search"
app:icon="#drawable/ic_action_back_black"
android:icon="#drawable/ic_action_back_black"
app:showAsAction="ifRoom|collapseActionView"/>
<item android:id="#+id/search"
android:title="#string/search"
app:icon="#drawable/ic_search_black_24dp"
android:icon="#drawable/ic_search_black_24dp"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
I tried to put the back button before the search view, and it works fine until I click on the search box.
The Search view expands and the back button stays on the right side of the screen.
Question : Is there a way to make sure the back button will stay on the left side even when the search box expands?
This is the toolbar I am using:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/PopupMenuStyle"
android:id="#+id/my_awesome_toolbar"
android:layout_width="fill_parent"
android:layout_height="?android:attr/actionBarSize"
app:titleTextColor="#android:color/black"
android:layout_alignParentTop="true"
android:gravity="right"
android:elevation="5dp">
</android.support.v7.widget.Toolbar>

Hope this will help someone
First, make sure your activity not have an ActionBar
<item name="windowActionBar">false</item>
Second, add this code to your .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:id="#+id/my_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:padding="#dimen/margin_small">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-9dp"
android:layout_weight="6" />
<android.support.v7.widget.SearchView
android:id="#+id/my_search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/md_white_1000"
android:clickable="true"
android:focusable="true"
android:iconifiedByDefault="false" />
</LinearLayout>
</android.support.design.widget.AppBarLayout>
Last, set your back arrow programmatically
Display Back Arrow on Toolbar

inside search view tag add this attribute android:maxWidth

Related

Android. Toolbar menu doesn't show items

Toolbar menu doesn't show items and I can't understand why. When I start VDA-emulator it's shows menu icon, but it's blank inside. Can't add screenshot, coz Stackoverflow has some promblems with servers, as it say.
Menu screen in VDA-emulator
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
style="#style/ToolbarStyle"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:menu="#menu/menu_toolbar"
app:navigationIcon="#drawable/ic_arrow_back"
app:title="#string/toolbar_name" />
</LinearLayout>
Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="#string/toolbar_menu_1"/>
<item android:title="#string/toolbar_menu_2"
android:icon="#drawable/ic_comment"/>
</menu>
Current appcompact version 1.5.1

I need to hide title of items in bottom navigation bar

I create bottom navigation bar and i need to hide title and show icons only but when i write title with empty string there is big space between icon and bottom it just replace string with empty string and i make many search but i don't found solutions so any help
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.easyschools.student.HomeActivity">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/navigation"
android:layout_alignParentTop="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="#android:color/white"
android:layout_alignParentBottom="true"
app:menu="#menu/navigation"/>
</RelativeLayout>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nav_messages"
android:icon="#drawable/msg_icon"
android:title=""/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/home_icon"
android:title=""
android:checked="true"/>
<item
android:id="#+id/nav_notify"
android:icon="#drawable/notify_icon"
android:title=""/>
<item
android:id="#+id/nav_my_profile"
android:icon="#drawable/user_icon"
android:title=""/>
</menu>
Starting from support library 28.0.0-alpha1, you can use:
app:labelVisibilityMode="unlabeled"
to hide title and
app:itemHorizontalTranslationEnabled="false"
add:
xmlns:app="http://schemas.android.com/apk/res-auto"
to disable shift mode to your BottomNavigationView in xml
You can use following property of BottomNavigationView:
app:labelVisibilityMode="unlabeled"
also include following line in you layout:
xmlns:app="http://schemas.android.com/apk/res-auto"
For reference you can use following link:
https://www.11zon.com/zon/android/remove-bottom-navigation-view-title-in-android.php
The BottomNavigationView have a lot of limitations.
You can set the title with an empty String "" for example but I recommend you to use a library for enhancing the bottom navigation bar easily.
You can try this one : https://github.com/ittianyu/BottomNavigationViewEx

Hide show keyboard issue, showing colored background behind the keyboard

When focusing an input field, a colored (colorPrimary) background appears behind the keyboard (sometimes it extends up to 20-30px above the keyboard, overlaying the view). Hiding the keyboard makes the background visible for about 200-300ms after the keyboard is hidden and before it fades away. I am attaching a screen-shot too -
I tried setting (I am using fragment not activity)-
android:windowSoftInputMode="adjustPan"
Below is my layout -
<android.support.design.widget.TextInputLayout
android:id="#+id/til_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_mobile_number"
android:layout_marginBottom="#dimen/margin_8dp"
app:errorText="#{viewModel.passwordError}">
>
<EditText
style="#style/TextInputLayoutEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/password"
android:hint="#string/password"
android:inputType="textPassword"
android:onTextChanged="#{(text, start, before, count) -> viewModel.onPasswordTextChanged()}"
android:text="#={viewModel.password}"
/>
</android.support.design.widget.TextInputLayout>
And here is my textstyle -
<style name="TextInputLayoutEditText" parent="#android:style/Widget.EditText">
<item name="android:drawablePadding">#dimen/drawable_icon_padding</item>
<item name="android:paddingLeft">#dimen/drawable_icon_padding_left</item>
<item name="android:textSize">#dimen/common_font_size</item>
<item name="android:textColor">#color/commonFontColor</item>
<item name="android:textColorHint">#color/commonHintColor</item>
</style>
And my parent activity is -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/ll_top_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
..../>
.....
</android.support.v4.widget.DrawerLayout>
Generally when keyboard open/ hide it takes the background color as shadow, and if it's white we can't see the effect on a white screen, so I changed it to white and that's all.
Below is my main activity and it is holding the fragment where I was getting issue, my fragment color was white but my main activity color was not white and that's why I was getting issue.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/ll_top_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
..../>
.....
</android.support.v4.widget.DrawerLayout>

navigation drawer is always inflated when the app starts

I am trying to add navigation drawer to my main activity.
in design view of activity_main.xml it should be visible at left side of the activity layout as a shadowed animation(i don't know what terminology i should use here for that :| ),which on drag/swipe to right should be visible.
but in my case it is covering up the whole activity by default and is not displaying the actual contents of the activity ...which it ought to be...
i have gone through the solutionhere,but it is not working for me.
my layout code for activity_main.xml is as following:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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="match_parent"
tools:context="com.example.mts3_.p1_d3_app_bar.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/app_bar"
layout="#layout/app_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/tv"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/drawer_menu"
/>
</android.support.v4.widget.DrawerLayout>
and drawer_menu.xml(layout file) is as following:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:title="#string/img1"
android:id="#+id/img11"
android:icon="#drawable/pic_1"></item>
<item android:title="#string/img2"
android:id="#+id/img22"
android:icon="#drawable/pic_2"></item>
<item android:title="#string/img3"
android:id="#+id/img33"
android:icon="#drawable/pic_3"></item>
<item android:title="#string/img4"
android:id="#+id/img44"
android:icon="#drawable/pic_4"></item>
</group>
<item android:title="#string/social">
<menu>
<item android:title="#string/add_to_group"
android:id="#+id/add_to_group"
android:icon="#drawable/pic_5"></item>
<item android:title="#string/share"
android:id="#+id/share"
android:icon="#drawable/pic_6"></item>
<item android:title="#string/group"
android:id="#+id/group"
android:icon="#drawable/pic_7"></item>
</menu>
</item>
</menu>
First,change your height and width android:layout_width="wrap_content" and android:layout_height="match_parent",
Second,set gravity android:layout_gravity="left" in your NavigationView.
Try this .
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
app:menu="#menu/drawer_menu"></android.support.design.widget.NavigationView>
Edit
If you use android:layout_gravity="left" in your xml code , you will start NavigationView on the left .
And android:layout_gravity="left" is the same of `android:layout_gravity="start``.
If you use android:layout_gravity="right" in your xml code , you will start NavigationView on the right.
And use tools:openDrawer="start" in your code ,it will display in the preview in the android studio .it did not effect your display in your device .
Put below Code in Drawerlayout tag
tools:openDrawer="start"
Like Following Code
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
After that implement that code in navigationView Tag
android:layout_gravity="start"
Hope it work fine.

How to make the android activity title editable?

Can someone help me make the title of my activity editable? I have this note taking app where I want to be able to add a title to the specific note that I want to add. I have tried using the id of the EditText from the menu as label in the Manifest.xml but it doesn’t work.
Sample code for the menu
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Ok"
android:icon="#drawable/ic_done_white_48dp"
android:id="#+id/note_details_save"
app:showAsAction="always"/>
<item
android:title="back"
android:icon="#drawable/ic_back_button"
android:id="#+id/note_details_back"
android:orderInCategory="0"
app:showAsAction="always"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/note_details_title"/>
</menu>
Use setTitle("My title") by itself in your activity.
"My title" should be the content of your editText.
String title = et.getText().toString();
styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:background="#color/colorPrimary"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:elevation="8dp" app:elevation="8dp">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:paddingLeft="10dp"
android:text="#string/app_name"
android:ems="10"
android:id="#+id/editText2"
android:layout_weight="1" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
#Mel
I have no idea why no one has answered this properly. Here is the solution:
in the layout file of the activity(where u want the editable title). Add a editText inside your toolbar tag. It should look something like this(using support-library):
(Important: What's important to notice here is the editText inside your toolbar, don't worry too much about the attributes of the other tags).
<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" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/yourId"
android:hint="yourHint"
android:background="#android:color/transparent"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Now on the onCreate method of said activity disable the default uneditable title:
getSupportActionBar().setDisplayShowTitleEnabled(false);
If u want control the positioning of the editText inside the toolbar, you can wrap it in a ConstraintLayout or RelativeLayout.
That's it... good luck to you all

Categories

Resources