LinearLayout addView doesn't work properly - android

I'm having issues with the addView method from LinearLayout. I don't know why, but if I add three views only the first one is displayed. Here is the code:
Comment[] comments = posts[position].getComments();
LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.post_list_item_comments);
layout.removeAllViews();
for(int i=0; i<comments.length; i++) {
View comment = inflater.inflate(R.layout.post_list_item_comment,null);
((TextView)comment.findViewById(R.id.post_list_item_comment_name)).setText(comments[i].getFrom().getName());
((TextView)comment.findViewById(R.id.post_list_item_comment_message)).setText(comments[i].getText());
layout.addView(comment,i);
}
I've tried with addView(comment) too, but with the same result.
This is the code of the View that I retrieve when to use the findViewById mehthod.
<LinearLayout
android:id="#+id/post_list_item_comments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="40dip"
android:layout_below="#id/post_list_item_footer_text"
android:visibility="gone"/>
And this is the XML that I inflate:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View
android:id="#+id/post_list_item_comment_divider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#drawable/divider"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"/>
<ImageView
android:id="#+id/post_list_item_comment_photo"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:layout_below="#id/post_list_item_comment_divider"
android:adjustViewBounds="true"/>
<TextView
android:id="#+id/post_list_item_comment_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/post_list_item_comment_photo"
android:layout_below="#id/post_list_item_comment_divider"
android:maxLines="2"
android:ellipsize="end"/>
<TextView
android:id="#+id/post_list_item_comment_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/post_list_item_comment_photo"
android:layout_below="#id/post_list_item_comment_name"
android:textSize="13sp"
android:maxLines="2"
android:ellipsize="end"/>
</RelativeLayout>

you do not declare the LinearLayout orientation... by default is Horizontal, try setting the orientation Vertical
<LinearLayout
android:id="#+id/post_list_item_comments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="40dip"
android:layout_below="#id/post_list_item_footer_text"
android:visibility="gone"/>

Use the hierarchy viewer to check if there really is only 1 view added, or that you can see only one view. For instance, this line android:layout_below="#id/post_list_item_footer_text" might be troublesome if you repeat it? I don't know the expected behaviour for that...

Related

Android: ScrollView does not re-size when adding child views

I have a ScrollView that I am trying to populate dynamically. Here is the XML that contains the ScrollView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f6f5f5" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#40ff0000"
android:paddingBottom="70dp"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<LinearLayout
android:id="#+id/option_list_container"
android:background="#40ffff00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="#292929"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:text="Stap 3/5"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
</LinearLayout>
</ScrollView>
And here is the XML for the element I'm inserting in the ScrollvVew:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<np.custom.FontIcon
android:id="#+id/option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/calendar_circleround"
android:textSize="40sp"
android:gravity="center"
android:text="#string/planit_selection_empty_circle"
android:textColor="#292929" />
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
This is the code I use to populate the scrollview:
ArrayList<String> g = new ArrayList<String>();
g.add("Fix a Bug");
g.add("Buy a new PC");
g.add("Make Coffee");
g.add("Take a Break");
g.add("Don't do that");
g.add("Throw your hands in the air like you just don't care!");
LinearLayout optionListLayout = (LinearLayout) rootView.findViewById(R.id.option_list_container);
LayoutInflater inflater = LayoutInflater.from(getActivity());
for(String p:g){
View v = inflater.inflate(R.layout.planit_option_item, null);
TextView optText = (TextView) v.findViewById(R.id.option_text);
optText.setText(p);
optionListLayout.addView(v);
}
This all works almost fine, except that the sizes of the ScrollView and the LinearLayout it contains do not come out as I expected. This a screenshot of that it looks like on a device, which shows text being cut out when it goes to a second line:
So how can I make sure the linear layout re-sizes to accommodate children views of different heights?
Your issue isn't the ScrollView not accommodating new children. The problem is that the text, when it spans more than one line, is being clipped due to layout parameters in R.layout.planit_option_item. Try changing the TextView of ID #+id/option_text so that its height is wrap_content.
Change your TextView for the ScrollView element to accommodate two lines:
...
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:maxLines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
...
This is not an issue with ScrollView, it's an issue with the specifications of the element you are inserting into the ScrollView. If you expect there may be more than 2 lines of text in some cases set maxLines = X where X is the maximum number of lines you expect.
Please let me know if this does not work and I would be happy to help further.

Add Button to XML Layout programatically

This problem has been asked many times here, but none of the solutions work for me, because my XML layout is a bit different.
I have an XML with LinearLayout containing an ImageView and a TextView. The XML is populated using Java code. It is a file picker library from here, it lists the files and folders that exists in the android filesystem.
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/file_picker_image"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/file" />
<TextView
android:id="#+id/file_picker_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:singleLine="true"
android:text="#string/file_name"
android:textSize="28sp" />
</LinearLayout>
Now, I want to add a button just below this list. When I try to add the button in the XML, it iterates though each row, and buttons are shown in every row.
When I do it using Java code, neighter the button, nor the textview is displayed, but the image view is displayed.
What I want is similar to the image in this post, but cannot achieve it till now. None of those solutions or any other that I read here works in my case.
I am trying to add the button using this code in the Activity whose source is in the link provided above. I tried to add the following code in FilePickerListAdapter's constructor.
RelativeLayout relLayout = new RelativeLayout(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Button filterBtn = new Button(context);
filterBtn.setText("Filter");
relLayout.addView(filterBtn, params);
Please guide me what am I doing wrong, and how to make it possible.
Regards.
Mixing the xml way and programmatic way to create views is not a good idea. You might as well create all views in xml first (including your button), only set your button to android:visibility="gone" to hide it. Set it to visible when you want.
This might not be something you asked for, but it's an alternative that you can try it out. I implemented layout_weight into your xml and included the button at the end of the screen. The outcome of this will be something similar with the link in your question.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight=".9" >
<ImageView
android:id="#+id/file_picker_image"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/file" />
<TextView
android:id="#+id/file_picker_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:singleLine="true"
android:text="#string/file_name"
android:textSize="28sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.1" >
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> <!--Your Button-->
</LinearLayout>
</LinearLayout>

enabling scrolling for textview

I have a following xml for my activity.
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Activity1" >
<RelativeLayout
android:id="#+id/titleStoryRelativeLayout"
android:layout_weight="#string/titleWeight"
android:layout_width="fill_parent"
android:layout_height="0dip">
<TextView
android:id="#+id/titleStory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/contentTextViewLayout"
android:layout_weight="#string/contentTextViewWeight"
android:layout_width="fill_parent"
android:layout_height="0dip">
<TextView
android:id="#+id/textViewStory1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/footerStoryRelativeLayout"
android:layout_weight="#string/footerWeight"
android:layout_width="fill_parent"
android:layout_height="0dip>
<Button
android:id="#+id/buttonPrevious"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:onClick="loadPrevious"
android:text="#string/stringButtonPrevious"/>
<Button
android:id="#+id/buttonNext"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:onClick="loadNext"
android:text="#string/stringButtonNext"/>
Here I am loading my Textview (#+id/textViewStory1) dynamically from my code with some text. The problem is that during loading, few lines go beyond the screen. I am planning to enable scrolling so as user can scroll down to view those missing lines. But the scrolling should only be limited till the last line and not beyond. Any suggestion on how can I set that?
Set these settings for your textview:
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
You can then set it to be scrollable in java:
TextView.setMovementMethod(new ScrollingMovementMethod());
Try to use ScrollView (official documentation)
You can limit the Height of TextView to match the number of lines you want to see, after that put your TextView in a ScrollView
I had done a simple example to demonstrate this...
<ScrollView android:layout_height="30dp"
android:layout_width="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:textSize="16sp"
android:id="#+id/tv1"
/>
</ScrollView>
Observe how I fixed ScrollView's height and matched TextView's height to it.
I had answered this exact question before here , please check for existing answers on SO before asking them again.

View layout after setvisibility

I've got problem with setting visibility to relative layout. I have part of big layout in relativelayout and below that next TextView. But in my code, when myRelativeLayout.setVisibility(View.GONE);
is called, TextView which is below that did not appear. I tried several ways to rearange layout, but i need that textview under it. Thanks
My XML:
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ScrollView
android:id="#+id/scrollView_liab_ra_flipper_04"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/linearLayout_liab_ra_flipper_04"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/someTextView"
android:text="Something" />
<!-- This relative layout should be removable -->
<RelativeLayout
android:id="#+id/vg_liab_ra_04_flipper_car_container_visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/someTextView" >
<TextView
android:id="#+id/tv_1"
style="#style/WhiteFormText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="2dp"
android:text="#string/licence_plate_counter" >
</TextView>
<EditText
android:id="#+id/et_1"
style="#style/WhiteFormField"
android:layout_below="#+id/tv_1"
android:hint="#string/licence_plate_hint" >
</EditText>
</RelativeLayout>
<!-- This textview is not visible if relative layout is gone -->
<TextView
android:id="#+id/tv_liab_ra_04_flipper_mandat"
style="#style/WhiteFormTextHint"
android:layout_below="#+id/vg_liab_ra_04_flipper_car_container_visible"
android:layout_marginBottom="15dp"
android:text="#string/mandatory_field" >
</TextView>
</RelativeLayout>
</ScrollView>
</merge>
Java Code:
private void hideCar() {
if (!accident.getParticipant(0)) {
rlCarContainer.setVisibility(View.GONE);
} else {
rlCarContainer.setVisibility(View.VISIBLE);
}
}
The easiest way to do what you want is to use LinearLayout instead of your root RelativeLayout.. the child views will get realigned after you hide the RelativeLayout just the way you want.. if you don't want that, you can use android:layout_alignWithParentIfMissing on the TextView see here the documentation. That won't work exactly how you want but it might help you to solve the problem :).

Programmatically add Views inside a (Linear)Layout (that is inside a ScrollView)

I have an app that, after some clicking, shows activity with news contents. I want at a bottom of that to show comments, which are dynamically loaded in async task.
One approach is to use ListView and custom ArrayAdapter, but, i would have to put ListView inside a ScrollView, and it is is a problem, even if i manually override ListView's height. It shows one list item, and one part of the following one.
The other approach is to define LinearLayout as a holder for comments, than inflate another LinearLayouts defined in it's own xml file, populate it's contents, attach to holder's view. It works fine, from program's point of view, except it pushes comments below contents of the news. It's like, it creates another scrollable view of comments underneath contents od news (contents overlap it).
Is there any other way of doing this that has nothing to do with lists, or how can i make the other approach to work.
The relevant code snippet from xml layout that holds news contents is:
<LinearLayout> //global layout
<LinearLayout>
//views that hold title, date, content
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/commentsHolder"
android:orientation="vertical">
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#ed1b24"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="KOMENTARI"
android:layout_marginLeft="5dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#ed1b24"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:id="#+id/comments_holder_view"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</LinearLayout>
and code that corresponds to one comment is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/comments_holder_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/comment_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/comments_back_details"
android:text="Ovde ide tekst komentara, koji se, naravno, dinamicki dodaje."/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/comment_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/categoryStyle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" | "
style="#style/categoryStyle"
/>
<TextView
android:id="#+id/comment_pubdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/categoryStyle"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:orientation="horizontal"
android:layout_margin="5dp"
android:background="#d1d1d1">
</LinearLayout>
Thank you very much for any reply. This is quite important for me.
I am not very sure how you inflate your comments, but here is how I would do it.
Declare the comments Layout as a LinearLayout and give it an id, and then get a reference to that LinearLayout :
LinearLayout commentsLayout=(LinearLayout)findViewById(R.id.commentsLayoutId);
and then just add child Views to this layout :
commentsLayout.addView(newComment);

Categories

Resources