What I did was I added a bunch of TextViews programmatically, but now the ScrollView won't resize to fit all the new TextViews created in the onCreate() method.
onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_LEFT);
for (int i = 1; i < 21; i++) {
TextView number = new TextView(this);
number.setText(Integer.toString(i));
Log.i("i", Integer.toString(i));
number.setLayoutParams(lp);
number.setY(i / 2.54f * getResources().getDisplayMetrics().densityDpi);
relativeLayout.addView(number);
}
}
activity_main.xml:
<ScrollView
android:id="#+id/scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"/>
</ScrollView>
Is there any way to make the ScrollView fit everything inside the RelativeLayout?
Try this,
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</RelativeLayout>
</ScrollView>
Add this to your code,
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lp);
tv1.setId(1);
rLayout.addView(tv1);
for(int i=1;i<25;i++)
{
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv = new Button(this);
tv.setText("Hello "+i+1);
newParams.addRule(RelativeLayout.BELOW, i);
tv.setLayoutParams(newParams);
tv.setId(2);
rLayout.addView(tv);
}
Related
I set layout_marginStart and layout_marginTop in xml and it is ok, but when I try to set programmatically margins it does not work. I try to find how to resolve, but can not find resolution.
What I do not correctly?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_resourses">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/MyGridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center"
android:background="#color/borderHigh"
android:columnCount="2"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/firstLinear1"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="#dimen/resourceBorder"
android:layout_marginTop="#dimen/resourceBorder"
android:background="#drawable/resource_panel"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/firstLinear2"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="#dimen/resourceBorder"
android:layout_marginTop="#dimen/resourceBorder"
android:background="#drawable/resource_panel"
android:orientation="vertical" />
</GridLayout>
</ScrollView>
XML above works
I try to do this by code, and it must work but i can not resolve this problem
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int dp9 = dpToPx(9);
GridLayout MyGridLayout = findViewById(R.id.MyGridLayout);
final int N = 10; // total number of textviews to add
for (int i = 0; i < N; i++) {
LinearLayout testlayout = new LinearLayout(this);
//LinearLayout.LayoutParams tst = new LinearLayout.LayoutParams(0,0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.height = (width/2)-dp9;
params.width = (width/2)-dp9;
params.setMargins(6, 6, 0, 0);
testlayout.setLayoutParams(params);
testlayout.setOrientation(LinearLayout.VERTICAL);
testlayout.setBackgroundResource(R.drawable.resource_panel);
TextView title = new TextView(this);
title.setText("TextView "+i);
title.setTextColor(getResources().getColor(R.color.colorPrimary2));
title.setAllCaps(true);
title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(title, 10, 24, 1, TypedValue.COMPLEX_UNIT_SP);
testlayout.addView(title);
TextView text = new TextView(this);
text.setText("Text "+i);
testlayout.addView(text);
TextView date = new TextView(this);
date.setText("date "+i);
testlayout.addView(date);
}
They currently appear on the center.
Android Java Code
public void populateTable() {
for(DoctorBean post: beanPostArrayList){
String result = "";
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ImageView docImageView = new ImageView(getActivity());
docImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
docImageView.setPadding(5, 5, 5, 5);
docImageView.setImageResource(R.drawable.ic_doctor);
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(5, 5, 5, 5);
result += "\n" + post.getHeroName();
textView.setGravity(Gravity.LEFT);
docImageView.setForegroundGravity(Gravity.LEFT);
row.setGravity(Gravity.LEFT);
textView.setText(result);
row.addView(docImageView);
row.addView(textView);
tableLayout.addView(row);
}
}
Layout Code
<?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">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*">
</TableLayout>
</ScrollView></LinearLayout>
So there I want to align the ImageView and the TextView to the left.
An alternative approach should solve your issue
You don't need to add explicit scroll for a TableView
TableView can be directly added to your main LinearLayout. As you have already specified the height of MapView
Suggested Modified xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLL"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
Corresponding code change
LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.mainLL);
//Table Layout parameters
TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
TableLayout tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);
TableRow trHead = new TableRow(context);
LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
trHead.setLayoutParams(tableRowParams);
TextView nameHead = new TextView(context);
nameHead.setText("Content left");
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.LEFT);//or Gravity.CENTER according to your requirement
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText("Content right");
detailHead.setGravity(Gravity.RIGHT);//or Gravity.CENTER according to your requirement
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
//add table layout to linear layout
linearLayout.add(tableLayout);
NOTE:
Many values have been referred from resource files, You can either
neglect/replicate those. I have added two textviews. You may need to
change according to your requirement
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;
}
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);
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?