I am trying to learn AutoCompleteTextView in android. I made a small app with AutoCompleteTextView against a few names. The app has no syntax errors. But, when I upload it to the emulator for testing, it crashes down at the start screen itself.
Please help.
package com.mavenmaverick.autocomplete_test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
String[] presidents= { "John F. Kennedy",
"Lyndon B. Johnson",
"Richard Nixon",
"Gerald Ford",
"Jimmy Carter",
"Ronald Reagan",
"George H. W. Bush",
"Bill Clinton",
"George W. Bush",
"Barack Obama"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
textView.setThreshold(3);
textView.setAdapter(adapter);
}
}
<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=".MainActivity" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/names"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10" >
<requestFocus />
</AutoCompleteTextView>
<TextView
android:id="#+id/names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="#string/hello_world" />
</RelativeLayout>
Change-
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
Into-
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
Also change-
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names, presidents);
Into-
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents);
Change
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
to
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
textView.setThreshold(1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,presidents);
textView.setAdapter(adapter);
Hope this will work
Here must check
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
that names is the id of your AutoCompleteTextView in your activity_main.xml file.
Because your logcat clearly says that you have a ClassCast Exception. So must check the id in your activity_main.xmlfile.
So change here from
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
to
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
You need to apply your Threshold to 1 instead of 3 because
When threshold is less than or equals 0, a threshold of 1 is applied.
UPDATE:
You have to change this from
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
to
ArrayAdapter<String>p = new ArrayAdapter<String>(YourActivityName.this, R.layout.customlayout, R.id.names, presidents);
Where customlayout is the layout in which your TextView will be which id is names.
Related
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);
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);
Here is the layout I am trying to get. As long as I don't actually try to populate my spinners, everything loads just fine. Based on what I have found in my searches and the log is that I need to use a TextView with the ArrayAdapter. Exactly how I do that while preserving the current layout - Spinners and a submit button at the top with a ListView for the returned results below the spinners is what I am having trouble achieving.
Here is my onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.master);
DBHelper db = new DBHelper(this);
List<Stations> st = db.getAllStations();
List<CharSequence> stations = new ArrayList<CharSequence>();
setList(4, 9);
for (int i = 0; i < st.size(); i++)
{
stations.add(st.get(i).getStation());
}
ListView list = (ListView) findViewById(R.id.list);
Spinner s1 = (Spinner) findViewById(R.id.spinnerStart);
Spinner s2 = (Spinner) findViewById(R.id.spinnerEnd);
ArrayAdapter<CharSequence> SimpleSpinner1 = new ArrayAdapter<CharSequence>(this, R.layout.spinner, R.id.textView1);
ArrayAdapter<CharSequence> SimpleSpinner2 = new ArrayAdapter<CharSequence>(this, R.layout.spinner, R.id.textView1);
SimpleAdapter nSchedule = new SimpleAdapter(this, departures, R.layout.row,
new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
for (CharSequence c : stations)
{
SimpleSpinner1.add(c);
SimpleSpinner2.add(c);
}
list.setAdapter(nSchedule);
s2.setAdapter(SimpleSpinner2);
s1.setAdapter(SimpleSpinner1);
}
master.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="#+id/spinnerStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<Spinner
android:id="#+id/spinnerEnd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spinnerStart" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/spinnerEnd"
android:text="Button" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
layout="#layout/listview" />
</RelativeLayout>
spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Instead of R.id.spinnerStart / R.id.spinnerEnd you need to pass a textView
EDIT:
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.spinner_item, R.id.textView1);
spinner1.setAdapter(adapter);
textView1 is present in spinner_item layout. Hope this is clear
Try this
// set id for spinner
spinner2 = (Spinner) findViewById(R.id.spinner2);
// prepare a list
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
// set adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set adapter to spinner
spinner2.setAdapter(dataAdapter);