Html.fromHtml() on TextView not working with layouts added dynamically - android

I'm using the Html.fromHtml() function to set the text of a TextView in multiple screens. All of them are of the following pattern: Name. I used the Log to see if the strings were correct, and they are.
But there's one common thing on the two cases that aren't working: i'm inflating and copying a row layout.
I'm using a LinearLayout as a row and adding multiple of these inside another linearlayout.
The row layout that is being copied:
<?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="3">
<TextView
android:id="#+id/bandName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginTop="15dp"
android:text="Band"
android:textColor="#color/darkTxtLightBg"
android:textColorLink="#color/darkTxtLightBg"
android:clickable="true"
android:linksClickable="true"
android:textSize="17sp" />
<TextView
android:id="#+id/bandOnTour"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:gravity="end"
android:text="Band"
android:textColor="#color/darkTxtLightBg"
android:textSize="16sp" />
</LinearLayout>
The LinearLayout where i insert the rows:
<LinearLayout
android:id="#+id/table_bandList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_marginTop="10dp">
</LinearLayout>
The code where i copy, populate and add these views:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout tr;
for (Band b : bandsFollowed) {
tr = (LinearLayout) inflater.inflate(R.layout.bandrow_followedlist, null);
TextView t1 = (TextView) tr.findViewById(R.id.bandName);
t1.setText(Html.fromHtml("" + b.name + ""));
t1 = (TextView) tr.findViewById(R.id.bandOnTour);
t1.setText(getResources().getString(R.string.onTour));
t1.setTextColor(getResources().getColor(R.color.bandFollowed));
tl.addView(tr);
}
What i tried:
Set all the parent layouts to android:clickable="true", but it made no difference.
Using the android:onClick attribute with a function, but that won't work as i need to.
Using a TableLayout and TableRows instead of LinearLayouts, but i had the same problem.
So, why does it work when i simply set a text to another TextView that i have, but it doesn't work when i add them dynamically? Could it be something with the inflater, the parent or even the xml file?
I know the link is there because the text is underlined, but nothing happens when i click it.

Try the following code, I have tested. Hope this helps!
TextView textView = (TextView) findViewById(R.id.textView);
textView.setMovementMethod(LinkMovementMethod.getInstance());
final String htmlString="Go to Google";
textView.setText(Html.fromHtml(htmlString));

Related

How to control EditText scrolling in dynamically added TableLayout

I am programmatically adding a TableLayout to a parent LinearLayout in Android. The table has one row with two columns; an EditText and a Button. I am also specifying that the EditText can be stretched to fill the width while the Button remains constant in size (kind of like an input-append component). Everything is fine until I populate a long text as the value for the EditText, which seems to make it stretch so that the whole value can be displayed (rather than being ellipsized). This of course means the button is pushed off the right edge, making it unusable.
The added dynamic layout:
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow>
<EditText
android:id="#+id/added_text"
android:padding="8dp"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_marginLeft="8dp"
android:layout_gravity="center_vertical"
android:text=""
androin:scrollHorizontally="true"
android:ellipsize="end"
android:enabled="false"/>
<ImageButton
android:id="#+id/added_button"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="8dp"
android:layout_gravity="center_vertical"
android:src="#drawable/minus"/>
</TableRow>
</TableLayout>
The code that populates the EditText and adds the table:
LinearLayout parent = (LinearLayout) findViewById(R.id.parent); // LinearLayout
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dynamic = li.inflate(R.layout.dynamic, null);
EditText et = (EditText) dynamic.findViewById(R.id.added_text);
et.setText("Some very long text...");
parent.addView(dynamic, 1);
What I want is simple; just to ellipsize the EditText value so that the added view honors the parent width.
Looks like adding the following to the EditText above solved it.
android:layout_weight="1"
Found it here actually:
Android: Stop EditText from going outside of tablerow

Android make Linear Layout childs flip

I have a problem with a linear layout inside a scrollview
<ScrollView
android:layout_width="fill_parent"
android:scrollbarStyle="outsideOverlay"
android:layout_height="420px">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="bottom">
<TextView
android:text="1st"
android:gravity="right"
android:textSize="100px"
android:layout_width="fill_parent"
android:layout_height="100px" />
<TextView
android:text="2nd"
android:gravity="right"
android:textSize="100px"
android:layout_width="fill_parent"
android:layout_height="100px" />
</LinearLayout>
</ScrollView>
The Textview that has text of "1st" shows on the top of the textview that has text of "2nd", is there a way that the "1st" shows on the bottom without changing the textview? because I plan to add textviews programmatically.
Adding views programmatically to a LinearLayout is fairly easy:
LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_layout);
TextView view = new TextView(/* Context and other params */);
yourLayout.addView(view);
Rather than invoking new TextView directly, it is recommended to specify an XML for your text view and use a layout inflater:
TextView view = (TextView) LayoutInflater.from(getBaseContext())
.inflate(R.id.your_textview_layout, yourLayout, false);

Adding Button To LinearLayout

I'm trying to add a button to a LinearLayout dynamically. Here is my code:
JAVA
LayoutInflater inflater = mMainActivity.getLayoutInflater();
View layout = inflater.inflate(R.layout.browse_list_fragment, (ViewGroup) getView(), false);
LinearLayout breadcrumb = (LinearLayout) layout.findViewById(R.id.browse_list_fragment_previous);
Button button = new Button(mMainActivity);
button.setText(name);
button.setTextColor(mMainActivity.getResources().getColor(R.color.action_bar_breadcrumb));
button.setTextSize(22);
button.setTypeface(Typeface.createFromAsset(mMainActivity.getAssets(), "HelveticaNeueBold.ttf"));
button.setTag(mHashMap);
breadcrumb.addView(button, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/browse_list_fragment_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/white">
<Button android:id="#+id/browse_list_fragment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="#string/button_menu"
android:textColor="#color/grey"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/carrot_grey"
android:onClick="buttonClick"/>
</LinearLayout>
<LinearLayout android:id="#+id/browse_list_fragment_previous"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingLeft="5dp"/>
</LinearLayout>
The Problem
I don't have any exceptions thrown and when I connect the debugger and step through the code I can see that the button is in fact added to the layout. I have tried inflating a button and adding it, different combinations of Java and XML code, stubbing out lines of code, using RelativeLayout as the root layout, removing different parts of the layout and using different widths and heights but I can't ge this button to show up on the screen. Can someone can see what I'm doing wrong or at least point me in the right direction? I'd greatly appreciate it.
You called inflater.inflate() with false as the last argument:
View layout = inflater.inflate(R.layout.browse_list_fragment,
(ViewGroup) getView(), false);
So you are not adding the layout to any view and thus can't see the button you added to this layout. Try to call inflate() with true or add the layout later to the view.

I need to add an EditText field with relative button when clicking on a "+" button

probably the title of my question is not really clear.
I'm developing my first Android Application for a University project. What I need now is to give the possibility of entering one "ingredient" to my users in order to look for recipes in a database. This is easy.
I read probably all the similar posts on this website but I couldn't solve my problem: now I want to give the user the possibility of specifying more than one ingredient so that there should be a "+" button below my first EditText field allowing to add new EditText fields between the first and the button itself.
Together with the EditText field I need a "-" button that, when clicked, "hide" the relative input field.
I hope the description is clear.
What I have now is my main XML layout and the second one, with edittext and "-"button...but I didn't understand how to push/inflate/show this layout in the main one...
Could you please help me?
This isthe layout I want to push:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_below="#+id/ingredient"
android:layout_height ="wrap_content"
android:layout_width="fill_parent"
android:visibility="gone"
android:id="#+id/newLine1">
<EditText
android:id="#+id/ingredient"
android:layout_width="100dp"
android:layout_height="50dp"
android:inputType="text"
android:hint="#string/hint_ingredient" />
<Button
android:id="#+id/remove"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="#string/remove"
android:layout_toRightOf="#+id/ingredient"
android:onClick="removeLine"
/>
</RelativeLayout>
This is the main layout:
<?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_height="fill_parent"
android:id="#+id/main_linear">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:scrollbars="horizontal"
android:id="#+id/scrollView"
android:layout_weight="1.0" android:fadingEdge="none">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="300dp" android:layout_height="wrap_content" android:id="#+id/ingredients">
<EditText
android:id="#+id/ingredient"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="#string/hint_ingredient" />
</RelativeLayout>
</ScrollView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height ="wrap_content"
android:layout_width="match_parent"
android:id="#+id/buttons">
<Button
android:id="#+id/add"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/add"
android:text="#string/search"
/>
</RelativeLayout>
</LinearLayout>
What about removing the new lines added by the inflator?
For now I solved like this:
public void removeLine(View v)
{
View line = (View) v.getParent();
line.setVisibility(View.GONE);
}
Is there some other way?
In the onClickListener for your "+" button you'll need to use a LayoutInflater to inflate your ingredient layout into a View. Then use findViewById() to get a reference to your ingredients RelativeLayout and add the new View to it.
Make sure that you specify LayoutParams for the new view that is inflated.
You may want to change ingredients to a LinearLayout. It is easier to add views to it than a RelativeLayout because you can just add views to a LinearLayout without having to worry about their relative position.
Good luck.
in the onClickListener to your '+' button write something like this :
int et_counter=1;
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
EditText et = new EditText(RecipeActivity.this);
//assign them an ID with an int counter
//use this counter later to refer to the EditText
//to retrieve the values
et.setId(et_counter);
ll.addView(et);
you could retrieve values like this :
for (n = 1; n <= et_counter; n++){
et = (EditText) findViewById(n);
String ingridient=et.getText().toString();
//use the text somewhere
}
Similarly you can assign an onClick listener to your '-' button, refer to the EditText with the ID using et_counter and remove it using et.setVisibility(View.GONE);
You should be able to use this with your logic. Good Luck :-)

Adding a view to a LinearLayout at a specified position

I have the following main.xml file with a LinearLayout
<?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_height="fill_parent"
android:weightSum="1" android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
I add an image to the LinearLayout via code at runtime like so
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
ll.addView(image);
However, I want to add the ImageView between the 2 TextViews in my LinearLayout. I can't seem to find a way in the android docs to add a view before another view, or after. How can I do this?
NB I call
setContentView(R.layout.main);
Before I add the ImageView to the LinearLayout.
When adding a View to a ViewGroup, you can specify an index which sets the position of the view in the parent.
You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1); to have it placed in between the two TextViews.
The docs state you can use the index to insert it where you want. I see you are using the signature of the view only, did you try the signature with the index parameter?
public void addView(View child, int index)
I faced a similar problem. In my case I wanted to add a LinearLayout at last position of another LinearLayout. To accomplish it, I did:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// add others views to your linear layout
parentLayout.addView(layout, parentLayout.getChildCount());
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imagefield);
img.setImageResource(your_image_here);
and in the xml
<?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_height="fill_parent"
android:weightSum="1"
android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<ImageView android:id="#+id/imagefield"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
Add an ImageView into the xml, and if its not being used, make it invisible (image.setVisibility(View.INVISIBLE)). It may not show anything anyway when no image is set.
To get position of view in a view group
val targetPosition = oldLL.indexOfChild(viewToAdd)
To add a view at a position in a view group
newLL.addView(viewToAdd,targetPosition)

Categories

Resources