I have a LinearLayout with Vertical orientation and I would like to add another horizontal LinearLayout with two childs.
Here is my xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Gray_background"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/GrayStroke" />
<LinearLayout
android:id="#+id/contacts_tab_menulist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/GrayStroke" /> </LinearLayout>
Here is the code which should do the job:
View rootView = inflater.inflate(R.layout.contacts_tab,
container, false);
contacts_tab_menulist = (LinearLayout) getActivity().findViewById(R.id.contacts_tab_menulist);
// add LayoutParams
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 35);
LinearLayout listItem = new LinearLayout(getActivity());
listItem.setLayoutParams(lparams);
listItem.setOrientation(LinearLayout.HORIZONTAL);
AspectRatioImageView icon = new AspectRatioImageView(getActivity());
LinearLayout.LayoutParams ic_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 25);
icon.setImageResource(R.drawable.contacts_add_contact);
icon.setLayoutParams(ic_params);
listItem.addView(icon);
TextView title = new TextView(getActivity());
LinearLayout.LayoutParams t_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
title.setText("Add Contact");
title.setId(0);
title.setLayoutParams(t_params);
listItem.addView(title);
contacts_tab_menulist.addView(listItem);
return rootView;
I get a nullPointerException on contacts_tab_menulist.addView(listItem). What am I doing wrong?
Try replacing
contacts_tab_menulist = (LinearLayout) getActivity().findViewById(R.id.contacts_tab_menulist);
with
contacts_tab_menulist = (LinearLayout) rootView.findViewById(R.id.contacts_tab_menulist);
Get the view from your inflated view, since inflating the View does not mean its the current activities view:
contacts_tab_menulist = (LinearLayout) rootView.findViewById(R.id.contacts_tab_menulist);
Related
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
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;
}
For example, I have some layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test">
</TextView>
</LinearLayout>
I want generate code from layout:
LinearLayout layout = new LinearLayout(context);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(lp);
layout.setOrientation(LinearLayout.VERTICAL);
TextView textview = new TextView(context);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
textview.setText("test");
layout.addView(textview);
Can anybody know tools for this?
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>
Application has scrollview in XML layout.
<ScrollView
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
>
</ScrollView>
I want to one textView into this scrollView center.
I tried This code, set horizontal center, but vertical top.. I want both centered.
LinearLayout l1 = new LinearLayout(getActivity());
l1.setOrientation(LinearLayout.VERTICAL);
l1.setGravity(Gravity.CENTER);
l1.setBackgroundColor(Color.WHITE);
TextView errorView = new TextView(getActivity());
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
errorView.setText("TextView");
errorView.setTextColor(Color.BLACK);
errorView.setLayoutParams(params);
errorView.setGravity(Gravity.CENTER);
l1.addView(errorView);
scrollViewCon.addView(l1, lparams);
Why is this happening?
Change your xml as shown below -
Add fillViewport as true.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:fillViewport="true"
>
Removed the relative layout addition part as it is not required.