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);
Related
I am trying to align dynamically created Linear layout to right of its parent which is a ListView. The code of list view is
<LinearLayout
android:id="#+id/mychatlinear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="7"
android:orientation="horizontal"
android:padding="5dp" >
<ListView
android:id="#+id/chatmainlist"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:divider="#color/ripple_material_dark"
android:dividerHeight="5dp"
android:padding="5dp"
tools:listitem="#layout/chatrecieve" >
</ListView>
</LinearLayout>
Now in the inner class extending the BaseAdapter i have used
arg1 = getLayoutInflater().inflate(R.layout.chatitem, null);
LinearLayout layout = (LinearLayout) arg1;
This layout is aligned to the left in the list view. I want it to aligned to right. Please suggest some solution. Thanks
Change LinearLayout in chatitem.xml add android:gravity="right"
I want to create the layout to be like what in the diagram
how can I make the listView Scroll independently and make the textView and ImageView stead?
just replace your xml layout like below code:
<?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:background="#android:color/white"
android:padding="5dp"
android:weightSum="2"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:layout_gravity="right"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/yourimg" />
</LinearLayout>
above xml given output below screenshot:
For the Lower Steady Image View you can use : Create a footer view
layout consisting of text that you want to set as footer and then try
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);
For Header: The solution that works for me is to create a TableLayout
that has a row with the heading and a row with the list like this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/header" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</TableLayout>
Please make sure that the ListView has #android:id/list as the ID.
OR
Rather google adding header and footer to listview in android that
will help u much better
Sorry for the pseudo code, I can try a better attempt later on, but you need:
RelativeLayout
TextView - align top
ImageView - align bottom
ScrollView - align above ImageView, align below TextView
Create a linear layout with orientation as vertical. Add textview listview and imageview with the weight as you want. Remember to give layout_height as 0dp for all the three components.
Add Header And Footer To the Listview,Like
ListView mList=getListView();
mList.addHeaderView(View v);//Your textView
and
mList.addFooterView(v);//your imageview
Considering this LinearLayout containing..
One nested Linear Layout with two child views
one imageView
one Textview
One textView
One Button
Why can I successfully show text content on the outer TextView using findViewById and setText while the inner Textview (inside nested linear view) shows blank?
Here is the code for onCreate:
LayoutInflater inflater = LayoutInflater.from(Item.this);
LinearLayout itemLayout = (LinearLayout)inflater.inflate(R.layout.activity_item, null);
Button proformaButton = (Button)itemLayout.getChildAt(2);
roformaButton.setOnClickListener(this);
setContentView(itemLayout);
TextView titleTitla = (TextView)findViewById(R.id.item_title);
TextView itemBody = (TextView)findViewById(R.id.item_body);
titleTitla.setText("Tomatoes");
itemBody.setText("Potatoes");
While debugging I can see that the view is found by Id and I can also see that setText does it work (mText field of TextView), but the activity does not show anything for item_title.
I am asking how to fix this but if it's not a trivial thing a hint of explanation would be very appreciated.
If it helps, here's the xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:id="#+id/item_picture"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:contentDescription="#string/image_item">
</ImageView>
<TextView
android:id="#+id/item_title"
android:layout_width="0dip"
android:layout_height="fill_parent">
</TextView>
</LinearLayout>
<TextView
android:id="#+id/item_body"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2">
</TextView>
<Button
android:id="#+id/btnOrderItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/order_button"
/>
</LinearLayout>
<TextView
android:id="#+id/item_body"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2">
</TextView>
It's the layout_height="0dp". The reason it works on the other LinearLayout is because you have a weight attribute. Change the 0dp to wrap_content.
I am trying to add dynamically created several RelativeLayouts into a LinearLayout which is inside a RelativeLayout, which is inside a ScrollView. When the total height of the all views exceed the size of the phone screen, all views are displayed correctly. But when the total size of dynamically added views is not enough for filling the screen, only the first RelativeLayout element is shown and the others are not displayed in the screen. I am really hopeless and do not understand why.
Here is the code to dynamically populate views inside linear layout:
LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout);
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(Comment c: commentsList) {
RelativeLayout layoutItem = (RelativeLayout) inflater.inflate(
R.layout.list_item_comment, null, false);
TextView tv = (TextView) layoutItem.findViewById(R.id.textView);
ImageView iv = (ImageView) layoutItem.findViewById(R.id.imageView);
// set tv's text
// set iv's image and onclicklistener, nothing fancy here, everything goes well
commentsLayout.addView(layoutItem);
}
Here is list_item_comment.xml:
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:textSize="16sp"
android:layout_toRightOf="#id/imageView"
/>
</RelativeLayout>
And here is the xml file for this activity:
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/main_layout"
>
...
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:id="#+id/scrollView"
>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/relativeContainer"
>
...
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/comments_layout"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
And the screenshots:
Without sufficient layouts: (INCORRECT, needs to show 3 comments)
With enough layouts: (CORRECT ONE, screen is filled)
I just need to show all three comments in the first case :/ Thanks in advance.
instead of fill_parent, try changing the layout_height of the <RelativeLayout> of your list_item_comment.xml to wrap_content.
Also, why do you need another <RelativeLayout> inside your <ScrollView> of the xml of your activity. The LinearLayout is sufficient to do what you want your activity to look like. Maybe you can just remove it.
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)