How to align dynamically created Linear Layout to right of its parent? - android

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"

Related

How can i create multiple RelativeLayouts(with existing xml layout) in an LinearLayout programmatically?

I want to put multiple RelativeLayouts in one LinearLayout programmatically. Instead of using a ListView. My layout XML looks like following:
<RelativeLayout 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:background="#drawable/background_select_app" >
<ScrollView
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#80000000" >
<LinearLayout
android:id="#+id/parent_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
and following is the RelativeLayout which I want to add as child in LinearLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="40dp"
android:background="#drawable/row"
android:gravity="center" >
<TextView
android:id="#+id/name_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#333333" />
</RelativeLayout>
Can i do it programmatically? Thanks in advance.
try like this:
public void testgenerate() {
LinearLayout rootLaout=findViewById(R.id.idofliner);
LayoutInflater layoutInflater = getLayoutInflater();
for(int i=0;i<10;i++){
RelativeLayout inflate = (RelativeLayout) layoutInflater.inflate(R.layout.relativelayutid, null);
rootLaout.addView(inflate);
}
}
Usually there is no need to nest a RelativeLayout in your LinearLayout, you can achieve the same as Linearlayout with just a single RelativeLayout (so your view hierarchy has one Layout less to traverse --> improves performance ) or if you simply want to align your elements verticaly / horizontally than use only one LinearLayout instead of an extra RelativeLayout
Im not sure what you want to do, but to me it sounds like that RecyclerView or ListView is what you are looking for?

setting linearlayout height does not work

I have a listview inside a linear layout whose height is set as "WRAP_CONTENT", so the list is appearing inside the linear layout, when i populate the list, i attach some hidden list items within each list row, i want to show these items onListitemClick listner,
Everything is working fine,but if i have only one list child, then i have to scroll the view to see that layout that is visible now,
My question is:
I want to increase the height of that linear layout when the list item is clicked and and the view is visible..
here is xml..
<LinearLayout
android:id="#+id/listContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
//used this relative layout to set different backgroung for each time..
<RelativeLayout
android:id="#+id/listHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/categoryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</RelativeLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
When the hidden item is visible, then i use this code to increase height,
LayoutParams paramsLinearLayout;
LinearLayout layoutContainer;
layoutContainer = (LinearLayout) findViewById(R.id.listContainer);
paramsLinearLayout = layoutContainer.getLayoutParams();
layoutContainer.setMinimumHeight(paramsLinearLayout.WRAP_CONTENT);
Use this piece of code :
LinearLayout linear = new LinearLayout(getContext());
LayoutParams linearParams=new LayoutParams(LayoutParams.MATCH_PARENT,Height what you want);
instead of :
LayoutParams paramsLinearLayout;
LinearLayout layoutContainer;
layoutContainer = (LinearLayout) findViewById(R.id.listContainer);
paramsLinearLayout = layoutContainer.getLayoutParams();
layoutContainer.setMinimumHeight(paramsLinearLayout.WRAP_CONTENT);
I think you should not have a view with height match_parent inside a layout with height wrap_content, as in your case:
<LinearLayout
android:id="#+id/listContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
//used this relative layout to set different backgroung for each time..
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Can't you set linearlayout's height to match_parent as well?

add listView and imageView at its end

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

ImageView on a GridView

I have a GridView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:orientation="horizontal" >
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/DarkSlateGray"
android:layout_weight="1"
android:columnWidth="200dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="none" >
</GridView></LinearLayout>
I want to add an ImageView on top of this.
Tried <include>:
<include layout="#layout/connection"/>
connection.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_connection"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:src="#drawable/globe_connected_icon_48"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /></RelativeLayout>
But result looks like this:
Is there a way to show it on top of the GridView like this:
Change the Linear Layout to a Relative Layout, then have both the grid view and the image view align to the top left of the parent. Make sure the the image view is below the grid view so that it appears on top.
Change your LinearLayout orientation to vertical and place the ImageView above the GridView code. For LinearLayout you can provide attributes to define where in the layout a view will be, such attributes: gravity and layout_gravity. RelativeLayout provides similar attributes: ex.layout_alignParentTop. Also, use android:background="#null" in your ImageView if you want the white boarder around your image to be transparent.

how to make a linear layout horizontall scrollable

My application containing different layouts.one of them is a linear layout.it's content is dynamically adding.i want to make this layout horizontally scrollable.for this i have put my layout in a scroll view.but still it s not scrolling...given below is my code
<LinearLayout android:id="#+id/scoreballparent_layout"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_above="#+id/score_layout">
<ScrollView android:layout_height="wrap_content"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
>
<LinearLayout android:layout_width="fill_parent"
android:id="#+id/scoreball_layout"
android:layout_height="wrap_content"
android:isScrollContainer="true"
android:scrollbars="horizontal">
</LinearLayout>
</ScrollView>
</LinearLayout>
Use HorizontalScrollView instead
Also your layout will become scrollabe after its contect can't fit layout area.
Simply use this in xml
android:fadeScrollbars="true" may be it works for you

Categories

Resources