how to wrap the width of gridview item dynamically in android - android

I am developing an application in android.i want to set the gridview item dynamically as shown in the below image.someone suggest with a solution .

You can use RecyclerView with StaggeredGridLayoutManager instead of GridView. You can find a good tutorial here StaggeredGridLayoutManager

You can control the size of your item from within the grid adapter - more specifically in your overridden getView() method :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(ctx).inflate(R.layout.grid_item, null);
// ...
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.
WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
lp.width = getResources().getDimensionPixelSize(R.dimen.card_width);
lp.height = getResources().getDimensionPixelSize(R.dimen.card_height);
v.setLayoutParams(lp);
return v;
}

Related

How to get font size in list view and set bigger for a specific item programmatically

The size of a textview in a listview is 19SP. I want to set the first position item's font size bigger, e.g. 21SP. How can I do it programmatically?
Thanks in advance!
You need to adjust the getView method from your adapter to suit your needs.
The method getView as defined here provides you with the position of the item in the dataset you are trying to display.
All you need to do is customize the inflated view based on the position like you are asking
getView(int position,
View convertView,
ViewGroup parent){
//inflate the view
View v = LayoutInflator.from(parent).inflate(R.layout.your_layout,parent,false);
TextView txt = (TextView) v.findViewById(R.id.your_txt);
if(position == 0){
txt.setTextSize(//get the dimen from resources);
}
return v;
}

Android - add Layout views into ListView

the problems is, that I wan't to make ListView with elements which are containing image, description and two buttons. I'm making them in my own BaseAdapter extension, but fragment which is containing ListView is closing (wihtout errors in logcat..). I've found, that ListView is working well, when I'm not returning layout-type elements. So there is my sample with 'sample linear layout', which is not working.. Is there any possibility, to show layouts in ListView?
Here is my code:
Creating part:
lv = (ListView) getView().findViewById(R.id.main_wall_ambajes_lv);
AmbajAdapter aa = new AmbajAdapter(getActivity().getApplicationContext(), StaticData.ambajes);
lv.setAdapter(aa);
My getView method from adapter:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(getActivity());
iv.setImageBitmap(placeholderBitmap);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
return ll;
}
I don't know why you don't have any error however I don't think you proceed the correct way.
Usually you create the layout in the xml file of the layout folder and only inflate it in the getView(), for example as follow :
private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = mInflater.inflate(R.layout.your_custom_layout, parent, false);
}
//your code for setting the image or other things goes here
//for example if you have a textView
TextView textView = (TextView) view.findViewById(R.id.my_textview_id);
textView.setText("my custom text for this cell");
return (view);
}
and your_custom_layout is simply the xml file of your layout.
Note that for performance reason due to cell recycling I only inflate the view when it is null and I only read once the LayoutInflater context and put it in mInflater. However for the best performance you should use a ViewHolder, but it is out of the scope of your question.

Prevent ListView from Trying to Recycle Footer View

I'm using addFooterView to add a footer view to a ListView, which is
populated by a CursorAdapter controlled by Loader. Sometimes the
ListView tries to recycle the footer view however (through
CursorAdapter.bindView). This causes either ClassCastException (if
I allow the recycle) or some item views shown as footer view (if I do
not allow the recycle).
If I understand it right, the footer views added by addFooterView are
not supposed to be recycled ("Footer views are special views at the
bottom of the list that should not be recycled during a layout").
So this is probably a bug in Android APIs.
Is there any way to work around this problem? What is the correct way
to add footer views to a ListView populated by CursorAdapter?
Some relevant code:
In the activity:
paletteView = (ListView)findViewById(R.id.palette);
paletteView.addFooterView(new PaletteAdapter.NewSlot(this));
paletteAdapter = new PaletteAdapter(this, null);
paletteView.setAdapter(paletteAdapter);
getLoaderManager().initLoader(0, null, this);
In the adapter (PaletteAdapter):
#Override public void
bindView(View view, Context context, Cursor cursor)
{
if (view instanceof NewSlot)
{
Log.wtf(TAG,
("Recycle NewSlot to ID "
+ cursor.getLong(cursor.getColumnIndex
(DataProvider.Palettes._ID))));
return;
}
final Slot slot = (Slot)view;
// Blah blah...
}
Worked it out by myself.
Thanks to this answer, I overrode the getView function as
following and everything works well. Thanks again to Abhinav. I
should look more into the source code when having problems.
#Override public View
getView(int position, View convertView, ViewGroup parent)
{
if (convertView instanceof NewSlot)
return super.getView(position, null, parent);
else return super.getView(position, convertView, parent);
}
Just in case it helps anyone else, I had the same symptom (where the listview was trying to recycle the footer view), and the reason for this is that I was updating the height of the footer view with this code:
private void setFooterViewHeight(int height) {
LayoutParams layoutParams = new ListView.LayoutParams(LayoutParams.MATCH_PARENT, height);
mFooterView.setLayoutParams(layoutParams);
}
What I didn't realise is that the ListView.LayoutParams is where the viewType is cached, and when I create a new LayoutParams, it's being reset to 0, which means that it's eligible for the view recycle process
What I have now instead is:
private void setFooterViewHeight(int height) {
LayoutParams layoutParams = mFooterView.getLayoutParams();
if(layoutParams == null) {
layoutParams = new ListView.LayoutParams(LayoutParams.MATCH_PARENT, height, ListView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER);
mFooterView.setLayoutParams(layoutParams);
} else {
layoutParams.height = height;
mFooterView.requestLayout();
}
}

Scrolling child view in gallery

Each item in my gallery is a custom view. One of the child's view is a gridView. When I'm scrolling the gallery everything works fine, but it wont scroll by touching the gridView. Its difficult to explain, I hope someone will understand me!
Touching and scrolling any part of the custom view suppose to trigger the scrolling? Or maybe only the imageView will trigger the scrolling?
public View getView(int position, View convertView, ViewGroup parent) {
final SubProduct subProduct=subProducts.get(position);
int quantity=subProduct.getQuantity();
int size=subProduct.getSizes().get(0).getWidth();
String productName=subProduct.getProductName();
int productPrice=subProduct.getSizes().get(0).getPrice();
int columnWidth = 0;
View view=convertView;
if (view==null){
holder=new SubProductHolder();
//The main container
holder.myLinearLayout= new LinearLayout(this.myContext);
holder.myLinearLayout.setOrientation(1);//vertical
//The custome button
LayoutInflater inflater = (LayoutInflater)
myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=(View)inflater.inflate(R.layout.custom_sub_product_layout, null);
LinearLayout btnLayout = (LinearLayout)v.findViewById(R.id.customSunProductButtonLayout);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(PRODUCT_BOTTUN_WIDTH,PRODUCT_BOTTUN_HEIGHT);
btnLayout.setLayoutParams(llp);
tvSubProductDescription=(TextView)v.findViewById(R.id.tvSubProductDescription);
tvSubProductPrice=(TextView) v.findViewById(R.id.tvSubProductPrice);
holder.btnProduct=btnLayout;
holder.btnProduct.setGravity(Gravity.CENTER);
holder.TvLinearLayout= new LinearLayout(this.myContext);
holder.TvLinearLayout.addView(holder.btnProduct);
//frame
holder.image = new ImageView(this.myContext);
holder.image.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(FRAME_WIDTH,FRAME_WIDTH);
holder.image.setLayoutParams(llp2);
//grid
holder.grid=new GridView(myContext);
//the number of rows is the round number of quantity sqaure
int numberOfColomns=(int) Math.floor((int) Math.sqrt(quantity));
holder.grid.setNumColumns(numberOfColomns);
holder.grid.setLayoutParams(new RelativeLayout.LayoutParams(PRODUCT_PICTURE_WIDTH,PRODUCT_PICTURE_WIDTH));
}else{
holder=(SubProductHolder)view.getTag();
}
//set text in description tv
tvSubProductDescription.setText(quantity+" "+productName+" "+size+"X"+size);
tvSubProductPrice.setText("$"+productPrice);
//set the grid
holder.grid.setAdapter(new emptySquaresAdapter(quantity,myContext,columnWidth,columnHeight));
holder.relativeGridLayout=new RelativeLayout(myContext);
holder.relativeGridLayout.addView(holder.image);
holder.relativeGridLayout.addView(holder.grid);
//set button and picture to layout
holder.myLinearLayout.addView(holder.relativeGridLayout);
holder.myLinearLayout.addView(holder.TvLinearLayout);
return holder.myLinearLayout;
}
static class SubProductHolder
{
ImageView image;
GridView grid;
RelativeLayout relativeGridLayout;
LinearLayout btnProduct;
LinearLayout TvLinearLayout;
LinearLayout myLinearLayout;
}
You have to use TouchListeners for galleryView since on clicking on its child view will not trigger scroll for galleryView
You have to use TouchListeners. Also, you're better off going with a ViewPager and using fragments. To my knowledge, Gallery is deprecated.

About the spinner's height in Android

How can I set the height of the spinner? I mean the dropdown items's height.
For I found in SDK12 my spinner will show a long long item list to show the dropdown items.
But it shows to long , how can i set the height?
You can call setDropDownViewResource method and pass corresponding layout, on ArrayAdapter.
In this example im setting simple_spinner_dropdown_item resource which is higher than regular and has a radiobutton. I think you can pass your custom layout to.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
And in case you are making custom layout, you'll have to declare TextView as root element in order to adapter fill it with text.
Maybe it could help..
ArrayAdapter yourSpinnerAdapter = new ArrayAdapter(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; // set the height
convertView.setLayoutParams(p);
return convertView;
}
};

Categories

Resources