I want to implement a search view by adding a EditText to toolbar.
Here is my layout,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:clickable="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<EditText
android:id="#+id/search_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="search"/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/place_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
, and my activity extends from ActionBarActivity.
As Google documented
One or more custom views. The application may add arbitrary child
views to the Toolbar. They will appear at this position within the
layout. If a child view's Toolbar.LayoutParams indicates a Gravity
value of CENTER_HORIZONTAL the view will attempt to center within the
available space remaining in the Toolbar after all other elements have
been measured.
I set the layout_gravity of EditText as center_horizontal. But it turns out that the EditText flows to the next row.
Is it possible to float the textedit to the right of the arrow icon?
Related
I have a blueprint which looks as thus:
I have the tablayout widget specified in xml like this:
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
style="#style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/tabBg"
android:minHeight="?attr/actionBarSize"
app:tabTextColor="#color/grey"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
I set it up with a ViewPager scrolling to swipe views for each tab. Now in one of the views - Tab 3, I need to arrange my components below the #id/tabLayout
This is the content of tab3.xml:
<?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:orientation="vertical">
<TextView
android:id="#+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tabLayout"
android:background="#android:color/white"
android:text="second"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/thirdtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/secondtext"
android:background="#android:color/white"
android:text="third"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Both the second and third textviews are displayed behind the tabLayout, they are covered/hidden by the tabLayout. How do i specify the textviews to display right under the tabLayout so the text is visible?
A RelativeLayout, like any other layout, is only responsible for laying out it's direct children. Therefore, the android:layout_below and similar attributes, can only work for two views within the same RelativeLayout parent.
In this case, you'd have to add it to the ViewPager. Then the TextViews can be just at the top of their container, as that is now below the TabBar.
If this is no good for your other tabs. You can do one of:
- Have the TabBar be a fixed height and set that as margin to the textviews
- Query the height of TabBar at runtime an apply it as padding or margin in tab3.
Define an id to RelativeLayout in tab3.xml and use it.
Use linear layout with vertical orientation .. It will work :)
I have tried to move the button using graphical interface and android:layout_alignParentLeft in XML file, anyway it does not work. My Android Studio version is 2.2.3. Have you ever had this problem?
You need RelativeLayout or other simlilar layout as parent container because FrameLayout just draw the views one over another plus you should check the properties section to see the attibute properties that you can apply on your layout.
To read further about ViewGroups and it's sub-types with their behaviours
If you forced to use FrameLayout(e.g in Toolbar or so) and you have only one element(or small amount) to operate with(button) you can use android:layout_gravity="" attribute to align element in the FrameLayout.
<FrameLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
If you have have few elements in your layout, you can: 1) Change FrameLayout to RelativeLayout or 2)Wrap all items into Relative layout and set parameters to match_parent
<FrameLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
</FrameLayout>
I have a dialog fragment that contains linear layout that involves a titleText above a RecyclerView, and at the very bottom, there's a button below the recyclerView.
Since a recyclerView expands or collapses based on the number of items the adapter sets, the button sometimes gets truncated and no longer appears to be on screen, since the recyclerView just covers the entire screen.
My question is, is there a way to set the maximum height of the recyclerView without ever hiding the button underneath. I also don't want to just give the view a random height just in case the recyclerView contains no items, and it would just be a blank section.
Please let me know if you've ever run into this issue, and how you resolved this. Thanks!
UPDATED
You can achieve this easily using layout weights. Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title"
android:textSize="21sp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="30dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Submit"/>
</FrameLayout>
The Title and RecyclerView will wrap content according to contents and button will always take up bottom place.
I suggest using RelativeLayout as it handles the positioning of views for cases like yours, so that you can actually focus on main design.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Some title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title"
android:layout_above="#+id/button"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"/>
</RelativeLayout>
Above XML code is the skeleton code for what you need. you can add margins and dimensions to control the spacing. But in any case (until you provide negative margins) your views will never overlap each other.
Main trick of using RelativeLayout is the ability to use XML tags like
android:layout_below or android:layout_above or android:layout_start
or android:layout_end which perfectly aligns your view the way you
want.
I have a CoordinatorLayout and inside it, two FloatingActionButton and below them a ScrollView.
The problem is that even though I can see my FloatingActionButtons on screen I cannot click them. I suspect that's because all onTouch events are being handled from ScrollView
Here is my layout xml (which is probably pretty bad so any tips are welcome):
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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">
<!-- Note: that 80dp padding is:
main_padding + fab_size + 8dp = 80dp
16dp + 56dp + 8dp = 80dp
and it was the only way I could think to add padding
between my FABs
I tried to use app:layout_anchor but my 2
FABs had no padding between them... -->
<android.support.design.widget.FloatingActionButton
style="#style/RefreshFab"
android:id="#+id/fab_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="80dp"
android:layout_marginEnd="16dp"
app:fabSize="normal"
app:pressedTranslationZ="12dp"/>
<android.support.design.widget.FloatingActionButton
style="#style/SendFab"
android:id="#+id/fab_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_gravity="bottom|end"
app:fabSize="normal"
app:elevation="6dp"
app:pressedTranslationZ="12dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Toolbar -->
<include
layout="#layout/view_toolbar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- Much stuff here, text views, spinners etc.. -->
</LinearLayout>
</ScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
The problem is because each item is laid out in a CoordinatorLayout in the same order it is written in the xml. By that logic, the FloatingActionButtons are each placed on the screen and then the LinearLayout on top of them, so I would expect the click is being overriden by the LinearLayout.
Rearrange your XML to place the FloatingActionButtons last, so that they are 'on top', so to speak, of your layout. Then they will detect your click listeners just fine.
I think the problem will persist since each FloatingActionButton is wrapped inside of a FrameLayout with match_parent dimensions. I do not believe you need these FrameLayouts, but you can simply put the FloatingActionButtons inside of the CoordinatorLayout.
I have a RelativeLayout which has a nested empty FrameLayout and a ListView below the FrameLayout. I am adding a view to the FrameLayout with animation (setting android:animateLayoutChanges="true" in xml) and I am doing this because I want it to appear at the top of my window. So it appears with the default fade in animation. The ListView on the other hand moves down, but it is not animated. What can I do for the ListView to slide down to its position, because this is not very good looking. If I have to change the way I define my views in the layout, that's ok also. I will consider every suggestion. Thx
Here is my code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"></FrameLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/frame"
/>
</RelativeLayout>