Sliding tab Layout Text splits up to two - android

i am using slidingtablayout and slidingtabstrip, i have 5 tabs but there is one large text like RESTAURANT it is not visible fully.. and if i changed the other tabstext to largetext it is working fine and scrolling even..but not in the first case. i want this.. but with above text on tabs
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.rajdeepsingh.newlistview_module.Search">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:id="#+id/navbut"
android:background="#drawable/arrow"
android:layout_margin="5dp"
/>
<com.example.rajdeepsingh.newlistview_module.SlidingTabLayout1
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundedcorneredittext"
android:layout_margin="16dp"
android:drawableLeft="#drawable/search01"
android:drawableStart="#drawable/search01"
android:id="#+id/searchedittext"
android:hint="Brands, Restaurant, Shops "
android:textColorHint="#bdbdbd"
android:typeface="serif"
/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/white" />
</LinearLayout>
</LinearLayout>

Please Check with this line in your Activity
slidingTabLayout.setDistributeEvenly(true);
change to false

#Aman Verma
Whats your Fault
At first Start your Imageview Tag <ImageView
Set slidingTabsObj.setDistributeEvenly(false); // Yes: To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
SlidingTabLayout to fit the screen

You can use below code to expand your text in tabs.
SlidingTabStrip tabs = (SlidingTabStrip)rootView.findViewById(R.id.tabs);
tabs.setShouldExpand(true);
Hope it helps.

With in your SlidingTabLayout1, Just replace below two methods and then check it.
/**
* Create a default view to be used for mTabs. This is called if a custom tab
* view is not set via {#link #setCustomTabView(int, int)}.
*/
protected TextView createDefaultTabView(Context context) {
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(
android.R.attr.selectableItemBackground, outValue, true);
textView.setBackgroundResource(outValue.resourceId);
textView.setAllCaps(true);
int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources()
.getDisplayMetrics().density);
textView.setPadding(padding, padding, padding, padding);
return textView;
}
private void populateTabStrip() {
final PagerAdapter adapter = mViewPager.getAdapter();
final OnClickListener tabClickListener = new TabClickListener();
for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
TextView tabTitleView = null;
if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate
// it
tabView = LayoutInflater.from(getContext()).inflate(
mTabViewLayoutId, mTabStrip, false);
tabTitleView = (TextView) tabView
.findViewById(mTabViewTextViewId);
}
if (tabView == null) {
tabView = createDefaultTabView(getContext());
}
if (tabTitleView == null && TextView.class.isInstance(tabView)) {
tabTitleView = (TextView) tabView;
}
if (mDistributeEvenly) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView
.getLayoutParams();
lp.width = 0;
lp.weight = 1;
}
tabTitleView.setText(adapter.getPageTitle(i));
tabView.setOnClickListener(tabClickListener);
String desc = mContentDescriptions.get(i, null);
if (desc != null) {
tabView.setContentDescription(desc);
}
mTabStrip.addView(tabView);
if (i == mViewPager.getCurrentItem()) {
tabView.setSelected(true);
}
tabTitleView.setTextColor(getResources().getColorStateList(
R.color.selector));
tabTitleView.setTextSize(14);
}
}
Let me know if this update does not resolve your issue.

Related

Using "toRightOf" programatically when inflating a layout

I have an XML layout looking like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0.1"
android:text="1"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/child"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<!-- Define these into code
<TextView android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="1"/>
<TextView android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/first"
android:layout_marginLeft="5dp"
android:textSize="20sp"
android:text="1"/>-->
</RelativeLayout>
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0.1"
android:text="1"/>
</LinearLayout>
The TextViews inside LinearLayout are not meant to be changed. RelativeLayout also consists of TextViews which are aligned 5dp away from each other. Unlike the parent layout's ones, they need to be defined inside my Java activity. This is what I have tried:
private View getGeneratedView(String[] texts) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View parentLayout = inflater.inflate(R.layout.parent, null);
RelativeLayout childLayout = (RelativeLayout) parentLayout.findViewById(R.id.child);
Integer lastViewId = null;
for (String text: texts) {
TextView displayedText = new TextView(this);
displayedText.setText(text);
displayedText.setTextSize(20);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (lastViewId != null) {
params.addRule(RelativeLayout.RIGHT_OF, lastViewId);
params.leftMargin = 5;
}
displayedText.setLayoutParams(params);
lastViewId = displayedText.getId();
childLayout.addView(displayedText);
}
return parentLayout;
}
But what the code above does is putting all of the TextViews in the center, the one on top of the other like so:
(parameter texts was new String[] {"1", "1", "1", "1"})
And this is what I am trying to get:
Can anyone point out what I am doing wrong?
Even though you are generating a new TextView for each string in texts, you do not generate an id for it. As a result, displayedText.getId() will always be -1 (View.NO_ID) and lastViewId will never be other than -1.
To correct the code, you need to generate a new id for each generated TextView. With API 17+, you can use View.generateViewId() to do this.
private View getGeneratedView(String[] texts) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View parentLayout = inflater.inflate(R.layout.parent, null);
RelativeLayout childLayout = (RelativeLayout) parentLayout.findViewById(R.id.child);
Integer lastViewId = null;
for (String text : texts) {
TextView displayedText = new TextView(this);
// Need API 17+ for generateViewId
displayedText.setId(View.generateViewId());
displayedText.setText(text);
displayedText.setTextSize(20);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (lastViewId != null) {
params.addRule(RelativeLayout.RIGHT_OF, lastViewId);
params.leftMargin = 5;
}
displayedText.setLayoutParams(params);
lastViewId = displayedText.getId();
childLayout.addView(displayedText);
}
return parentLayout;
}

android :Removing random space between dynamically generated ImageView and TextView

I am currently working at developing an app that requires a dynamically generated GridView. In the layout added to the GridView through an adapter I have a LinearLayout that takes information from an ArrayList and then it should display an ImageView followed by a TextView. I have done that programmatically, but some random space appears between the two of them.
I have tried removing the padding, removing the margin, setting the adjustViewBounds of the image to true and also removing the inner padding of the TextView and none of these things work.
Here is the .xml file containing the LinearLayout to be populated:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rounded_text_black">
<ImageView
android:id="#+id/separator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:scaleType="fitXY"
android:src="#drawable/horizontal_white_line"
android:layout_centerVertical="true"/>
<LinearLayout
**android:id="#+id/orderLayout"**
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#id/separator"
android:orientation="vertical"
android:paddingTop="40dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#id/separator"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:scaleType="fitXY"
android:src="#mipmap/effects"
android:layout_gravity="center"/>
<TextView
android:id="#+id/moodText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/smallText"
android:padding="0dp"/>
<TextView
android:id="#+id/tastesText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/smallText"
android:textAllCaps="false"
android:padding="0dp"/>
<TextView
android:id="#+id/tastesText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/smallText"
android:textAllCaps="false"
android:padding="0dp"/>
</LinearLayout>
</RelativeLayout>
Here is the code that generates the image-text pairs:
for(int i = 0; i < 3; i++){
int currentPos = orderSize - i;
if(currentPos >= 0){
LinearLayout container = new LinearLayout(v.getContext());
LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
container.setOrientation(LinearLayout.HORIZONTAL);
container.setGravity(Gravity.RIGHT);
container.setLayoutParams(containerParams);
ImageView img = new ImageView(v.getContext());
LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
imgParams.gravity=Gravity.RIGHT;
img.setLayoutParams(imgParams);
img.setPadding(0,0,0,0);
if(order.get(currentPos).delivered){
img.setImageResource(R.drawable.green_circle);
}
else{
img.setImageResource(R.drawable.yellow_circle);
}
TextView txt = new TextView(v.getContext());
txt.setTextColor(Color.WHITE);
txt.setText(order.get(currentPos).orderItem);
txt.setPadding(0,0,0,0);
container.addView(img);
container.addView(txt);
ll.addView(container);
}
And here is the result:
The red rectangle indicates the extra space.
Thank you very much.
I suggest you to create your line item view (green ball and drink name) in an xml layout, checking that all is working fine on a single line.
Then you can change your for cycle as follows:
for(int i = 0; i < 3; i++){
int currentPos = orderSize - i;
if(currentPos >= 0){
// inflate a new instance of view starting from an xml file
View tableLineView = getLayoutInflater().inflate(R.layout.table_line, null);
// Image view
ImageView iv = (ImageView)tableLineView.findViewById(R.id.ball);
if(order.get(currentPos).delivered){
iv.setImageResource(R.drawable.green_circle);
} else{
iv.setImageResource(R.drawable.yellow_circle);
}
// TextView
TextView tv = (TextView)tableLineView.findViewById(R.id.textview);
tv.setText(order.get(currentPos).orderItem);
// add to LinearLayout
ll.addView(tableLineView);
}
}
I can't test it now, if you find problems ask me.
Here you can read a discussion about view inflating
TextView txt = new TextView(this);
LinearLayout.LayoutParams txtParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
txt.setLayoutParams(txtParams);
txt.setTextColor(Color.BLACK);
txt.setText("Hello World");
txt.setPadding(0,0,0,0);
Use this and you wont see padding anymore!!

Tablayout custom view not taking full height android?

I have a tablayout with 2 tabs. I am using custom view to set the tabs
Tablayout XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabPaddingBottom="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingStart="-1dp"
app:tabPaddingTop="-1dp"
app:tabGravity="fill"
app:tabIndicatorHeight="5dp"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Custom View XML
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:andorid="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#android:color/white"
andorid:text="XXX" />
Java code for tab set up
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null);
tabOne.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.tabText1));
tabOne.setText(tabTitle[0]);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null);
tabTwo.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.tabText2));
tabTwo.setText(tabTitle[1]);
tabLayout.getTabAt(1).setCustomView(tabTwo);
}
This is my output
How do I match the height of the textview to tablayout height?
Had to do it programmatically
ViewGroup.LayoutParams layoutParams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Setting the params to textview
tabOne.setLayoutParams(layoutParams);
instead of this try this layout for full access layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:andorid="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#android:color/white"
andorid:text="XXX" />
</RelativeLayout>
Try to use this.. this is how i customize my tabs
TabLayout tabs = (TabLayout)findviewById(R.id.tabs);
ViewGroup vg = (ViewGroup) tabs.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
// Get TextView Element
if (tabViewChild instanceof TextView) {
// change font
((TextView) tabViewChild).setTypeface(tf);
// change color
((TextView) tabViewChild).setTextColor(getResources().getColor(R.color.white));
// change size
((TextView) tabViewChild).setTextSize(18);
// change padding
tabViewChild.setPadding(0, 0, 0, 0);
//..... etc...
}
}
}
Set height of tablayout wrap_content and set textview height 60dp/

Setting layout weight to dynamically added view is not working in Android

I am developing an Android App. In my app, I am adding view dynamically and setting its weight in code as well. But it is not working.
This is my XML for container of dynamic views:
<ScrollView>
.
.
.
<LinearLayout
android:orientation="vertical"
android:id="#+id/id_related_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
.
.
.
</ScrollView>
This is how I am adding controls dynamically to it.
private void addRelatedItemViews(final ArrayList<Item> relatedItems)
{
LinearLayout subContainer= new LinearLayout(this);
LinearLayout.LayoutParams initialParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(initialParam);
relatedItemsContainer.addView(subContainer);
for(int i=0;i<relatedItems.size();i++)
{
if(i>0 && i%2==0)
{
//initialize sub container
subContainer = new LinearLayout(this);
LinearLayout.LayoutParams subContainerParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(subContainerParam);
relatedItemsContainer.addView(subContainer);
}
final int index = i;
View view = layoutInflater.inflate(R.layout.realated_item_view,null);
ImageView imageView = (ImageView)view.findViewById(R.id.iv_related_item_image);
TextView tvName = (TextView)view.findViewById(R.id.tv_related_item_name);
TextView tvPrice = (TextView)view.findViewById(R.id.tv_related_item_price);
TextView tvLikeCount = (TextView)view.findViewById(R.id.tv_related_item_like_count);
Picasso.with(getBaseContext()).load(relatedItems.get(i).getMediumImageUrl()).into(imageView);
tvName.setText(relatedItems.get(i).getName());
tvPrice.setText(CommonHelper.formatCurrency(relatedItems.get(i).getPrice()));
tvLikeCount.setText(CommonHelper.formatLikeCount(relatedItems.get(i).getLikeCount()) + " like");
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openItemActivity(relatedItems.get(index).getId());
}
});
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
}
}
This is my realated_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:layout_height="wrap_content">
<LinearLayout
android:background="#drawable/item_bg"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_related_item_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:textSize="17dp"
android:id="#+id/tv_related_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="13dp"
android:id="#+id/tv_related_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="10dp"
android:id="#+id/tv_related_item_like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
But it is not settings its weight properly. Actually, each row has two column and each column must be half of screen width. But it is not working.
This is the screenshot:
As you can see, it not taking screen half by half. How can I fix it?
Try this below in your realated_item_view.xml (wrap_content to fill_parent)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_weight="1"
android:padding="3dp"
And change your Java code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
But maybe your image will be stretched by the width. But it will fill half of the width as you want.
UPDATE:
I dont know why you tried failed with above solution. So I write a small demo for referencing. It uses ScrollView as you want. But I strongly recommend you should use GridView instead for better perfomance!!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < 4; i ++){
LinearLayout subLayout = new LinearLayout(this);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
for (int j = 0; j < 3; j ++){
View v = inflater.inflate(R.layout.test_item_layout, null, false);
if (j == 1){
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.icon);
}
subLayout.addView(v, subParams);
}
containerLayout.addView(subLayout, params);
}
}
test_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="100dip" />
<TextView
android:text="Test label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
test_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
Screenshot:
Instead of ViewGroup.LayoutParams try it with LinearLayout.LayoutParams and also set layout width to 0
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
It might work.
try this
use straggerdgridlayout manager
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(column, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
column replaces your requirement of column

Android LinkedList empty deque

I'am trying to populate 2 LinearLayout to achieve something like this:
This is a simple test and the code it's not fully implemented to do what i want.
The problem is after the first pool the linkedList it's empty, and the size before the pool was 2.
My Layout to populate each column space.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/latest_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/latest_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="#drawable/card_w_shadow"
android:onClick="onTopicsPreviewClick"
android:orientation="vertical"
android:paddingBottom="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="#+id/latest_preview_title"
style="#style/CardTitle_small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="4dp"
android:background="#color/C_Moodle_Orange" />
<LinearLayout
android:id="#+id/latest_description_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="4dp" >
<TextView
android:id="#+id/latest_preview_description"
style="#style/CardText_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:ellipsize="end"
android:maxLines="4"
android:text="Lorem ipsum dolor sit amet" />
</LinearLayout>
</LinearLayout>
My fragment:
public class FragLatest extends Fragment {
#SuppressWarnings("unchecked")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = getActivity().getApplicationContext();
ManContents content = new ManContents(context);
// The Linked List, in this example is size =2 and it contains
// Mancontents objects
LinkedList<ManLatest> latestList = (LinkedList<ManLatest>) new ManDataStore(
context).getData("Latest");
// The Vertical LinearLayout
LinearLayout outerLayout = new LinearLayout(context);
outerLayout.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT));
outerLayout.setOrientation(LinearLayout.VERTICAL);
outerLayout.addView(setLayoutTitle(context));
// Get from resource the number of cards per line
int cardsPerLine = getResources().getInteger(
R.integer.latest_item_per_line);
// Calculate the number of rows to build
int lines = setRows(latestList, cardsPerLine);
for (int i = 0; i < lines; i++) {
// The Horizontal Linear Layout
LinearLayout innerLayout = new LinearLayout(context);
innerLayout.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT));
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j < cardsPerLine; j++) {
ManLatest latest = latestList.poll();
Long topicId = Long.parseLong(latest.getTopicId());
String courseId = latest.getCourseId();
MoodleCourseContent[] courseContent = content
.getContent(courseId);
MoodleCourseContent topic = content.getTopic(topicId,
courseContent);
View view = inflater.inflate(R.layout.frag_latest, null);
view.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1f));
TextView title = (TextView) view
.findViewById(R.id.latest_preview_title);
title.setText(topic.getName());
TextView description = (TextView) view
.findViewById(R.id.latest_preview_description);
description.setText(topic.getSummary());
innerLayout.addView(view);
}
outerLayout.addView(innerLayout);
}
return outerLayout;
}
/**
* #param latest
* #param cardsPerLine
* #return
*/
private int setRows(LinkedList<ManLatest> latest, int cardsPerLine) {
int colums;
if (latest.size() > cardsPerLine) {
Double count = Math.ceil(latest.size() / 2.0);
colums = count.intValue();
} else {
colums = 1;
}
return colums;
}
/**
* #param context
* #return
*/
private TextView setLayoutTitle(Context context) {
TextView large_title = new TextView(context);
large_title.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
large_title.setText("Latest Contents");
large_title.setTextAppearance(context, R.style.CardsLayoutTitle);
// large_title.setTypeface(null, Typeface.ITALIC);
large_title.setPadding(30, 10, 0, 10);
return large_title;
}

Categories

Resources