How to hide framelayout dynamically in android - android

I want to hide framelayout dynmically in android, How I can achieve this.

provide an id attribute to your frameLayout by defining it in xml file as:
android:id="#+id/someID"
and in code, write following:
FrameLayout layout = (FrameLayout)findViewById(R.id.someID);
layout.setVisibility(View.GONE);
You can also use
View.INVISIBLE
which means the element will be still there.

Change the visibility like this:
FrameLayout layout = (FrameLayout) findViewById (R.id.your_id);
layout.setVisibility (View.GONE); // or View.INVISIBLE, depending on what you exactly want

You can hide or show views using setVisibility(int).

Related

How to programmatically hide adview in Android?

Is there a way to programmatically make the visibility of an adview == false?
e.g. in XML I am able to do this;
android:visibility="invisible"
However, I want to achieve this in Java - I have tried this but it doesn't seem to work;
adView.getVisibility().set(false);
adView.setVisibility(false);
adView.setVisibility("invisible");
You should use View.INVISIBLE
adView.setVisibility(View.INVISIBLE);
INVISIBLE
This will just hide the view.
adview.setVisibity(View.INVISIBLE);
GONE
This will completely hide the view from ParentLayout
adview.setVisibity(View.GONE);
You can call adView.setVisibility(View.GONE) if you want to remove it from the layout.
Or adView.setVisibility(View.INVISIBLE) if you just want to hide it.

Show custom view in all activities

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

I cannot setVisibility and specific layout from onCreate() in android but in onClick you can do it?

When I get a layout from xml and use setVisibility(INVISIBLE) I cannot write that in the onCreate() method but inside action Listener like onclick you can write the code setVisibility and hide the layout, how come I cannot write and set visibility of that layout in onCreate?
You don't have View's view object inside the onCreate() obviously you should specify it like setVisibility(View.INVISIBLE);
Where as in onClick(View v) it already passes the View's view object so no need to specify there. simply you can use that.
I hope it would help you
Use setVisibility(View.INVISIBLE) .
After setContentView() in onCreate(), find the View on which you want to call setVisiblity().
setContentView(R.layout.activity_main);
Output = (TextView) findViewById(R.id.Output);
Output.setVisibility(View.VISIBLE);
You can set Layout visibility in android to (visible/invisible/gone) by the XML code like: android:visiblity = "invisible"
Or you can use the Attributes list, example:screenshot of "Option" in Attributes list to set the visibility of a layout.

edit layout of existing child in RelativeLayout

I have a RelativeLayout with a number of children. The RelativeLayout is the main layout of my activity. I need to add to one of the children the layout value layout_alignParentBottom="true". I need to do this programmatically. the link How to set the android property layout_alignParentBottom to a button dynamically is not working for me because, basically, I don't want to have to inflate the RelativeLayout as I already have it at the setContentView(...). Any ideas how to do this?
I figured it out:
RelativeLayout.LayoutParams layout = (LayoutParams) theChild.getLayoutParams();
layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

Is it possible to have another layout in my main layout?

Is it possible to have another layout in my main layout?
Such that i can set my imageview in another layout.
You can create a child layout from your Java code and then use addView() method.
For example:
LinearLayout l = new LinearLayout(this);
l.addView(new LinearLayout(this));
Sure, take a look at this blogpost.
http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html

Categories

Resources