AutoCompleteTextView background/foreground color - java

I am working on AutoCompleteTextView.Its working fine but the dropdown text is always white text on white background.
this picture explain my problem
picture explain my problem
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
>
<!-- <AutoCompleteTextView -->
<AutoCompleteTextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Tapez votre 1texte"
android:textColor="#000"
/>
</LinearLayout>
Java:
view = (AutoCompleteTextView)findViewById(R.id.text);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
adapter.setDropDownViewResource(R.drawable.ic_action_search);//dd
view.setAdapter(adapter);

If you want to change the look of the dropdown items change the XML layout you pass to the ArrayAdapter, (in your case this is android.R.layout.simple_dropdown_item_1line).
Lets make a new layout named my_list_item.xml in res/layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/dropDownItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:textColor="#00f" />
This text is smaller, blue, and centered. I don't recommend using this layout, it's more to demonstrate that you can customize it.
Now use this instead:
view = (AutoCompleteTextView)findViewById(R.id.text);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_list_item, data);
view.setAdapter(adapter);
When you set attributes here (like the text color):
<AutoCompleteTextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Tapez votre 1texte"
android:textColor="#000"
/>
You are only changing the box above the dropdown list (the box that you see before you interact with the AutoCompleteTextView). Hope that helps!

This question might be old but I believe this is very important for me to share.
I had been experiencing the same problem as OP but it is because I was using 'getApplicationContext()' in place of 'this'
Wrong
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getApplicationContext(), android.R.layout.simple_list_item_1, PLACES);
Correct
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, PLACES);

A tricky solution for this is:
Add this line
setTheme(android.R.style.Theme); before setContentView() in your activity file in which your autocompletetextview is present. Let me know if this works.

I tried setting up the theme before setcontext, tried different resources parameter in arrayAdapter and tried different theme ,but nothing helped.
Then I changed the context from 'this' to 'getApplicationContext' but the problem was persistent.
Finally I changed the context parameter to "getBaseContext()" and the problem was solved.

i got my solution in slecting layout of "select_dialog_singlechoice
ArrayAdapter<String > s1= new ArrayAdapter<String>
(this,android.R.layout.select_dialog_singlechoice,state);enter image description here
instead of get application context write "this" in ArrayAdapter<>;
try using different layouts u will definately solve your queries

I solved this issue by doing a simple tricky Way,
Just change the Theme of your activity declared in your AndroidManifest.xml as,
<activity
android:name=".YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:windowSoftInputMode="stateHidden"></activity>
and in the Activity as follows,
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.select_dialog_item, arraylist);

Answer by didldum https://code.google.com/p/android/issues/detail?id=5237
workaround by extending the theme and overriding the 2 styles for the typed text and the suggest text:
use an extended theme in your manifest:
...
...
create the new theme (res/values/themes.xml) which uses fixed styles:
...
#style/AutoCompleteTextViewLight
#style/Widget.DropDownItemLight
...
create the styles (res/values/styles.xml) which fix the color:
...
#android:color/primary_text_light
#android:color/primary_text_light

Related

How to show a custom floating option menu in onLongPress and hold item of recyclerView?

I am trying to achieve this view in my native android app in item long-press from the list :
My list is:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/chatList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/chat_toolbar" />
List initialization java code:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(ChatList.this);
chatList.setLayoutManager(mLayoutManager);
chatList.setItemAnimator(new DefaultItemAnimator());
chatList.setAdapter(chatAdapter);
You can use android's Popup Menu and inflate custom layout in this to get your expected result.
This is a reference blog for this, have a look.
or, there are also third-party libraries available
https://github.com/skydoves/PowerMenu
Feel free to ask if something is unclear.

Android Studio - How to programmatically add layout_below param to CardViews inside a PercentRelativeLayout

I am here because I am trying to set programmatically the layout_below parameter of some cardViews without success.
These cardViews are located inside a PercentRelativeLayout (I do not know if it is relevant but these cardviews have been added programmatically too).
I show you the code to make it clear.
...
<android.support.percent.PercentRelativeLayout
android:id="#+id/prl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv0"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv1"
// here I would like to add android:layout_below="#+id/cv0"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv2"
// here I would like to add android:layout_below="#+id/cv1"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
</android.support.percent.PercentRelativeLayout>
...
I tried to do something like this in my Java code but without success.
PercentRelativeLayout prl = (PercentRelativeLayout) view.findViewById(R.id.prl);
CardView cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cv_block, prl, false);
cv.setId(currentId++);
prl.addView(cv);
cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cv_block, prl, false);
cv.setId(currentId++);
RelativeLayout.LayoutParams cv_lp = (RelativeLayout.LayoutParams) cv.getLayoutParams();
cv_lp.addRule(RelativeLayout.BELOW,currentId-2);
/* I have also tried to call cv.requestLayout() but nothing changed */
prl.addView(cv);
As you can see, I am trying to generate cardviews and insert them one below the other.
Thanks in advance for your help.
PercentRelativeLayout is depreciated, so you are encouraged to use other type of layout.
This class was deprecated in API level 26.1.0.
consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:
ConstraintLayout gives lots of opportunities.
More about PercentRelativeLayout deprecation here.
More about ConstraintLayout and how to use it here or video tutorial.

there is some way to make my own item in xml?

i work on android-studio project, and my question is about if
there is some way i can define in xml type of some item that contain
few edit text and buttons, and open listview that contain the item i create?
somthing like :
<item
<Editext(some setting)/>
<Edittext 1(some setting)/>
<Button(some setting)/>
/>
and then some adapter that adjust or something like that, that i can add to ListView.
i saw in youtube some videos that try to explain that but i get stock.. i dno't really get this.
This took my a while to figure out, but it works.
Create your Layout file just however you want. Save it as res/layout/something.txt. Example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:text="hello"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="#+id/text1"/>
<TextView
android:text="world"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="#+id/text2"/>
</LinearLayout>
Use it in your code as follows:
LinearLayout mainLinearLayout = new LinearLayout(getApplicationContext());
// inflate your container
View view = inflater.inflate(R.layout.something, null);
// you can make changes to the elements like this:
TextView txt1 = (TextView) view.findViewById(R.id.text1);
TextView txt2 = (TextView) view.findViewById(R.id.text2);
view.setTag(tag);
// add to main layout
mainLinearLayout.addView(view);
Where tag is an unique integer to identify the View. You don't need to set the tag if you're using the container only once.
I didn't use an Adapter when I made this. I just used a ScrollView to make a continous list of entries. Change this to your needs.

Customized spinner

I have added a spinner in an application which gives the package information in only a word or couple of word. it seems like regular dropdown menu.
I tried to make changes in simple_spinner_dropdown_item.xml file but it does not help and also it is having a radio button which i dont require.
I am done with handling functionality and only needed to change design of that dropdown menu of spinner.
for that i am using checkedTextView like:
service_provider_spinner.setPopupBackgroundResource(R.drawable.ser_menu);
now i don't want that layout rather i like to add a background for each item and also want to set text padding and margin and all that. How can i do this in java file specificly and also in XML.
You can customize the spinner_layout and spinner_dropdown_layout by simply creating an xml layout for both containing only TextView like this as shown.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
And you can apply this xml to your adapter as shown below:
ArrayAdapter<String> adapter= new ArrayAdapter<String>(
getActivity(), R.layout.your_spinnerLayout, yourArray);
adapter.setDropDownViewResource(R.layout.your_spinner_dropdown_layout);
spinner.setAdapter(adapter);

Android: Default layout for listitem containing title and subtitle

I have made a listitem, containing a title and a subtitle. Looking to the result, I think it doesn't look really professional. So that's why I'm asking.
The listitem I'm looking for is pretty common (it's used in a lot of apps i.e. Android's default settings menu and it's also shown when you add a listview in the graphical layout editor tab in eclipse).
So my question is: Where can I find a default layout for a listitem with a title and a subtitle?
Resource id is android.R.layout.simple_list_item_2
Upper text line has id android.R.id.text1 and lower one - android.R.id.text2
Layout is located in the <ANDROID_SDK_ROOT>/platforms/<any_api_level>/data/res/layout folder
OR
You can use TwoLineListItem from the default Android controls list(it is located under "Advanced" tab in Eclipse Layout Editor)
OR
You can build your own layout with anything you like(for example LinearLayout with orientation="vertical" and two TextEdits added
So The Best Way Is:- I took out the simple list item 2 and made an layout in my project and with little editing it took the same layout as you may give in android.R.simple_list_item_2
So the code is:-
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingLeft"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingEnd="?attr/listPreferredItemPaddingRight"
android:paddingRight="?attr/listPreferredItemPaddingRight">
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#+id/text1"
android:layout_alignLeft="#+id/text1"
android:textAppearance="?attr/textAppearanceListItemSmall" />

Categories

Resources