Is there a way to force GridView to only be a single row? Right now by default it will extend its height to accommodate all views supplied by its adapter
You Should combine a horizontal SCROLLVIEW and a LINEARLAYOUT and a GRIDVIEW to achieve what you want!
put grid view in linearlayout and put the linear layout in the horizontal scroll view;
then when setting adapter! count the number of data!
after that you should calculate the desired width to show all your items!
for example you want to show 8 item! each width is 100dp . so the desired width would be 800dp!
then you should add these lines of code
yourGridView.setNumColumns(8);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(800, ViewGroup.LayoutParams.MATCH_PARENT);
yourGridView.setLayoutParams(lp);
dnt forget to set the width of the linear layout as WRAP_CONTENT in the xml!
*** IMPORTANT NOTE
as i know, by doing this, gridview can't garbage collection because Scroll View doesn't support such a thing and your grid view nested in it!
so dnt use this method for lots of images or you will get HEAP SIZE error!
Related
I'm inflating views inside a linearlayout dynamically, however once the linear layout reaches the end of the first row, it cuts off the rest and doesn't start on the second row.
for(int a = 0; a < mSkills.get(i).size(); a++){
View singleSkill = LayoutInflater.from(mContext)
.inflate(R.layout.singleskill, holder.mSkillLayout, false);
TextView skillText = singleSkill.findViewById(R.id.singleskilltext);
skillText.setText(mSkills.get(i).get(a));
holder.mSkillLayout.addView(skillText);
}
For the linear layout I have it set to wrap_content for the height:
<LinearLayout
android:id="#+id/ll_skills"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/tv_description"
android:layout_margin="16dp"/>
I've tried setting it to a defined height e.g 300dp however that doesn't work either.
How can I make the layout start on the second row, once the first is full?
Linear Layout can either fill views horizontally or vertically so the 2nd row you are expecting cant to be done with linear layout only. you can try a horizontal scroll view for that to scroll horizontally. For the exact view-like flow that you described, you can use this 3rd party https://github.com/nex3z/FlowLayout
It can manage the flow of your dynamically inflated view such as if there is no space in the first line then it will put the next view in the second line.
also, you can use material design library chips https://material.io/components/chips/#usage
LinearLayout works exactly how it has to be because you specify it as horizontal. For such behavior, you need RecyclerView With GridLayoutManager or create your own layout;).
Actually it's doing exactly as it should be, LinearLayout is Linear!, and place its subviews in a single horizontal or vertical row.
My advice to you is that create dynamic horizontal LinearLayout as you already doing with TextViews. and put every 3 or 4 textviews (depending on screen size) inside it.
and put all LinearLayouts inside one vertical LinearLayout...
Of course in your case, it's not a good idea, the best thing you can do is to use recycler view. but I consider you have problem with that.
how can i mange Textview in layout.? please help me..
how can i set textview and border then again textview.
Just create a horizontal LinearLayout, and add an ImageView, a TextView, a View with width 2dp and height match_parent, and a TextView as children.
Either repeat that for every row, or use the recommended approach of using a ListView with the above mentioned layout as rows.
I'd use two compound TextViews: Just create a horizontal LinearLayout, and add a TextView with drawableLeft="#drawable/icon_location" and another one with drawableLeft="#drawable/line_vertical" as children.
For both, a drawablePadding="8dp" or whatever you like best
I have my ListView with image background (it's actually a fragment) but still i have set the background with: getListView().setBackgroundResource(R.drawable.table); I noticed that every time this list shows up bacground image is allways like "fill_parent" also i noticed that listview is fill_parent too but i can fix that with getListView().setPadding() but i cant crop background image. Is there any other way to do it? I need background view with aditional buttons...
the Method setPadding is just affecting the content of the ListView, but not it's container e.g. the Background. So basically the padding produces unused space inside the ListView. If you want to restrict the ListView you can use the margin attribute:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Another and in my opinion better way to hava a ListView with a Button below is:
Use a LinearLayout with orientation vertical. Add the ListView and the Button to this layout.
The ListViews height is fill_parent and the Button's wrap_content. Now add to both the layout_weight with value 0 for the Button and 1 for the ListView. The result is that the Button is drawn at the bottom of your screen and the ListView just can expand to the rest of the screen (also its background).
My android page has 10 EditText and 10 TextView. but there is no space in my screen in the Graphical Layout. i just added 5 only. im using Scroll layout. how to add additional 5 items in the screen without reducing the items height. Is there any coding here.?
<ScrollView>
<LinearLayout>
<TextView/>..
....
..
</LinearLayout>
</ScrollView>
You can add multiple items inside a LinearLayout. Since ScrollView is scrollable it won't affect the dimensions of the Views inside. You can add as many views as you need without worrying about screen size or View size..
You should add a LinearLayout as the only child inside your ScrollView. Then get a reference to that LinearLayout :
mLayout=(LinearLayout)findViewById(R.id.myLayout);
and then add Views dynamically from code using :
EditText et=new EditText(...);
//....
mLayout.addView(et);
A ScrollView can have only one direct child, so you need to put all the other Views in a Layout, such as LinearLayout and put that layout in ScrollView
Make your ScrollView's height as match_parent and the inner LinearLayout's height as wrap_content. The LinearLayout will stretch according to the number of children inside it and if the height exceeds the height of the ScrollView, the overflow can be seen by scrolling. If the ScrollView and inner Layout both have same height or if ScrollView have larger height than inner Layout, the scrolling won't happen for obvious reason.
How do I define grid-lines for a GridView?
Is there an attribute or do I have to draw my own background with them in it?
There is no such attribute. The easiest thing is to create a subclass of GridView and override dispatchDraw() or onDraw() to do it yourself.
set the background to the view(inflate layout) inside the GridView so that gridview display the line automatically.
You can just set a padding for each cell of the grid view so that that looks like the grid line. Each cell say would be made up of a linear layout.
Another way of doing this would be to have the cell as a linearlayout and have a view inside that with width of say 0.5dp and height as mp and vice-versa for the horizontal line.