how to set layout_span, layout_margin dynamically in code in android - android-layout

how to set layout_span, layout_margin dynamically in code in android

You can set layout_margin with a TableRow.LayoutParams instance given to TableRow.addView():
TableLayout newTable = new TableLayout(DisplayContext);
int rowIndex = 0;
// First Row (3 buttons)
TableRow newRow1 = new TableRow(DisplayContext);
newRow1.addView(new Button(DisplayContext), 0);
newRow1.addView(new Button(DisplayContext), 1);
newRow1.addView(new Button(DisplayContext), 2);
newTable.addView(newRow1, rowIndex++);
// Second Row (2 buttons, last one spans 2 columns)
TableRow newRow2 = new TableRow(DisplayContext);
newRow2.addView(new Button(DisplayContext), 0);
Button newButton = new Button(DisplayContext);
// Create the layout to span two columns
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 2;
newRow2.addView(newButton, 1, params);
newTable.addView(newRow2, rowIndex++);

Related

Text dynamic size - Animate CC HTML5canvas

I need to have a dynamic text field to control by code the size of the text and the text.
I create a dynamic text field on the Stage with the "notification" instance and the following code:
function Message () {
var text1 = new createjs.Text ("Message", "15px Arial", "# ff7700");
text.textBaseline = "alphabetic";
stage.addChild (text);
}
this.notification.text = text1;
What am I doing wrong? From already thank you very much
half resolved
var text = new createjs.Text("Mensaje", "25px Times Bold", "#ff0000");
text.x = 150;
text.y = 150;
text.color = "#0000ff";
text.textAlign = "center";
text.textBaseline = "alphabetic";
stage.addChild(text);

Cannot find API to add error bars using Apache POI

I was trying to find how to add error bars to the scatter plot chart using Apache POI as shown in the picture below. My research could not help me find the corresponding API. Can you please help to find the API.
Adding error bars is not supported by the high level apache poi classes until now. It would must be a method in XDDFChartData.Series.
But *.xlsx files are simply ZIP archives. So we can set the error bars using Excel's GUI. Then unzip the *.xlsx ZIP archive and have a look at /xl/charts/chart1.xml to find what has changed.
We will find something like:
...
<c:ser>
...
<c:errBars>
<c:errDir val="y"/>
<c:errBarType val="both"/>
<c:errValType val="percentage"/>
<c:val val="10.0"/>
</c:errBars>
...
</c:ser>
...
Now we can create that XML using the low level classes of ooxml-schemas:
...
// add error bars
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).addNewErrBars();
// set error bars direction only Y
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrDir().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrDir.Y);
// set error bars type to both (minus and plus)
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrBarType().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrBarType.BOTH);
// set error bars value type to percentage
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrValType().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrValType.PERCENTAGE);
// set error bars value to 10%
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewVal().setVal(10d);
...
Complete example:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
public class ScatterChart {
public static void main(String[] args) throws IOException {
Double[][] data = new Double[][]{
new Double[]{0d, 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d},
new Double[]{1.1d, 1.5d, 1.2d, 2.5d, 2.7d, 6.5d, 6.5d, 1.0d, 1.0d, 0.5d},
};
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
final int NUM_OF_ROWS = data.length;
final int NUM_OF_COLUMNS = data[0].length;
Row row;
Cell cell;
int rowIndex = 0;
int colIndex = 0;
for (Double[] dataRow : data) {
row = sheet.createRow(rowIndex++);
colIndex = 0;
for (Double value : dataRow) {
cell = row.createCell(colIndex++);
cell.setCellValue(value);
}
}
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 3, 10, 23);
XSSFChart chart = drawing.createChart(anchor);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
XDDFSolidFillProperties gridLinesFill = new XDDFSolidFillProperties(XDDFColor.from(
new byte[]{(byte)230,(byte)230,(byte)230}));
XDDFLineProperties gridLineProperties = new XDDFLineProperties();
gridLineProperties.setFillProperties(gridLinesFill);
XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("x");
XDDFShapeProperties shapeProperties = bottomAxis.getOrAddMajorGridProperties();
shapeProperties.setLineProperties(gridLineProperties);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("f(x)");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
shapeProperties = leftAxis.getOrAddMajorGridProperties();
shapeProperties.setLineProperties(gridLineProperties);
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(
sheet,
new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)
);
XDDFNumericalDataSource<Double> ys = XDDFDataSourcesFactory.fromNumericCellRange(
sheet,
new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)
);
XDDFScatterChartData chartData = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);
chartData.setVaryColors(false);
XDDFScatterChartData.Series series = (XDDFScatterChartData.Series) chartData.addSeries(xs, ys);
series.setTitle("Series 1", null);
series.setSmooth(false);
chart.plot(chartData);
solidLineSeries(series, PresetColor.BLUE);
// add error bars
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).addNewErrBars();
// set error bars direction only Y
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrDir().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrDir.Y);
// set error bars type to both (minus and plus)
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrBarType().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrBarType.BOTH);
// set error bars not have no end caps - necessary for current Excel versions
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewNoEndCap().setVal(false);
// set error bars value type to percentage
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrValType().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrValType.PERCENTAGE);
// set error bars value to 10%
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewVal().setVal(10d);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) {
wb.write(fileOut);
}
}
}
private static void solidLineSeries(XDDFChartData.Series series, PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFLineProperties line = new XDDFLineProperties();
line.setFillProperties(fill);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setLineProperties(line);
series.setShapeProperties(properties);
}
}

How can I prevent my image from changing size when placing it on a spreadsheet (Aspose Cells)?

I have an image that is embedded in my solution and is used on the main form of a Winforms app, and also for pasting into a spreadsheet. The image size is 156X121.
I put it on the sheet like so:
var ms = new MemoryStream();
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Yet when it is on the sheet, it stretches out and spills into neighboring cells, partially obscuring other data:
As you can see, the size is no longer 156X121. The height has been increased by 25%. Why? And how can I prevent that?
This code:
MessageBox.Show(string.Format("image height is {0}", _logo.Height));
MessageBox.Show(string.Format("image width is {0}", _logo.Width));
...showed me "126" as the height and "151" as the width, matching the image as it is in the project. So why is the original size changed? Is there a property I can set to leave the size alone and not stretch it? Or how can I prevent this gumbification of the image?
It's bizarre to me that the image is one size (126X151), its original size is purported to be 1.26" X 1.63", and its size after being scaled is 1.57" X 1.63".
Who or what is allowing this 25% increase in Height?
NOTE: If I select the "Reset" button in the image's "Size and Properties" dialog, it shrinks up as I want it to be, setting the Height "back" to 100% from 125%. Is there a way to do this "Reset" programmatically?
UPDATE
Based on the answer, I tried this:
var ms = new MemoryStream();
//_logo.Height = 121; <= cannot set; Height is a readonly property
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Picture pic = pivotTableSheet.Pictures[0];
//Workbook.Worksheets[0].Pictures[0]; <= does not compile
pic.HeightScale = 100;
pic.WidthScale = 100;
(Workbook.Worksheets[0] does not compile for me).
It makes no difference; the image is still stretching vertically.
UPDATE 2
I realized I needed "Workbook" to be "workBook" due to this:
private static Workbook workBook;
...and so I tried this:
Picture pic = workBook.Worksheets[1].Pictures[0]; // Worksheet 0 is the data sheet that feeds the PivotTable and subsequently gets hidden, so need to use 1
pic.Height = 121;
pic.WidthScale = 100;
...but it still gumbifies the image vertically. So does replacing "pic.Height = 121" with "pic.HeightScale = 100;"
So this is the code currently, which adds the image, but in a vertically gumbified manner:
var ms = new MemoryStream();
//_logo.Height = 121; readonly
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Picture pic = workBook.Worksheets[1].Pictures[0]; // Worksheet 0 is the data sheet that feeds the PivotTable
//pic.Height = 121;
pic.HeightScale = 100;
pic.WidthScale = 100;
Please use this code to reset it to original height.
Picture pic = wb.Worksheets[0].Pictures[0];
pic.HeightScale = 100;
pic.WidthScale = 100;
Note: I am working as Developer Evangelist at Aspose

iTextSharp pdf table cell height issue

I have iTextSharp 5.4.4 (nuget) and have a nice table with a barcode (ean13) and text below it.
I have specific tablecell heights (and cell widths) because I want to print the pdf to an A4 with stickers.
Here is the current layout:
as you can see, there is a rather large gap between the ean13 code and the text below.
here is my C# code:
PdfPCell c = new PdfPCell();
c.FixedHeight = 21.2f * postScriptPointsPerMilimeter; // to get to accurate milimeters
c.HorizontalAlignment = Element.ALIGN_CENTER;
Paragraph p = new Paragraph();
p.Font.Size = 6;
Chunk code = new Chunk(dr["productcode"].ToString());
p.Alignment = Element.ALIGN_CENTER;
p.Add(code);
BarcodeEAN ean13 = new BarcodeEAN();
ean13.CodeType = BarcodeEAN.EAN13;
ean13.Code = dr["ProductEan13"].ToString();
ean13.BarHeight = 4.0f * postScriptPointsPerMilimeter;
var a = ean13.CreateImageWithBarcode(cb, null, null);
a.ScalePercent(90);
c.AddElement(a);
c.AddElement(p);
t.AddCell(c);
My question is to reduce the space between the barcode and the text. I cannot see if it has something to do with the barcode's margins or the paragraph's or maybe both... hard to troubleshoot.
p.Leading = 0;
That was missing. I thought that
p.SpacingBefore = 0;
would do the trick, but it didn't. Leading did!

Buttons in LinearLayout not filling entire space programmatically

So I have a LinearLayout in horizontal mode which I add buttons to at random times in code and I like them all to fill up the space equally.
This is my current code so far:
layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.FILL;
and I add it to my view here:
linearLayout.addView(button, layoutParams);
All it does is add the buttons as if it was wrap_content and not expanding their width to fill up the available space as in the buttons look left justified.
I also have tried linearLayout.setGravity(Gravity.FILL_HORIZONTAL); and
linearLayout.setWeightSum(++weightSum);
linearLayout.addView(button, layoutParams);
where layoutParams in this case is:
layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 1);
I also tried setting the layout width to zero per answers to other questions.
Am I missing another technique?
Edit: I figured it out, I forgot to set the width of the LinearLayout to match_parent instead of wrap_content.
Button's background has "margins" so they will always have gaps even if there's no space between buttons. You have to change background to your own image which doesn't have these margins.
EDIT: You should use weights for buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight = 1;
for( int i = 0; i < 5; ++i) {
Button button = new Button(this);
button.setText("Button" + (i + 1));
linearLayout.addView(button, params);
}

Resources