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.
Related
I wanted to find different TextViews, which are depending on an integer.
Let's say I have got three TextViews:
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
And in my MainActivity I have got following:
String resourceText = "R.id.text";
int textCounter = 1;
TextView text1 = (TextView) findViewById(resourceText + textCounter);
So the result should be that I am able to access different TextViews over the textCounter but it doesn't work.
But using this code gives me an error which says that the findViewByID function expects an integer.
You can use getIdentifier() to convert string version of a resource id .
You just need to modify your code in this way.
String resourceText = "R.id.text";
int textCounter = 1;
textViewResId=getResources().getIdentifier(resourceText+textCounter, "id", getPackageName());
TextView text1 = (TextView) findViewById(textViewResId);
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 have a java code for an apk. I want to change the color from java activity
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +
"User : "+json.getString("username")+"\n"+
"Send : "+json.getString("time")+
" & "+json.getString("date")+"\n"+
Now, I want to set 2 different color, for the "user" one color and json.getString("username") a different color as well as font weight, bold or not.
and my XML code is
<TextView
android:id="#+id/result"
android:background="#2E2EFE"
android:layout_width="match_parent"
android:textColor="#F3F781"
android:textSize="24dp"
android:layout_height="wrap_content"
android:paddingLeft="20sp"
android:text="TextView"/>
But I do not want to change the color from XML
This question already has answers here:
Android: textview hyperlink
(9 answers)
Closed 8 years ago.
In my android app how can i show hyperlinks as clickable links to open in browser .
For this I am fetching json messages from backend , saving the data in SQLite database of app and then displaying them on screen using TextView -
Fetching json messages using AsyncTask and Progress Dialog -
protected Void doInBackground(Void... params) {
//some code goes here
mMessages = json.getJSONArray(TAG_MESSAGES);
// looping through all posts according to the
// json
// object returned
for (int i = 0, length = mMessages.length(); i < length; ++i) {
JSONObject c = mMessages.getJSONObject(i);
// gets the content of each tag and put in
// database
String content = c.getString(TAG_MESSAGE);
// add field in database and update
db.addFieldInGcm(content);
}
}
In onPostExecute() i am refreshing the screen with all messages saved in database using TextView lblMessage object -
// show messages on screen
TextView lblMessage;
lblMessage.setText("");
List<String> messages = db.getAllGCMMessages();
for (int k = messages.size() - 1; k >= 0; --k) {
lblMessage.append(messages.get(k).toString() + "\n\n");
This is my XML layout for lblMessage -
<TextView
android:id="#+id/lblMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dip"
android:padding="5dip"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="16dip"
android:autoLink="all" ></TextView>
Seems like , XML android:autoLink is not applicable for strings fetched from database .
So , if while displaying messages i use something like -
if( messages.get(k).toString().contains("http://www.") )
How can i change this string in clickable hyperlinks using java ?
Thank you
Use Linkify.addLinks(textView, Linkify.ALL).
This is my working implementation after getting answers -
private void showMessage() {
// TODO Auto-generated method stub'
// show messages on screen
lblMessage.setText("");
List<String> messages = db.getAllGCMMessages();
for (int k = messages.size() - 1; k >= 0; --k) {
String message = messages.get(k).toString();
lblMessage.append(message + "\n\n");
}
Linkify.addLinks(lblMessage, Linkify.ALL);
}
and to change color of hyperlinks , i editted my xml for textview -
android:textColorLink="#69463d"
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.