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.
Related
My question is similar to this
How to stop a GridView from cropping my images?
Iam Using Flowlayout for creating Tag Cloud, I was able to create it, Iam inflating the text view since the text can vary, I have seen all the examples which uses new line has a fixed layout xml. how to implement new line in inflated textview.
It appears that the new line flag on the FlowLayout.LayoutParams object is not modifiable at runtime. You will either need to modify the library itself to add this ability or keep a separate layout file for when you need to inflate a TextView with the newline xml.
If you are looking to break the text across two lines and continue to use the FlowLayout, you will need to inflate two separate TextViews one for the first line and one for the second. You will also need to manually calculated the amount of text you can fit on screen to do this.
Parent layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.customns"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".CustomActivity"
android:orientation="vertical">
<com.example.customns.FlowLayout
android:id="#+id/loadlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
</com.example.customns.FlowLayout>
</RelativeLayout>
Layout to be inflated in the parent
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.customns"
android:id="#+id/buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/blue"
android:orientation="horizontal" >
<TextView
app:layout_newLine="true"
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
layout_newLine is not working in inflated layout, which works if i have all the textview in the parent layout. any suggestion please
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);
I'm trying to place a TextView at the bottom of every activity by using the include... My code is below... I don't know why, but it remains at top... Although it works fine for TextView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
android:orientation="vertical" />
<include
android:layout_alignParentBottom="true"
android:id="#+id/footer"
layout="#layout/footer" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:text="Hello"
android:textColor="#000000"
android:textSize="15dp" />
¿Maybe you are using a LinearLayout instead of a RelativeLayout? The latter doesn't have android:orientation property. We don't see the parent XML node, so just a guess
Also, you'll need to place the include at the bottom and the TextView above it
<TextView
...
android:layout_above="#id/footer" />
Right now you are overlapping both views
Another suggestion is to use FragmentActivity and a Fragment to handle the Footer, but that's out of the scope of this question.
Use RelativeLayout in your same xml code as the parent layout.
Well i got the solution.... I had to add the include inside another child relative layout...This is what i did..
enter code here
RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<include
android:id="#+id/footer"
android:layout_alignParentBottom="true"
layout="#layout/footer" />
</RelativeLayout>
but...for a textview it works directly without using another child layout...So why do you need to do this for an include.....????:|
I checked your code. It works fine as posted in 1st post.
Please note that when you use layout tag then Eclipse' Graphical Layout won't draw the child xml-layout you've included (in your case footer.xml).
At the same time, you'll get a footer like you want if you run same code on actual device or emulator.
NOTE: you might want to remove android:layout_alignParentBottom="true" from textView1 as currently textView1 and footer layouts overlaps.
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);
I have made myself a custom LinearLayout by the name of com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget that hosts a couple of spinner widgets. This custom LinearLayout inflates the following XML layout (You might not need to look at this too carefully but it's here for completeness):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Min value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_min_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/to_text"
android:text="to"
android:textSize="14sp"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="0">
</TextView>
<!-- Max value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_max_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1" />
I have placed one such object in the layout for one of my activities like so:
<?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="vertical">
<include layout="#layout/search_form_section_generic_top"/>
<include layout="#layout/search_form_section_car_specific"/>
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
<include layout="#layout/search_form_section_advanced_options" />
</LinearLayout>
The problem is that my app force closes immediately upon startup. I've checked by putting breakpoints in my custom LinearLayout that none of my custom code is even being run yet. Furthermore, if I copy-paste the layout code for my compound widget in place everything works, which indicates to me that I probably haven't left any important XML attributes out. What could be going wrong?
I fixed it by making the LinearLayout XML element in the widget layout into a merge instead, and moved all of the layout parameters out of the widget XML file and into the activity XML itself, thus replacing
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
with
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
If someone could tell me why this worked, it might help me and others doing it again, and you can take credit.
because you must specify the width and height of every view you use in you xml?