Android AutoCompleteTextView popup moving after displaying - java

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.

Related

How to accelerate smooth scrolling in Android multiline Edittext?

Here is the xml (activity_matches.xml) that displays the output in the screen shot below in an EditText object named txaMatches:
<EditText
android:id= "#+id/txaMatches"
android:text= ""
android:scrollbars= "vertical"
android:layout_below= "#+id/pattern"
android:textColor= "#color/yellow_foreground"
android:textSize= "#dimen/matches_text"
android:focusable="false"
android:inputType="textMultiLine"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd= "true"
tools:ignore= "LabelFor"
android:typeface="monospace"
android:focusableInTouchMode="false"
android:paddingEnd="#dimen/matches_padding"
android:paddingStart="#dimen/matches_padding"
/>
I would like the contents of this multiline EditText object to scroll smoothly with acceleration if the user swipes inside it.
Here's how I fill txaMatches (using doInBackground). This is the only place in Java code that I refer to it (of course I also define and initialize it in Java):
protected void onProgressUpdate(String... progress)
{
for (int i = 0; i < progress.length; i++)
if(progress[i].length() > 1) MatchesActivity.txaMatches.append("\n" + progress[i]);
else MatchesActivity.showLetter("Reading " + progress[i].charAt(0));
}
Is this an easy change to be made in xml?
Should I be using a ListView? Should the EditText be inside a ListView?
Am I going to have to write some Java code?
Given the small amount of code I've provided, I don't expect many details. Just an overview of what to Google would be a good start.
Pretty easy (thanks to Fllo's THIRD link). (The first was no help; the second focuses on Java code and the ScrollView class and is more involved than the third, which focuses on xml and was perfect for my situation (and required only changing the type of txaMatches to TextView):
start of new code
<ScrollView
android:layout_alignParentStart="true"
android:layout_alignParentEnd= "true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
end of new code except for closing tag
<TextView
android:id= "#+id/txaMatches"
android:scrollbars= "vertical"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
/>
</ScrollView>

Do not show images of ImageView in real devices

I want show image in ImageView, but don't show it. I use XML code and Java code but don't show. show in simulator but in real devices don't show. tested on LG G2, HTC One X, Samsung Galaxy S3.
my xml code :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:background="#drawable/show_sms_header" />
my java code :
Dialog_Header_Img = (ImageView) findViewById(R.id.sms_dialog_header);
Dialog_Header_Img.setImageResource(R.drawable.show_sms_header);
I used to be separated from each.
Please tell me the solution
First of all you should set image in xml by code:
android:src="#drawable/show_sms_header"
not by android:background, well you can set background to color or other image but your main image you should set by android:src
If you changing something about your image, leave first line in your code that you show, and delete second, if setting image is only thing you set, then you can delete both, because you set source of image in xml.
The "src" attribute should be set to your drawable if you want it to be "centerCrop" correctly.
Check this out :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/show_sms_header" />
Make sure that your file, called "show_sms_header", is in your drawable ( /res/drawable) directory.

View over surface view : Works on some android version

I'm actually making an android app which uses a SurfaceView to draw the Camera.
I want to add some views over it.
In fact, i already done it. It works on my android 4.1.1. All my views are drawn over the Camera.
But when i try it on a Android 2.3, it don't works anymore.
Depends of what i'm doing, it shows a part of the view in the left-top corner (As if the parent layout were too small)
Here is the xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<com.example.calculatrice.CustomCameraView
android:id="#+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/poiGroup">
<Button
android:id="#+id/bBack"
android:layout_width="150dp"
android:layout_height="80dp">
</Button>
</LinearLayout>
</FrameLayout>
I have a button, wich is perfectly shown over the camera.
I put the views i want to draw under that button (with "layout(x,x,x,x)"). They appear cuted (or don't appear, depend).
Here is some usefull code
//In oncreate method
customCamera = (CustomCameraView)this.findViewById(R.id.cameraView);
//Create view and add it to the layout
public void initObjects()
{
listPoiMarker = new LinkedList<POIMarker>();
int i = 0;
while (i < names.length && names[i] != null)
{
Location locTmp = new Location(LocationManager.NETWORK_PROVIDER);
locTmp.setLatitude(coordonates[0][i]);
locTmp.setLongitude(coordonates[1][i]);
POIMarker tmp = new POIMarker(getApplicationContext());
tmp.setPoiId(id[i]);
tmp.setId(i);
tmp.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tmp.setName(names[i]);
tmp.setLocation(locTmp);
tmp.posScreenY = 100 + (i) * 60;
listPoiMarker.add(tmp);
tmp.setOnTouchListener(onTouchListener);
((LinearLayout) findViewById(R.id.poiGroup)).addView(tmp);
tmp.layout(50, 50, 250, 250);
i++;
}
}
Not exactly what i do. In fact the layout function is somewhere else.
But it's the same.
That works fine in Android 4.1.1, but not in 2.2 or 2.3
I looking for a solution for about 2 days. I tried 999 things, none worked.
That's why i come here to ask some help.
(Here is an example of what it does : http://drawsave.com/19Z
I don't want to add a picture.. Take too long
The red thing is the view i add. It's supposed to be a rectangle. But it's cuted, on 2.2 | 2.3 ..
)
Thanks for help
EDIT:
I found a way to fix it.
How ?
I added a transparent background to the layout containing the views. So it don't resize anymore, it keeps the right size.
(Yep, that's not really logical, but it works)

How to programmatically hide android some, but not all, view items in an XML output?

I'm trying to program a disc golf scoring app on Eclipse for my android phone. I'd like to set it up for up to 6 players, but mostly 2 people will use it for a game. The data is being stored in a sqlite DB, and I am using a SimpleCursorAdapter to populate the data for holes that have already been scored. here is that code:
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{DiscGolfDbAdapter.KEY_HOLE,
DiscGolfDbAdapter.KEY_PAR,
DiscGolfDbAdapter.KEY_TOM_HOLE,
DiscGolfDbAdapter.KEY_TOM_GAME,
DiscGolfDbAdapter.KEY_CRAIG_HOLE,
DiscGolfDbAdapter.KEY_CRAIG_GAME,
DiscGolfDbAdapter.KEY_TOMS_POSITION,
DiscGolfDbAdapter.KEY_SKIP_PLAYER
};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.schole, R.id.scpar, R.id.scth, R.id.sctg, R.id.scch, R.id.sccg, R.id.sctp,
R.id.skip};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.hole_info, notesCursor, from, to);
setListAdapter(notes);
}
From searching the internet I've found what I think are two posibilities that SHOULD work, but do not.
First I've tried the XML Attribute: android.visibility. It looks like this in the PORTION of the view that I am trying to "test" hide:
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android.visibility="GONE">
<TextView android:id="#+id/scch"
android:layout_width="45dip"
android:gravity="right"
android:textSize="25sp"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/sccg"
android:layout_width="45dip"
android:gravity="right"
android:textSize="25sp"
android:layout_height="wrap_content"/>
</LinearLayout>
I have tried it with "GONE", "Gone" and "gone". NONE of them work in the eclipse emulator OR on my actual phone. So, there is no point in trying to parameterize this attribute.
Next I've tried setting the XML attribute for android:layout_height to "0dip". This indeed works in the emulator and on my phone WHEN IT IS HARDCODED.
Then I moved to the next logical step (as I see it), storing a parameter in the DB so that I can "show" or "not show" the item DEPENDING on conditions within the record. So, I've stored a field in the DB with two values "0dip" and "wrap_content". I pass these to the layout as shown in the java above as R.id.skip. I've also added these to the output just to audit that they are really there. Here is that XML:
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="#+id/skip">
<TextView android:id="#+id/scch"
android:layout_width="45dip"
android:gravity="right"
android:textSize="25sp"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/sccg"
android:layout_width="45dip"
android:gravity="right"
android:textSize="25sp"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="#+id/skip"
android:gravity="center"
android:layout_width="315dip"
android:textSize="10sp"
android:layout_height="wrap_content"/>
In the above test, both via the Eclipse emulator and my android phone, the last TextView confirms that the DB contains either "0dip" or "wrap_content", BUT the LinearLayout with:
android:layout_height="#+id/skip">
behaves as if it were "0dip" ALL of the TIME. In other words, I cannot PROGRAMMATICALLY" affect the XML attribute for android:layout_height.
If there is a better/more standard way of accomplishing what I am trying to do, please share - BUT BE CLEAR. I am new, so CODE EXAMPLES wwill work best for me.
May 29th - It seems to me (based on testing) that you cannot alter layout attributes for the layout specified in this code:
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.hole_info,
notesCursor, from, to);
setListAdapter(notes);
Anything I try leads to some error ort another. So, I've seen examples of custom list adapters where these attributes are altered, so I'm trying to convert to a custom list adapter.
Why not do it in code?
LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
ll.setVisibility(View.GONE);
Your XML layout code
android.visibility="GONE"
should be
android:visibility="GONE"
Change visible of a LinearLayout like Gabriel NeguĊ£ say:
LinearLayout ll =
(LinearLayout)findViewById(R.id.your_layout_id);
ll.setVisibility(View.GONE);
or change height of LinearLayout:
LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
lp.height = 0; // or lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
ll.setLayoutParams(lp);
What about this guy's solution http://enjoyandroid.wordpress.com/2012/03/12/customizing-simple-cursor-adapter/
Kind of worked for me.

Android: Context menu doesn't show up for ListView with members defined by LinearLayout?

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

Categories