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>
Related
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'm trying to achieve a search activity where when the search button is clicked from the action bar, a list is shown with possible products. I'm trying to connect my search button with my list view but have been unsuccessful.
MainActivity.java
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
// Listview Data
String products[] = {"A", "B", "C", "D",
"fdsafds", "fdsavds",
"fdsafdsafsd", "greadgfd", "F", "thrgefargra"};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
// adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
// lv.setAdapter(adapter);
return true;
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/inputSearch"
android:title="Search places"
android:hint="Search places..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="29dp"/>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Single ListItem -->
<ListView android:id="#+id/list_item"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
<!-- Product Name -->
<TextView android:id="#+id/product_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
You shoild reggister 'ListView' for the Menu
registerForContextMenu(listview);
I've done something like what you want. try and see if it can help you.
public class YourActivity extends Activity {
private ListView l;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
l = (ListView) findViewById(R.id.your_list_view_id);
List<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
l.setAdapter(arrayAdapter);
}
}
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 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.
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);