0

I want to show a view that should be shown in all activities. I don't know how to inherit views in android. What i did is below, its showing the view in first activity but not in all activities. This pease of code is form my BaseActivity, please help

LayoutInflater inflater = getLayoutInflater();
View child = inflater.inflate(R.layout.custom_error, null);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

addContentView(child, params);
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57

4 Answers4

1

You could get an Android specific View in the Activity. For example the following code below will add a TextView to the Activity's content area.


    TextView tvSample = new TextView(this);
    tvSample.setText("Hello!");
    ((ViewGroup) hostActivity.findViewById(android.R.id.content)).addView(this);

Whereby hostActivity is your current Activity and android.R.id.content is a specific element (the content area, not including the ActionBar).

Alternatively, as already stated, make use of <merge> and <include> tags in your layout XMLs.

deubaka
  • 1,447
  • 12
  • 22
  • thank you for your answer. Actually i can not use and because i have listener with some condition to show this view, and i want if i change the activity this view should not hide like toast in android. – Muhammad Aamir Ali Jul 01 '14 at 05:12
  • If that is the case, you could place this code in your `BaseActivity`'s `setContentView` (but make sure to call `super.setContentView` on it), so that every time it opens an `Activity` (extending `BaseActivity`) will apply the code changes. – deubaka Jul 01 '14 at 05:51
  • Yes i did it, but then it do not set content view of child – Muhammad Aamir Ali Jul 01 '14 at 06:09
  • Sorry didn't get that. It would be something like, `public void setContentView(int resourceId) { super.setContentView(resourceId); // then the code above } ` – deubaka Jul 01 '14 at 06:58
0

you can do this with two solution for programmatically 1)After adding child view to you parent View need to call setContentView(parentView) and pass you parent layout to it. and With XMl 2) You can use include tag. follow this link will help you. http://developer.android.com/training/improving-layouts/reusing-layouts.html

chet's
  • 193
  • 2
  • 8
0

Have you tried 'include' tag of xml? It will do the job.

 <include
 android:id="@+id/container_header_lyt"  
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:layout_above=...
 android:layout_toLeftOf=...
 layout="@layout/header_logo_lyt" //Name of the xml layout file you want to include
 />

In the layout/xxxx use the name of your layout file that should be repeated.

After use the above code in your xml file like any other widget.

Minto
  • 2,004
  • 1
  • 15
  • 19
0

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

Answer by nmw

Community
  • 1
  • 1
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59