XML Spinner attributes textAlignment = "right" API 16 - android

I'm trying to set attributes to a spinner on my android app and one of the attributes in textAlignment = "right" (the problem being I've a spinner that layout_width="match_parent" so there's a lot a space and I'd like to have it to the right) but this is only supported in API 17 and up whereas I want to make an app for API 16 - Is there a work-around?
My attributes are:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAlignment="gravity"
android:id="#+id/MainSpinner"
tools:listitem="#layout/support_simple_spinner_dropdown_item"/>
The current linearLayout looks like this:
And I want it to look like this:
Where:
LinearLayout (horizontal) =
RelativeLayout =
TextView =
LinearLayout (Vertical) =
Spinner =
Button =

This ans work for me...
<Spinner
android:id="#+id/example_spinner"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:textAlignment="right"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp" />
I've followed this : http://nevescheng.blogspot.fr/2013/05/spinner-with-item-text-aligned-to-center.html & this worked fine...

You can create an adapter like this :
SpinnerAdapter spinnerAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"tata", "toto", "titi"});
spriner.setAdapter(spinnerAdapter );
and in your layout (res/layout/spinner_item.xml):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical|end"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />

Heyy Jeremy, Try to add android:textDirection="rtl" attribute in your spinner. This attribute will give the text a direction from right to left.
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAlignment="gravity"
android:textDirection="rtl"
android:id="#+id/MainSpinner"
tools:listitem="#layout/support_simple_spinner_dropdown_item"/>

Related

Add Padding top in a Linear Layout

This is my linear layout:
It is too close to the top. I am trying to add some space to the top. I have tried using android:paddingTop but that does not work. How do I add space at top in linear layout?
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingTop="200dp"
android:paddingRight="20dp"
android:paddingBottom="20dp">
<TextView
android:id="#+id/pNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/pDescriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/pTypeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/pPriceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/pQuantityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
Given what you show in your image, it looks likely that you've already specified android:padding to give padding to all four edges. If that is the case, adding android:paddingTop won't do anything; the system will ignore that attribute in favor of the generic android:padding.
If, for example, you currently have android:padding="16dp", you'll have to delete that and replace it with something like this:
android:paddingTop="32dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
If you are using ListView and want to add only some spacing above the very first item, then the situation is different. Instead of adding paddingTop to your item view, you have some choices.
Padding on the ListView
Add these two attributes to your ListView or RecyclerView tag:
android:paddingTop="32dp"
android:clipToPadding="false"
This is by far the easiest solution, but it will affect how the scrollbar is drawn, which you may not like.
Add a header view to the ListView
Create a layout file for a header view. I suggest using a <Space> tag if you just want some extra space at top:
<Space
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="32dp"/>
Then inflate this and add it to your ListView:
val header = inflater.inflate(R.layout.list_header, listView, false)
listView.addHeaderView(header)
By default, a divider will still appear between this header and your first list item. If you don't want that, you can disable it:
listView.setHeaderDividersEnabled(false)
Dynamic padding in getView()
In your adapter's getView() method, you can programmatically set the padding based on the item's position. First, define two dimen resources like this:
<dimen name="extra_padding">32dp</dimen>
<dimen name="normal_padding">16dp</dimen>
Then, in getView(), add code like this:
val paddingTop = when (position) {
0 -> resources.getDimensionPixelSize(R.dimen.extra_padding)
else -> resources.getDimensionPixelSize(R.dimen.normal_padding)
}
view.setPadding(view.paddingLeft, paddingTop, view.paddingRight, view.paddingBottom)

TextView Alignment Issue on Android

I am having two TextView in LinearLayout.
<LinearLayout
android:layout_marginTop="-10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/product_discounted_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/margin_between_prices"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="AED200000000.00"
android:textColor="#color/black"
android:textSize="#dimen/font_size_sub_normal"
android:visibility="visible" />
<TextView
android:id="#+id/product_unit_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/activity_vertical_margin"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="AED200000000000.00"
android:textSize="#dimen/font_size_emphasized"
android:visibility="visible" />
</LinearLayout>
I want something like if 2nd TextView doesnt come in single line than it should shift to second line.
Issue is :
With SingeLine=true
With maxLine=1
What I want :
change
android:orientation="horizontal"
to
android:orientation="vertical"
and also add maxline and singleline to textView
It is easy put your text views in a RelativeLayout and all you need to set layout_above or layout_bottom for your TextViews
or you can change orientation:vertical and then the gravity works.
You can also set layout_gravity

Change AppCompatSpinner popup background color

I tried to make Spinner with white text as my theme but when Spinner popup show then it has default white background so my text isn't visible there. I also tried to set with both android:popupBackground and app:popupBackground but it doesn't reflect there.
Screenshots of Spinner :
Spinner 1
Spinner Popup 2
XML for spinner
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinLocation"
style="#style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground=""
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
app:backgroundTint="#color/white" />
XML for Spinner Item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txtValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:maxLines="1"
android:paddingBottom="7dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="7dp"
android:text=""
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
I know it is because of item textColor but if I do not define it then it showing in popup but not proper in Spinner. So suggest me a best approach for this and also suggest for header if appCompatSpinner supports it. Thanks.
You need to set themes, or create a custom one.
Here is a sample code:
<android.support.v7.widget.AppCompatSpinner
style="#style/Base.Widget.AppCompat.Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/array_radius"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Light" />
Possibly you can add your own Textview for spinner
<Spinner
android:id="#+id/carMake"
android:textColor="#606060"
android:textSize="12sp"
android:layout_gravity="end"
android:layout_alignParentEnd="true"
android:layout_below="#+id/vin_border"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="14dp">
</Spinner>
java for adding items
List<String> model = new ArrayList<>();
model.add("Ford");
model.add("BMW");
model.add("Audi");
model.add("Hyundai");
model.add("Suzuki");
model.add("Porsche");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(getContext(), R.layout.spinner_textview_layout, model);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
carMake.setAdapter(dataAdapter);
textview for spinner item
<com.tagx.view.CustomTextView
app:font="#string/monte_light"
android:layout_width="wrap_content"
android:maxLines="1"
style="?android:attr/spinnerItemStyle"
android:ellipsize="marquee"
android:paddingStart="=20dp"
android:paddingEnd="40dp"
android:textColor="#color/fragment_owner_title_color"
android:layout_gravity="end"
android:textSize="12sp"
android:layout_height="wrap_content"
android:gravity="center"
/>

Separate padding values for checkbox and text inside CheckedTextView

Im trying to build my multiple choice layout according to latest material design guidelines with CheckedTextView widget. But I can't find the way to set left padding for checkbox and textview separately (16dp for icon, 72 for text).
The most acceptable solution i could find is to use android:drawablePadding attribute but i think it is not quite right.
Is it possible to set padding values separately inside CheckedTextView?
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:textSize="16sp"
android:textColor="#000000"
android:paddingLeft="16dp"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
android:drawablePadding="36dp"/>
Try this, it work for me, add a "padding" property:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:background="#color/grey"
android:textColor="#color/white"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:ellipsize="marquee"/>
Try this one: (This is under RelativeLayout)
<CheckedTextView
android:id="#+id/checkedTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="#string/text"
android:checkMark="?android:attr/textCheckMark"
android:checked="false" />
It will stay like this:
Layout:
| |
| Text for checkedTextView: V |
| |
I'm late to the party, but just in case someone is looking for a solution, here is how I did it using the CheckBox widget:
<CheckBox
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:minHeight="30dp"
android:textColor="#ffffff"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:paddingStart="56dp"/>
android:layout_marginStart="16dp" sets margin to both icon and text, while android:paddingStart="56dp" sets padding between text and icon.

Android - CheckedTextView checkbox does not conform with padding attribute?

I have following xml code:
It seems like the textview reflects to the 10 dp padding however the checkbox on the right side does not. The textview and checkbox are not aligned with each other. Anyone know why the checkbox is not conforming to the 10 dp padding? And is there a way to align my textview and checkbox?
<CheckedTextView
android:id="#+id/nearMeCheckedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:clickable="true"
android:paddingTop="10dp" />
See this example... work for me
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/checkedTextViewInterest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="12dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
try like this
<CheckedTextView
android:id="#+id/nearMeCheckedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:clickable="true"
android:padding="10dp"
android:gravity="center_vertical" />

Categories

Resources