I followed the AutoCompleteTextView tutorial exactly. The layout gets wrong when a soft keyboard is involved.
After tapping inside the dropdown (to select or scroll), the layout is broken:
I tried various combinations of attributes on the AutoCompleteTextView but none seems to work. I also tried setting windowSoftInputMode. The dropdown always pops up above the textbox, and stays there forever.
In landscape orientation, the layout is OK:
Is the standard autocomplete useless? Should I use a different one / write my own?
The code I used is the following:
<?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>
MainActivity.java:
#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);
}
Instead of using AutoCompleteTextView, I used EditText and a ListView as shown here.
Related
I can't seem to find a way to make a layout that contains a ListView that show the entire list of my contacts (its a long list), with 2 buttons one next to the other below the list.
its freaking me out, nothing i do seems to work. i searched some related quiestions and found this one: Android Layout with ListView and Buttons
but none of the alternative solutions over there is working for me.
when i look at the graphical presntation (of eclipse) it looks fine, but when i run it on my device i can only see the list of contacts. the listView overlaps the buttons.
I am using a simpleList that render on another layout that represent a row. i'm starting to suspect something i do programtically ruins it, because i ran over my java code and found no mention of the activity_contact_list that i was talking about. only connection to the row layout.
so i got this activity_contact_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_alignParentTop="true" >
</ListView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1.0"
android:orientation="horizontal" >
<Button
android:id="#+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />
<Button
android:id="#+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
</LinearLayout>
this is after many tries such as: using RelativeLayout instead of the LinearLayout, puting the ListView's code below the LinearLayout of the buttons, and many more..
and here is the code of the activity: ContactList.java:
public class ContactList extends ListActivity{
//some global variables for late use.
public Model model;
SimpleAdapter adapter;
List<Map<String, String>> data; /* data is a list of Map */
String[] from = {"name","phone"}; /* names of the columns */ //TODO - add date!
int[] to = {R.id.name, R.id.number};
List<Contact> contacts; /* list of contacts */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_contact_list);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayHomeAsUpEnabled(true);
model = Model.getInstance(this);
data = new ArrayList<Map<String, String>>();
contacts = fillContactsList();
fill_data();
adapter = new SimpleAdapter(this, data, R.layout.contact_list_row, from, to);
setListAdapter(adapter);
}
i commented the setContentView line cause it doesnt compile otherway...
As is said. the list works perfactly. but it overlaps the buttons at the bottom of the activity. any idea? i'm sure its something silly i'm missing..
thanks in advance!
Did you try to use Relative Layout ?
Change Linear Layout to Relative Layout that's all.
As Linear Layout Doesn't support align parent..
Edited
set List Height to mach parent.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_below="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="#+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />
<Button
android:id="#+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
How long is your ListView. Maybe it is longer than your screen and is the LinearLayout with buttons shown outside your screen.
What happens when you rearrange with the buttons above the ListView.
EDIT:
I think you have to set a Max-size in your ListView. Maybe you can try something like this in your java-code, but I don't know if it is scrollable then...
ListView listView = (ListView) findViewById(R.id.listView):
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) listView .getLayoutParams();
params.height = 200; // or something bigger / smaller
listView .setLayoutParams(params);
I may be missing something simple here, but I have an AutoCompleteTextView that contains some very long items. When one is clicked, it shows the text correctly in the EditText and spans it over several lines.
However, I want it to be in multiple lines on the popup as well so that the users can see which item they are selecting.
Here is my custom layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false" />
Here is my initialisation of the array and adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.dropdown_item_wrap_line,
getResources().getStringArray(R.array.building_descriptions));
mBuildingDesc.setAdapter(adapter);
My guess is that your AutoCompleteTextView is limited to singleLine, so using a custom layout with default TextView should work neatly.
You will need to make a new Layout and name it custom_item.xml, then do it like this...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="14sp"
/>
</LinearLayout>
Then while using Adapter on AutoCompleteTextView do
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, StringArray);
textView.setAdapter(adapter);
A defined a spinner in my XML file like so:
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/spinnerbackground"
android:layout_marginLeft="96dp" />
The background loads correctly, however the text representing the selected item is aligned fully to the left. The default spinner however has the text offset from the left by a small amount.
Does anyone know the code or XML I can use to replicate the effect of offsetting the item display text?
To make custom spinner background according to your textstyle you can use my code that I have used in one of my projects.
Inside string.xml Write:
<string-array name="spinner_array_environtment">
<item>Test</item>
<item>Production</item>
</string-array>
Inside your MainActivity.java :
public class MainActivity extends Activity {
Spinner spinner_environment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_environment = (Spinner) findViewById(R.id.spinnerview);
adapter =ArrayAdapter.createFromResource(this, R.array.spinner_array_environtment,R.layout.spinner_phone);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner_environment.setAdapter(adapter);
}
Inside spinner_phone.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13dp"
android:textColor="#4C4646" />
try this out. Hope it will help you.
I'm building a single choice list with a button at the bottom, and I can't seem to get the text in the actual list to display. Basically, it's a simple implementation that looks like:
public class SetPrefsActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.radiolist);
ArrayList<Hall> listItems = new ArrayList<Hall>();
ArrayAdapter<Hall> ar = new ArrayAdapter<Hall>(this, android.R.layout.simple_list_item_single_choice, listItems);
setListAdapter(ar);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{ ... //stuff});
...
//((ArrayAdapter<Hall>)getListAdapter()).add(stuff)
...
}
And Xml for radiolist is:
<?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="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:drawSelectorOnTop="false"
/>
<Button
android:id="#+id/theButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to go to next Activity"
/>
</LinearLayout>
What I see is a black area where the list should go, and then my button under it. Before I added: setContentView(R.layout.radiolist); it showed the populated list just fine. How do I get it to show up in this layout?
Sorry I'm just getting into android!
Along with changing your width, you should add : android:cacheColorHint="#00000000"
Here is a link that might help: http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html
Otherwise, when you scroll, you will get black boxes and other unwanted results when you scroll with your list.
I am trying to experiment with a custom Spinner / ArrayAdapter. Following is my code:
myspinner=(Spinner)findViewById(R.id.myspinner);
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setOnItemSelectedListener(this);
myspinner.setAdapter(myadapter);
Following is the custom layout mytextview that contains mytv:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="#+id/mylayout" android:layout_width="match_parent" android:layout_height="wrap_content">
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="#+id/myview1" android:background="#color/bordertop"></View>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="30dip" android:id="#+id/mytv"></TextView>
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="#+id/myview2" android:background="#color/borderbottom"></View>
</LinearLayout>
When i'm press the spinner to bring the popup, Android crashes. The came logic works perfectly fine, when I comment out the setDropDownViewResource method. I still don't understand what view setDropDownViewResource actually sets. Can anyone show me how it looks like by default. And, if one wants to override it with a custom layout - does the layout need to have a specific structure?
here is what i have typically used for spinners:
Spinner distancespinner = (Spinner) findViewById(R.id.distance);
ArrayAdapter<CharSequence> distanceadapter = ArrayAdapter.createFromResource(
this, R.array.distance, android.R.layout.simple_spinner_item);
distanceadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
distancespinner.setAdapter(distanceadapter);
distancespinner.setOnItemSelectedListener(new distanceSelector());
(obviously replace distance with whatever you want)
<Spinner
android:id="#+id/distance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:prompt="#string/app_name"
/>
to me it looks like your problem is in this line :
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
but then i dont know, your ArrayAdapter is based on instead of as i have and as per Spinner "the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform. Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened (simple_spinner_dropdown_item is another standard layout defined by the platform)."
i think if you want to customize your spinner, .setDropDownViewResource(); is where to do it.