change background of text field in searchview of android sdk - java

I am making an app and I have an activity with a searchview. My point is that I want to change the background color of the text field (the black rectangle in screnn shot).
My drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
My layout:
<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"
android:fitsSystemWindows="true"
tools:context=".SearchActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="160sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/search_background"
android:orientation="vertical" >
<SearchView
android:id="#+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"
android:layout_marginTop="60sp"
android:actionViewClass="android.widget.SearchView"
android:background="#drawable/searchview"
android:iconifiedByDefault="false"
android:paddingLeft="10sp"
android:paddingTop="5sp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:showAsAction="collapseActionView"
android:focusable="false" >
</SearchView>
</LinearLayout>
Screenshot: http://imageshack.com/a/img829/3203/pd85.png
Can anybody help me with that?
thanks

After a little detective work you'll see that if you look at the source for the SearchView widget, you will see it extends LinearLayout with some added functionality. Looking at the line #242 it contains a widget (specifically a SearchAutoComplete which extends the AutoCompleteTextView which in the end extends EditText.
With that said, you'll need to modify the background attribute of that child element, with something on the lines of this:
SearchView c = findViewById(R.id.searchView);
EditText e = (EditText)c.findViewById(c.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
e.setBackgroundColor(Color.BLACK); //←If you just want a color
e.setBackground(getResources().getDrawable(R.drawable.YOUR_DRAWABLE));
//↑ If you want a drawable ↑
Useful links:
Source for SearchView

Works for me:
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_plate).setBackground...
Also for voice button:
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn).setBackground...

Related

Change the border around spinner android

Hello guys so I want to add a border around my spinner. just a rectangular box would work. I have already added the border. The problem is that the border keeps the size of the biggest object in the list not the selected object.
Just if it is not clear, My problem is that I have a border(rectangular box) around the spinner which is great, but the size of of the box is the problem. the size of the box is determined by the largest object in the list. Instead, I want the size of the box to be determined by the selected object from the spinner. So if I choose test the box should be small but if I choose "ajidhfkljhfiidsff1233" the box should become bigger.
I already added a border but it is around the spinner
xml of spinner:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<Spinner
android:id="#+id/spinner_for_reason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/color_for_botton_off"
android:gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/test"
</androidx.constraintlayout.widget.ConstraintLayout>
xml of color_for_botton_off:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/white" />
<stroke
android:width="1dp"
android:color="#607D8B" />
<corners android:radius="5dp" />
</shape>
xml spinner_xml_to_design:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#color/fav"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
Main.java:
options_list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_xml_to_design, options_list);
dataAdapter.setDropDownViewResource(R.layout.spinner_xml_to_design);
spinner_for_reason.setAdapter(dataAdapter);
Thank you in advance guys any help would be appreciated. :) it is my 2 nd post of overflow

setElevation does not show shadow

I can not draw the shadow with setElevation.I do not want to use a drawing to do this. Here is the code:
This is the parent.
Page.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/background">
<LinearLayout
android:id="#+id/pp_cont_new_list"
android:background="#color/background"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="5dp">
</LinearLayout>
</LinearLayout>
Here I build a simple layout.
PersonalPage.Java
final LinearLayout ROOT = (LinearLayout) findViewById( R.Id.pp_cont_new_list);
final LinearLayout LL = new LinearLayout(context); //Container
LL.setLayoutParams (new LinearLayout.LayoutParams(
Generic.PARAM_MP, //eq. match_parent
(int) context.getResources ().getDimension (R.dimen.bt_preview_list_height)
));
LL.setBackgroundResource (R.drawable.background_new_list);
LL.setElevation (5f);
LL.setClickable (true);
ROOT.addView( LL );
/*
* other code inner the container
*/
this background
background_new_list.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#color/background"/>
</shape>
</item>
<item>
<ripple android:color="#color/textColorAlpha"/>
</item>
</layer-list>
I would like an explanation of my error.
And I would also like to know what to use for pre-LOLLIPOP.
set elevation just doesnot work for pre-lolipop
You can create a custom button image that has a shadow and set it as button background..or any view's background to give shadow effect
use ViewCompat.setElevation(your_view(in your case LL), elevation_amount);
but for devices pre-Lollipop this will not have effect,
for pre-Lollipop cases you might use:
android:background="#android:drawable/dialog_holo_light_frame"
This is working if you don't mind doing things in xml. I tried it out. I did not have background color value that you are using so I used a different color for parent layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E8E8E8"
android:paddingBottom="20dp"
android:clipToPadding="false"
android:translationZ="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/background_new_list"
android:clipToPadding="false"
android:elevation="25dp"
android:orientation="vertical"
/>
Note: I am using a higher elevation value just to make it easier to check if it's working. you can adjust it to what you need.

Android : Create translucent edittext with image inside

I am working on an Android project in which I have a login page where I want to add Username and password fields. The design I have requires me to put them in this translucent manner and with an image inside. Any ideas what I can change for EditText to make it translucent and shape it, there are many threads to make it transparent by setting the background to null or by color and so on, but nothing like this I found.
Design :
I am using Relative layout, and those icons on right side, I have them in separate PNG files. I can directly add them inside Edittext, but I am concerned that if the username and password is long, it will start going over the image. Any strategies for that? Thank you.
Code :
<?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:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Text"
android:id="#+id/textView"
android:textSize="32sp"
android:textColor="#color/common_signin_btn_dark_text_default"
android:layout_marginTop="75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_below="#+id/textView"
android:layout_marginTop="41dp"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView" />
</RelativeLayout>
Updated screenshot
Updated EditText
<EditText
android:layout_width="wrap_content"
android:layout_height="45dp"
android:id="#+id/emailEditText"
android:inputType="textEmailAddress"
android:layout_below="#+id/textView"
android:layout_marginTop="41dp"
android:drawableEnd="#drawable/Mail"
android:drawableRight="#drawable/Mail"
android:maxLines="1"
android:ellipsize="end"
android:hint="Email"
android:textColorHint="#color/common_signin_btn_dark_text_focused"
android:background="#drawable/layout_bg"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView" >
</EditText>
layout_bg.xml :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/traslucent_background" />
<stroke android:width="1dip" android:color="#B1BCBE" />
<corners android:radius="17dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
For the image you should be able to use android:drawableEnd="#drawable/..." and android:drawableRight="#drawable/..." which also will limit the text length.
For the background you can define a custom drawable like this:
<!-- background_edittext.xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#color/traslucent_background" />
</shape>
Where traslucent_color would be defined in colors.xml, for example, like this:
<color name="shadow_lighter_color">#64000000</color>
EDIT: this would be an example of how to use drawableEnd / drawableRight with a space between text and icon:
<EditText
android:background="#drawable/background_edittext"
android:drawablePadding="16dp"
android:drawableEnd="#drawable/icon"
android:drawableRight="#drawable/icon"
android:maxLines="1"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Try this:
Define a shape drawable in your project like this:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#88ff0000"/>
<corners android:radius="32dp"/>
</shape>
And set it as the background for your EditTexts. Also, add this as an attribute to your EditText in the XML:
android:drawableRight="#drawable/any_drawable_file"
If you're concerned about the text overlapping with the drawable, you could add a custom paddingRight to your EditText to tackle that. It would save you the effort of inflating a custom layout.

Inflating drawable from XML

I'm trying to do a bottom-level menu like this:
I've defined the layout this way:
<LinearLayout
android:id="#+id/bottomBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="4dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- First item of the menu -->
<ImageButton
android:id="#+id/BConex1"
android:contentDescription="#string/desc_gen_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:layout_gravity="left"
android:background="#drawable/menu_bottom"
android:layout_marginLeft="4dp" />
<!-- Second item of the menu -->
<ImageButton
android:id="#+id/BConex2"
android:contentDescription="#string/desc_gen_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:layout_gravity="left"
android:background="#drawable/menu_bottom"
android:layout_marginLeft="4dp" />
<!-- ... Some additional items in the menu -->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
The #drawable/menu_bottom is a layer-list, defined this way:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1px"
android:color="#55ffffff" />
<padding
android:left="16px"
android:top="6px"
android:right="16px"
android:bottom="6px" />
<solid
android:color="#cc000000" />
<gradient
android:startColor="#222222"
android:centerColor="#111111"
android:endColor="#000000"
android:angle="-90" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
</item>
<item>
<bitmap
android:src="#+drawable/bitmap_to_change"
android:tileMode="disabled"
android:width="30dp"
android:height="15dp"
android:gravity="center" />
</item>
</layer-list>
Now the thing is: I want to use exactly the same background for each button in the menu, in exception of the src field of the bitmap. I've tried to use LayoutInflater on one of the buttons and tried to change it programatically, like this:
View first = LayoutInflater.from(this).inflate(R.drawable.menu_bottom, null);
first.findViewById(R.drawable.bitmap_to_change).setBackground(this.getResources().getDrawable(R.drawable.another_resource));
But this returns an exception:
12-11 11:35:53.556: E/AndroidRuntime(897): java.lang.RuntimeException:
Unable to start activity ComponentInfo{XXX.MainActivity}:
android.view.InflateException: Binary XML file line #2: Error
inflating class layer-list
So I assume that's not the correct way. Is there a way to accomplish this instead of defining one XML file per menu item?
Thanks!
----------- EDIT -----------
I've found a "dirty" workaround to achieve my goal: getting the background of one of the buttons as a LayerDrawable and substituting the Bitmap with the correct. This is not the way I want to do it (as I want it entirely programatically) nor it's the original question, so I'm not going to answer myself letting the question opened in the hope someone can help, but here's what I'm doing now:
I assigned an id to the corresponding "item" in the XML file:
<item android:id="#+id:bitmap_layer">
And programatically:
ImageButton firstButton = (ImageButton) findViewById(R.id.BConex1);
LayerDrawable bg = (LayerDrawable) firstButton.getBackground();
bg.setDrawableByLayerId(R.id.bitmap_layer, getResources().getDrawable(R.drawable.the_new_drawable));
You don´t need to use the iflater, getting the Drawable resurse is enough:
android.graphics.drawable.Drawable shape = getResources().getDrawable(R.drawable.item_panel_borde);
with your workaroud you can get trouble if you use this drawable resource in other views, you can create your LayerList programatically using the android.graphics.drawable.LayerDrawable(Drawable[]) constructor.

help with listview and R.layout.main (Android)

im quite new to android, so i apologise if this is a noob-ish question (:
i designed my list following the example found here: http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html
but what i would like to know is how would i go about adding setContentView(R.layout.main) so that i can display other (xml) elements along with the list?
thanks for any advice (:
You just edit your main.xml to look whatever you want it to look, eg:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View android:id="#+id/emptyview"
android:layout_height="30dp"
android:layout_width="fill_parent"
/>
<ListView android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true"
android:stackFromBottom="true"
android:layout_below="#id/emptyview"
android:headerDividersEnabled="true"
/>
</RelativeLayout>
Change your activity to extend only Activity, not ListActivity. Finally, in your activity you can then fetch your list view with:
ListView list = (ListView) findViewById(R.id.listview);
and do whatever you need to do with the list.
You create your main.xml
Add to it a ListView
<ListView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/myListView"
android:divider="#ffa500" android:dividerHeight="1px"
android:background="#drawable/somedrawable_xml"
android:choiceMode="singleChoice"></ListView>
somedrawable_xml.xml could be any drawable example:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ffa500" />
<stroke android:width="2dp" android:color="#ffffa500" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
Add a layout xmlFile myLayout.xml example: (I added imageView for demonstration) anyway what is important is the id of the textview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dasdasd">
<TextView android:text="TextView" android:layout_height="wrap_content"
android:id="#+id/thisIsTheTextView" android:layout_width="wrap_content" android:textAppearance="?
android:attr/textAppearanceLarge"></TextView>
<ImageView android:src="#drawable/icon" android:id="#+id/imageView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"></ImageView>
finally in your Activity
ArrayAdapter myAD=new ArrayAdapter(mContext,R.layout.myLayout,R.id.thisIsTheTextView,new String[] {"item1", "item2", "item3", "item4", "item5"});
myListView.setAdapter(myAD);

Categories