selected item doesnt show on the spinner android - java

I have a spinner and I have added a custom style to spinner. Problem is when I select an item it doesnt show up on the spinner but when I use android spinner style it shows what I selected on the spinner. Is there any more coding to add to make it work? Otherwise everthing of the spinner works. I have written the app when an item is selected in spinner to show a text. These things work. But it doesnt show what I selected.
Here is my code
MyAdapter dataAdapter3 = new MyAdapter(this, R.layout.spinner, list3);
spinner1.setAdapter(dataAdapter3);
list 3 referes a list
List<String> list3 = new ArrayList<String>();
Here is the class for custom spinner style
public class MyAdapter extends ArrayAdapter<String>
{
private List<String> listString = new ArrayList<String>();
public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
this.listString = objects;
}
#Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.textView1);
label.setText(listString.get(position));
return row;
}
}
please can anybody tell whether I have done any mistake here?
This is how spinner is shows when I selected item

I tried several days on this problem. Actually code is pretty ok. problem was in the spinner.xml file. I had added a large padding to textview. Therefore, though spinner works it is not visible the selected item on spinner. Point is I forget xml file. :D

Modify the padding in the layout spinner.xml file to a lower value,
for ex:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="#000000"
android:textSize="14sp" />

Related

Change background color of ListView specific row without CustomAdapter

I'm trying to change the background color of a specific row in a ListView to match a specific color in my List<String> hexcodeList, which contains colors hex codes. However, I'm not using a CustomAdapter for this, instead I'm overriding getView from ArrayAdapter. I want the 1st row to have the 1st color of my list, the 2nd to have the 2nd color and so on. I'm very new to Java, my first thought was to use a for statement, so the code below is what I tried with no success, since this changes the background color of all rows using the last color of hexcodeList.
ListView CoresListView = (ListView) findViewById(R.id.ListViewId);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hexcodeList){
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position,convertView,parent);
for (position = 0; position < hexcodeList.size(); position++) {
view.setBackgroundColor(Color.parseColor(hexcodeList.get(position)));
}
return view;
}
};
CoresListView.setAdapter(adapter);
On each view created you are looping through the list and setting all colors so the last color is being set for each view.
Remove the loop and just write this:
view.setBackgroundColor(Color.parseColor(hexcodeList.get(position)));
getView(...) is called for each view so need of a loop.
You don't need for loop because the getView() always called for each row item view.
Try this:
ListView CoresListView = (ListView) findViewById(R.id.ListViewId);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hexcodeList){
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position,convertView,parent);
view.setBackgroundColor(Color.parseColor(hexcodeList.get(position)));
return view;
}
};
CoresListView.setAdapter(adapter);

Android, overwrite items view in ListView to display slightly different to actual value

So in my listview I have a bunch of strings, but I'd like to display the values with £ in front of the strings, but NOT actually edit the values themselves in the ArrayList.
Anyone know how I can do this?
I'm already overwriting the view anyway, I'm just not sure how to alter the display of the items values themselves.
Some Code:
adapter = (new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 != 0)
view.setBackgroundColor(Color.rgb(135,206,250)); // Pale blue
return view;
}
});
foodListView = (ListView) findViewById(R.id.foodListView);
foodListView.setAdapter(adapter);
In you getView method you can use:
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText("£" + listItems.get(position));

Android spinner to change setContentView

So, I have a spinner with two different options thus far. What I am trying to accomplish is, if "First Spinner Option" is chosen, then I setContentView to a specific layout and execute code corresponding to that layout. The same goes if "Second Spinner Option" is chosen. I know that I need to use setOnItemSelectedListener to a certain extent, but I am not sure how this would work exactly. Below is a quick mock up of what I'm trying to do in coding terms
spinner.setonItemSelectedListener(this);
if(spinner = first spinner option){
setContentView(R.layout.lay1);
//other code here
}elseif(spinner = second spinner option){
setContentView(R.layout.lay2);
//other code here
}
I know the syntax is bad here, Im just trying to get a general idea of how this could be done.
EDIT: #CodeMagic
This is how my code is setup thus far. 'items' is just an array of strings with 2 elements.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.my_spinner_style, items) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Exo-Bold.otf");
((TextView) v).setTypeface(tf);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Exo-Bold.otf");
((TextView) v).setTypeface(tf);
//v.setBackgroundColor(Color.GREEN);
return v;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gasChoice.setAdapter(adapter);
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
gasChoice = (Spinner) findViewById(R.id.gasChoice);
gasChoice.setOnItemSelectedListener(new OnItemSelected());
}
I don't know exactly what the problem is you are having but its pretty close. You just need to add the method
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView tv = (TextView)arg1; // get the TextView selected
String text = tv.getText().toString();
if(text.equals("FirstText")){ // compare the text in the box
setContentView(R.layout.lay1);
//other code here
}elseif(text.equals("FirstText")){
setContentView(R.layout.lay2);
//other code here
}
}
There are different ways of doing this such as getting the position (arg2) and comparing that to what's in your adapter but since I don't know how you are doing any of that, this is the easiest way to get you started.

Resizing text on a spinner

I use the following function to resize the text on a spinner when an item is selected:
mySpinner.post(new Runnable()
{
public void run()
{
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
TextView tempView = ((TextView) parent.getChildAt(0));
((TextView) parent.getChildAt(0)).setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(tempView.getText().toString(), tempView.getWidth() - tempView.getPaddingLeft() - tempView.getPaddingRight(), (int) fieldHeight - tempView.getPaddingTop() - tempView.getPaddingBottom()));
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing
}
});
}
});
The problem is, before it resizes the text to the correct size, the text is drawn in the incorrect size. What I need to do is call my function SetTextSize as soon as the item is selected. I cannot find anyway to do this. setOnItemSelectedListener won't do because text is displayed in the incorrect size before the event runs.
I have changed:
mySpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, CHOICES));
To:
mySpinner.setAdapter(new MyArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, CHOICES));
and added the following class:
public class MyArrayAdapter extends ArrayAdapter
{
public MyArrayAdapter(Context context, int textViewResourceId, Object[] objects)
{
super(context, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
((TextView) convertView).setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(((TextView) convertView).getText().toString(), convertView.getWidth() - convertView.getPaddingLeft() - convertView.getPaddingRight(), (int) fieldHeight - convertView.getPaddingTop() - convertView.getPaddingBottom()));
return convertView;
}
}
Unfortunately, now my program crashes immediately when it starts.
I have created the following class:
class MySpinnerAdapter extends ArrayAdapter<String>
{
MySpinnerAdapter()
{
super(MainActivity.this, R.layout.row, R.id.label, CHOICES);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
TextView label = (TextView)row.findViewById(R.id.label);
if (label != null)
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), label.getWidth() - label.getPaddingLeft() - label.getPaddingRight(), (int) fieldHeight - label.getPaddingTop() - label.getPaddingBottom()));
return(row);
}
}
I now have 2 problems. Firstly, this line causes my program to crash:
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), label.getWidth() - label.getPaddingLeft() - label.getPaddingRight(), (int) fieldHeight - label.getPaddingTop() - label.getPaddingBottom()));
Secondly, the styles that are in "row.xml" are applied to my spinner. If I don't specify any styles, then my program crashes. I simply do not want the xml file to alter the way my spinner looks because I do not know at compile time. I would like the spinner to use the default values that it was using before I created the XML file, and anything else I can then change in code.
Change your this code
mySpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, CHOICES));
to
mySpinner.setAdapter(new ArrayAdapter<String>(this, R.layout.spinner_layout, CHOICES));
where the code for spinner_layout.xml is
<?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="fill_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:textColor="#color/green"
android:paddingLeft="4dp"
android:textSize="18sp"
android:ellipsize="marquee"
android:singleLine="true" />
change layout_height and text size according to your need
I finally have the text on my spinner resizing perfectly and I think the code is efficient. Thanks everyone who tried to help. :)
class MySpinnerAdapter extends ArrayAdapter<String>
{
MySpinnerAdapter(int dropDownResource, String[] choices)
{
super(MainActivity.this, dropDownResource, choices);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
row = super.getView(position, convertView, parent);
if (parent.getWidth() > 0)
{
TextView label = (TextView) row.findViewById(android.R.id.text1);
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), parent.getWidth() - label.getPaddingLeft() - label.getPaddingRight(), (int) fieldHeight - label.getPaddingTop() - label.getPaddingBottom()));
label.setPadding(label.getPaddingLeft(), 0, 0, 0);
}
return(row);
}
}

Passing a list to a custom view?

I am trying to make a custom style for android spinner, however there are several problems.
I have noticed in many examples, they pass array to getCustomView method. I am wondering whether I can pass a list instead of an array.
Second problem is why they have declared variable and initialized in class variable scope?
They have declared arrays like this.
String []data1={"Brainbitz:java","Brainbitz:Android","Brainbitz:Embedded Systems","Brainbitz:PHP"};
in class variable scope. But when I try to do for the list I get an error. why?
Third is,
If we can pass list to getCustomView how do I do that? Here is the link to the tutorial tutorial
I am considering this source code.
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.textView1);
label.setText(list3.get(position));
// TextView sub=(TextView)row.findViewById(R.id.textView2);
// sub.setText(data2[position]);
//
// ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
// icon.setImageResource(images[position]);
return row;
}
In above code I don't know the syntax to pass position to list3 list type reference.
Please be kind enough to explain this.
First of All,
You are using default ArrayAdapter Class..
new ArrayAdapter<String>();
Which Uses String class for data bind. If you want to make an ArrayAdapter with a ArrayList or List you have to make a Custom Adapter by extending ArrayAdapter<Custom_Class> or BaseAdapter.
The ArrayAdapter class can handle any Java object as input. It maps the data of this input to a TextView in the layout.
ArrayAdapter uses the toString() method of the data input object to determine the String which should be displayed.
Look at How to use ArrayAdapter<myClass> and this Tutorial
Update:
public class MyAdapter extends ArrayAdapter<String>
{
private List<String> listString = new ArrayList<String>();
public MyAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
listString = objects;
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.textView1);
label.setText(listString.get(position)); // How to use listString
.
.
.
Here is the corrected solution,
public class MyAdapter extends ArrayAdapter<String>
{
public MyAdapter(Context context, int textViewResourceId,List<String> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
#Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.textView1);
label.setText(list3.get(position));
// TextView sub=(TextView)row.findViewById(R.id.textView2);
// sub.setText(data2[position]);
//
// ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
// icon.setImageResource(images[position]);
return row;
}
}
Basiclly, I had done 1 mistake, one is I hadnt initalised constructor correctly.

Categories