I am using MPAndroidChart library. I want customize legends in MPAndroidChart. In MPAndroidChart library i tried to set the position of legends. by given code legend.setPosition(LegendPosition.BELOW_CHART_CENTER) but unable to do it. I have to set legends as shown in following image
help will be appreciate
In your case I would recommend that you disable the Legend that is drawn by the chart and instead come up with your own implementation.
chart.getLegend().setEnabled(false)
In the case shown above you will probably need a ListView that takes data from the charts Legend object and displays it.
When you have a look at the Legend class you will notice that it has member variables for colors and labels.
You can retrieve those arrays (getColors(), getLegendLabels()) and use them to be displayed in the ListView.
Please look for the given answer MPAndroidChart - Legend labels are being cut off. I have already provided the answer according to your problem.
Look for given code which will definitely help you.
You will have to implement customized legends with their legends colours and labels by following the steps below:
Step 1
Legend legend = mChart.getLegend();
Step 2
int colorcodes[] = legend.Colors();
Steps 3
for (int i = 0; i < legend.Colors().length-1; i++) {
.....
.....
}
Steps 4
Then you will have to take one layout horizontal or vertical and get legends color codes and legends label and according to legends length create layout and label. Code sample is given below:
LinearLayout.LayoutParams parms_left_layout = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
parms_left_layout.weight = 1F;
LinearLayout left_layout = new LinearLayout(context);
left_layout.setOrientation(LinearLayout.HORIZONTAL);
left_layout.setGravity(Gravity.CENTER);
left_layout.setLayoutParams(parms_left_layout);
LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
20, 20);
parms_legen_layout.setMargins(0, 0, 20, 0);
LinearLayout legend_layout = new LinearLayout(context);
legend_layout.setLayoutParams(parms_legen_layout);
legend_layout.setOrientation(LinearLayout.HORIZONTAL);
legend_layout.setBackgroundColor(colorcodes[i]);
left_layout.addView(legend_layout);
TextView txt_unit = new TextView(context);
txt_unit.setText(legend.getLabel(i));
left_layout.addView(txt_unit);
LinearLayout.LayoutParams parms_middle_layout = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
parms_middle_layout.weight = 1F;
LinearLayout middle_layout = new LinearLayout(this);
middle_layout.setOrientation(LinearLayout.HORIZONTAL);
middle_layout.setGravity(Gravity.CENTER);
middle_layout.setLayoutParams(parms_middle_layout);
TextView txt_leads = new TextView(this);
txt_leads.setText("450");
middle_layout.addView(txt_leads);
LinearLayout.LayoutParams parms_right_layout = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
parms_right_layout.weight = 1F;
LinearLayout right_layout = new LinearLayout(this);
right_layout.setOrientation(LinearLayout.HORIZONTAL);
right_layout.setGravity(Gravity.CENTER);
right_layout.setLayoutParams(parms_right_layout);
TextView txt_leads_percentage = new TextView(this);
txt_leads_percentage.setText(munit_percentage_list.get(i) + "");
right_layout.addView(txt_leads_percentage);
childlayout.addView(left_layout);
childlayout.addView(middle_layout);
childlayout.addView(right_layout);
And after this add your (child layout which you have created at runtime ) to the main layout.
for setting custom Legends:
public void setLegends(){
Legend l = holder.pieChart.getLegend();
l.getEntries();
l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
l.setYEntrySpace(10f);
l.setWordWrapEnabled(true);
LegendEntry l1=new LegendEntry("Male",Legend.LegendForm.CIRCLE,10f,2f,null,Color.YELLOW);
LegendEntry l2=new LegendEntry("Female", Legend.LegendForm.CIRCLE,10f,2f,null,Color.RED);
l.setCustom(new LegendEntry[]{l1,l2});
l.setEnabled(true);
}
Follow below code for custom legend.
Create table_row_legend.xml in layout resource
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:weightSum="3">
<LinearLayout
android:id="#+id/tv_color_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.30"
android:orientation="horizontal"
android:gravity="right"
android:padding="5dp">
<LinearLayout
android:id="#+id/tv_color"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:orientation="horizontal"
/>
</LinearLayout>
<TextView
android:id="#+id/tv_label"
android:layout_width="0dp"
android:layout_gravity="top"
android:layout_weight="1.35"
android:gravity="left|top"
android:padding="3dp"
android:singleLine="true"
android:textColor="#2b2b2b"
android:textSize="16sp" />
<TextView
android:id="#+id/tv_amt"
android:layout_width="0dp"
android:layout_weight="1.35"
android:gravity="left|top"
android:padding="3dp"
android:textColor="#2b2b2b"
android:textSize="16sp" />
</TableRow>
Create new LinearLayout below your Pie chart and wrap parent layout with scroll layout with static height to pie chart
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:layout_marginBottom="10dp"
android:layout_centerInParent="true"
>
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/chart1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="#+id/tv_info"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:clickable="true" />
<TableLayout
android:id="#+id/child_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/chart1"
android:orientation="vertical" />
</RelativeLayout>
</ScrollView>
Make changes in your activity class as follows
public void setCustomLegend(){
int colorcodes[] = l.getColors();
Context context = DistributorGraphActivity.this;
for (int i = 0; i < l.getColors().length - 1; i++) {
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow) inflater.inflate(R.layout.table_row_legend,
childlayout, false);
childlayout.addView(tr);
LinearLayout linearLayoutColorContainer=(LinearLayout) tr.getChildAt(0);
LinearLayout linearLayoutColor= (LinearLayout) linearLayoutColorContainer.getChildAt(0);
TextView tvLabel = (TextView) tr.getChildAt(1);
TextView tvAmt = (TextView) tr.getChildAt(2);
linearLayoutColor.setBackgroundColor(colorcodes[i]);
tvLabel.setText(l.getLabel(i));
tvAmt.setText(arrListDealerGraph.get(i).getAmt());
}
mChart.getLegend().setWordWrapEnabled(true);
mChart.getLegend().setEnabled(false);
}
For Kotlin the command is:
mChart.legend.isEnabled = false
Related
I have a LinearLayout (horizontal) with several child TextViews inside. I have set fixed layout_width for both the LinearLayout and all the TextViews. When the total width of the children exceeds the width of the layout, it automatically shrink the children, making them narrower, which is not what I want. I want the layout to clip the children content and keep their width.
How can I achieve that?
For example: Let's say my LinearLayout is 200dp wide, and there are 4 TextView each 60dp wide. I want it to show the first three TextViews, and 1/3 of the 4th (60 + 60 + 60 + 20 = 200)
Thanks.
Use android:weightSum in LinearLayout
And use android:layout_width="0dp" and android:layout_weight="1" in TextView
Try this.
<?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="horizontal"
android:weightSum="10">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hello"/>
</LinearLayout>
Also
You can change android:layout_width="match_parent" to android:layout_width="wrap_content" in TextView .
Edit
And you can use in your java code .
You can change the width or weight by yourself .
private LinearLayout mLinearLayout;
private int viewsCount = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
for (int i = 0; i < viewsCount; i++) {
TextView textView = new TextView(this);
if(i == viewsCount-1){
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
} else {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3);
}
mLinearLayout.addView(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!!
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
I am creating a TextView dynamically. Here, I am creating a mainLayout in which I have two child layout and I wan gave them weight.
Here, is my Layout
<?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">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/Layout_second_overs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:orientation="vertical">
<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_second_balls"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<!--Second textview balls--> //here i inflate 12 textview dynamically but not 12 seen on the device while i run the app.
</LinearLayout>
</LinearLayout>
</LinearLayout>
Code to create a 12 TextViews and put into layout.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0; a <= 12; a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
}
Problem is my all data are print into log but not visible on the device.
Please, help me to solve out this problem.
There are two issues
One is for the Layout Design related
Second based on the layout design and property you also need to
handle is while you are adding dynamically view into linear layout
Here what i have changed in Your xml file
<?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">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/Layout_second_overs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_second_balls"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" android:background="#FF13C7FF"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</LinearLayout>
In Layout_second_overs linearlayout height , Width & weight is the issue.
& in Layout_second_balls linear layout you have used weight as 1 so you also required layout_height as 0dp if orientation is horizontal
Now below things i have changed in code
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams
(0, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0;a<=12;a++)
{
TextView first = new TextView(getActivity());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
params.weight = 1; /// Need to give weight if you want equal size of textview inside Linear
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
Layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
}
above things are based on the question which you ask
Suggestion
Avoid adding dynamic view like you asked . prefer to use ListView, GridView, RecyclerView for this type of functionality.
Your layout_second_overs requires all of your screen space - so your layout_second_balls is moved out of your screen.
If you want to see your two LinearLayouts beside each other, you need to set the layout_width to "0dp":
//pseudo, simply change width to 0dp
<LinearLayout
android:layout_height="40dp"
.... >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="6"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"/>
</LinearLayout>
To replace this kind of loops you are supposed to use a RecyclerView. It is easy to use and responsive (since your list will be scrollable).
The things you have to remember while implementing a RecyclerView are to add an adapter and a LayoutManager to it.
For your example :
LayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true);
YourAdapter adapter = new YourAdapter([...]);
RecyclerView recyclerView = findViewById(R.id.your_recyclerview_here);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
If you need any advices for creating your adapter just ask.
You can use this line of code.
LinearLayout layout_second_balls=(LinearLayout) findViewById(R.id.Layout_second_balls);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0;a<=12;a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first);
}
i run this code in my emulator and i got this out put.
You have to assign index for a view while performing addview(view,index) set index for each textview created in for loop like layout_second_balls.addView(first,index);
Code should look like :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0; a <= 12; a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first,a);
}
Hope it will work.
I am trying to create an Activity with a ScrollView in it. In the ScrollView will be "boxes" (sort of like a button) that is just a bit smaller than the width of the parent view and spaced slightly apart vertically. Because the boxes will have a couple different size text in a couple areas on them, I think I need to make the out of RelativeLayouts. (I would post a picture but can't with my reputation point so hopefully this makes sense).
I tried create an XML for the activity and than programatically adding the relativeviews. I got it to work, sort of, but am having trouble with the box spacing.
My test XML looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- scrolling section -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
My Activity looks like this...
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}
I get the scrollable section, no problem. Problem is my blue "boxes" appear to be touching one another and I can't figure out how to make them look like buttons with padding left and right and between them. I tried
rlView.setPadding(20,20,20,20);
right after the setBackground color. All that appears to do is pad the text... not the RelativeViews.
Any suggestions no how to get this to work?
Thanks
===========
NEW INFORMATION:
Here is some additional information after following some suggestions and trial an error. I was also able to post a pic for easier reference.
===========
What I am trying to achieve, programmatically, is something that looks like this.
I achieved this example using the straight XML which looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="#+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
// individual groups start here - this section will be controlled by a database
// #1 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#color/blue"
android:padding="5dp"
android:clickable="true"
android:id="#+id/grp1"
>
<TextView
android:id="#+id/text1"
android:text="Item #1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/leftext1"
android:text="123.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp" />
</RelativeLayout>
// #2 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/blue"
android:padding="5dp"
android:clickable="true"
android:id="#+id/grp2"
>
<TextView
android:id="#+id/text2"
android:text="Item #2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/leftext2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/leftext2"
android:text="456.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp"
/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I took this apart and created an XML file and and activity that will programmatically create the middle groups.
The modified XML (activity_main2) looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="#+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
I started rewriting my activity code to test some of the suggestions. It currently looks like this...
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.setMargins(10,10,10,0);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
// other textviews will go here
llGroups.addView(rlView,rlp);
}
}
}
This compiles and runs, but the setMargins is not working. My screen has the "UPDATE ALL" button at the top like it should and (19) "Text #" with a blue background, but they are full width and touching one another so it is a solid blue background.
I am getting close, but not sure how to get the margins to set correctly for the rlView relativeview (not the textviews).
Hopefully this helps explain what I am seeing now..
SOLUTION
After suggestions, and some trial and error... here is what I had to do to achieve the desired layout in case someone else is interested.
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout mTvContainer = (LinearLayout) findViewById(R.id.llAccts);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.BLUE);
RelativeLayout rl = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setPadding(5, 5, 5, 5);
tv.setText("Test #" + i);
tvp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(5, 5, 5, 5);
tv2.setText(String.valueOf(i));
tv2.setTextSize(30);
tvp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, tvp);
rl.addView(tv2,tvp2);
ll.addView(rl);
llp.setMargins(32,16,32,16);
mTvContainer.addView(ll,llp);
}
}
For each entry in the FOR, I had to use a LINEARLAYOUT to be able to set the margins properly, and in that LINEARLAYOUT I had to use a RELATIVELAYOUT to use the addRules to get the proper positioning of the TextViews.
If there is an easier way, let me know. Thanks for everyones help!!
You are using too much nested layouts needed, here is stripped version.
xml:
<ScrollView 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=".MainActivity">
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
Activity code:
mTvContainer = (LinearLayout) findViewById(R.id.ll_main);
int p = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setBackgroundColor(Color.BLUE);
tv.setTextColor(Color.WHITE);
tv.setPadding(p, p / 2, p, p / 2);
tv.setText("TextView " + i);
TextView tv2 = new TextView(this);
tv2.setBackgroundColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(p, p / 2, p, p / 2);
tv2.setText("TextView " + i);
ll.addView(tv);
ll.addView(tv2);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(p, p / 2, p, p / 2);
mTvContainer.addView(ll, params);
}