Adding Button To LinearLayout - android

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.

Related

LinearLayout getChildAt works different in Nougat

I've this layout as first child of a TableRow:
<LinearLayout style="#style/BodyAllegati"
android:layout_width="0dp"
android:layout_weight="0.65"
android:paddingLeft="0dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/locGpsIcon"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:scaleType="center"
tools:ignore="ContentDescription"/>
<LinearLayout
style="#style/BodySegnalini"
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingLeft="0dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
I need to get the most inner linear layout (the one after the ImageView).
I did in the following way:
LinearLayout colAll = (LinearLayout) r.getChildAt(0); // outer l.layout (r is the TableRow)
LinearLayout colAllChild = (LinearLayout) colAll.getChildAt(0); // first inner l.layout
LinearLayout colSeg = (LinearLayout) colAllChild.getChildAt(1); // l.layout after the ImageView
It happens that all works correctly in older Android versions. If i run the app in Android 7, colSeg is null.
Any help would be appreciated.
I am guessing you have two layout that one in layout-v24 and you forget add layout in it.
so you must add changes in both layouts

Set Android Components Programmatically

I have an xml like below.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="#+id/etMsisdn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/allView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/msisdn"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimaryDark"
android:hint="MSISDN"
android:inputType="numberDecimal"
/>
<ImageView
android:layout_width="60px"
android:layout_height="match_parent"
android:src="#drawable/scan"/>
</LinearLayout>
</LinearLayout>
............
Another View
............
</LinearLayout>
How do I add EditText and ImageView programatically inside the horizontal LinearLayout (allView) and add the allView inside Vertical LinearLayout(etMsisdn) while keeping the same attribute as in xml.
The EditText and ImageView r supposed to below the msisdn edittext
here is your solution
LinearLayout allview=(LinearLayout)findViewById(R.id.allView);
EditText edt=new EditText(this);
ImageView img=new ImageView(this);
allview.addView(edt);
allview.addView(img);
put this in your activity
You have to use the addView method on the layout object.
But, if you want to add more times the same "sub layout" in the main layout it's better to create an xml layout with the "sub part" and add it programmatically. Let me know if the second case is what you need to provide the code.
Find LinerLayout and add views:
LinearLayout root = (LinearLayout)findViewById(R.id.allView);
root.addView(someView);
and add the attributes like this on your view:
How to programmatically set textview-s and their properties?
You need to get a reference to the outermost LinearLayout (the first one in your layout), so the best idea is to give it an Id:
<LinearLayout
android:id="#+id/myContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
Then, you need to get the reference to this layout and add the children views, like so:
LinearLayout containerLayout = (LinearLayout)findViewById(R.id.myContainer);
containerLayout.addView(yourView1);
containerLayout.addView(yourView2);
To set the desired layout alignment, you can either manually set the required LayoutParams (see this answer in SO) or you could inflate a layout and add it to your current layout, instead of two individual views (EditText and ImageView) (see this answer in SO).

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?

Dynamically adding views to RelativeLayout inside ScrollView

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.

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