Spinner dropdown height to match parent - android

Is it possible to adjust height of Spinner dropdown to fill the entire screen. Right now i have 3 items that i dynamically add into the spinner adapter but these only cover half of the screen. What i have right now is something like this:
What i want is something like this:
I could add empty items but that won't solve the problem for different screen sizes
I tried to implement a style on the spinner but it didn't work
<style name="MyCustomSpinner" parent="Widget.AppCompat.Spinner.DropDown">
<item name="android:dropDownHeight">match_parent</item>
</style>
UPDATE
I have a view between action bar and spinner so i cannot use layout_weight=1 for my spinner

try using this with your adapter.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

The only way to achieve this is by writing your own view that works like a spinner.

In yourspinner.xml,
add the android:minHeight="48dp" to TextView element. see the example below-
<TextView
android:id="#+id/textViewRowFacility"
android:minHeight="50dp" />

Try to put your Spinner in LinearLayout & set spinner weight to 1 like
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

Set the weight of the spinner to take the entire space. If no other view is there in the Layout then use this code:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
To learn more about android weight refer to this:
What does android:layout_weight mean?

Try using the code below
ArrayAdapter<String> yourSpinnerAdapter = new ArrayAdapter<String>(this,
R.layout.spinner_item, yourItem) {
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
convertView = super.getDropDownView(position, convertView,
parent);
convertView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams p = convertView.getLayoutParams();
p.height = 100; // assign the required height here
convertView.setLayoutParams(p);
return convertView;
}
};

Related

Custom ListView set Background Drawable in XML doesnt work

I'm getting frustrated about this:
When I define a custom ListView Layout,
Android Studio doesn't keep the background drawable I set in there.
Tried many things, and setting background programmatically doesn't work
since it's ignoring the layout_width which must be set to "wrap_content".
Actual style of background
Result without coding
If anyone could help me, I'd be very grateful !:)
EDIT:
I'm creating a Messenger and I want to display messages in a similar way to WhatsApp, where messages are shown in a listView. Depending on message is sent or received, items should be aligned ParentStart or ParentEnd.
But more importantly, if a message only contains a few chars, I don't want the ListItem Background to fill the entire screen, so it should be set dynamically.
I thought I could achieve this through simply setting wrap content in the parent layout file.
Files look like this:
<?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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentEnd="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
tools:background="#drawable/background_message_sent">
//Here are TextViews
</LinearLayout>
</RelativeLayout>
Here is my ListViewAdapter, where I set background
(#drawable/background_message_sent/received) programmatically.
However, this covers the entire width of ListView, regardless of message length.
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
int currentUserID = 1;
int senderID = messagesArrayList.get(pos).getSenderID();
if (senderID == currentUserID){
View v = View.inflate(context, R.layout.layout_chat_message_sent, null);
v.setBackgroundResource(R.drawable.background_message_sent);
TextView tvMessageText = v.findViewById(R.id.tvMessageText);
TextView tvTimeStamp = v.findViewById(R.id.tvTimeStamp);
tvMessageText.setText(messagesArrayList.get(pos).getMessageText());
tvTimeStamp.setText(messagesArrayList.get(pos).getTimeStamp());
return v;
}
else {
View v = View.inflate(context, R.layout.layout_chat_message_received, null);
v.setBackgroundResource(R.drawable.background_message_received);
TextView tvMessageText = v.findViewById(R.id.tvMessageText);
TextView tvTimeStamp = v.findViewById(R.id.tvTimeStamp);
tvMessageText.setText(messagesArrayList.get(pos).getMessageText());
tvTimeStamp.setText(messagesArrayList.get(pos).getTimeStamp());
return v;
}
}
Well, after trying, I got the solution if anyone comes to this point:
You have to set your background drawable directly for each TextView, not for Parent Layouts.
These two Lines finally solved everything ^^
android:maxEms="14"
android:background="#drawable/background_message_sent"
<TextView
android:id="#+id/tvMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginEnd="0dp"
android:layout_marginTop="5dp"
android:layout_weight="0.22"
android:text="42456456456546"
android:textColor="#color/tentakelPrimary"
android:maxEms="14"
android:background="#drawable/background_message_sent"/>

Android ListView with Title

I want to add a title bar in the list view.
I have added a text view inside the listview which can act as title:
Layout File :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/layout_rounded"
android:layout_marginRight="10px"
android:layout_marginLeft="10px"
android:layout_marginTop="30px"
android:layout_below="#id/linlaypay"
android:orientation="vertical"
android:padding="10dip" >
<ListView
android:id="#+id/rectran"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout >
Here is the code used to add the title listview dynamically:
listView = (ListView) findViewById(R.id.rectran);
TextView textView = new TextView(getApplicationContext());
textView.setText("header view");
listView.addHeaderView(textView);
The problem is ,an emty text view isadded to the listview without the text "header view".
Why?Am I missing anything?
I don't believe you can just add a view like this. The view being passed into listView needs to be inflated.
create a header.xml layout file with the text view in it with the text set.. and then run the code below.
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(header, null, false);
If I were you, I would add a title to the ActionBar, assuming that your ListView is the whole Activity. If it isn't the whole Activity or it doesn't take up the whole screen, you could add the title to the layout rather than dynamically adding it.
http://developer.android.com/reference/android/app/ActionBar.html
Check out the Android Developers documentation on the ActionBar
My activity holds 3 small listviews and I wanted to give title to each of the view by adding textview.
However the text in the Textview was not visible because the size of the text was too small.
setting the text size of the textview resolved the issue.
Thanks !!

How to put divider at particular position in an Android list view

I need to make a list view in which I want to have a divider at some position only not after every list item. I am using custom list view.
Is there any solution of this problem?
you can you this xml file in list adapter class like
ItemsAdapter ItemsAdapter = new ItemsAdapter(EnterpriseFertilisersScreen.this,
R.layout.list, Constant.FERTILIZERMANAGERARRAY);
R.layout."below xml file " and user as further white color.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#color/list_bg" >
<TextView
android:id="#+id/post"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="7dp"
android:layout_toRightOf="#+id/bite_image"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textSize="20sp" />
</RelativeLayout>
If you have another issues, ask feel free..
You can create Adapter for list, which will be place dividers as elements (via getView).
This is standart android approach
If you have used Custom ListView to show your .you need to make position where you need to show different View from xml by condition .you should have to do this in getView Method.
Either you need to see how this example use divider using CursorAdapter.
Check this https://github.com/cyrilmottier/ListViewTipsAndTricks/blob/master/src/com/cyrilmottier/android/listviewtipsandtricks/SectionedListActivity.java
you can add it to getview Methoid as follow :
public View getView(int position, View convertView, ViewGroup parent) {
if(items.get(position).get("name").startsWith("-")){
View divider = mInflater.inflate(R.layout."yourlayout",null);
return divider; }
also, you must add item names starting with "-" where you want to add a divider.
Hope this helpful

Can a GridView have a footer and header just like ListView?

A quick question:
In ListView I use this code:
list.addHeaderView(headerView);
How to deal with it when working on gridview?
Thanks.
There is no support for header or footer views with GridView, sorry.
There is a quite good implementation of GridView with header support in Google Photos application, as it's OSS code you can use it as is or take it as a reference for your own implementation:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.3_r2.1/com/android/photos/views/HeaderGridView.java
Basic idea is quite simple - WrapperAdapter creates an fake row by increasing number of items by number of columns and then return a header view for the item.
You can use this. The footer appears/hides at the bottom of the grid when you reach/leave the last number of items. It does not actually scroll, but I hardly notice the difference.
In your activity/fragment's onCreate/onCreateView you add an OnScrollListener to the GridView:
....
GridView gridview = (YMAnimatedGridview) v.findViewById(R.id.my_gridview);
gridview.setAdapter(adapter);
final View footerView = mainView
.findViewById(R.id.my_grid_footer_view);
gridview.setOnScrollListener(new GridView.OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount) {
// last item in grid is on the screen, show footer:
footerView.setVisibility(View.VISIBLE);
} else if (footerView.getVisibility() != View.GONE) {
// last item in grid not on the screen, hide footer:
footerView.setVisibility(View.GONE);
}
}
#Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
}
});
Your layout should look something like the below. Notice the layout_weight (and layout_height) parameter in the gridview, it is needed to make the correct space for the footer when it becomes visible.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/my_gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:columnWidth="160dp"
android:gravity="center_horizontal"
android:horizontalSpacing="12dp"
android:numColumns="auto_fit"
android:layout_weight="1"
android:stretchMode="columnWidth"
android:verticalSpacing="6dp" />
<TextView
android:id="#+id/my_grid_footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:visibility="gone"
android:text="footer text here" >
</TextView>
</LinearLayout>
Sample code:
GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) v.findViewById(R.id.ly_image_list_grid);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View headerView = layoutInflater.inflate(R.layout.test_header_view, null);
View footerView = layoutInflater.inflate(R.layout.test_footer_view, null);
gridView.addHeaderView(headerView);
gridView.addFooterView(footerView);
Gradle build:
.
compile 'in.srain.cube:grid-view-with-header-footer:1.0.12'
You'd have to use a ListView, then make each row of the list look like it's actually a row of a grid. So if you have a 3 column grid, you'd make a layout for the ListView that looks like 3 columns. You'd then have to modify certain aspects of the Adapter to make it work so that each ListView row actually represents 3 lines of data -- so you know, getCount()/3 type stuff.
How about checking for the "0" index element in your adapter? You can inflate the custom view for the first one.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view==null){
if(position==0){
// ...inflate header view
}else{
// ...do as usual
Haven't tested it, but should work.
You can use AsymmetricGridView and specify headers/footers with a bigger rowSpan so they would take the entire row.
Why don't you change the appearance of the cells for the first rows? if you know how many columns you have, you know how many items will appear in the header = number of columns.It works for me
You could use this library, http://tonicartos.github.io/StickyGridHeaders/
which allows you to create headers that are sticky (for grouping the list and keeping the header visible for the current group). You can turn off the sticky feature as well.
There is a way to accomplish the desired functionality WITHOUT using a library or anything.
EDIT: Just borrow the HeaderGridView Implementation by google, see Here
You could also customize it for footer. The below suggestion is just too complicated and required more tweaking.
Without going into specific details, all you need to do is this.
1) Subclass GridView
2) override onScrollChanged
3) Calculate the offset everytime it scrolls
4) Set the parentView(view that contains the headerView and gridview) translation y to -Offset.(view.setTranslationY(-offset). Also have an if statement to that once it reaches a certain offset it would stop scrolling.
5) obviously you want to structure this well so your gridview can have a method like attachToGridview(View view). I have a complete implementation of this which works.
See Scroll offset of GridView for help on getting offset since GridView has a bug were the views get recycled.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<com.test.Breadcrumbs android:layout_width="fill_parent" android:layout_height="100dp" />
<GridView
android:id="#+id/grid"
android:numColumns="auto_fit"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp">
</GridView>
</LinearLayout>
and Breadcrumbs:
public class Breadcrumbs extends LinearLayout {
public Breadcrumbs(final Context context, final AttributeSet attrs) {
super(context, attrs);
final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutView = inflater.inflate(R.layout.breadcrumbs, this, true);
works fine, scroll for grid works as well.

Dynamically update list element height in ListView

I have a list.
When you click on element, it becomes selected, and should be expanded vertically to displat some more data.
I am trying to accomplish this by adding to each element's layout "Details" view which is hidden by dafault and is set to VISIBLE onClick.
The only problem that the height of element isn't changed.
If I try to it like this:
holder.line_view
.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, dip(
convertView.getContext(), 30)));
then I get unknown exception.
Just
holder.details_pane.setVisibility(View.VISIBLE);
doesn't work, the size isn't changed.
Please advise.
Layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/line_view">
<TextView
android:id="#+id/desc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Description" />
<TextView
android:text="LALALA"
android:id="#+id/detailed_desc"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
In adapter I try:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
switch (getItemViewType(position)) {
case TYPE_ITEM_EXPANDED:
holder.details_pane.setVisibility(View.VISIBLE);
...
case TYPE_ITEM_NORMAL:
holder.details_pane.setVisibility(View.GONE);
}
"doesn't work, the size isn't changed"
something to try if you allow your view to be rotated: make your view redraw itself and see if the row is bigger.
It might be that you just need to invalidate the listview so that it redraws. Possibly invalidate the row.
maybe it's even as easy as calling notifyDataSetChanged(); on your custom listview adapter
I have no idea about the issue of you code. but you should try this
com.android.widget.ExpandableListView
com.android.widget.ExpandableListAdapter
this is designed for your requirement and shipped with the OS

Categories

Resources