I'm trying to add an icon at the end of my SearchView like this
Here's my actual code:
<RelativeLayout
android:id="#+id/home_relativelayout_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_primary">
<android.support.v7.widget.SearchView
android:id="#+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#color/grey_medium"
android:layout_margin="#dimen/mpoint_divider_padding"
android:gravity="center"
android:layout_centerVertical="true"
android:inputType="textNoSuggestions"
android:background="#drawable/wo_search_background" />
</RelativeLayout>
Seems there are no property such as android:drawableRight, I even tried to add a child like an ImageView but nothing appears on screen
Is it possible to achieve what I want with a SearchView or should I switch to an EditText+ImageView ?
Thanks for your help
You can do it something like this:
Add the ShapeDrawable something like this to your drawable folder:
Border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#android:color/white" /> //Background color here
<stroke android:width="3dip" android:color="*Border color*"/>
</shape>
Then Add the SearchView as follows:
<LinearLayout
android:layout_width="match_parent"
android:background="#drawable/Border"
android:layout_height="wrap_content"
android:Orientation="horizontal">
<android.support.v7.widget.SearchView
android:id="#+id/search_view"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:textColorHint="#color/grey_medium"
android:layout_margin="#dimen/mpoint_divider_padding"
android:gravity="center"
android:layout_centerVertical="true"
android:inputType="textNoSuggestions"
android:background="#drawable/wo_search_background" />
<ImageView android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
.../>
</LinearLayout>
Using this you can handle the click event by adding it to the image.
Revert in case of queries and if it doesn't work.
Related
Using Android's Layout mechanism, how do I create an input box with the password icon, and a split between the icon and the input field, as shown below? I'd like to have the border around the input field as well.
you can use imageview and an edit text inside a layout like this
<RelativeLayout
android:layout_width="#dimen/_280sdp"
android:layout_height="#dimen/_30sdp"
android:id="#+id/rl_password"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="#dimen/_15sdp"
android:layout_height="#dimen/_15sdp"
android:src="#drawable/key"
android:layout_centerVertical="true"
android:id="#+id/iv_password"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password"
android:layout_toRightOf="#+id/iv_password"
android:layout_centerVertical="true"
android:background="#00000000"
android:layout_marginLeft="#dimen/_8sdp"/>
</RelativeLayout>
Try This ....
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/background"
android:layout_margin="40dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="10dp"
android:src="#drawable/your_image" />
<View
android:id="#+id/view"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#dbdbdb" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:padding="10dp"
android:inputType="textPassword"
android:background="#android:color/transparent"/>
</LinearLayout>
#drawable/background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#fafafa" />
<corners android:radius="2dp" />
<stroke android:width="1dp" android:color="#dbdbdb" />
</shape>
</item>
</selector>
Sorry, but you will not find a tutorial here.
we guys here don't put tutorial if you have any problem in implementing your code you can ask.
for now, you can watch this tutrial may help.
I am creating an app where a layer-list is used as background and a floating action button is also used. When the layer-list is set as the background of the relative layout, it changes the position of the FAB, but I cannot understand why it happens.
I want the FAB to be positioned at the bottom left. When I don't have the background set as my layer-list, it appears where I have coded.
But when I add the background attribute to the Relative layout, FAB automatically moves in.
I can't understand what is affecting this behavior of FAB.
XML for layer_list:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary" />
<padding
android:bottom="64dp"
android:left="32dp"
android:right="32dp"
android:top="64dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners
android:bottomLeftRadius="64dp"
android:bottomRightRadius="64dp"
android:topLeftRadius="64dp"
android:topRightRadius="64dp" />
</shape>
</item>
XML for Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/home_layer_list">
<TextView
android:id="#+id/text_view"
android:layout_centerInParent="true"
android:textSize="36sp"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/ic_fab"
android:layout_margin="16dp"
app:backgroundTint="#FFFFFF"/>
How can I make the FAB move back to the position where it was initially?
Try to change your layout like below:
<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="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/home_layer_list">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="hello world"
android:textColor="#color/colorPrimary"
android:textSize="36sp"
android:textStyle="bold" />
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_fab"
android:layout_margin="16dp"
app:backgroundTint="#FFFFFF" />
</RelativeLayout>
I am using RecyclerView + GridLayout (3 columns). Now to make each square of the grid more "responsive", I want that each square will show some sort of divider, and that there will be a ripple effect within each square that the user clicks.
Edit : I added android:foreground="?attr/selectableItemBackground", but nothing happens.This is the single item xml code right now:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip" >
<ImageView
android:foreground="?attr/selectableItemBackground"
android:id="#+id/icon"
android:layout_width="128dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
This is how it currently look:
And this is how I want it to look:
This is how it works currently after fix, after adding:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
you can create a ripple drawable like this if you want it to be customize:
ripple.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorPrimaryDark">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryLight" />
<!--<corners android:radius="#dimen/button_radius_large" />-->
</shape>
</item>
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/background"
android:startColor="#color/background"
android:type="linear" />
<!--<corners android:radius="10dp" />-->
</shape>
</item>
and use it in your layout like this:
android:clickable="true"
android:background="#drawable/ripple"
OR
you can simply do it like this:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
You can add divider by adding a GridItemDecoration to your recyclerview. i suggest you to use this.
a simple way for adding ripple when you click on items is adding a foreground with selectableItemBackground value to your item's view:
android:foreground="?attr/selectableItemBackground"
try this way for Ripple Effect in xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground">
<ImageView
android:id="#+id/icon"
android:layout_width="128dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
and follow this link for Dividers in between items
How to add dividers and spaces between items in RecyclerView?
Hello guys i know this is not a new question. I am getting help from HERE. In which i have success to make circle button with plane background but i want to make a button with background image like this:
Here is related code for the layout:
<LinearLayout
android:id="#+id/viewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:background="#color/lighter_gray"/>
<Button
android:id="#+id/btnEdit"
android:background="#drawable/button_circular"
android:ellipsize="none"
android:layout_width="0dp"
android:layout_weight=".18"
android:layout_height="wrap_content" />
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="0.2"
android:layout_gravity="center_vertical"
android:background="#color/lighter_gray"/>
</LinearLayout>
#drawable/button_circular
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/app_main_color"/>
<stroke android:width="2dp" android:color="#color/app_main_color" />
</shape>
I also want to know is this a right way to get button like this? Thanks in advance.
You should use Imageview Instead of Button
<ImageView
android:id="#+id/btnEdit"
android:background="#drawable/button_circular"
android:ellipsize="none"
android:layout_width="0dp"
android:layout_weight=".18"
android:layout_height="wrap_content" />
If I want to get a look as below, what do I do? More specifically, how do I set the white background around multiple items?
EDIT:
The grey is the background for the whole screen, and the white is like a "box" placed on the grey background, together with the buttons, divider and textview.
I'm thinking like this:
<TableLayout
android:background="#eae8e8>
...
<WhiteBox
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
android:background="#FFFFFF/>
<TextView>
...
<Divider>
...
<Button>
...
<Button>
...
</WhiteBox>
</TableLayout>
Where "WhiteBox" of course is something else, but would it be possible to do something like this?
I would recommend leaving as many of the elements as possible transparent to prevent overdraw issues but it depends on your overall needs. By using #null backgrounds you can change the base background element and everything drawn over it would change accordingly (true transparent).
Layout
<?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:background="#color/white"
android:padding="10dp">
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:gravity="center"
android:inputType="text"
android:text="Test"
android:textColor="#CCC" >
<requestFocus />
</EditText>
<View
android:id="#+id/divider"
android:layout_below="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#CCC" />
<LinearLayout
android:id="#+id/buttons"
android:layout_marginTop="10dp"
android:layout_below="#+id/divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="#drawable/testbutton"
android:text="BUTTON 1"
android:textColor="#CCC" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="#drawable/testbutton"
android:text="BUTTON 2"
android:textColor="#CCC" />
</LinearLayout>
</RelativeLayout>
Button style
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#null" />
<stroke android:width="1dp" android:color="#CCC" />
</shape>
</item>
<item>
<shape>
<solid android:color="#null" />
<stroke android:width="1dp" android:color="#CCC" />
</shape>
</item>
</selector>
Yes you can go ahead with your approach.just give margin to your inner layout to which your background is set to white.and you can also set the background to white for the text views and buttons you are using inside your inner layout.Set the text color as gray for text view and button.eg:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eae8e8">
<RelativeLayout
android:layout_width="200dp"
android:layout_margin="30dp"
android:layout_height="200dp"
android:background="#FFFFFF"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text"
android:layout_margin="50dp"
android:text="TextView"
android:textColor="#android:color/darker_gray"
android:background="#FFFFFF"
/>
<Button
android:layout_marginTop="20dip"
android:text="Button"
android:textColor="#android:color/darker_gray"
android:layout_below="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:background="#FFFFFF"
/>
</RelativeLayout>
</LinearLayout>
By default when you create an xml with no backgrounds applied to your parent layout and if the widgets inside also does not have any background image/color set.it renders white on the screen.Just don't add any image/color as background.You can also set the background as white.
android:background="#FFFFFF" set this to your widgets in xml. like button or text view etc to have it appear white.