Android: addview () - adding a new view on top of activity

I have the following layout with an image and a text box,

<?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:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="26dp" android:layout_marginTop="22dp" android:src="@drawable/a01" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/imageView1" android:layout_marginTop="31dp" /> </RelativeLayout> 

This layout will be transparent, and I want to name this layout on top of a specific action when a specific work begins, how to implement it using addview() ?

+4
source share
3 answers

If you want to show it:

 FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content); View.inflate(this, R.layout.overlay_layout, rootLayout); 

Then when you want to remove it:

 FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content); rootLayout.removeViewAt(rootLayout.getChildCount()-1); 

This is a short solution, you must remove the View by specifying the RelativeLayout identifier in the XML file, and then delete: rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout)); .

+33
source

The layout of your call activity should be inside FrameLayout (since in this layout the last added view will always be on top of the previous view), in onCreate, the calling activity method inflates this layout using LayoutInflater and directly uses the addView activity method.

+1
source

Please use the following code to add a view.

 LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = vi.inflate(R.layout.your_layout, null); // fill in any details dynamically here TextView textView = (TextView) v.findViewById(R.id.a_text_view); textView.setText("your text"); // insert into main view View insertPoint =(View) findViewById(R.id.insert_point); // edited. insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 
0
source

All Articles