Hello fellow Android programmers,
today I wanted to create a simple AutoCompleteTextView with a custom Adapter, which works perfectly fine on the Emulator. But if I now run the whole thing on my Samsung Galaxy Young and I enter some text into the ACTextView, it suggests me everything as it should, but I can't click the "OK" button in the bottom right corner of the keyboard that is showing up.
If I flip the Smartphone and change orientation, I get rid of the annoying keyboard, but I can't close it any other way. Solutions? Here is the code and xml used:
<AutoCompleteTextView
android:id="#+id/vereinAutoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="#string/vereinHint"
android:inputType="textNoSuggestions">
</AutoCompleteTextView>
And the Java code for the Adapter (it's a custom one):
rootView = inflater.inflate(R.layout.verein_layout, container, false);
AutoCompleteTextView acTextView = (AutoCompleteTextView)rootView.findViewById(R.id.vereinAutoComplete);
acTextView.setAdapter(new SuggestionAdapter(getActivity(),acTextView.getText().toString()));
acTextView.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
I am using the whole thing in a Fragment, so don't wonder the rootView and so on...
There is some code for the Listener, but I don't think it is important for my question.
Related
When I use the autocompletetextview everything works fine except it keeps switching between two positions: the correct one right below the textview and quite a ways lower. It starts wrong, but almost immediately moves to the correct position. However this is very annoying when typing or backspacing as it happens for every letter. I am using android studio.
It appears as if two events are simultaneously trying to decide the layout. Sometimes it will stick in one position or the other.
**I slowed down the filtering process with a custom adapter and it looks like when text is entered it moves into the incorrect position, and then when the filtering is done it moves back into the correct position.
Incorrect
Correct:
java (in OnCreate())-
String[] drugs = new String[]{"Nexium","Amoxicillin","LEVOCETIRIZINE DIHYDROCHLORIDE", "Advil", "Advair Diskus", "Daraprim"};
AutoCompleteTextView drugNameAutoComplete = ((AutoCompleteTextView) findViewById(R.id.drugNameEditText));
drugNameAutoComplete.setAnimation(null);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,drugs);
drugNameAutoComplete.setAdapter(adapter);
And the layout code-
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/drugNameEditText"
android:enabled="true"
android:singleLine="true"
android:layout_below="#+id/lookingForView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:dropDownVerticalOffset="50dp"
android:hint="#string/drug_name" />
If I remove the dropDownVeticalOffset I get flickering between the correct value and this-
To change this position use dropDownAnchor attribute and reference another view id.
(view under autocomplete)
android:dropDownAnchor
View to anchor the auto-complete dropdown to. If not specified, the text view itself is used.
and you have many attributes for dropdown..
You better to use implement Filter to autocomplete adapter and pass the entered text from OnQueryTextListener of autocomplete, and set the selected text by calling adapter.getitem.
Change your xml to-
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/drugNameEditText"
android:enabled="true"
android:singleLine="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
**android:dropDownVerticalOffset="0dp"**
**android:dropDownAnchor="#id/drugNameEditText"**
android:hint="drug" />
Did you by any chance use the same id on the other AutoCompleteTextView? I can reproduce the behaviour you describe by including two views with the same id.
I had the same problem on my end with the newest API for some reason. I solved the problem by explicitly setting dropDownAnchor, dropDownVerticalOffset, and dropDownHeight in both the XML and the onTextChanged event.
Edit: After some checking, it seems this behavior is INCONSISTENT BETWEEN APIs starting with Marshmallow (API 23). Try changing your code to the following:
final float scale = context.getResources().getDisplayMetrics().density;
int verticalOffsetDP;
int dropDownHeightDP = (int) (200 * scale + 0.5f);
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
if (currentApiVersion < Build.VERSION_CODES.M)
{
// Prior to Marshmallow DropDownVerticalOffset works as expected
verticalOffset = 0;
}
else
{
// Marshmallow has some weird DropDownVerticalOffset behavior so we have to compensate
verticalOffset = (int) (50 * scale + 0.5f);
}
AutoCompleteTextView drugNameAutoComplete = ((AutoCompleteTextView)findViewById(R.id.drugNameEditText));
drugNameAutoComplete.setAnimation(null);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,drugs);
drugNameAutoComplete.setAdapter(adapter);
drugNameAutoComplete.setDropDownAnchor(R.id.drugNameEditText);
drugNameAutoComplete.setDropDownVerticalOffset(verticalOffsetDP);
drugNameAutoComplete.setDropDownHeight(dropDownHeightDP);
And your XML to the following:
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/drugNameEditText"
android:enabled="true"
android:singleLine="true"
android:layout_below="#+id/lookingForView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:dropDownVerticalOffset="50dp"
android:dropDownAnchor="#id/drugNameEditText"
android:dropDownHeight="200dp"
android:hint="#string/drug_name" />
Note that you have to calculate DP based on screen dimensions in code. I got the code example from the excellent accepted response in this thread: Android and setting width and height programmatically in dp units
The reason you have to set dropDownHeight as well is because otherwise Android has a behavior to push it up to the top of the screen if the list extends beyond the height boundaries of the view, further complicating the moving-around issues. If you actually don't mind this (some people don't, depends on your view setup), you can remove dropDownHeight from both programmatic and XML.
I should note that these changes still don't make things EXACTLY the same between Marshmallow and other APIs. Looks like there is still a few pixels difference. But it gets pretty close.
I had the same problem and too many hours later I've found the solution.
You should change:
android:layout_width="wrap_content" to android:layout_width="match_parent"
I hope it works for you too.
I have an app that for some reason, on the latest Android SDK (4.4) launches to a black screen.
I can still hear the activity behind it (for example if I click on somewhere in the black, a button sound is clicked, so Its almost like the screen is loaded behind the black screen).
It works fine on my device which is a Nexus 4 running 4.4, it appears to be an issue on the later version (Nexus 5) devices.
It also shows the banner ad (admob) that I have, so it literally is a case that it seems to be an issue with the background image file.
//#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mGLSurfaceView = new CCGLSurfaceView(this);
CCDirector.sharedDirector().setScreenSize(CCDirector.sharedDirector().winSize().width,
CCDirector.sharedDirector().winSize().height);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
CCDirector.sharedDirector().getActivity().setContentView(mGLSurfaceView, createLayoutParams());
InitParam();
getAdmob();
}
Activity game xml is;
<?xml version="1.0" encoding="utf-8"?>
<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=".GameActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</RelativeLayout>
width & height of the imageview is "wrap content" however the scaletype is "fitXY".
thats kind of saying 2 different things to the device.
if that image should be full screen change it's width & height to match/fill parent.
Check onCreate(), onStart() and onResume() methods in your code, you might be doing some time consuming, blocking tasks. If so, load heavy background tasks on a separate thread.
Profile your code with Traceview and dmtracedump to check what is taking more time.
I have a normal autocomplete:
<AutoCompleteTextView
android:id="#+id/autocomplete_names"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/namesauto_prompt"
android:imeOptions="actionDone"
android:completionThreshold="2"/>
...
AutoCompleteTextView nameinp = (AutoCompleteTextView) findViewById(R.id.autocomplete_names);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
nameinp.setAdapter(adapter);
I won't bother showing the names list, because it which works fine on my tablet running ICS, both in landscape and portrait modes. In landscape on my phone (Gingerbread) it does OK - the names appear the same way that autosuggest for texting does. But in portrait mode it just renders a blank listview beneath the autocomplete input. Any ideas why?
UPDATE
So, having played around with it, I notice that my list is there, it's just that it's white text on a white background (kind of hard to read). I can set my
android:popupBackground
to whatever color I like (dark gray is a contender at the moment) but I can't see a way to change the text color for the popup (or dropdown as it were), nor do I really understand why it's white in the first place...
I don't know the problem of this issue, because even I faced this issue with AutoCompleteTextView. I found a workaround for this.
What you can do is set a custom xml in the ArrayAdapter that will have the TextView color set to black.
The custom TextView file my_custom_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/dropDownItemStyle"
android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
And set this xml in your ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_custom_textview, names);
nameinp.setAdapter(adapter);
Hope this helps you..
I created an Activity displays an ImageView on the screen. I want get haptic feedback when the image is clicked.
In the main layout main.xml I added the next ImageView tag:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/dog"
android:onClick="doBark"
android:hapticFeedbackEnabled="true"/>
Then, in the Activity code I add this method:
public void doBark(View v) {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
Log.d("BarkingDog", "is hapticFeedbackEnabled: " + v.isHapticFeedbackEnabled());
}
When I click on the image I can see that doBark() is called and the output of the Logcat says "is hapticFeedbackEnabled: true", but I can't feel anything. I've also tried with the other two HapticFeedback constants, and no luck.
I know that HapticFeedback is enabled because each time I press the menu button, the device vibrates.
Any ideas? Suggestions?
PS: I don't want to use the Vibrator object. By using it, I can make the device vibrate, but I don't think it's the right way to do it.
Take a look at this: http://groups.google.com/group/android-developers/browse_thread/thread/de588e3d15cb9055?pli=1
Do note that it is old though, but the last time I had to use haptic feedback, I followed what Dianne had to say here
I've got a ListActivity and ListView and I've bound some data to it. The data shows up fine, and I've also registered a context menu for the view. When I display the list items as just a simple TextView, it works fine:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
However when I try something a bit more complex, like show the name and a CheckBox, the menu never shows up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/namecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Can long-presses work on more complex elements? I'm building on 2.1.
(edit)
Registering with this on the ListActivity:
registerForContextMenu(getListView());
The code I posted is the item template for the list.
Your CheckBox may be interfering with matters. Consider using a CheckedTextView instead of a LinearLayout, CheckBox, and TextView combination, since CheckedTextView is what Android expects for a CHOICE_MODE_MULTIPLE list.
Check out $ANDROID_HOME/platforms/$VERSION/data/res/layout/simple_list_item_multiple_choice.xml, where $ANDROID_HOME is wherever you installed the SDK and $VERSION is some Android version (e.g., android-2.1). This resource is the standard resource you should use for CHOICE_MODE_MULTIPLE lists. Feel free to copy it into your project and adjust the styling of the CheckedTextView as needed.
set checkbox property
focusable = false;
and run project again..
Found at this place: http://www.anddev.org/view-layout-resource-problems-f27/custom-list-view-row-item-and-context-menu-t52431.html
Setting the checkbox to not be focusable fixes the problem.
Not sure if it would cause issues when navigating the UI with something else than a touchscreen (with a wheel or arrow keys), but it fixed my problem (my layout was a bit more complicated than just a TextView and a Checkbox...)
Context menu's can only be registered to subclasses of View. I don't know how you registered the LinearLayout with a context menu, did you package it in some type of View? if so, you should post that code.
Anyways why not just register the TextView of each list item? Who would long press a checkbox...
This should from a regular ListView as well. But if you're starting from scratch on a new list I would consider using the CheckedTextView:
checkBox.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// return false to let list's context menu show
return false;
}
});