I have a table layout in XML that does exactly what I want:
<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#drawable/evenrow">
<TextView android:text="Check" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_weight="1"></TextView>
I am trying to add the table row programmatically, but I can't seem to get the layout_weight set. Here is the java code:
TableRow.LayoutParams trlayout = new TableRow.LayoutParams (TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
TableLayout.LayoutParams tablelayout = new TableLayout.LayoutParams (TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT);
TableRow tr = new TableRow(this);
tr.setLayoutParams(trlayout);
TextView check = new TextView(this);
check.setText(String.format("%5.2f", i));
tr.addView(check);
I thought the 3rd param to new TableRow.LayoutParams was the default weight for the row's children, but it is not working.
What am I missing?
Well in a regular XML if you set wrap_content on the dimension of orientation you want to expand in then layout weight has no effect.
Change the width parameter to "0dp" and then your layout weight should work.
Related
I am working on Watson Conversation chatbot. I have been trying to implement the 'options' response type in my chatbot application. My problem is "Creating 'n' number of dynamic buttons in Android where n is the number of label names of options present in the backend(IBM Watson Conversation)"
I have been able to retrieve the label names in form of text. Now I have to put these label names in "clickable buttons". Such that when a user clicks on a button, a value is passed to the backend (Watson Conversation API).
This is how I am retrieving option(response type) from backend. Watson Conversation sends reply in form of JSON.
Label name retrieving code:
str = response.getOutput().getGeneric().get(i).getResponseType();
JSONArray arrayOptions = new JSONArray(response.getOutput().getGeneric().get(i).getOptions());
int j=0; //j is used to count the number of options
while (j<arrayOptions.length()){
final Message outMessage2 = new Message();
outMessage2.setMessage(response.getOutput().getGeneric().get(i).getOptions().get(j).getLabel());
outMessage2.setId("2");
System.out.println(outMessage2);
messageArrayList.add(outMessage2);
j++;
}
Try this to solve your problem
First create a LinearLayout Layout inside xml
<LinearLayout
android:id="#+id/layout_dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
</LinearLayout>
After that, use below code
LinearLayout layout_dynamic =(LinearLayout) findViewById(R.id.layout_dynamic);
for (int i = 0; i < YOURARRAY.length(); i++) {
String label = <Button Name as You Like>;
LinearLayout childLayout = new LinearLayout(getActivity());
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT LinearLayout.LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
Button btnName = new Button(getActivity());
btnName.setLayoutParams(newTableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
childLayout.addView(btnName, 0);
layout_dynamic.addView(childLayout);}
Hope this will help you.
I've tried for two day now to append an image to a TextView, but it didn't work. I have tried this for Android 6/API Level 23. ImageSpan, Html.fromHtml and other Methods did not work. So is it possible to append image (Bitmap) between other words in a TextView on Android 6?
EDIT
Here is the XML:
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:text="------"
android:gravity="bottom|left"
android:typeface="monospace"
android:id="#+id/mainTextView1"
android:textIsSelectable="true"/>
And image span Code:
SpannableStringBuilder ssb = new SpannableStringBuilder(mOutEditText.getText()); ssb.setSpan(new ImageSpan(bitmapArray.get(0), ImageSpan.ALIGN_BASELINE), cursorPosition, cursorPosition+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mOutEditText.setText(ssb, BufferType.SPANNABLE);
Add image in xml
<TextView
android:drawableBottom="#drawable/ic_add_circle_black_24dp"
android:drawableEnd="#drawable/ic_add_circle_black_24dp"
android:drawableLeft="#drawable/ic_add_circle_black_24dp"
android:drawablePadding="#drawable/ic_add_circle_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
programmatically
textView1.setCompoundDrawables(left,top,right,bottom);
Or only left
textView1.setCompoundDrawables(left,null,null,null);
I have found the solution now for Api 21, 22, 23+:
private void appendImage(Bitmap bmp)
{
tv.setTransformationMethod(null);
SpannableString ss = new SpannableString(" ");
ss.setSpan(new ImageSpan(bmp, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append(ss);
}
And in the XML:
android:textAllCaps="false"
The error was that in api level 21+ you have to set the TransformationMethod null.
If you want to set the background of the TextView use the following methods:
txt.setBackgroundResource(int rsid);
txt.setBakgroundDrawable(Drawable object);
If by append, you mean you want to add it to the end of the TextView, then use an ImageView for the image. Or you can overlay your TextView in an ImageView
It is not possible to add images between text in a TextView. You can add an image a TextView at the start and end. This is done with
android:drawableLeft
I have been stuck on this issue for hours and I have no idea how to get passed it.
I have programatically created a table (no problem), but this is the issue I am having. When I create the row, I would then like to create two separate columns. As you'll see in my example, I have two different TextViews on the same line ('Type' and 'Number'). As it stands, both of them are shifted over to the left.
What I would like is for them to be in two separate columns, equally split apart centred in each column.
Here is the relevant code to what I am trying to accomplish.
TableLayout serviceLineLayout = new TableLayout(getContext());
serviceLineLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
TableRow firstTableRow = getFirstTableRow(getContext());
serviceLineLayout.addView(firstTableRow);
private TableRow getFirstTableRow(Context context) {
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
TextView typeHeader = new TextView(context);
typeHeader.setId(0);
typeHeader.setText("Type");
typeHeader.setTextSize(20);
tableRow.addView(typeHeader);
TextView numberHeader = new TextView(context);
numberHeader.setId(0);
numberHeader.setText("Number");
numberHeader.setTextSize(20);
numberHeader.setGravity(Gravity.RIGHT);
tableRow.addView(numberHeader);
return tableRow;
}
For this tablelayout defined in xml, following will be the dynamic code in corresponding java file (Activity/Fragment).
<TableLayout
android:id="#+id/fenceTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
Activity logic for TableView
//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.fenceTableLayout);
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.CENTER);
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText("Content right");
detailHead.setGravity(Gravity.CENTER);
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
NOTE:
Many values have been referred from resource files, You can either neglect/replicate those.
I'm creating my first app on android, accordingly I have low experience to develop android apps and I'm fist time use java too.
What I want ?
I want to generate table from ArrayList
What's the problem ?
I have problem when I generate table. When "for" loop finished debugger stands in catch block (but when I mouse over "e", it writes - "local variable e is not defined" and I can not read some error) after that I see on phone screen -"unfortunately app is closed"
What's happen without For Loop ?
Without For loop everything works well, when I try to show only one object.
Please help and give me suggestions, Thanx.
Hare is Java code:
try {
ArrayList<Persons> otherPersons = new ArrayList<Persons>();
otherPerosons = GetPersonsList();//from service
// /* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.dynamictable);
TextView[] title = new TextView[1000];
TableRow[] tr = new TableRow[1000];
LinearLayout.LayoutParams trparams;
LinearLayout.LayoutParams titleparams;
for (int y=0; y < otherPersons.size(); y++) {
/* Create a new row to be added. */
tr[y] = new TableRow(this);
tr[y].setId(y+100);
tr[y].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
trparams = (LinearLayout.LayoutParams) tr[y].getLayoutParams();
trparams.setMargins(0, 8, 0, 5); //substitute parameters for left, top, right, bottom
tr[y].setLayoutParams(trparams);
/* Create a TextView to be the row-content. */
title[y] = new TextView(this);
title[y].setText(otherPersons.get(y).Name);
title[y].setId(y +200);
title[y].setTextColor(Color.parseColor("#000000"));
title[y].setGravity(Gravity.LEFT);
title[y].setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 34));
titleparams = (LinearLayout.LayoutParams) title[y].getLayoutParams();
titleparams.setMargins(5, 0, 0, 0); //substitute parameters for left, top, right, bottom
title[y].setLayoutParams(titleparams);
tr[y].addView(title[y]);
/* Add row to TableLayout. */
tl.addView(tr[y], new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
} catch (Exception e) {
e.printStackTrace();
}
Hare is my Design XML:
<TableLayout
android:id="#+id/dynamictable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp"
android:stretchColumns="*"
android:background="#ffB224">
</TableLayout>
I don't know the answer, but try to put your exception-output in your logcat output with
try{
}catch(Exception e){
Log.e("ClassName", e.toString());
}
Maybe you see more information about the exception.
Here you can find more information about Android Logging
I am trying to add multiple components into linear layout programatically. Here are the codes:
private View createCalloutView(Graphic graphic) {
LinearLayout ret = new LinearLayout(this);
ret.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TextView reportContent = new TextView(this);
reportContent.setText(eventName + "\n" + eventBy + "\n" + eventAddress + "\n" + eventDesc
+ "\n" + eventDate + "\n" + eventTime);
reportContent.setTextColor(Color.BLACK);
reportContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
reportContent.setPadding(1, 0, 1, 0);
Button viewDtlEventBtn = new Button(this);
viewDtlEventBtn.setText("View details");
viewDtlEventBtn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ret.addView(reportContent);
ret.addView(viewDtlEventBtn);
return ret;
}
With these codes, I only manged to see the textview and my button is missing. Any ideas? Thanks in advance.
that depends on how do you want to arrange the items in the LinearLayout. If you want to arrange the button next to the TextView, then, probably, the button width should be WRAP_CONTENT instead of FILL_PARENT. If you want to show the button under the TextView, then, your LinearLayout should have vertical as orientation (default is horizontal). Imo, the easiest way is to have your layout defined in a xml file. At least you can see the output at compile time, and use an inflater to retrieve the View's object at run time
The default orientation of linear layout is horizontal. You need to set the orientation first.
LinearLayout ret = new LinearLayout(this);
ret.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ret.setOrientation(LinearLayout.VERTICAL);
This will solve your problem of missing button.
You have forgot to set Layout Orientation for Linear Layout, just set it as below:
ret.setOrientation(LinearLayout.VERTICAL);