Why spinner is so big? - android

I'm developing an Android application and I have a problem with designing layouts:
With this layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
<TextView
android:id="#+id/textUploadPhotos"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="#string/layout_upload_photos"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinUploadPhotos"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:background="#drawable/spin_selector" />
</LinearLayout>
And the spin_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/select" android:state_pressed="true"/>
<item android:drawable="#drawable/select" android:state_focused="true"/>
<item android:drawable="#drawable/select"/>
</selector>
I get this:
#drawable/select is this one:
How can set spinner size to #drawable/select height?

Use 9 patch as background of your spinner. I think this will be interesting for you: http://adanware.blogspot.com/2012/03/android-custom-spinner-with-custom.html

You image seems to be the culprit. If you can't use a 9-patch png, try setting the height attribute of the spinner to a better value. Perhaps, right now, you have set it to wrap_content.
Or, may be you can try reducing the height of the image itself.

Related

Flip arrow on android spinner in toolbar

I have a spinner in a toolbar at the bottom of the screen, but the arrow next to the spinner points down, which is counterintuitive. Is there any quick way to flip the arrow to point upwards instead?
If it helps, the spinner is defined as follows:
<Spinner
android:id="#+id/spinner_floors"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And the row layout is just android.R.layout.simple_spinner_dropdown_item.
On another note, is there any way to change the font color to white without affecting the font color when the spinner is expanded?
Update
I managed to switch the color of the text using this answer. I also switched the background for this image so that now the code is
<Spinner android:id="#+id/spinner_floors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_arrow_drop_up_white_24dp" />
Now it looks like this:
How do I move the button to the right?
Note: I'm using Android Marshmallow, API level 23
The default background of a spinner is a 9-patch drawable, so it can scale accordingly. If you want to invert the arrow you have to copy the drawable and invert it manually.
Here I took the background of the spinner found on Lollipop1 and flipped it vertically:
You can copy that and save it into the drawable-xxxhdpi folder as a 9-patch, i.e. with the extension .9.png (not just .png).
1 [android-sdk]/platforms/android-22/data/res/drawable-xxxhdpi/spinner_mtrl_am_alpha.9.png
Since Marshmallow, the default background is an XML drawable (selector and vector drawable). If you only want to support Android 6.0 and newer you can copy that from the Ansroid SDK and modify it such that it is vertically flipped.
You can find the drawable in the [android-sdk]/platforms/android-23/data/res/drawable/ directory. It's named spinner_background_material.xml and depends on control_background_40dp_material.xml and ic_spinner_caret.xml.
Try this
<Spinner
android:id="#+id/spinner_floors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown" <!-- You can use your own drawable -->
/>
Use this with your own icon for example: https://design.google.com/icons/#ic_arrow_drop_up
toolbar_spinner.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="#+id/toolbar_spinner"
style="#style/Widget.MyApp.HeaderBar.Spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
toolbar_spinner_item_actionbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/ic_arrow_drop_up_black_24dp"
android:drawablePadding="8dp"
android:fontFamily="sans-serif"
android:paddingEnd="4dp"
android:paddingStart="16dp"
android:textColor="#ffffffff"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
toolbar_spinner_item_dropdown.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:drawablePadding="8dp"
android:gravity="center_vertical|start"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#ff333333"
android:textSize="16sp"/>
</LinearLayout>
And add this to your Styles:
<style name="Widget.MyApp.HeaderBar.Spinner" parent="Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
<item name="android:background">?android:selectableItemBackground</item>
<item name="android:dropDownSelector">?android:selectableItemBackground</item>
<item name="android:divider">#null</item>
<item name="android:overlapAnchor">true</item>
</style>
Then add it in the OnCreate:
Toolbar toolbar = getActionBarToolbar();
View spinnerContainer = LayoutInflater.from(this).inflate(R.layout.toolbar_spinner,
toolbar, false);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
toolbar.addView(spinnerContainer, lp);
YourObjectSpinnerAdapter spinnerAdapter = new YourObjectSpinnerAdapter();
spinnerAdapter.addItems(getMyObjectSpinnerData());
Spinner spinner = (Spinner) spinnerContainer.findViewById(R.id.toolbar_spinner);
spinner.setAdapter(spinnerAdapter);
Taken from: https://dabx.io/2015/01/02/material-design-spinner-toolbar-style-fix/
With some changes and updating.
And here is the another method with changing the icon, thanks to Floern for suggest:
You have to create two drawable file named:
1.control_background_40dp_material.xml
2.spinner_background_material.xml
And, the first one content:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/darker_gray"
android:radius="20dp" />
Second one:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingEnd="24dp"
android:paddingLeft="0dp"
android:paddingMode="stack"
android:paddingRight="0dp"
android:paddingStart="0dp">
<item
android:width="24dp"
android:height="24dp"
android:drawable="#drawable/control_background_40dp_material"
android:gravity="end|center_vertical" />
<item
android:width="24dp"
android:height="24dp"
android:drawable="#drawable/ic_arrow_drop_up_black_24dp"
android:gravity="end|center_vertical" />
</layer-list>
That's it, just use that Spinner like this:
<Spinner
android:id="#+id/spinner_floors"
android:background="#drawable/spinner_background_material"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
There you go, have fun.

Android: How to create a customized edittext?

I am wondering if anybody can help me with designing an editText like the following:
Note the red vertical line meant to show the obligatory field.
I recreated what you are looking for through the layout and it may not be exactly what you are looking for. I gave it a try because I thought it looked pretty good.
This is how it looks when created
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#d2d2d2">
<EditText
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_margin="1dp"
android:paddingLeft="16dp"
android:gravity="center_vertical"
android:background="#fff"
android:textColor="#6d6d6d"/>
<view
android:layout_width="1dp"
android:layout_height="56dp"
android:layout_alignParentRight="true"
android:background="#android:color/holo_red_dark"/>
</RelativeLayout>
You can use Selector.
For example:
In res-->drawable folder create another xml file with name "custom_edittext.xml" and paste below code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_edittext_focused" android:state_focused="true"/>
<item android:drawable="#drawable/image_edittext_normal"/>
</selector>
Finally call custom_edittext.xml as background in your editText
<EditText
...
android:background="#drawable/custom_edittext"
...
/>

Custom Checkbox changes its size when isChecked is true Android?

I have an single choice ExpandableListView with a custom checkbox defined by a Drawable I created in the Android Asset Studio.
For some reason, when the checkbox is checked it looks smaller than when it is not. I already checked if I made a mistake with the size of the images and they all have the same size. (disabled or enabled).
Here's the code of my custom checkbox:
drawable/custom_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_success_enabled" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="#drawable/ic_accept_disabled" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="#drawable/ic_accept_disabled" android:state_checked="false"/>
<item android:drawable="#drawable/ic_success_enabled" android:state_checked="true"/>
</selector>
My drawable/ic_success_enabled & disabled are:
The rows for each element in the ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="#+id/title_one_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/one_item_checkbox"
android:layout_toRightOf="#+id/one_item_row_image"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/akdemia_0_black" />
<com.akdemia.application.CircularImageView
android:id="#+id/one_item_row_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:src="#drawable/placeholder_icon_iphone" />
<CheckBox
android:id="#+id/one_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/akdemia_checkbox"
android:button="#null"
android:focusable="false" />
</RelativeLayout>
And here is a screen shot of the ExpandableListView, where you guys can see the problem: (All the orange rectangles where added on purpose, just ignore them)
Hope anyone can help me with this, thanks in advance!
Give your checkbox a layout width and height like this:
android:layout_width="25dp"
android:layout_height="25dp"

Android selector adding weird padding

Here is my simple background selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/filters_group_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/filters_group_pressed" android:state_selected="true"/>
<item android:drawable="#drawable/filters_group"/>
</selector>
In the layout editor layout look's like:
After launching on nexus 7 (first generation) it look's like:
It looks fine if I set as background my 9patch graphic (which selector contains) and not the selector. It looks like an android bug to me...
White bar should be stretched at whole width and the icon should be centered. Here is the layout:
<org.mycustomview.MyCustomLinearLayout
android:id="#+id/group_button"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#drawable/filter_group_button_selector"
android:gravity="center"
android:clickable="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/icon_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_table" />
<TextView
android:id="#+id/title_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sala 1"
android:lines="1"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:background="#color/white"
android:gravity="center"
android:textColor="#color/white" />
</org.mycustomview.MyCustomLinearLayout>
It must be a bug. It added right padding itself. I added android:paddingRight="0dp" to my MyCustomLinearLayout and it's work like expected.

ImageView is not highlighting on click

I am designing an app for multiple devices.In that I am using a imageview and using selector i am setting the background image depends on the state.I works fine for all the devices except only one 10 inch device.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" >
<LinearLayout
android:layout_width="145dp"
android:layout_height="239dp"
android:layout_marginRight="6dp"
android:background="#drawable/common_selector_thumbnail_shadow_title_background"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<FrameLayout
android:layout_width="130dp"
android:layout_height="186dp"
android:layout_marginTop="5dp"
android:background="#color/RGB_100_215_216_217" >
<ImageView
android:id="#+id/seasonal_favorites_default_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="#drawable/tw_noitem_movie" />
</FrameLayout>
<TextView
android:id="#+id/seasonal_favorites_list_text"
android:layout_width="140dp"
android:layout_height="43dp"
android:duplicateParentState="true"
android:ellipsize="end"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:singleLine="true"
android:textColor="#drawable/common_selector_thumbnail_shadow_title_textcolor"
android:textSize="18dp" />
</LinearLayout>
</FrameLayout>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/thumbnail_title_bg_focus" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/thumbnail_title_bg_focus" android:state_pressed="true"/>
<item android:drawable="#drawable/thumbnail_title_bg_focus" android:state_focused="true"/>
<item android:drawable="#drawable/thumbnail_title_bg"/>
</selector>
Thanks in advance.
Probabilly you have to put in your manifest file: hardwareAccelerated="true"
Try that and let me know if it worked!
Use ImageButton there are two methods setBackgroundResource, setImageResource to set resources for button(which one will be pressed) and for image itself
You need to add android:clickable="true" to the LinearLayout or set a OnClickListener there. Otherwise your selector background does not get activated.
Use ImageButton instead of ImageView to get the selector work.
If you can provide the different states of the image then see this
post
Otherwise...
Use a LayerDrawable for the image source. One layer is the actual image, the other layer is a state list selector.
LayerDrawable d = new LayerDrawable(new Drawable[]{getResources().getDrawable(R.drawable.my_image), getResources().getDrawable(R.drawable.my_selector_list)});
imageView.setImageDrawable(d);
Or you can define a layer drawable XML resource and use that in your layout XML.

Categories

Resources