ScrollView not working with fragment - android

I am trying to create a dynamic fragment. I am adding inside of it 30 buttons and I want it to be scrollable.
Here is my xml code for Fragment:
<LinearLayout 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:background="#android:color/holo_orange_light"
tools:context="com.viewpagerexample.app.FragmentA">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<TableLayout
android:id="#+id/table_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
</TableLayout>
</ScrollView>
</LinearLayout>
And this is code for my fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_a,container,false);
// Inflate the layout for this fragment
int numberOfButtons= getArguments().getInt("someInt",0);
linearLayout = new LinearLayout(v.getContext());
view = new TableLayout(v.getContext());
TableRow tr;
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// Inflate the layout for this fragment
view.setOrientation(TableLayout.VERTICAL);
view.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams
.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20,20,20,20);
int numberOfRows = numberOfButtons/2;
int masaCounter = 0;
for (int i = 0;i<numberOfRows;i++)
{
tr = new TableRow(view.getContext());
tr.setLayoutParams(layoutParams);
for (int j= 0;j<2;j++)
{
masaCounter++;
btn = new Button(getActivity());
btn.setBackgroundResource(R.drawable.buttonstyle);
btn.setHeight(200);
btn.setText("Masa" + masaCounter);
btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams
.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1));
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
tr.addView(btn);
}
view.addView(tr);
}
linearLayout.addView(view);
myView = linearLayout;
return myView;
}
I adds buttons but I can't see all of them. Because scrollview is not working. I could not find what i am doing wrong. How can I make it to work?

Change the height,width of parent layout and ScrollView to match_parent and add property of
android:fillViewport="true"
in ScrollView and change the height,width of table layout to wrap_content and fill_parent respectivity will solve your problem.

Try having the ScrollView as rootView on the layout and put LinearLayout and all the other Views inside it.
Like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_light"
tools:context="com.viewpagerexample.app.FragmentA" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/table_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" >
</TableLayout>
</LinearLayout>

Related

how to set inflate layout center which added dynamically in linear layout Android

I am adding inflate layout dynamically in linear layout,i need to set layout center based on number of layouts.suppose if layouts are two , then it start showing from center.
following is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="#+id/hsv_category_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/ll_placeHolder"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
and following are my class
public class ScrollTest extends Activity {
HorizontalScrollView hsv_category_list;
LinearLayout ll_placeHolder;
View layoutView[];
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scroll_test);
hsv_category_list = (HorizontalScrollView)
findViewById(R.id.hsv_category_list);
ll_placeHolder = (LinearLayout) findViewById(R.id.ll_placeHolder);
layoutView = new View[2];
for (int i = 0; i < 2; i++) {
layoutView[i] =
LayoutInflater.from(this).inflate(R.layout.category_list_item, null);
layoutView[i].setId(i);
View parent = layoutView[i].findViewById(i);
TextView tvTime = (TextView)
parent.findViewById(R.id.tv_arrival_time);
tvTime.setText("" + i);
ll_placeHolder.setGravity(Gravity.CENTER);
ll_placeHolder.addView(layoutView[i]);
}
}
}
i need to set the inflate layout start from center.
Please help me to solve this.Thank you.
Set layout gravity to center_horizontal in xml file like this
<LinearLayout
android:id="#+id/ll_placeHolder"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:orientation="horizontal">
</LinearLayout>

Android table align tidy with frozen header

I am trying to create a table with a frozen header. I can achieve that by having a tablelayout which encompasses a scrollview wrapping another tablelayout but my header alignment is out.
Heres the XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/table_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
</TableLayout>
</ScrollView>
</TableLayout>
and heres the code
public class PrepositionRulesActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rules);
init();
}
public void init(){
createTableHeader();
createTableBody();
// alignRows();
}
private Collection<Preposition> load(Context context) {
DataSource<Preposition> dataSource = new DataSourceFactory().createPrepositionDataSource(context);
return dataSource.get();
}
private void createTableHeader() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_header);
tableLayout.addView(createHeaderRow(), 0);
}
private void createTableBody() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_body);
int row = 0;
TableRow tableRow = createHeaderRow();
tableLayout.addView(tableRow, row++);
for (Preposition preposition : load(this)) {
tableLayout.addView(createBodyRow(preposition), row++);
}
}
private TableRow createHeaderRow() {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(getString(R.string.preposition)));
row.addView(createCell(Kasus.Akkusativ.name()));
row.addView(createCell(Kasus.Dativ.name()));
row.addView(createCell(Kasus.Genitiv.name()));
return row;
}
private TableRow createBodyRow(Preposition preposition) {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(preposition.getPreposition()));
row.addView(createCell(preposition.contains(Kasus.Akkusativ)));
row.addView(createCell(preposition.contains(Kasus.Dativ)));
row.addView(createCell(preposition.contains(Kasus.Genitiv)));
return row;
}
private void alignRows() {
TableLayout headerLayout = (TableLayout) findViewById(R.id.table_header);
TableRow headerRow = (TableRow)headerLayout.getChildAt(0);
TableLayout bodyLayout = (TableLayout) findViewById(R.id.table_body);
TableRow bodyRow = (TableRow)bodyLayout.getChildAt(0);
headerLayout.setLayoutParams(bodyLayout.getLayoutParams());
for(int i = 0; i < bodyRow.getChildCount(); i++) {
headerRow.getChildAt(i).setLayoutParams(bodyRow.getChildAt(i).getLayoutParams());
}
}
private View createCell(boolean isEnabled) {
if (isEnabled) {
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.ic_done_black_24dp);
return view;
}
return new Space(this);
}
private View createCell(String text) {
TextView view = new TextView(this);
view.setPadding(15, 0, 15, 5);
view.setTypeface(Typeface.DEFAULT_BOLD);
view.setText(text);
return view;
}
}
I thought I could achieve the alignment by adding a copy of the header row to the body table and than copying its layout to the row added to the header table but that did not work.
Any suggestions would be greatly appreciated
Image showing misalignment:
It would be hard to control the alignment in a tablelayout, because a column's width will always depend and will be affected by another column's width of another row in the same table.
In order to create a table with tidy alignment, I would suggest you use a LinearLayout (for the header and row) and listview to show to items (row).
Example row.xml for both table's header and list's row:
<LinearLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="beginning|middle|end">
<TextView
android:id="#+id/model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:padding="4dp"
android:text="#string/model"
android:textStyle="bold"/>
<TextView
android:id="#+id/color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="#string/color"
android:textStyle="bold"/>
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="#string/status"
android:textStyle="bold"/>
</LinearLayout>
This is how you apply it in your activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<include layout="#layout/row"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Lastly, you inflate the same row.xml for each of your list item in your listview's adapter. Below here is the screenshot of the table created by the above xmls. Kindly note that I have removed the divider and set the textstyle to normal programmatically when showing each row item in the table.

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

IllegalState Call removeView() on the child's parent first

My problem is, i am trying add custom library view to certain layouts. I tried catRow1.removeAllViews(); as specified on error, but it seems to not working. what am i doing wrong?
//Fragment that fills view
monthlyLimit = (LinearLayout) getActivity().findViewById(R.id.monthlyLimit);
catRow1 = (LinearLayout) getActivity().findViewById(R.id.catRow1);
catRow2 = (LinearLayout) getActivity().findViewById(R.id.catRow2);
cat1 = (LinearLayout) catRow1.findViewById(R.id.cat1);
cat2 = (LinearLayout) catRow1.findViewById(R.id.cat2);
cat3 = (LinearLayout) catRow2.findViewById(R.id.cat3);
cat4 = (LinearLayout) catRow2.findViewById(R.id.cat4);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mPieChart = new DrawDonut(getActivity());
monthlyLimit.addView(mPieChart.getChart(), lp);
catRow1.removeAllViewsInLayout();
catRow1.removeAllViews();
cat1.addView(mPieChart.getChart(), lp);
The layout that is added
<?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:weightSum="10"
android:layout_height="match_parent" android:id="#+id/budgetLayout">
<LinearLayout android:id="#+id/monthlyLimit" android:layout_margin = "5dp"
android:layout_width="match_parent" android:orientation="vertical"
android:layout_height="0dp" android:layout_weight="4"/>
<LinearLayout android:id="#+id/catRow1"
android:orientation="horizontal" android:layout_margin = "5dp"
android:layout_width="match_parent" android:weightSum="2"
android:layout_height="0dp" android:layout_weight="3">
<LinearLayout android:id="#+id/cat1" android:orientation="vertical"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent"/>
<LinearLayout android:id="#+id/cat2" android:orientation="vertical"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout android:id="#+id/catRow2"
android:orientation="horizontal" android:layout_margin = "5dp"
android:layout_width="match_parent" android:weightSum="2"
android:layout_height="0dp" android:layout_weight="3">
<LinearLayout android:id="#+id/cat3" android:orientation="vertical"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent"/>
<LinearLayout android:id="#+id/cat4" android:orientation="vertical"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
You can use class LayoutInflater in method onCreateView in Fragment:
View inflate;
LinearLayout monthlyLimit;
LinearLayout catRow1;
LinearLayout catRow2;
LinearLayout cat1;
LinearLayout cat2;
LinearLayout cat3;
LinearLayout cat4;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
inflate = inflater.inflate(R.layout.test_fragment, container, false);
monthlyLimit = (LinearLayout) inflate.findViewById(R.id.monthlyLimit);
catRow1 = (LinearLayout) inflate.findViewById(R.id.catRow1);
catRow2 = (LinearLayout) inflate.findViewById(R.id.catRow2);
cat1 = (LinearLayout) inflate.findViewById(R.id.cat1);
cat2 = (LinearLayout) inflate.findViewById(R.id.cat2);
cat3 = (LinearLayout) inflate.findViewById(R.id.cat3);
cat4 = (LinearLayout) inflate.findViewById(R.id.cat4);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mPieChart = new DrawDonut(getActivity());
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
monthlyLimit.addView(mPieChart.getChart(), lp);
catRow1.removeAllViewsInLayout();
catRow1.removeAllViews();
cat1.addView(mPieChart.getChart(), lp);
}
});
return inflate;
}

Have two LinearLayouts have equal height using layout_weight, not working

I am having difficulty getting these two LinearLayouts nested in a single LinearLayout to have equal height. The first LinearLayout has 0 height while the second takes up the entire screen.
Not sure if it's important but I programmatically populate the second LinearLayout with buttons.
XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yako.mimibot.pages.RemoteCtrlFragment">
<LinearLayout
android:id="#+id/remote_ctrl_ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<LinearLayout
android:id="#+id/terminal_ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/terminal_window">
<ScrollView
android:id="#+id/terminal_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/terminal_rl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/remote_gesture_btns_ll"
android:gravity="center">
</LinearLayout>
</LinearLayout>
</FrameLayout>
Code For Populating Second LinLay (R.id.remote_gesture_btns_ll)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_remote_ctrl, container, false);
mRemoteGestureBtnsLL = (LinearLayout) view.findViewById(R.id.remote_gesture_btns_ll);
mTerminalRL = (RelativeLayout) view.findViewById(R.id.terminal_rl);
String[] mimiGestures = getActivity().getResources().getStringArray(R.array.mimi_capable_gestures_array);
LinearLayout mimiBtnsLL = null;
Button mimiBtn;
for (int i=0; i < mimiGestures.length; i++) {
if (i%2 == 0) {
mimiBtnsLL = new LinearLayout(getActivity());
mimiBtnsLL.setOrientation(LinearLayout.HORIZONTAL);
mimiBtnsLL.setGravity(Gravity.CENTER_HORIZONTAL);
mimiBtnsLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
mimiBtn = new Button(getActivity());
mimiBtn.setText(mimiGestures[i]);
mimiBtn.setHeight(100);
mimiBtn.setWidth(200);
mimiBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mimiBtnsLL.addView(mimiBtn);
if (i%2 == 1) {
mRemoteGestureBtnsLL.addView(mimiBtnsLL);
}
}
return view;
}
Just set the height of parent LinearLayout (with id remote_ctrl_ll ) to match_parent.
You're setting the weight & height explicitly on your first nested LinearLayout. try setting the height explicitly on your Root LinearLayout, and use ONLY layout_weights for the nested LinearLayouts

Categories

Resources