android autocomplete renders as blank list in portrait mode - java

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..

Related

CardView.setRadius() is not working when assigned programmatically

So in my program, I want to generate CardViews dynamically on a predefined Layout that I intend to place inside a LinearLayout with horizontal orientation, which is inside a horizontal ScrollView. Everything in the code works fine except:
cv.setRadius(Tools.convertDpToPx(context,5));
[cv is a CardView Object]
[Tools.convertDpToPx(context,float) resides in Tools as static function and returns translated values in Pixels from DP]
Tried getting cv.getRadius() to check if my values are getting assigned, and they are.
Does anyone have an idea why the setRadius() is not working?
IMPLEMENTATION OF CODE
CardView cv = new CardView(context);
// Set external card view wrapper(cv) properties
CardView.LayoutParams cvlp=new CardView.LayoutParams(Tools.convertDpToPx(context,155),Tools.convertDpToPx(context,200));
cvlp.leftMargin=Tools.convertDpToPx(context,10);
cv.setLayoutParams(cvlp);
cv.setBackground(new ColorDrawable(Color.parseColor("#002038")));
cv.setPreventCornerOverlap(false);
cv.setRadius(Tools.convertDpToPx(context,5));
//And more elements added to Card View Layout
cv.setId(View.generateViewId());
Adding a Background or Background Color to a CardView will remove Rounded Corners.
My solution: add a LinearLayout(or other) inside the Cardview ,change backgroundColor of LinearLayout instead using setBackgroundColor
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp"
>
<LinearLayout
android:id="#+id/video_card_duration_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
...
</LinearLayout>
</androidx.cardview.widget.CardView>
in java code
linearLayout.setBackgroundColor(SurfaceColors.SURFACE_2.getColor(context));

how to set android ImageView visibility inside an included layout?

We are using android API 17 in our application. I have defined a layout containing two images vies as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_container_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_1_resource"/>
<ImageView
android:id="#+id/image_2"
android:layout_marginTop="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image_container_layout"
android:src="#drawable/image_2_resource"/>
This layout is included inside another layout as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/wizard_content_style"
tools:context=".ui.Wizard"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
>
<include layout="#layout/image_container_layout"
android:id="#+id/included_view"
/>
<TextView
style="#style/wizard_content_text_style_medium"
android:id="#+id/text_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/included_view"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="#string/instruction"
android:layout_marginBottom="15dp"/>
The reason that the layout is included is that we want to reuse it in two more layouts.
Now based on some condition I want to hide or show the image views inside image_container_layout.
The java code looks like this:
containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
image1 = (ImageView) containerLayout.findViewById(R.id.image_1);
image2 = (ImageView) containerLayout.findViewById(R.id.image_2);
switch (accuracy) {
case 1:
log().i(getClass().getSimpleName(), "case 1 chosen");
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.GONE);
log().i(getClass().getSimpleName(), "image 1 has been shown");
break;
case 2:
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
break;
case 3:
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.VISIBLE);
break;
}
I am debugging this code and I am sure the code is running. The log messages are printed in Logcat as well, but nothing happens no change in the images. Also, both images are always shown.
I wonder if there is something that I have to do when working with the included layout?
Thanks for any help in advance.
Based on answers I got below, seems that inflating a view will create a new object and because of this, changes in the visibility are not shown on the user interface.
Then the question is that if we have a wizard and inside 3 different pages of the wizard I want to have an image and depending on some condition I want to show or hide the image, what is the best solution? I mean I want to reuse the code which is responsible for hiding and showing the image regardless which page of wizard is active.
Why are you complexing with so much code. If you include some layout in your xml then you can use those widgets also same as the xml have. There is no need to inflate.
ImageView image_2 = findViewById(R.id.image_2);
image_2.setVisbility(Visible.GONE);
You said at this comment the code not inside activity but wherever it is you inflated a new layout to your view currently displaying by this line:
containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
When you try to change visibility of those images actually it works, i think so. But if your activity or fragment layout contains image_container_layout maybe you see
those images.
And I wonder that what do you do with inflated view containerLayout. Do you add it to inside of any other view. If you dont it wont be visible for you.
you have to use it like this:
View included_view1 = findViewById(R.id.included_view1);
ImageView image_1 = included_view1.findViewById(R.id.image_1);
ImageView image_2 = included_view1.findViewById(R.id.image_2);
image_1.setVisibility(View.VISIBLE);
image_1.setVisibility(View.GONE);
image_2.setVisibility(View.VISIBLE)
image_2.setVisibility(View.GONE)
View included_view2 = findViewById(R.id.included_view2);
ImageView image_11 = included_view2.findViewById(R.id.image_1);
ImageView image_22 = included_view2.findViewById(R.id.image_2);
image_11.setVisibility(View.VISIBLE);
image_11.setVisibility(View.GONE);
image_22.setVisibility(View.VISIBLE)
image_22.setVisibility(View.GONE)
Above code will be helpful in the case of multiple time you want to use same layout.

AutoCompleteTextView items' text not visible

I'm loading an AutoCompleteTextView with lots of items but none of them is shown/visible in the popup.
P.s.: If I click somewhere in that empty white view, the text appears in the AutoCompleteTextView (it's white text)
resource.xml
<AutoCompleteTextView
android:id="#+id/reg_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#color/input_register_bg"
android:hint="Città"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/input_register"
android:textColorHint="#color/input_register_hint" />
activity.java
ArrayAdapter<String> citta_adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list_cities);
in_city.setAdapter(citta_adapter);
Most probably the suggestions are white-on-white, which is why you're not seeing them, but pressing on a suggestion works.
See this for a list of possible solutions: AutoCompleteTextview Color set white by default

Spinner is baraly visible

I have a problem, when I run my Android App on the emulator, I can fine see my Spinner, but when I run it on my phone it's hard to see the spinner.
I tried to cut out some color, to see if there is anything in the spinner, but no :( It just show a number but I can fine press the number and it shows the content of my Spinner , but I want to see the background of my Spinner
MainActivity.java
final String[] plHand = getResources().getStringArray(R.array.yourHand_array);
final String[] dlHand = getResources().getStringArray(R.array.dealerHand_array);
final Spinner sp1 = (Spinner) findViewById(R.id.spinPlayer);
final Spinner sp2 = (Spinner) findViewById(R.id.spinDealer);
final ArrayAdapter<String> ar1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, plHand);
final ArrayAdapter<String> ar2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, dlHand);
sp1.setAdapter(ar1);
sp2.setAdapter(ar2);
My XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.black.jack.rechner.MainActivity"
android:background="#drawable/table"
tools:ignore="MergeRootFrame" >
<Spinner
android:id="#+id/spinPlayer"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:textColor="#FFFF00"
android:layout_marginTop="20dp" />
<Spinner
android:id="#+id/spinDealer"
android:layout_width="110dp"
android:textColor = "#000000"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="20dp" />
I tried to change the Textcolor but with no luck, hope you can spot the tiny mistake I made.(Don't worry about the design it's a beta :P)
The styling of components like that can vary from device to device. So, if you want it to stay the same on all devices you can specify a background for it. Something like;
<Spinner
android:id="#+id/spinPlayer"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#FFFFFF"
android:layout_marginTop="20dp" />
The example above will make the background entirely white. If you want a more robust background like the one you linked in some comments, then you'll need to create an image file, store it in your drawable folder and refer to it like so;
android:background="#drawable\your_spinner_background_image"
Seems you need to style it correctly. Now it uses the phone theme whats probably not matching the rest of the color scheme in your application. For a guide styling the spinner see the following website:
http://adanware.blogspot.in/2012/03/android-custom-spinner-with-custom.html

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