Dropdown size adjust according to displayed results - java

I use autoComplete in my project.
Here is the usage of autoComplete:
In Code:
val arrayAdapter = context?.let {
ArrayAdapter<String>(
it, // Context
android.R.layout.simple_list_item_1,
autocompleteOptions // Array
)
}
autoCompleteTextView.setAdapter(arrayAdapter)
// Set an item click listener for auto complete text view
autoCompleteTextView.onItemClickListener =
AdapterView.OnItemClickListener { parent, _, position, _ ->
//some logic
}
In XML:
<AutoCompleteTextView
android:id="#+id/actvSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true""
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:completionThreshold="0"
android:dropDownHeight="200dp"
android:layout_gravity="center_vertical" />
My question is there any way to adjust the size of the dropdown list according to the current options displayed in the dropdown?

You would need to set it manually in code, based on the number of entries in your arrayAdapter:
autoCompleteTextView.setDropDownHeight(arrayAdapter.getCount() * rowHeight)
where you either calculate each row height, or just have it pre-set as a static height

Related

TextView integer numbers as drawable png

I would like to make TextView counter with my own graphic numbers. How can I do this? Example: integer number will be 14356, so I would like to show this number in TextView as a Drawable:
number1.png + number4.png + number3.png + number5.png + number6.png
Can somebody help me with some hint/tips how to code it? I don't need code, just some tips, or hints. I would like to code myself, just don't know how to start this problem.
Thank you very much.
Create a layout file with a LinearLayout and a few ImageViews in it:
<LinearLayout orientation="horizontal" ...>
<ImageView id="#+id/digit_1000" .../>
<ImageView id="#+id/digit_100" .../>
<ImageView id="#+id/digit_10" .../>
<ImageView id="#+id/digit_1" .../>
</LinearLayout>
Use as many ImageView as needed. Then when you update the number, you can do something like this:
private int[] digitDrawables = {R.drawable.digit_0, R.drawable.digit_1, ...}
private ImageView[] digitViews;
private void initialize() {
// Get image views from layout by ID.
int[] ids = new int[]{R.id.digit_1, R.id.digit_10, R.id.digit_100, R.id.digit_1000};
digitViews = new ImageView[4];
for (int i = 0; i < 4; i++) {
digitViews[i] = findViewById(ids[i]);
}
}
private void updateNumber(int number) {
String str = String.valueOf(number);
// Go over each digit in string, updating the image views with:
// -> digitViews[i].setImageResource(digitDrawables[digit]);
// Hide the image views that you don't need with:
// -> digitViews[i].setVisibility(View.GONE);
}
You can start from there.
SO I would suggest doing this with Constraint Layout Horizontal Chain(in packed Configuration) of Image Views.
In the code you can just update a counter within a loop with 1-sec delay if its count down timmer else do it the way you indent to(game points) and if conditions checking if the new number changes 10's place, 100's place ....100000's place and update accordingly.

Grey space in listview when adapter is updated

So i have a listview and adapter set up. When i add an item trough adapter.add() in the oncreate() method everything is fine. But when i add it trough a listener it creates this weird grey space as if the items are underneath the other items:
This is the code that adds the items:
ArrayList<Task> newTasks = new ArrayList<>(Arrays.asList(parser.ParseTasksJson(result)));
for (int i =0; i < 3;i++)
{
Task task = new Task(i, "Item" + i, 0);
tasks.add(task);
}
for(Task t : newTasks)
{
adapter.add(new Task(t.getTask_id(), t.getTask_title(), t.getTask_done()));
}
adapter.add(new Task(99,"Task after forloop", 0));
adapter.notifyDataSetChanged();`enter code here`
And this is my layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar_home"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview">
</ListView>
</LinearLayout>
EDIT: I was not clear enough. The real problem is in the fact that the items from the parser dont get shown in the listview although they are added to the adapter. SEE updated code.
Your code is working properly, however the problem is in this line:
adapter.add(new Task(t.getTask_id(), t.getTask_title(), t.getTask_done()));
Make sure that the values are correct, to me it seems that your data does not contain a valid title, and your layout items have a height of wrap_content. That being said, the items are added, but not high enough since they don't have any content.
That's why you see that weird grey space, they are actually several items, but not big enough to see.

ListView only printing first value from ArrayList

I've tried searching other questions, and most of them are to do with having the listview inside a scrollview, but I have no scrollview so it wasn't of much help. I'm sure it's something small I'm missing but I can't seem to pinpoint it. The below is supposed to print out the 3 animalNames to the listview, but is only printing the first.
JSONArray mainNode = new JSONArray(loadJSONFromAsset()); // call the connection to json
ArrayList<String> animalsArray = new ArrayList<String>();
if(mainNode != null) //puts the values into an array
{
for(int i=0;i<mainNode.length();i++)
{
JSONObject eachObject = mainNode.getJSONObject(i);
animalsArray.add(eachObject.getString("animalName"));
}
//prints array to check
for(String stuff : animalsArray)
{
Log.i("name", stuff);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, animalsArray);
animalsList = (ListView) findViewById(R.id.animalList);
animalsList.setAdapter(arrayAdapter);
}
This prints out the 3 values within my JSON to logcat, so I'm sure it's getting the values, I'm just unsure as to why it's only printing the first position into the list.
In case it's of any use, my current layout is below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/animalList"/>
Any ideas as to why it's only printing the first would be greatly appreciated.
Thanks.
It's because the height of your linear layout correspond to the height of one listView item :
android:layout_height="?android:attr/listPreferredItemHeight"
Set the height with other value (like match_parent if you want full screen height)

Automatic spacing in gridview columns?

I have a simple GridView. The following is the XML
<LinearLayout
android:id="#+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:orientation="vertical" >
<GridView
android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="7"
android:verticalSpacing="2dp"
android:rotationY="180" >
</GridView>
</LinearLayout>
I create a TextView and insert it as an item in the gridview. Basically the idea is to create a customized calendar.
In the adaptor I have
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(mContext);
textView.setText(days.get(position).toString());
textView.setRotationY(180);
textView.setGravity(Gravity.CENTER);
textView.setBackground(mContext.getResources().getDrawable(R.drawable.grey_box));
int x = mContext.getResources().getDimensionPixelSize(R.dimen.calendar_slot);
textView.setLayoutParams(new GridView.LayoutParams(x, x));
return textView;
}
R.dimen.calendar_slot equals to 30dp.
What I fail to understand is , given the above why does my gridview appear like below ? . I need the columns to be merged together. But they have spaces between them.
Can anyone aid ?
The reason why there is a lot of space between the columns of gridview is that the textview in your layout is not occupying the complete column space provided by the grid view
To avoid this problem , you have to calculate the device's screen width and height and divide it by number of columns and rows respectively. This will give you the exact width and height you needed for your single textview.Set this as the dimensions of your text view. You will get equal space between your rows and columns
The code will be as follows
DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
screen_width=displayMetrics.widthPixels; //width of the device screen
screen_height=displayMetrics.heightPixels; //height of device screen
int view_width=screen_width/columns; //width for text view
int view_height=screen_height/rows; //height for text view
textview.getgetLayoutParams().width=view_width;
textview.getgetLayoutParams().height=view_height;

Android list view wrap content until a certain size based on footer size

I am working on an android application.
In an XML Layout I need to do the following:
I have a list view at the top (listViewProducts) , and another Relative view under it (receiptSection).
The list view should take as much space as it has items. And the rest is taken by the receiptSection.
So for example if I have 2 items in the listViewProducts:
The list view is as big as the 2 items and the rest is taken by the receiptView.
If I add another item, the list view now take more space and push the receiptView lower:
However if I add a lot more items, I want the list view height to stop growing to leave a minimum height for the receiptView that cannot go smaller:
As you see in the picture, the receiptVIew has a minimum height of 50dp. once the receipt view get to that height, it should stop shrinking and now the list view has a fixed size based on the remaining of the space. The rest will be scrollable.
What I have tried
I created a list view. I have android:layout_alignParentTop="true" and android:layout_height="wrap_content".
This will make it grow with its content and its at the top of the view
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ListView>
THen I created a RelativeLayout that will hold the checkout_receipt_view that is in a seperate xml layout file.
For this view I have android:layout_alignParentBottom="true" and android:layout_below="#id/listViewProducts" that will make it go under the list view and align with the bottom of the view.
I also used android:minHeight="50d" in order to set the minimum height of the receiptSection.
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/listViewProducts"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>
The listViewProducts is growing with the items, and the receiptView is taking the remaining space correctly.
The problem
however the minimum height did not work. The list view keeps on growing infinitely and the receiptSection will be pushed out of the view.
Is there a way I can make the listView stop growing when the receiptView reaches 50dp?
Thanks a lot for any help.
Unfortunately I think your best bet is to do this by making a custom view that extends ListView and overrides onMeasure.
public class CustomView extends ListView {
#Override
int maxHeight = 0;
View parentView = (RelativeLayout) (or whatever) getParent();
if (parentView != null){
maxHeight = parentView.getHeight() - 50;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Try swapping the 'layout_below'.
What you are actually saying is the following: please put my relativelayout BELOW the listview. If you want your listview to respect the height of the relativelayout, you'll have to say in the listview:
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/receiptSection"
android:layout_alignParentTop="true" >
</ListView>
And your relativelayout:
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>

Categories

Resources