I have a problem regarding the spinner design. I am using this code to generate a dropdown spinner:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
LinearLayout layout = new LinearLayout(this);
ArrayList < String > spinnerArray = new ArrayList < String > ();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter < String > spinnerArrayAdapter = new ArrayAdapter < String > (this,
android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
}
It displays this:
How can I remove the black color that blocks the spinner?
layout.addView(spinner);
setContentView(layout);
When you dynamically add the view to the layout. you are missing some configuration. thats the reason you see a black box.
try the below code:
xml:
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
activity:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
arraydata, android.R.layout.simple_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
spinner.setAdapter(adapter);
Create a layout file simple_list.xml in your layout folder:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Sample Text"
android:padding="5dp"
android:gravity="center"
android:textColor="#android:color/black"
android:background="#android:color/white"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And refer this in arrayadapter:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
R.layout.simple_list, spinnerArray);
Update 1 Add this:
spinner.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),android.R.color.white));
update 2
Instead of using constraint use Linear once in your layout file:
<?xml version="1.0" encoding="utf-8"?>
<LineartLayout xmlns:android="schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"
android:id="#+id/linearLayout"
xmlns:tools="schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> </LinearLayout>
Set id to LinearLayout (your root view), then use findViewById for this view and add spinner to this rootView and remove setContentView(layout);
Change it like this:
LinearLayout layout = new LinearLayout(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
Interestingly your code worked for me. So make sure that there is no element there or some other piece of code related to theme or colors.
Add a linearlayout to your activity_my layout and name it linearLayoutContainer . Then get a reference to it from code.
LinearLayout container = findViewById(R.id.linearLayoutContainer);
Spinner spinner = new Spinner(this);
container.addView(spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
Related
I am trying to display a spinner in FrameLayout but it's not showing the drop down menu. i am unable to find the issue.
XML
<FrameLayout
//design continue here
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b89ff"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner"
android:spinnerMode="dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</FrameLayout>`
Code
final List<String> list=new ArrayList<>();
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, list);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
My onCreate method
TabHost host;
host = findViewById(R.id.tabHost);
spinner= findViewById(R.id.spinner);
progressDialog=new ProgressDialog(this);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("News Feed");
spec.setContent(R.id.tab1);
spec.setIndicator("News Feed");
host.addTab(spec);`
Update 1
Using RelativeLayout instead FrameLayout throws
android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout
Remove this line and check,
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //Comment this line
Try this change also :
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item, list); //Changed layout resource id
Try this
final List<String> list=new ArrayList<>();
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, list);
spinner.setAdapter(arrayAdapter);
Please try this:
code:
public class MainClass extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOnSpinner();
}
private void addOnSpinner() {
Spinner spinner = findViewById(R.id.spinner2);
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
ArrayAdapter adapter = new ArrayAdapter<String>( this, R.layout.support_simple_spinner_dropdown_item, list);
spinner.setAdapter(adapter);
}
}
XML:
<FrameLayout
//layout
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner2"
android:spinnerMode="dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I have an AutoCompleteTextView and I want to set a filter on it, the problem I'm having is when I go to type something it'll only let me type 1 character in the text field. if I remove textView.setFilters from the code it works fine I just don't have any filters. I have also tried android:textAllCaps="true" in my xml file and it doesn't work. any help would be great, thanks.
My code:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textfield);
String[] MyArray = getResources().getStringArray(R.array.myarray);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, MyArray);
textView.setFilters(new InputFilter[]{new InputFilter.AllCaps(), new InputFilter.LengthFilter(40)});
textView.setAdapter(adapter);
Xml
<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"
tools:context=".myactivity">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textfield"
android:hint="Search..."/>
<Button
android:id="#+id/btn"
android:onClick="buttonClickHandler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Do something"
android:layout_toRightOf="#+id/textfield"/>
</RelativeLayout>
With the help of #pskink's post I figured it out
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textfield);
String[] MyArray = getResources().getStringArray(R.array.myarray);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, MyArray);
textView.setThreshold(1);
InputFilter[] filters = {
new InputFilter.AllCaps(),
new InputFilter.LengthFilter(40),};
textView.setFilters(filters);
textView.setAdapter(adapter);
I have 2 xml file
MainActivity.xml
2.Example.xml
I have listview in Example.xml.the code I have written in Example.java. But the data is not displaying in listview..Kindly go through my code
Example.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listPasscode"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"/>
</LinearLayout>
</ViewSwitcher>
Example.java
public class ListViewAndroidExample extends Activity {
ListView listView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = (ListView) findViewById(R.id.listPasscode);
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
// Array of strings...
String[] countryArray = {"India", "Pakistan", "USA", "UK"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, countryArray);
// Assign adapter to ListView
listView.setAdapter(adapter);
setContentView(R.layout.activity_list_view_android_example);
}
}
Is linking between xml and java file right ?If not suggest me
Try this..
Add setContentView before initializing the ListView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_android_example);
listView = (ListView) findViewById(R.id.listPasscode);
EDIT
Change this..
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, countryArray);
to
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countryArray);
I am working on a first basic android app as a university project and I have an array list of names. I want it to be put into a ListView. The problem I have is nothing appears when I bring in the ListViewunder the xml document. I don't get the basic template for item1, sub item 1 etc.
Then when I set up the array to play in the list view still empty list. Below I have the xml document
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="277dp"
android:id="#+id/listView" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="7dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
then here is the java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
populateListView();
}
private void populateListView() {
String[] myItems = {"frank","george","alice", "anna"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.main, myItems);
ListView list = (ListView)findViewById(R.id.listView);
list.setAdapter(adapter);
}
Your problem is that you have the same layout R.layout.main for two different use.
You set the content view as setContentView(R.layout.main); where your ListView is but you also use this layout with your adapter as new ArrayAdapter<String>(this, R.layout.main, myItems);.
So with your code, you don't have any item layout to display your array myItems. You need to create another layout as R.layout.item_layout with a TextView inside (or use an android layout like android.R.layout.simple_list_item_1) for your adapter.
Note: to avoid some bugs with ListView, you have also to set the height to match_parent or fill_parent
Try this for your adapter:
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myItems);
The adapter has to inflate a TextView. You can make your own TextView (if you want to customize it) in XML.
Then it will be:
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this, R.layout.yourcustomtextview, myItems);
The default array adapter in Android takes the layout for the row as the second argument.
Change your ArrayAdapter constructor to something like this:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)
I am trying to populate a spinner but I seem to be missing something in my layout file
ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, R.layout.spinner_view,
getResources().getStringArray(R.array.cuisines));
I can't find R.layout.spinner_view and can only assume that I have to make it myself in my layout file. How do I do that?
Include spinner in xml file as:
<Spinner
android:id="#+id/spin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
and in activity:
Spinner spinner=(Spinner) findViewById(R.id.spin);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(YourActivity.this, android.R.layout.simple_spinner_item,R.array.cuisines);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Use import com.companyname.product.R;
instead of
import android.R;
If you want to load with default spinner view, then use,
ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.cuisines));