hide radio button from the spinner selected value - android

am using spinner in android with the layout (android.R.layout.simple_spinner_dropdown_item)
because i need the radio button there , but after i select a value the radio button still showing on the spinner. is their any way to hide it?
this is my spinner:
<Spinner
android:id="#+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_gravity="left"
android:background="#drawable/spin" >
</Spinner>
and this is the activity lines:
ArrayAdapter first_adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, start_list);
spinnermo.setAdapter(first_adapter);
and this is the spinner after selection:
ooo i cant dispaly the image but its simmiler to this:
seleced text (0)

Using this Code you can hide your spinner.
spinnermo.setVisibility(View.GONE);
if you want to hide radio button use same code
radioButton.setVisibility(View.GONE);

Yes create two layouts because once you implement ArrayAdapter it will have two methods to be overridded getView and getDropDownView
for getDropDownView you have to create a layout with TextView and set corresponding value in it
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/your_own_unique_id_for_checkedtextview"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
for getView you have to create a layout with CheckedTextView and call the corresponding method
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/your_own_unique_id_for_textview"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
Then in your custom adapter class
override the methods to access the above values respectively
#Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomCheckedTextView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomTextView(position, convertView, parent);
}
// This funtion called for each row ( Called data.size() times )
public View getCustomCheckedTextView(int position, View convertView, ViewGroup parent) {
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.your_own_layoutxml_file_name, parent, false);
/***** Get each Model object from Arraylist ********/
CheckedTextView label = (CheckedTextView)row.findViewById(R.id.your_own_id_for_checkedtextview);
// Set values for spinner each row
label.setText("value");
return row;
}
// This funtion called for each row ( Called data.size() times )
public View getCustomTextView(int position, View convertView, ViewGroup parent) {
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.your_own_layout_xml_file_name_for_textview, parent, false);
TextView label = (TextView)row.findViewById(R.id.your_text_view_id_name);
// Set values for spinner each row
label.setText("value");
return row;
}

Related

Android custom ArrayAdapter rendering Objects in list, not values

I'm trying to bind a custom layout (a LinearLayout containing two TextViews) to a Spinner. I subclassed ArrayAdapter (mostly) correctly. Selecting an item in the Spinner calls getView() correctly, setting the LinearLayout's TextViews' values correctly. The problem is the initial display of the items in the Spinner (when clicking on the Spinner) just shows Objects; not the TextViews they should be displaying. Only AFTER clicking on one of the Objects does the Spinner correctly set the TextViews using my custom adapter's getView() method. Here's the custom adapter class:
public class BusRouteAdapter extends ArrayAdapter<BusRoute> {
...
public BusRouteAdapter(Context context, int resource, int textViewId, ArrayList<BusRoute> routes) {
super(context, resource, textViewId, routes);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
BusRoute route = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.bus_route, parent, false);
}
TextView tvBusRoute = (TextView) convertView.findViewById(R.id.tvBusRoute);
TextView tvBusRouteNumber = (TextView) convertView.findViewById(R.id.tvBusRouteNumber);
tvBusRoute.setText(route.routeName);
tvBusRoute.setTag(route.route);
tvBusRouteNumber.setText(route.route);
if (!route.routeColor.equals("")) {
tvBusRouteNumber.setBackgroundColor(Color.parseColor(route.routeColor));
}
return convertView;
}
}
Here's the layout that is to be used for each Spinner list item (bus_route.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/tvBusRouteNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:padding="8dp" />
<TextView
android:id="#+id/tvBusRoute"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:padding="8dp" />
</LinearLayout>
And in my Activity, I'm setting the adapter to a properly-populated list of BusRoute objects:
busRouteAdapter = new BusRouteAdapter(getBaseContext(), R.layout.bus_route, R.id.tvBusRoute, arrayOfBusRoutes);
routesSpinner.setAdapter(busRouteAdapter);
It does seem strange that I need to pass the Layout id (R.layout.bus_route) AND one of the TextViews contained in that Layout (R.id.tvBusRoute).
Here's what is rendered when clicking on the Spinner:
But if I click one of the Objects, getView() is called, and the selected Layout and TextViews are rendered properly (apparently I selected "GMU - Pentagon"):
What am I missing to get the Spinner's popup list to display ALL my bus route items rendered correctly?
I think you need to override getDropDownView to deal with spinners
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
BusRoute route = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.bus_route, parent, false);
}
TextView tvBusRoute = (TextView) convertView.findViewById(R.id.tvBusRoute);
TextView tvBusRouteNumber = (TextView) convertView.findViewById(R.id.tvBusRouteNumber);
tvBusRoute.setText(route.routeName);
tvBusRoute.setTag(route.route);
tvBusRouteNumber.setText(route.route);
if (!route.routeColor.equals("")) {
tvBusRouteNumber.setBackgroundColor(Color.parseColor(route.routeColor));
}
return convertView;
}

Aligning Selected Spinner Item

I'm trying to make a slight adjustment to the positioning of the selected item in the spinner. Not the dropdown list items, as I already have a custom view in my adapter for that, but the selected item specifically.
As you can see in the screenshot, "Any" is the currently selected item. But it is aligned oddly within the container because it has to accommodate the longest string in the dropdown, which is "Dark Purple Burnt Sienna" (or whatever). I want to align the selected text to the right so that "Any" is next to the dropdown indicator instead of way out in the middle.
I've attempted to make adjustments to my custom spinner-item view, but it doesn't have any affect on the selected item.
I've also attempted to set gravity and text alignment on the Spinner itself, but it has no effect.
Here's the image and the xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/default_black"
android:layout_centerVertical="true"
android:text="Color" />
<Spinner
android:id="#+id/spn_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Edit: Here's my adapter:
public class ColorsAdapter<T> implements SpinnerAdapter {
ArrayList<String> mColors;
ArrayList<Integer> mValues;
Context mContext;
public ColorsAdapter(ArrayList<String> colors, ArrayList<Integer> values,
Context context) {
mContext = context;
mColors = colors;
mValues = values;
}
#Override
public int getCount() {
return mColors.size();
}
#Override
public Object getItem(int position) {
return mColors.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return R.layout.list_item_color;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(mContext);
v.setText(mColors.get(position));
return v;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE );
View row = inflater.inflate(getItemViewType(position), parent, false);
TextView tvName = (TextView)row.findViewById(R.id.tv_name);
tvName.setText(mColors.get(position));
row.setTag(mValues.get(position));
return row;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isEmpty() {
return false;
}
}
And here's the XML for the list item:
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="right"/>
You can adjust the settings for selected item in getView(int position, View convertView, ViewGroup parent) method. If you just want to set the gravity and text alignment, you should set it to the TextView in the method like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(mContext);
v.setText(mColors.get(position));
v.setGravity(Gravity.END); // OR v.setGravity(Gravity.RIGHT);
return v;
}
Or you can simply inflate custom layout if you want to make further customization:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View row = null;
if (inflater != null) {
row = inflater.inflate(R.layout.list_item_view_color, parent, false);
TextView textView = row.findViewById(R.id.textview);
textView .setText(mColors.get(position));
}
return row;
}
Where list_item_view_color.xml is your layout file for the selected value.
(NOTE: If you want to use options like autoSizeTextType, you can use them with app prefix for AppCompatTextView in xml files)
Simple solution would be to build another Custom view for the spinner TextView layout, and specify the gravity for your TextView in that. Something like :
<!--spinner_layout.xml (in layout/)-->
<?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:gravity="right"
/>
Use that in initalising the ArrayAdapter of your spinner :
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.spinner_layout,
spinnerArray);
UI output :
You can creating the TextView for the getView method programmatically so no property for alignment is being set, easy thing to do is inflate it in the same way you are doing it on the getDropdownView, or simply set the proper gravity in the view you are instantiated.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(mContext);
v.setText(mColors.get(position));
v.setGravity(Gravity.RIGHT);
return v;
}
You can try this
android:autoSizeTextType="uniform"
Padding, Margins and Alignments get confusing when it comes to Spinners. I ran into a similar issue as well. But for me I wasn't using a custom view and adapter, so I managed to use styles to get around this issue.
For you since you are already using custom view and adapter, you can make the background null for the spinner so that the dropdown arrow will not be visible.
android:background="#null"
Then in the TextView, you can assign drawableRight to be the dropdown arrow.
android:drawableRight="#drawable/ic_spinner_drop_arrow_white_24dp"
The icon can be a dropdown vector drawable. Also to specify particular margin between the arrow and text, you should be able to use drawablePadding
Hope this helps.
I can give you a method that may work.
In XML
1) add a new text view called the_current_color text view
2) allign this text view above the spinner and to the start of the arrow icon
or wherever you want it to be.
In your code
1) when you listen to (on item selected ) from the spinner simply get the current text and set it in the (the_current_color) text view.
2) and try to hide the texts in the spinner by changing color to transparent.
Hope it works
Edit
I might also suggest something maybe it changes things
lets say that we have a spinner that uses this string resource file to populate its adapter
<string-array name="my_adapter">
<item>Blue </item>
<item>Red</item>
</string-array>
until now we have a spinner that shows (red item) and (blue item) at the far left from the arrow icon of the spinner.
create another string resources now like that
<string-array name="my_adapter_two">
<item> Blue </item>
<item> Red</item>
</string-array>
as you can see now the added space will give an illusion that the strings are beside the arrow icon
you can add space that fit your need and you can maybe switch the files after you click a certain item and then switch them back.
You can try the following Custom view.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="right"
android:padding="15dp"
android:textAlignment="gravity"
android:textColor="#color/black"
android:textSize="10dp" />

ListView doesn't accept onClickListener when list item contains ViewPager

I have a list view and inside it's items I have a view pager as you can see in the below code.
While the viewPager is inside the item view List doesn't accept onItemClickListener and when I click Items nothing happens.
One solution that I found was adding onClickListener to ViewPager pages , But I think this is not a good solution because If you list contains 100 Items and each item have a view pager that contains 5 page so I should define 500 Listener Instead of one Listener.
I tried all solutions that I searched for example changing the focusable or clickable attribute of root view.
Please share your suggestions with me.
This is the list item layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/propertylist_item_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/purple"
android:paddingBottom="1dp"
android:descendantFocusability="blocksDescendants" >
<!-- background slider -->
<android.support.v4.view.ViewPager
android:id="#+id/property_images_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- favorite icon -->
<ImageButton
android:id="#+id/favorite_button"
android:contentDescription="#string/favorite_button_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:layout_margin="10dp"
android:src="#drawable/ic_favorite" />
...
<!-- Some other tags -->
This is the list adapter code :
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater inflater;
inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.property_listing_item, parent, false);
}
RPropertyStorage property = getItem(position);
if (property != null)
{
PropertyListingImagesAdapter adapter = new PropertyListingImagesAdapter(property.getImages());
ViewPager imagesPager = (ViewPager) view.findViewById(R.id.property_images_view_pager);
imagesPager.requestDisallowInterceptTouchEvent(false);
imagesPager.setAdapter(adapter);
}
return view;
}
OnItemClickListener of ListView :
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
RToast.getInstance().showMessage("Clicked");
RLog.info("Clicked");
}
});
ViewPager has very "strong" code that handle touch events. For me it is only one way to catch viewpager clicks through it's own clicklisteners.
Don't care about
If you list contains 100 Items and each item have a view pager that contains 5 page so I should define 500 Listener Instead of one Listener.
because listview creates only as many view as can display on the screen at the same time + 2; You can check it by such code in your adapter
int count=0;
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater inflater;
inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.property_listing_item, parent, false);
Log.v("","creating "+count);
count++;
}
//your code
}
Listener is only links. It is no matter how many links you have on some method. Just check if it is not null.

how do you set the spinner text color?

I have been unable to set the color on the spinner widget. How is this styled?
Try using this adapter for your spinner:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(Home.Home_Group, R.layout.my_spinner_style, yourstringarray)
{
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(16);
((TextView) v).setTextColor(
getResources().getColorStateList(R.color.white)
);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
v.setBackgroundResource(R.drawable.spinner_bg);
((TextView) v).setTextColor(
getResources().getColorStateList(R.color.spinner_text)
);
((TextView) v).setTypeface(fontStyle);
((TextView) v).setGravity(Gravity.CENTER);
return v;
}
};
Add this xml to your layout,
my_spinner_style.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:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
And at last,
spinner.setAdapter(adapter);
Simple and crisp ....
private OnItemSelectedListener your_spinner _name= new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
The simplest form is:
m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);
If you want to change the text color :
Spinner Widgets Text Color
Else, making your own layout is the best way like JoxTraex said.
A shorter alternative to Andro'd answer is to let the ArrayAdapter create the item views for you from a layout resource:
final List<String> values = [SPINNER VALUES];
final ArrayAdapter<String> adapter = new ArrayAdapter<>(
activity, R.layout.my_spinner_item, values);
adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
spinner.setAdapter(adapter);
Then style your text to suit your needs in my_spinner_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="#style/my_spinner_item_style"
/>
Note: my_spinner_dropdown_item is used when the list of choices appear
For more information read the Spinners documentation.
Try understanding that you are using the dropdown list as provided by the defaults that are available in the SDK.
SIMPLY make your own layout with a custom adapter.

how to set custom font to one of the TextView's in ListView item?

I have code like:
SpecialAdapter adapter = new SpecialAdapter(
this,
list,
R.layout.list_display_item,
from,
to );
myTextListView.setAdapter(adapter);
.
.
.
.
public class SpecialAdapter extends SimpleAdapter
{
public SpecialAdapter(Context context, List<HashMap<String,
String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
#Override
public View getView(
int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
}
}
and list_display_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myDisplayItemLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/arabicTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:lineSpacingMultiplier="3"
android:text="Arabic"
android:textSize="30dp"/>
<TextView
android:id="#+id/translationTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="English"
android:gravity="left|center_vertical"
android:textSize="30dp"/>
</LinearLayout>
I just want to set my own arabic font to android:id="#+id/arabicTextView" and english font to android:id="#+id/translationTextView".
In getView() function of SpecialAdapter class i get View view = super.getView(position, convertView, parent) and view.getId() is actually the id of linearlayout android:id="#+id/myDisplayItemLinearLayout"
I just want to get inner textviews of this linearLayout to set seperate fonts to each using typeface here... how can i get these textViews there??
Is it correct way to set my fonts here or it should be done in someother way???? please help me..
Use the findViewById method of View and the setTypeface method of TextView
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
//assuming you've put your arabic font file in your assets directory
((TextView)view.findViewById(R.id.arabicTextView)).setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), "yourarabicfont.otf"));
//assuming you've put some custom english font in your assets directory
((TextView)view.findViewById(R.id.translationTextView)).setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), "yourenglishfont.otf"));
return view;
}

Categories

Resources