I follow the official instruction of autocompletetextview to create a new one in my project. I think that is easy, but when I run the code, I find the outcome is far from my expectation.
As you can see on the picture, when I type "f" in the textview, the dropdown view which is supposed to display "Franch", only displayed "Fr..", though I have add "setDropDownWidth(100)" to the textview.
I really want to know what happen and how to solve it.
And below is my code:
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/countries_list"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
package com.example.autocompletetextview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setThreshold(1);
textView.setDropDownWidth(100);
textView.setAdapter(adapter);
}
}
This occurred because of this textView.setDropDownWidth(100). It too small so the text will be wrap.
My solution is just remove it, so the autocomplete will wrap your edittext width, or setDropDownWidth with a bigger value.
Related
It works perfectly with linear layout but when I choose ConstraintLayout it doesn't work, I don't know why the app crashes
NumbersActivity.java
package com.example.bharat.learnmiwok;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class NumbersAct extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ArrayList<String> words = new ArrayList<String>();
words.add("one");
words.add("two");
words.add("three");
words.add("four");
words.add("five");
words.add("six");
words.add("seven");
words.add("eight");
words.add("nine");
words.add("ten");
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, words);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
}
}
activity_numbers.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bharat.learnmiwok.MainActivity"
android:id="#+id/list"
android:orientation="vertical">
</android.support.constraint.ConstraintLayout>
Thank you
You need to have a ListView with id list in your layout. Remove/Change the id from your ConstraintLayout, add a listview and try again.
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
Your ConstraintLayout is not a ListView.
Probably on this line you getting crash
ListView listView = (ListView) findViewById(R.id.list);
You tried to cast ConstraintLayout to ListView
You are using this:
ListView listView = (ListView) findViewById(R.id.list);
to find a ConstraintLayout. Try this instead:
ConstraintLayout listView = (ConstraintLayout) findViewById(R.id.activity_main);
This would only resolve your crash, but it's best to use a listView or contain one into the ConstraintLayout to achieve your goal.
I'm new to Android Studio.For me there is a problem with listview. I've set all the ids properly and yet the problem occurs.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.prashanthwagle.myapplication.MainActivity">
<ListView
android:id="#+id/sample"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
</RelativeLayout>
Java:
package com.example.prashanthwagle.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String[] s={"asfasaf","bafaf","cbfbd"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.sample);
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s);
listView.setAdapter(a);
}
}
I Think that you should change
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s)
;
to
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R. android.R.layout.simple_list_item_1,s);
You have set the wrong layout for ArrayAdapter. Use this:
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,s);
The layout reference in the adapter initialization and assignment should not be the layout reference of your activity. Basically, this parameter tells the ListView what type of layout will each row of the list have. Read more about this from here: https://developer.android.com/guide/topics/ui/layout/listview.html
I'm at my first steps into Android... I already read some of the answered question, and I tool a lot of tips, but I don't find how to solve my problem.
In the activity Categories.java I have a listview built starting from a database. The listview displys correctly, then I would like to start another activity from the OnItemClick, but I realized that I can't display even a toast.
I really hope some of you can help me I really don't find where is my error!! Thank you in advance!
Here is my code:
package com.example.myshopping;
import java.util.ArrayList;
import android.os.Bundle;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.app.Activity;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class Categories extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
final ListView lista= (ListView) findViewById(R.id.id_categorie);
MioDatabaseHelper myHelper;
myHelper= new MioDatabaseHelper(this);
final SQLiteDatabase db= myHelper.getReadableDatabase();
String [] columns_cat ={"nome_categoria"};
Cursor cursor= db.query("categorie", columns_cat, null, null, null, null, null);
int len= cursor.getCount();
cursor.moveToFirst();
final ArrayList <String> array_cat= new ArrayList<String>();
for (int i=0; i<(len); ++i){
array_cat.add(cursor.getString(0));
cursor.moveToNext();
}
ArrayAdapter<String> myadapter= new ArrayAdapter<String>(this, R.layout.listitem, R.id.listitemTextView, array_cat);
lista.setAdapter(myadapter);
lista.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(getApplicationContext(), "hello toast!!!!", Toast.LENGTH_LONG).show();
}
});
}
and here the xml file of the list items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/listitemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:includeFontPadding="false"
android:padding="5pt"
android:scrollHorizontally="false"
android:textSize="10pt" />
</LinearLayout>
and here the xml file of the categories.java file:
<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="wrap_content"
android:gravity="center_horizontal"
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=".Categories" >
<ListView
android:id="#+id/id_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textIsSelectable="true">
</ListView>
</RelativeLayout>
I see basic inconsistency in your code:
R.layout.categories//in set content view
and in array adapter:
ArrayAdapter<String> myadapter= new ArrayAdapter<String>(this, R.layout.listitem, R.id.listitemTextView, array_cat);
Change R.layout.listitem to R.layout.categories
or use a different constructor.
Is android:textIsSelectable="true" and android:descendantFocusability="blocksDescendants" attributes needed in your case? Using them wrong can cause focusing problems and the click listener doesn't work. Try removing both.
Also the list item contains only one child for the LinearLayout so it could just be TextView having width set to match_parent and height wrap_contentcombined with padding.
Also if you want to specyfy textView appearance Create XML Just with that textView without parent Noode, coz lists can be ricky sometimes :P try create another XML just to test that one thingChange this:<relativeLayout>
attributes..
<textView>
attributes..
</textView>
</relativeLayout> To<TextView>
attributes
</TextView>Basicaly you want stand alone TextView
I am looking to have a image button which acts like a spinner in XML. I have found some information on this but nothing like what am after. So when i click a image view a spinner will appear with a list to choose from that will come from my DB if passable. What i found which didn't do anything
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/KeUxBt4SJLw
I want to be able to have 10 image buttons in my activity which have a spinner for each one.
And when the users pick from the spinner list a dialog will appear(which i will try later hopefully after i can code this first).
Any help will help thanks
package com.spinn;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.array_name, android.R.layout.simple_spinner_item );
adapter.setDropDownViewResource(
android.R.layout. simple_spinner_dropdown_item );
Spinner s = (Spinner) findViewById( R.id.unique_id );
s.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" >
<Button
android:id="#+id/unique_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/c3" />
</RelativeLayout>
Why not using spinners with setting their background your image buttons' background? This should do the trick...
How to handle an empty ListView if I use simple_list_item_multiple_choice layout?
'Android Developers' say that I should use the android prefix
android:id="#android:id/empty
But cannot take in mind how to use it with simple_list_item_multiple_choice
protected ListView mFavList;
ArrayList<String> fakeFavs;
mFavList.setAdapter(new ArrayAdapter<String>(this.android.R.layout.simple_list_item_multiple_choice,fakeFavs);
if fakeFavs is null?
I solved my problem.
All I need - "empty" tag in main.xml layout and string of code:
mFavList.setEmptyView(findViewById(android.R.id.empty));
In my program fakeFavs is populating from text file.
In case of empty file, the empty list is properly handled.
Maybe it will be useful for somebody. Working example:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class HelloAndroid3 extends Activity {
/** Called when the activity is first created. */
static final String split_symbol = "\n";
protected ListView mFavList;
protected ArrayList<String> fakeFavs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fakeFavs = new ArrayList<String>();
this.mFavList = (ListView) this.findViewById(R.id.list_favorites);
refreshFavListItems();
}
private void refreshFavListItems() {
//put some code here that populates fakeFavs
//.......
mFavList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, fakeFavs));
mFavList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mFavList.setEmptyView(findViewById(android.R.id.empty));
//if fakeFavs will be empty, list shows "No Data"
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/list_favorites"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="No Data"/>