android spinner dropdown custom - android

I use spinner to show something, this is my code :
ArrayAdapter<ClassName> adapter = new ArrayAdapter<ClassName>(getActivity(), android.R.layout.simple_spinner_dropdown_item, nameList);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
with this code, when click on spinner, I can see text and checkbox.
after that, I try to custom spinner like this :
ArrayAdapter<ClassName> adapter = new ArrayAdapter<ClassName>(getActivity(),
R.layout.list_spinner, tenfavList);
adapter.setDropDownViewResource(R.layout.list_spinner_dropdown);
this is list_spinner_dropdown.xml :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#0004ff"
android:textSize="#dimen/20sp"/>
when but with this code, when click on spinner, I just see text, and no checkbox.
how to fix it ?

OK, now i got your issue. Issue is only that you please add style in your CheckedTextView for checked or unchecked like :
Add File default_checkbox.xml inside res/drawable
<?xml version="1.0" encoding="utf-8"?>
<item android:state_checked="true"
android:drawable="#drawable/checkbox_checked" /> <!-- checked -->
<item android:state_pressed="true"
android:drawable="#drawable/checkbox_checked" /> <!-- pressed -->
<item android:drawable="#drawable/checkbox_default" /> <!-- default -->
In your Xml :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:checked="true"
android:checkMark="#drawable/default_checkbox"
android:text="sdfsdfsdfsdfdsfdsf"
android:textColor="#0004ff"
android:textSize="25dp"/>

Related

Change color selector/text new Text MaterialButton

I need to customize the text color or background color of the selector when button is selected, because the color of the text its too similar and the text disappears
This is my code:
<android.support.design.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:text="#string/lbl_forgotten_password"
android:textColor="#color/gray_text_1"
android:textSize="14sp" />
This is how it looks:
You can solve this problem by using selector.
Create a new xml file into the drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/text_color" android:drawable="#drawable/button_bg"/>
<item android:color="#color/text_normal" />
</selector>
After
<android.support.design.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/your_xml_file_name"
android:text="#string/lbl_forgotten_password"
android:textSize="14sp" />

Two Texts on a Single Button in Android?

I have a button in Android Studio:
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Event Name (23)" />
It looks like this:
-------------------------------------
| Event Name (23) |
-------------------------------------
What I would like is two pieces of text in a single button.
Basically, I want on the left side of the button, the name of an event... then I want on the right side of the button to have the number of people registered for the event, so it looks like this:
-------------------------------------
| Event Name (23) |
-------------------------------------
Is something like this possible?
I am completely new to Android, so keep that in mind.
You can create Layout instead of Button and Then perform click event on layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:id="#+id/relLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Name"
android:layout_alignParentLeft="true"
android:textSize="16sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="(23)"
android:textSize="16sp"/>
</RelativeLayout>
In Activity :
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.relLayout);
relLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Implement Your login on RelativeLayout click
}
});
pls try this
<RelativeLayout
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Event Name " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="(23)" />
You can use a layout to show two text in it.And you can add the listener in the layout.
use spannable strings
String text;
SpannableString finalText = new SpannableString(text);
finalText.setSpan((new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), <start index of right String>, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setText(finalText);
You can add a file named item_color_grey.xml in you drawable directory.The code is like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/item_grey" />
<item android:state_focused="true" android:drawable="#color/item_grey" />
<item android:state_pressed="true" android:drawable="#color/item_grey" />
<item android:drawable="#color/white" />
</selector>
You can use the following way to do whatever you need.
in your layout define button like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#drawable/lay_back"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Name"
android:layout_alignParentLeft="true"
android:textSize="26sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="(23)"
android:textSize="26sp"/>
And in Your drawable folder create a xml file lay_back.xml and write below code into it.
For ripple effect:(For Higher API levels)
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white">
<item android:drawable="#color/item_grey"/>
</ripple>
For Selector Effect(Lower APIs) :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/white" />
<item android:state_focused="true" android:drawable="#color/item_grey" />
<item android:state_pressed="true" android:drawable="#color/item_dark_grey" />
<item android:drawable="#color/item_grey" />
</selector>
You can Give Any color stored in colors.xml of values directory.
Even if this post is pretty old. I've found a solution to it is by having Two buttons inside a relative layout.
Button1 having text left align (clickable=false) and stylesheet of a button.
Button2 having text right align (clickable=true) and stylesheet of a button; and having background transperent.
catch on the Activity Side, one will have to handle enable and disable state for both these button. While handling the click for the main button only.
Let me know if someone is look out for similar solution; in which case I can post the sample code.

Change the divider color of Spinner

Since my popup background is white in color I need to change the color of the spinner divider. I have tried styling the spinner in the following way but it does not work:
styles.xml
<style name="applicationTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:dropDownListViewStyle">#style/SpinnerStyle</item>
</style>
<style name="SpinnerStyle" parent="android:Widget.ListView.DropDown">
<item name="android:divider">#0193DE</item>
<item name="android:dividerHeight">1dp</item>
</style>
main xml
<Spinner
android:id="#+id/year"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:background="#drawable/apptheme_spinner_default_holo_dark"
android:layout_marginLeft="75dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:spinnerMode="dropdown"
style="#style/SpinnerStyle"
android:popupBackground="#FFFFFF" />
java
ArrayAdapter<Integer> adapter_year = new ArrayAdapter<Integer>(this, R.drawable.custom_spinner_holidays, year);
adapter_year.setDropDownViewResource(R.layout.custom_spinner_popup);
custom_spinner_holidays.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/white" />
custom_spinner_popup
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#0193DE" />
Can I just combine all these into one?
You need to put this theme in your manifest file like this:
<activity android:name="com.example.activity.Solution"
android:theme="#style/applicationTheme">
</activity>
Maybe the best idea it's doing your own spinner and you can style it in the way that you want. Take a look in the next thread it was so useful for me.
How to customize a Spinner in Android

android- How to change style of my Edit text and Spinner

these are how my spinner and edit text looks like :
these are spinner and edit text that I saw somewhere else and I liked them :
How can I change my edittext and spinners to looks the second image ?
I'm using app compact anyway
thanks
Set Background for Spinner and EditText will help you to figure it out.
<Spinner android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/spinner_bg"/>
<EditText android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittext_bg"/>
</LinearLayout>
By now I can guide you for EditText section ..
Define your style drawable like this ..
res/drawable/edit_text_style.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape >
<solid android:color="#color/wine_red" />
</shape>
</item>
<!-- main color -->
<item android:bottom="1.5dp"
android:left="1.5dp"
android:right="1.5dp">
<shape >
<solid android:color="#color/white" />
</shape>
</item>
<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="5.0dp">
<shape >
<solid android:color="#color/white" />
</shape>
</item>
</layer-list>
Now define same in your EditText like this ..
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.vivektest.MainActivity" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText
android:background="#drawable/edit_text_style"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_main"
android:imeOptions="actionSearch"
android:singleLine="true"
/>
</RelativeLayout>
Hope it helps!
This is the best tool that you can use for all views and its FREE many thanks to #Jérôme Van Der Linden.
The Android Holo Colors Generator allows you to easily create Android components such as EdiText or spinner with your own colors for your Android application. It will generate all necessary nine patch assets plus associated XML drawable and styles which you can copy straight into your project.
http://android-holo-colors.com/
UPDATE 1
This domain seems expired but project is open source you can find here
https://github.com/jeromevdl/android-holo-colors
try it
this image put in background of EditText
android:background="#drawable/textfield_activated"
UPDATE 2
For API 21 or higher, you can use android:backgroundTint
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
android:backgroundTint="#android:color/holo_red_light" />
Update 3
Now We have with back support AppCompatEditText
Note: We need to use app:backgroundTint instead of android:backgroundTint
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
app:backgroundTint="#color/blue_gray_light" />
Solution for spinner style:
Create a custom adapter with a custom layout for your spinner.
Spinner spinner = (Spinner) findViewById(R.id.pioedittxt5);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.travelreasons, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
R.layout.simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="#style/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
R.layout.simple_spinner_dropdown_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="#style/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee" />
In styles add your custom dimensions and height as per your requirement.
<style name="spinnerItemStyle" parent="android:Widget.TextView.SpinnerItem">
</style>
<style name="spinnerDropDownItemStyle" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
</style>
Taken from here

Change spinner background

I have created a spinner which looks like
My layout file is:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/round_corners"
android:padding="7dip" >
<Spinner
android:id="#+id/select_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:prompt="#string/selectCity" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:text="#string/City"
android:textColor="#color/dark_grey"
android:textSize="18dp" />
</RelativeLayout>
What I want is to make the background of the spinner transparent but still show the downward arrow. Is there any way that I can do this?
Create xml file and place it in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/slim_spinner_pressed" />
<item android:drawable="#drawable/slim_spinner_normal" />
</selector>
.
<Spinner ..... android:background="#drawable/selector_slim_spinner" ..... />
For more Information Check this Change Spinner Style In Android
You should create a slector and give that selector as background to your spinner
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/..." />
<item android:drawable="#drawable/..." />
</selector>
you can use this resource Android Holo Colors Generator to generate android components like Android 4.0

Categories

Resources