spinner dropdown coming in the middle of the screen with android 2.3 - android-spinner

I am trying following with android 2.3 as well as andorid 4.0.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/myColor"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinnerControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#android:color/white"
android:drawSelectorOnTop="true"
android:divider="#000000"
android:dividerHeight="1dp"
style="#style/Sherlock.__Widget.Holo.Spinner"
/>
</LinearLayout>
The style xml file is -
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Sherlock.__Widget.Holo.Spinner" parent="#android:style/Widget">
<item name="android:dropDownVerticalOffset">0dip</item>
<item name="android:dropDownHorizontalOffset">0dip</item>
<item name="android:dropDownWidth">wrap_content</item>
<item name="android:gravity">left|center_vertical</item>
<item name="android:spinnerMode">dropdown</item>
<item name="android:clickable">true</item>
</style>
</resources>
With android 4.0, I am getting spinner dropdown, right under the spinner control. But with andorid 2.3, it is coming in the middle of the screen, I don't know why. Please help.

To be precise, it was a new theme added to 4.0 by android. To get this style you need to provide the theme called "holo". But unfortunately it is not available for 2.X devices. And so you will not be able to get such a drop down in 2.X.

Related

How to make StatusBar Covered by DrawerLayout

this is how my activity_main.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<!-- the root view must be the DrawerLayout -->
<androidx.drawerlayout.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/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
/>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
style="#style/Widget.Custom.NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header_navigation_drawer"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
This is how my theme looks like:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.CardsXML1" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/white</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
</style>
</resources>
I want to make drawer cover the status bar. Everything I've treid is in those pieces of code.
I used android:fitsSystemWindows="true" in activity_main.xml
and in theme i placed windowActionBar, windowNoTitle and android:windowTranslucentStatus parameters
but it still doesn't work.
Keep in mind that I want to use MaterialComponents in themes!

Bottom Sheet Dialog - Transparent background header with close button

I am trying to make a transparent background header for close button. The background header is working as expected, however the background layout can see the visible and button is also not clear.
Am trying to build like this below picture
https://ibb.co/CVgrzjq
My code is as follows:
Activity Code
final BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetMainStyle);
`dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
`dialog.show();
Layout Code
<RelativeLayout 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:background="#android:color/transparent"
android:gravity="center|top"
android:orientation="vertical">
<ImageView
android:id="#+id/img_close"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:clickable="true"
android:forceDarkAllowed="false"
android:paddingBottom="#dimen/dimen_8"
android:src="#drawable/bottom_close"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#id/img_close"
android:background="#drawable/rounded_dialog"
app:background="#color/bkgcolor"
android:orientation="vertical">
<!-- All your required widgets here-->
</RelativeLayout>
</RelativeLayout>
Activity code
final BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.trans_bg_dialog);
`dialog.show();
Add this in your style.xml file
<style name="trans_bg_dialog" parent="#style/Theme.AppCompat.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>

Adding space between tabs in a TabLayout

I am trying to add spaces between the tabs.
I would like to add gaps between the tabs, I have tried using padding but that changes the entire tab layout padding and not individual tabs. I have also tried other methods e.g. minWidth, while searching for an idea but couldn't figure it out, so i am here. Most of the project is default from when i created the tabbed layout activity, Here is the code i added/changed:
Thanks in Advance.
tab_background
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="18dp"/>
<!-- TabLayout background color -->
<solid
android:color="#color/colorPrimaryDark"/>
</shape>
tab_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- radius should be half of the desired TabLayout height -->
<corners
android:radius="18dp"/>
<!-- color of the selected tab -->
<solid
android:color="#color/colorWhite"/>
</shape>
tab_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- drawable for selected tab -->
<item
android:drawable="#drawable/tab_selected"
android:top="-2dp" android:left="-5dp" android:right="-5dp" android:bottom="2dp"
android:state_selected="true"/>
<!-- drawable for unselected tab -->
<item
android:drawable="#drawable/tab_background"
android:top="-2dp" android:left="-5dp" android:right="-5dp" android:bottom="2dp"
android:state_selected="false"/>
</selector>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="top|center_horizontal"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/app_name"
android:fontFamily="#font/sofiabold"
android:background="#color/colorPrimary"
android:textAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="56dp"
app:tabTextColor="#color/colorWhite"
app:tabMode="auto"
app:tabGravity="center"
android:layout_gravity="center"
android:background="#color/colorPrimary"
app:tabBackground="#drawable/tab_selector"
app:tabSelectedTextColor="#color/colorPrimary"
app:tabPaddingStart="16dp"
app:tabPaddingEnd="16dp"
android:paddingBottom="20dp"
app:tabIndicatorHeight="0dp"
app:tabRippleColor="#null"
app:tabTextAppearance="#style/TabTextAppearance"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
You should use xml drawable, specially designed for this purposes - Inset Drawable: http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset
For example:
res/drawable/tab_selector_inset.xml:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/tab_selector"
android:insetRight="10dp"
android:insetLeft="10dp" />
And use it in your TabLayout:
app:tabBackground="#drawable/tab_selector_inset"
(Inspired by Vasily Sochinsky)
It is late but it may be useful for other people .if you have custom background for tabs(selector), you just need add layer list to tabs background and set margin for them
selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_selected_tab" android:state_selected="true"/>
<item android:drawable="#drawable/bg_unselected_tab"/>
</selector>
selected tab:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="8dp" android:left="8dp">
<shape>
<stroke android:width="1dp" android:color="#color/Green"/>
<corners android:radius="#dimen/cardCornerRedius"/>
</shape>
</item>
</layer-list>
unselected tab:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="8dp" android:left="8dp">
<shape>
<stroke android:width="1dp" android:color="#color/Gray"/>
<corners android:radius="#dimen/cardCornerRedius"/>
</shape>
</item>
</layer-list>

I have background color in button.How can I set selector for button also?

<Button
android:id="#+id/info_button"
android:layout_width="0dp"
android:layout_weight="1"
android:textColor="#color/white"
android:background="#color/colorPrimary"
android:onClick="getInformation"
android:text="#string/information"
android:layout_height="48dp" />
I have selector below I want to use in button and use background color also.Is there any way?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/btn_preess" android:state_pressed="true"/>
<item android:drawable="#color/btn_preess1" android:state_focused="true"/>
<item android:drawable="#color/btn_preess1"/>
</selector>

How to design a spinner in Android

I want to design a spinner as displayed in the image below:
I am not getting the arrow symbol pointing downside in spinner. How can I do this?
If I make a button design like shown above then I have to write extra code to get similar functionality as for a spinner, as Spinner doesn't have android:drawableRight="#drawable/arraodown", but in the button we have this method.
Do not mess around with right/left/... drawables.
Just set one 9-patch drawable as background that limits the inner content.
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/you_spinner_drawable" />
Regarding the 9-patch drawable have a look at the android resources or at this example picture taken from this blog post (which shows in little more detail on how to make a custom spinner):
For information about 9-patch drawables see the android documentation:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
http://developer.android.com/tools/help/draw9patch.html
Of course you can also specify a State list xml file as drawable, eg.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When disabled -->
<item android:state_enabled="false"
android:drawable="#drawable/your_disabled_spinner_drawable" />
<!-- When selected -->
<item android:state_pressed="true"
android:drawable="#drawable/your_selected_spinner_drawable" />
<!-- When not selected-->
<item
android:drawable="#drawable/your_default_spinner_drawable" />
</selector>
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/you_spinner_drawable" />
**You can use the below mentioned Drawable to set as background for this spinner**.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When disabled -->
<item android:state_enabled="false"
android:drawable="#drawable/your_disabled_spinner_drawable" />
<!-- When selected -->
<item android:state_pressed="true"
android:drawable="#drawable/your_selected_spinner_drawable" />
<!-- When not selected-->
<item
android:drawable="#drawable/your_default_spinner_drawable" />
</selector>
You will have to create a custom layout for the spinner. I think the following XML might give you an idea.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dip">
<TextView
android:id="#+id/number"
android:padding="3dip"
android:layout_marginTop="2dip"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Just use GIMP as your image editor and change background color and border with a color you like.
For example, I did this image by editing an existing one I found on the Internet: http://sdrv.ms/1lRkTbG

Resources