9

I have the following layout with an imageview and textfield,

<?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 call this layout on top of a particular activity ,when the particular activity first starts, how to implement it with addview()?

nmw
  • 6,664
  • 3
  • 31
  • 32
teekib
  • 2,821
  • 10
  • 39
  • 75
  • Also have a look at my question https://stackoverflow.com/questions/76529993/add-view-to-android-rootview-application-without-permission – Taimoor Khan Jun 22 '23 at 11:23

3 Answers3

44

When 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);

That's a concise solution, you should remove the View by giving the RelativeLayout an id in the XML file, then remove by: rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));.

turbo
  • 1,887
  • 2
  • 21
  • 35
nmw
  • 6,664
  • 3
  • 31
  • 32
  • getting error at this line View.inflate(this, R.layout.overlay_layout. rootLayout); – teekib Jan 07 '13 at 12:10
  • Change 'overlay_layout' to whatever you've named the layout that you posted. – nmw Jan 07 '13 at 12:13
  • i did..error saying R.layout.quicktip doesn't have a field rootlayout – teekib Jan 07 '13 at 12:15
  • Ah, there was a typo - typed '.' instead of ',' - now fixed. – nmw Jan 07 '13 at 12:17
  • FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content); View.inflate(this, R.layout.overlay_layout, rootLayout); this worked – teekib Jan 07 '13 at 12:26
  • how to remove layout view when cancel button in the same layout is clicked – teekib Jan 07 '13 at 12:28
  • Set an onClickListener (google 'view setonclicklistener') on your cancel button, then put the second part of the code I listed inside onClick(). – nmw Jan 07 '13 at 12:32
  • i did that ..thanks...can u suggest how i can disable calling activity layout untill the my trasparent layout is on top? – teekib Jan 07 '13 at 12:42
  • Not sure what you mean, post another question with more detail. – nmw Jan 07 '13 at 12:44
  • now when my trasparent layout is on top of the activityA, the actual layout of the activityA is still functional, how to diable it. – teekib Jan 07 '13 at 12:47
  • 1
    See http://stackoverflow.com/questions/14181394/how-to-disable-all-child-elements-of-view-to-capture-mouse-touch-event. If you don't understand how to do what that post explains, then ask another question. – nmw Jan 07 '13 at 12:52
  • @nmw Have a look at my question https://stackoverflow.com/questions/76529993/add-view-to-android-rootview-application-without-permission – Taimoor Khan Jun 22 '23 at 11:23
2

Please use the following code to add the 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));
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
1

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

android2013
  • 415
  • 2
  • 5
  • my calling activity dont have framelayout, without framelayourt can i use? – teekib Jan 07 '13 at 11:51
  • You can put a FramLayout in calling activity xml file (topmost node ) , as you need to put new view on top of current activity otherwise it will be added after last view . – android2013 Jan 07 '13 at 11:59