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).
Related
please help me, how to align the height to width. I've tried everything, it is not working.
Edit: Added full layout.
The code to create the button looks like this
LinearLayout showLayout = (LinearLayout) view.findViewById(R.id.show_full_btn);
showButton = new ImageButton(activityContext);
<LinearLayout
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/count"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="#+id/w_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:typeface="normal"/>
<LinearLayout
android:id="#+id/show_full_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:scaleType="fitXY"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Thanks.
You are giving android:scaleType to a LinearLayout which is not applicable, it works with ImageButtons, so declare your view as an ImageButton instead and apply scalyType to it, or if you kinda stuck with a layout, just make as this answer says:
Try using FrameLayout with an ImageView and LinearLayout inside. For
example, try changing the alpha of the image and move it to foreground
in your FrameLayout, thus the LinearLayout stays on background.
If you're finding it difficult to get your results from the XML layout, then try to do it within the onCreate method of your activity. You can easily get it done.
You are talking about an ImageButton in your question, but have not used an ImageButton in your XML, just LinearLayout. Also, in your java, the showLayout and showButton are two separate variables, and not connected.
I have a layout contain one image and 3 text field
I've tried to align the image to right and text field to left but I've failed
I've used
android:layout_gravity="right" for image and left to text but it did not work also I've used end and start in gravity with no success
this is the layout code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:orientation="vertical"
android:background="#drawable/card_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/listthumb"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:contentDescription="Rss video thumbnail"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/listtitle"
style="#style/listTitle"
android:maxLines="3"/>
</LinearLayout>
<TextView
android:id="#+id/shortdescription"
android:layout_width="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/listpubdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11dp"/>
</LinearLayout>
</FrameLayout>
Try to use a <RelativeLayout> instead of a <LinearLayout>
With the RelativeLayout you could place a widget depending on the position of another widget
Here the Relative Layout description
Hope this will help, I have not had time to test....
One linear layout should have vertical orientation and contain the 3 text fields.
One linear layout should have horizontal orientation and contain both the above linear layout and the image.
To push two views to the edges of the screen, you can also give each a left/right margin and then put a blank view with weight = 1 in between them.
Please read a bit more on how layouts work on Android and the different types available to you. A LinearLayout will stack the containing Views either Horizontally or Vertically one after the other. A FrameLayout is simply a container and the items within have to position themselves. RelativeLayout allow you to position your views with a relative reference to other views (in your case, you can position your ImageView, and then your 3 TextViews relative to where the ImageView is).
If you can use LinearLayout instead of RelativeLayout, you should do so, as RelativeLayout is always slower, due to having to perform two passes prior to rendering as it needs to measure each view and then also perform the layouts based on that. You might be looking for something like (pseudo-code):
<LinearLayout orientation=horizontal>
<LinearLayout orientation=vertical>
<TextView />
<TextView />
<TextView />
</LinearLayout>
<ImageView />
</LinearLayout>
You have not described your question well . Check below code if it works .
You just forgot to add orientation in linear layout containing one text view and a Image view .
Add Orientation to Your Linear Layout.
In my application I have to create a button(plus). Upon clicking that button, it should append a view like shown below dynamically below another.
the view has two edittext in a row. I don't know how to do that one. Please suggest me some tutorial for it. Do I have to make custom view for that? Thanks in advance.
Create a layout to generate it dynamically, layout needs to contain your email edittext and your spinner.
Then create your main layout as one linearlayout, oriented as vertical, and one your plus button. OnClick of this button, you can inflate generic layout and add it to above linearlayout, so it will automatically push your plus button.
say this is your main layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
and this is your layout, to be added (in a separate file)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hidden_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="#+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, this is the inflated text of hidden layout"/>
<EditText android:id="#+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, this is your name"/>
</LineraLayout>
in your activity
LinearLayout main = (LinearLayout)findViewById(R.id.main_layout);
now your button.onclick code would look like
View view = getLayoutInflater().inflate(R.layout.hidden_layout, main,false);
main.addView(view);
Lets say that I have a simple XML layout such as the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/my_container"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/leftContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Col A - Text 1"
/>
</LinearLayout>
<LinearLayout android:id="#+id/rightContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Col B - Text 1"
/>
</LinearLayout>
</LinearLayout>
Then I want to add a TextView to the rightContainer LinearLayout. I am currently doing this, unsuccessfully:
LinearLayout container = (LinearLayout) findViewById(R.id.rightContainer);
TextView textToAdd = new TextView(this);
textToAdd.setText("Col B - Text 2");
container.addView(textToAdd);
I have looked at LayoutInflater, but am not sure how I would use it here. Any help would be appreciated, thanks! If I try calling setContentView(container), I receive a Force Close error.
Don't call setContentView(), if you are using findViewById() then that view is already inside of your currently set content.
Adding views works fine. All a layout XML is, is a description of the views to create and add to the hierarchy. Make sure you are passing the correct layout params when adding a view -- here since container is a LinearLayout, you want a LinearLayout.LayoutParams.
You don't say in what way your code is "unsuccessful" so it is hard to help further.
Also you can use hierarchyviewer to look at what is actually going on in your view hierarchy.
In my app, I have one (and only one) UI element which isn't referenced in the XML layout file.
That element is a button, instantiated and returned at run-time by a 3rd party library (i.e. I don't have control over that).
My problem is that I would like some of the elements (TextViews) in the XML layout file to be placed relative to that button, using RelativeLayout.
Is it possible to "reserve an empty slot" in the XML layout file for that button such that I can do something like the following?
<TextView android:id="#+id/tv_text_under_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_dynamically_created_button"
android:text="" />
Alternatively, if I were to set the layout at run-time using RelativeLayout.LayoutParams.addRule(), what would be the ID of that dynamically created button, if it has no reference at all in the XML layout file?
For example, in the following call:
layoutParams.addRule(RelativeLayout.BELOW, R.id.btn_dynamically_created_button);
What would I put instead of R.id.btn_dynamically_created_button?
Update: Thanks to the answer below, I created a place holder like this:
<LinearLayout android:id="#+id/btn_dynamically_created_button"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
The challenge now is: How to associate the returned object from getDynamicallyCreatedButton() (returned object is subclass of LinearLayout, not Button), with R.id.btn_dynamically_created_button?
EDIT: This thread seem to address a similar issue, but I am not sure that I understand the solution offered.
I'd suggest:
Put a LinearLayout with width/height set to wrap-content, horizontal orientation and zero padding as the placeholder.
Orient all the other things to that LinearLayout.
When its time to put the button, simply stick it into the LinearLayout.
See if that works for you.
EDIT: attempt at a short example:
The layout (suitably shortened): you can place other components relative to the LinearLayout with id LinearLayout01.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_marginTop="2sp" android:layout_marginBottom="2sp" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="right" style="#style/SimpleButtonBar" android:layout_below="#+id/rootlayout" android:layout_alignParentBottom="true">
</LinearLayout>
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_above="#+id/LinearLayout01" android:fillViewport="true">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/detaillayout">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The code (for example, this would go in onCreate): fetch your button (you need to make sure it has the right Context, but I figure you're doing that alright), fetch the LinearLayout, create a layout parameters object and stick your button into the LinearLayout.
Button b = getButton(); // retrieve your button somehow
LinearLayout l = (LinearLayout)findViewById(R.id.LinearLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
l.addView(b, lp);