AutoCompleteTextView Filter not working properly - java

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);

Related

Android Spinner not Showing DropDown menu

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>

How to Display Data to Listview from SharedPreferences

I have two activity (DetailProduct and Wishlist) and i want to display data from DetailProduct to Wishlist, in here i using SharedPreferences for put data from DetailProduct to Wishlist.
This is my activity DetailProduct
wishlist.setOnClickListener(new View.OnClickListener() {
String imgpicaso = getIntent().getStringExtra("aaa");
String vardetailed = getIntent().getStringExtra("acb");
String thisname = getIntent().getStringExtra("name");
String text = getIntent().getStringExtra("abb");
#Override
public void onClick(View view) {
Intent tambah = new Intent(DetailProduct.this, Wishlist.class);
SharedPreferences sharedPreferences = DetailProduct.this.getSharedPreferences("baba", MODE_PRIVATE);
SharedPreferences.Editor shreditor = sharedPreferences.edit();
shreditor.putString("aaa", imgpicaso);
shreditor.putString("name", thisname);
shreditor.putString("abb", text);
shreditor.commit();
startActivity(tambah);
}
});
This is img DetailProduct
This is img Wishlist, please open this img
And this is file Wishlist.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wishlist);
setTitle("Your Wishlist");
ImageView resultgmb = (ImageView) findViewById(R.id.resultgmb);
TextView detailtok = (TextView) findViewById(R.id.detailtok);
TextView resulttext = (TextView) findViewById(R.id.resulttext);
Button bayarwish = (Button) findViewById(R.id.check);
Button hapus = (Button) findViewById(R.id.delete);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("baba", MODE_PRIVATE);
String imgpicaso = sharedPreferences.getString("aaa", "");
Picasso.with(getBaseContext()).load(imgpicaso).into(resultgmb);
String detailtext = sharedPreferences.getString("name", "");
detailtok.setText(detailtext);
String text = sharedPreferences.getString("ccc", "");
resulttext.setText("$: " +text);
listproduct = (ListView) findViewById(R.id.listproduct);
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listproduct.setAdapter(arr);
arr.notifyDataSetChanged();
}
this is xml Wishlist
<SearchView
android:id="#+id/search"
android:background="#color/cardview_light_background"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</SearchView>
<LinearLayout
android:orientation="vertical"
android:id="#+id/relone"
android:layout_below="#id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/noimage"
android:id="#+id/resultgmb"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_alignParentRight="true"/>
<TextView
android:layout_marginTop="10dp"
android:text="setext"
android:textStyle="bold"
android:textColor="#000"
android:textSize="17dp"
android:id="#+id/detailtok"
android:layout_width="wrap_content"
android:layout_height="30dp" />
<TextView
android:text="setdescription"
android:layout_below="#id/detailtok"
android:textColor="#f00"
android:id="#+id/resulttext"
android:layout_width="wrap_content"
android:layout_height="30dp" />
<LinearLayout
android:layout_below="#id/resulttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHECK"/>
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DELETE"/>
</LinearLayout>
<ListView
android:id="#+id/listproduct"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
</LinearLayout>
Please help me, thank you
As #sark9012 pointed out, you might want to use Intent Data rather than shared preferences for that purpose. Here is a tutorial on that.
Edit
Although I'd really advise against using Shared Preferences data for the purpose you're looking for, it seems you got the code correct for retrieving the data from the Shared Preferences, what you might be missing out on is populating the ListView. This is the code you sent us:
listproduct = (ListView) findViewById(R.id.listproduct);
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listproduct.setAdapter(arr);
arr.notifyDataSetChanged();
I don't really see you populating the adapter with the data retrieved, so you might want to have a look into that. Check this example from CodePath on how to setup the ListView with an ArrayAdapter.
Generate comma separated string and store that in shared preference
while storing to SP.
like string1 = a1+","a2+","a3;
while retrieving from SP.
like array[] = string1.split(",");
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, array);

Design spinner programmatically (android)

I need create spinner programmatically. I add spinner to linearLayout, its work but I don't know how chache designe it.
Example, I have this:
How remove it?(highlighted in red)
And how remove spinner text to center?
My xml
LinearLayout spinnerL add this my spinner
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:id="#+id/spinnerL"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
/>
</RelativeLayout>
and main class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Array of choices
View linearLayout = findViewById(R.id.spinnerL);
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
Spinner spinner = new Spinner(this);
//Make sure you have valid layout parameters.
spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, list);
spinner.setAdapter(spinnerArrayAdapter);
((LinearLayout) linearLayout).addView(spinner );
}
To remove small triangle, simplest method is to set background drawable, for example:
spinner.setBackgroundResource(android.R.drawable.btn_default);
To align text center, you need to create layout for spinner, see examples

Why is my application with AutoCompleteTextView crashing down

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.

How do I add a TextView to the current layout so I can populate my spinners?

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);

Categories