I need to use scrollview pragmatically, while include linearlayout in scrollview its shows error like E/AndroidRuntime(22309): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.program/com.sample.program.details}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Check it out my coding :
My linearlayout which i want to add in scrollview
mScrollTolinear = (LinearLayout) findViewById(R.id.toscroll);
My ScrollView
ScrollView mScroll = new ScrollView(Appointmentdetails.this);
mScroll.addView(mScrollTolinear);
To remove the view
mScrollTolinear.removeAllViews();
mScrollTolinear.removeView(mScrollTolinear);
Try to to remove the view some thing like that but no luck, have tried with google also but dint get the solution, any suggestion or help highly accepted.
My XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/whitebackground"
android:orientation="vertical"
>
<!-- <ScrollView -->
<!-- android:id="#+id/favscroll" -->
<!-- android:layout_width="fill_parent" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:visibility="invisible" > -->
<LinearLayout
android:id="#+id/toscroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/topheaderbg" >
<ImageView
"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:contentDescription="#string/app_name"
android:paddingLeft="10dip"
android:src="#drawable/listbutton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#color/White"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:contentDescription="#string/app_name"
android:paddingRight="10dip"
android:src="#drawable/notificationicon" />
</LinearLayout>
</LinearLayout>
Thanks
The problem is that you are trying to add a view which was inflated from XML file hence it already has a parent (because it is already situated in view hierarchy). Instead of the following
mScrollTolinear = (LinearLayout) findViewById(R.id.toscroll);
you will have to implement something like this:
mScrollTolinear = getLayoutInflater().inflate(R.layout.your_layout_file, null);
And also keep in mind that ScrollView can only have one child view, otherwise you can get exceptions
UPD
Update corresponding to your comment
So in case if you want to add ScrollView to LinearLayout you will have to call:
ScrollView scroll = new ScrollView(yourContext); //you may also want to specify LayoutParams for it
mScrollTolinear.addView(scroll);
I hope I've understood your comment correctly
Related
I have the following Android layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:orientation="vertical" >
<TextView
android:id="#+id/slideTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:padding="#dimen/px20"
android:text="#string/login_message"
android:textSize="#dimen/px25"
android:textStyle="bold" />
<TextView
android:id="#+id/slideDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/password"
android:drawablePadding="#dimen/px20"
android:drawableStart="#drawable/password"
android:padding="#dimen/px20"
android:text="#string/login_message_body"
android:textSize="#dimen/px20" />
<TextView
android:id="#+id/swipeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="" />
</RelativeLayout>
But all the elements are positioned at the top of the screen one on top of the other, like if they didn't occupy any space.
That's not what what it seems to happen in the RelativeLayout documentation, where all elements are vertically positioned one below the other.
What's going on here?
So you need to use the id of the other components to align then properly.
For example the TextView with the id #+id/slideDescription should also have
android:layout_below="#+id/slideTitle" as one of the properties of the xml.
And the TextView with the id #+id/swipeMessage should also have
android:layout_below="#+id/slideDescription" as one of the properties of the xml.
In order to place one view below another in RelativeLayout you have to use layout_below property and set the ID of View you want to be above the specified one. But actually in order to place views vertically below each other it is more convenient to use LinearLayout with orientation set to vertical
layout_below is missed in the above xml code.I replaced the code with that please use that.
In Relative layout elemnets will be arranged relative to other elements in order to do this we should use id values of individual view elments
android:layout_below="#id/slideTitle" should be placed in description text view
android:layout_below="#id/slideDescription" should be placed in message text view
in order to get the output you desired please use the below code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp" >
<TextView
android:id="#+id/slideTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:padding="#dimen/px20"
android:text="#string/login_message"
android:textSize="#dimen/px25"
android:textStyle="bold" />
<TextView
android:id="#+id/slideDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/slideTitle"
android:drawableLeft="#drawable/password"
android:drawablePadding="#dimen/px20"
android:drawableStart="#drawable/password"
android:padding="#dimen/px20"
android:text="#string/login_message_body"
android:textSize="#dimen/px20" />
<TextView
android:id="#+id/swipeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/slideDescription"
android:text="" />
I'm answering my own question because even though I figured it out, I wasn't able to get a clear solution. This may help someone else.
I know the exception I receive is a common one, but I couldn't solve my problem with the following pages:
The specified child already has a parent. You must call removeView() on the child's parent first
java.lang.IllegalStateException: The specified child already has a parent
Call removeView() on the child's parent first
http://www.phonesdevelopers.com/1716777/
What I want to do it add a bunch of TextViews to a ScrollView, and then put the ScrollView on the screen. The problem is that the ScrollView is only part of the screen, so I can't just use setContentView(scrollView) because that would replace the buttons and things I already have.
I have an XML file that contains the template for my layout, and I simply want to replace the ScrollView with the one I generated in my Activity.
Here is the layout:
<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:id="#+id/mostParent" >
<RelativeLayout
android:id="#+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
<Button
android:id="#+id/btnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/logout" />
<ImageButton
android:id="#+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_action_refresh" />
<ToggleButton
android:id="#+id/lockToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text=""
android:textOff="#string/toggle_off"
android:textOn="#string/toggle_on" />
<TextView
android:id="#+id/loggedInAs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/refresh"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<ScrollView
android:id="#+id/postsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btnLogout"
android:layout_marginTop="30dp"
android:scrollbarSize="3dip" />
</RelativeLayout>
The ScrollView at the bottom is the one I want to add the TextViews to and it has the id postsView.
So I'm going to create a LinearLayout, add the TextViews to that layout, and then add that layout to the ScrollView. Like this:
ScrollView postsView = (ScrollView)findViewById(R.id.postsView);
LinearLayout layout = new LinearLayout(MyActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
for (final TextView post : posts) {
// add post to layout
layout.addView(post);
}
postsView.addView(layout);
But with this, I get the LogCat:
java.lang.IllegalStateException: ScrollView can host only one direct child
How can I remove the view from the ScrollView?
You can use removeAllViews() on the ScrollView. This is happening because whenever the code is being run more than once, more than one view is being added to the ScrollView.
Your code would change this:
postsView.addView(layout);
To:
postsView.removeAllViews()
postsView.addView(layout);
I'm trying to insert a LinearLayout inside another LinearLayout. I don't know if what I am doing is right or not. I need to try it this way, without using inflation.
LinearLayout address2;
address2 = new LinearLayout(this);
address2 = (LinearLayout)findViewById(R.id.sfsp2_layout);
LinearLayout teste3 = (LinearLayout)findViewById(R.id.se_contentAdressPostal);
LinearLayout teste4 = (LinearLayout)teste3.findViewWithTag("teste");
teste4.addView(address2);
LinearLayout teste3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/se_contentAdressPostal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:background="#drawable/background_tile_address_postal"
android:orientation="vertical" >
<include
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/socio_form_structured_postal" />
LinearLayout teste4
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_tile"
android:orientation="vertical"
android:tag="teste" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp_layout_horizontal"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:layout_gravity="right"
android:background="#drawable/background_tile"
android:orientation="horizontal"
android:tag="teste" >
<Button
android:id="#+id/sfsp_btStructuredPostal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:hint="#string/sfsp_btStructuredPostal" /> .......
LinearLayour address2 ( The layout that i need to insert in layout4)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp2_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_tile"
android:orientation="vertical" >
<EditText
android:id="#+id/sfsp2_etStructuredPostalApartado"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="55dp"
android:layout_marginTop="2dp"
android:layout_weight="0.14"
android:ems="10"
android:hint="#string/sfsp2_etStructuredPostalApartado"
android:inputType="textMultiLine"
android:scrollHorizontally="false" >
The LinearLayout "teste4" is inside "teste3". I need to insert LinearLayout "address2" inside "teste4". Can you tell me what I'm doing wrong?.
I think there is some misunderstanding here about how the findViewById works. I'll try to explain it below. Hope this helps, let me know if it doesn't.
There are two possible findViewById() that can be used.
Activity.findViewById(): this can be used to find a view in the current activity's main content view -- that is, the view that was set using setContentView(). It cannot be used for any other purpose. For example, it cannot find a view from any other layout file -- the function simply would have no way of knowing how to find it. As far as I can tell, this is the function you're trying to use. If the last XML file you have is not the main layout, then the line address2 = (LinearLayout)findViewById(R.id.sfsp2_layout) will fail.
View.findViewById(): this function can be used to find a view that is contained in some other view. E.g. if you have view1 that contains view2, and view2 has ID some_id, then you can find view2 by calling view1.findViewById(R.id.some_id). In order for this to work, view1 has to be fully initialized. E.g. if view1's description is in XML, it has to be fully inflated first.
In essence, if you want to work with a view that is described in a separate XML file, you have to inflate it first. I don't think there is a way around it.
I've got problem with setting visibility to relative layout. I have part of big layout in relativelayout and below that next TextView. But in my code, when myRelativeLayout.setVisibility(View.GONE);
is called, TextView which is below that did not appear. I tried several ways to rearange layout, but i need that textview under it. Thanks
My XML:
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ScrollView
android:id="#+id/scrollView_liab_ra_flipper_04"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/linearLayout_liab_ra_flipper_04"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/someTextView"
android:text="Something" />
<!-- This relative layout should be removable -->
<RelativeLayout
android:id="#+id/vg_liab_ra_04_flipper_car_container_visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/someTextView" >
<TextView
android:id="#+id/tv_1"
style="#style/WhiteFormText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="2dp"
android:text="#string/licence_plate_counter" >
</TextView>
<EditText
android:id="#+id/et_1"
style="#style/WhiteFormField"
android:layout_below="#+id/tv_1"
android:hint="#string/licence_plate_hint" >
</EditText>
</RelativeLayout>
<!-- This textview is not visible if relative layout is gone -->
<TextView
android:id="#+id/tv_liab_ra_04_flipper_mandat"
style="#style/WhiteFormTextHint"
android:layout_below="#+id/vg_liab_ra_04_flipper_car_container_visible"
android:layout_marginBottom="15dp"
android:text="#string/mandatory_field" >
</TextView>
</RelativeLayout>
</ScrollView>
</merge>
Java Code:
private void hideCar() {
if (!accident.getParticipant(0)) {
rlCarContainer.setVisibility(View.GONE);
} else {
rlCarContainer.setVisibility(View.VISIBLE);
}
}
The easiest way to do what you want is to use LinearLayout instead of your root RelativeLayout.. the child views will get realigned after you hide the RelativeLayout just the way you want.. if you don't want that, you can use android:layout_alignWithParentIfMissing on the TextView see here the documentation. That won't work exactly how you want but it might help you to solve the problem :).
Edition of question
It is already in a separate xml (tablet_shortterm_column.xml) see above.
Anyway, it seems logcat complains that rlo_shortterm_col already has a parent, so it won't allow an another ptr_rlo_rght_middle.addView(rlo_shortterm_col ). Makes no sense.
I spent so many hours on thsi problem and still can't solve it. Can someone please give me a hand? Thanks in advance.
I have an xml file (tablet_shortterm_column.xml) that contains a RelativeLayout that I need to re-use, again and again. Sometimes many times on the same screen stacked one after the other horizontally. I am attempting to insert one into an existing RelativeLayout (ie. one inside the other.)
//exerpts
public class TabletMain extends Activity {
setContentView(R.layout.tablet_main);
public RelativeLayout ptr_rlo_rght_middle;
ptr_rlo_rght_middle = (RelativeLayout) findViewById(R.id.rlo_rght_middle);
//rlo_rght_middle is in tablet_main.xml
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View llo_tmp = (View) inflater.inflate(R.layout.tablet_shortterm_column,null);
RelativeLayout rlo_tmp = (RelativeLayout) llo_tmp.findViewById(R.id.rlo_shortterm_col);
// rlo_shortterm_col is the object I want to reuse it a RelativeLayout and is inside
// tablet_shortterm_column.xml
RelativeLayout.LayoutParams rlo_layoutparams;
rlo_layoutparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlo_layoutparams.addRule(RelativeLayout.RIGHT_OF, R.id.llo_rght_middle_col1);
// llo_rght_middle_col1 is a RelativeLayout inside tablet_main.xml,
// I want to put another RelativeLayout view right next to it.
rlo_tmp.setLayoutParams(rlo_layoutparams);
ptr_rlo_rght_middle.addView(rlo_tmp); //Application crashes right on this line.
} //end Activity
//********************* content of tablet_shortterm_column.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="#+id/rlo_shortterm_col"
android:layout_width="180dp"
android:layout_height="fill_parent"
android:background="#436699"
android:orientation="vertical"
android:layout_margin="3px"
android:weightSum="1"
> <!-- android:background="#32CD32" android:layout_height="365dp" android:layout_margin="30px" -->
<Button
android:id="#+id/btn_shortterm_col"
android:layout_alignParentTop="true"
android:text="Tuesday Afternoon"
android:layout_margin="15px"
android:textSize="12px"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#296699"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
> <!--android:background="#32CD32" -->
</Button>
<ImageView
android:id="#+id/iv_shortterm_col"
android:layout_below="#+id/btn_shortterm_col"
android:src="#drawable/tblet_icon14_med"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
><!-- android:src="#drawable/tblet_shape1" android:layout_gravity="center_horizontal" -->
</ImageView>
<TextView
android:id="#+id/tv_shortterm_col1"
android:layout_below="#+id/iv_shortterm_col"
android:text="-10ÂșC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:background="#DCDCDC"
android:textColor="#000000"
android:textSize="12px"
android:layout_centerHorizontal="true"
> <!-- android:layout_gravity="center_horizontal" -->
</TextView>
<TextView
android:id="#+id/tv_shortterm_col2"
android:layout_below="#+id/tv_shortterm_col1"
android:text="Flurries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:background="#DCDCDC"
android:textColor="#000000"
android:textSize="12px"
android:layout_centerHorizontal="true"
>
</TextView>
<RelativeLayout
android:id="#+id/rlo_shortterm_col_1"
android:layout_below="#+id/tv_shortterm_col2"
android:src="#drawable/tblet_shape2"
android:background="#32CD32"
android:layout_height="113dp"
android:layout_margin="40px"
android:layout_width="125dp"
> <!--android:background="#32CD32" android:orientation="vertical" -->
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Create a separate xml file for your RelativeLayout and then inflate it as many times as you want.
llo_tmp is the parent view of the RelativeLayout you're trying to reuse. Thus, you can't add it to another ViewGroup and you get that logcat error.
You can remove the LinearLayout stuff from your xml file and inflate the xml file in the same way (though maybe instead of returned View you'd return a RelativeLayout). You shouldn't have to change much of the java code since the reference is still the same.
Or, a quick fix may be to add llo_tmp to your ViewGroup instead of rlo_tmp. Either way, rlo_tmp already has a parent and can't be reused. Since you don't have your layout fill the entire screen width, you probably don't want this.