I am attempting to render two spinners on my Android activity. They both will have to take their item values from a string.xml resource file.
Here is how my code looks like currently:
#OptionsMenu(R.menu.menu_helper)
#EActivity(R.layout.activity_helper)
public class HelperActivity extends AppCompatActivity {
#ViewById(R.id.cultureSpinner)
Spinner cultureSpinner;
#ViewById(R.id.harvestSpinner)
Spinner harvestSpinner;
ArrayAdapter<CharSequence> adapter;
#AfterViews
public void initialize() {
initializeSpinners();
}
public void initializeSpinners() {
adapter = ArrayAdapter.createFromResource(this, R.array.cultures, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cultureSpinner.setAdapter(adapter);
adapter = ArrayAdapter.createFromResource(this, R.array.harvestValues, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cultureSpinner.setAdapter(adapter);
}
}
The style xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:fillViewport="true"
android:orientation="vertical"
android:showDividers="beginning|end"
>
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="10dip"
>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cultureSpinner"
android:spinnerMode="dropdown"
/>
</TableRow>
<Space
android:layout_height="3dp"/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/harvestSpinner"
android:spinnerMode="dropdown"
/>
</TableRow>
</TableLayout>
</ScrollView>
Now when I launch the activity, The second spinner is displayed before the first one and the first spinner that is now for some reason below the second one, does not function. If I press it, the dropdown menu does not appear.
What could be the problem here?
public void initializeSpinners() {
adapter = ArrayAdapter.createFromResource(this, R.array.cultures, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cultureSpinner.setAdapter(adapter);
adapter1 = ArrayAdapter.createFromResource(this, R.array.harvestValues, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
harvestSpinner.setAdapter(adapter1);
}
Just try it. Hope its work.
Related
I have a xml view that I'm inflating. I'm then doing a network call and with the results populating all these spinners. This is what the view looks like before I call setadapter on the spinners.
However, when I call spinner.setadapter(), my last TextView gets cut off.
As you can see, the TextView is larger than the TableRow.
Here is snippet of xml layout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bcast_table">
<TableRow style="#style/row">
<CheckBox style="#style/chbx"
android:text="#string/title_num_retx"
android:id="#+id/num_retx_chbx" />
<Spinner style="#style/spinner"
android:id="#+id/num_retx_spinner" />
</TableRow>
<TableRow style="#style/row">
<CheckBox style="#style/chbx"
android:text="#string/title_cwm0"
android:id="#+id/cwm0_chbx" />
<Spinner style="#style/spinner"
android:id="#+id/cwm0_spinner" />
</TableRow>
</TableLayout>
</LinearLayout>
And here is how I setadapter()
static void populateSpinner(int spinId, ArrayList<String> fields, String currPos, View template, Context context){
Spinner spinner = (Spinner) template.findViewById(spinId);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, fields);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
int position = adapter.getPosition(currPos);
spinner.setSelection(position);
}
Thanks for the help!
Android AutoCompleteTextView search and returns the result in List instead of default dropdown.
Is there a way that I could redesign and put the result of an AutoCompleteTextView into a list other than to its default list design?
In my MainActivity, I have this:
String[]fruits = {"apple", "avocado", "banana", "blackberry", "blueberry", "coconut", "durian", "mango", "melon"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, fruits);
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocom);
autoCompleteTextView.setAdapter(stringArrayAdapter);
autoCompleteTextView.setTextColor(Color.RED);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(stringArrayAdapter);
}
In My xml,
<?xml version="1.0" encoding="utf-8"?>
<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.android.autocompletenativesample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="Pick a Fruit"
android:id="#+id/tv"
/>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/autocom"
android:layout_below="#+id/tv"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:completionThreshold="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListView Implementation"
android:layout_below="#+id/autocom"
android:id="#+id/tv2"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lv"
android:layout_below="#+id/tv2"
>
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RecyclerView Implementation"
android:layout_below="#+id/lv"
/>
</RelativeLayout>
Furthermore, here is a Visual Explanation. It will search in AutoCompleteTextView field but the result will not be displayed below instead on another layout.
Should I edit the ArrayAdapter ang change the layout, or have a CustomAutoCompleteTextView that implements AutoCompleteTextView or and EditText that implements AutoCompleteTextView.
I desire for my AutoCompleteTextView's Result to be formatted like this.
RecyclerView and CardView Result design
Thanks.
i can give you the logic. check out this link.
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and this too http://www.tutorialsbuzz.com/2014/08/android-filters-for-simple-listview.html
and filteration is done in adapter so first bind listview with your empty adapter and in textWatcher bindlistview with datasource filteration and notifie for changes.
I am trying to use two spinners with a custom dropdown yet, only the bottom one is showing with the custom layout when pulled down. I have been trying on my own to figure out why but I cannot.
public class setup extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setuplayout);
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner01);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.numberPlayers, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1.setAdapter(adapter);
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner02);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.gameDifficulty, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner2.setAdapter(adapter2);
}
}
This is the xml code for spinner_item mentioned above.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#F9B12F"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:padding="5dip"
android:popupBackground="#000000"
android:background="#000000"
/>
This is the setuplayout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:id="#+id/mainSetupImage"
android:src="#drawable/setup"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
<ImageView
android:id="#+id/players"
android:clickable="true"
android:src="#drawable/players"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true">
</ImageView>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner01"
android:textColor="#F9B12F"
android:layout_toRightOf="#+id/players"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:background="#drawable/spinner"/>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner02"
android:textColor="#F9B12F"
android:layout_below="#+id/Spinner01"
android:layout_toRightOf="#+id/difficulty"
android:layout_marginTop="25dp"
android:background="#drawable/spinner"/>
<ImageView
android:id="#+id/difficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/Spinner02"
android:clickable="true"
android:src="#drawable/difficulty" />
</RelativeLayout>
in 2nd spinner u used adapter rather than adapter2. so use adapter2 and run your code hope it will run. :)
I figured it out with your help. I had used the code "adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)" which was overriding my custom layout. Thanks for highlighting where the two codes were different. That was a huge help. I deleted the quoted lines and it now works.
I'd like to share a little code that will make all people's code much cleaner when making ArrayAdapters.
Just define a static method in a class of your preference (even MainActivity...) to give you the Array adapter without dirting so much your code:
public static ArrayAdapter<CharSequence>
getArrayAdapter( Context c, int arrayId, int idLayout1, int idLayout2){
ArrayAdapter<CharSequence> aa;
aa = ArrayAdapter.createFromResource( c, arrayId , idLayout1);
aa.setDropDownViewResource( idLayout2);
return aa;
}
Then in your code you just need to do it:
myspinner.addAdapter(
this, ThatClass.getArrayAdapter(
R.array.myid, R.layout.id1, R.layout.id2 );
myspinner.setOnItemSelectedListener(this);
/* Remember that the default Android Spinner layout can be get passing these specific layout ids:
android.R.layout.simple_spinner_item as layout1
android.R.layout.simple_spinner_dropdown_item as layout2
*/
I try to do simplest listView. I create layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/history_menu_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="60dp" >
</TextView>
And ListActivity:
public class HistoryMenu extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
String[] values = new String[] { "Edit", "Send" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.history_menu, values);
setListAdapter(adapter);
}
}
Element of listView highlight not fully, only where text.
How to fix it?
Use this as your list item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/history_menu_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="60dp" >
</TextView>
</LinearLayout>
Make changes in Textview
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/history_menu_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="60dp" />
I am writing an Android app that uses an AutoCompleteTextView just like the API example. The problem is that my text is too long and needs to wrap when the dropdown occurs. I have a custom list_item.xml, but only one line of text displays. The list_item.xml layout wraps to 2 lines if used with a spinner, but not with the AutoCompleteTextView.
main.java
public class main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
textView.setAdapter(adapter);
}
static final String[] COUNTRIES = new String[] {
"This is the first line that is too long and it needs to wrap content",
"Albania", "Algeria", "American Samoa", "Andorra",
"This is a second line that also needs to wrap its content",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium"
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country" />
<AutoCompleteTextView android:id="#+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
>
</TextView>
Wrap the TextView in a layout and use that for your ArrayAdapter
<?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="fill_parent" >
<TextView
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
/>
</LinearLayout>
And the new calls for the ArrayAdapter:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.item, COUNTRIES);
textView.setAdapter(adapter);
easy way :
adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.two_line_list_item,android.R.id.text1,....);
android.R.layout.two_line_list_item this is easy change.