i'm using following code to set "visibility=gone" for a linear layout
//onCreate method
//setcontentview
. . . .
LinearLayout rlayout1 = (LinearLayout) findViewById(R.id.readerBottomLayout);
rlayout1.setVisibility(2);
But the controls are still visible when activity runs.why? any idea?
set rlayout1.setVisibility(View.INVISIBLE)
Yes, view.GONE and view.INVISIBLE will work. The reason it didn't work before is because two (2) is the incorrect integer value.
The correct values for set.Visibility are:
0 = visible
4 = invisible
8 = gone
you can also do something like this:
Get Parent layout object through its id
Get layout which you want to remove/hide through its id
parentlayout.remove(childlayout);
Related
In Android application,
I create a LinearLayout which contains a TableLayout which contains some rows and one of the rows contains a TextView called mText1
I set up a pop up window with this LinearLayout with mText1 setting as following :
mText1.getLayoutParams().height = OLD VALUE
Then when in the pop up window in one other row of the table a click on an ImageView happens by the user, I'd like to change the height of mText1 as following :
mText1.getLayoutParams().height = NEW VALUE
The problem is this will not take effect but when I type the following code, it takes effect:
mText1.setGravity(Gravity.CENTER)
Or if I type the following code instead of the previous one, it takes effect too:
mText1.setText("some value")
Why?
You need to call requestLayout() after changing the LayoutParams.
requestLayout()
Call this when something has changed which has invalidated the layout
of this view. This will schedule a layout pass of the view tree.
setGravity and setText are handling this for you.
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.
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).
I have bookmark ImageButton with ALIGN_WITH_PARENT set to true in XML.
If I programatically do (I want to remove that rule)
RelativeLayout.LayoutParams params = (LayoutParams) bookmark.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
bookmark.setLayoutParams(params);
The rule just doesn't apply (in my layout I can see that rule is still active) -> layout isn't good. If I manually do it (some sort of "simulation", set ALIGN WITH PARENT to false) in my Layout Editor, my layout is fine which leads to this code up there. Something is wrong.
What?
(I believe temporary) Solution is to wrap up that ImageButton in one dummy RelativeLayout and then take the params of that dummy layout and add or remove rules. That way it's working just fine.
Insead of calling setLayoutParams(), try to use requestLayout() method:
bookmark.requestLayout();
From API docs:
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.
simply call removeRule() function on params.
for example,
params.removeRule(RelativeLayout.CENTER_IN_PARENT);
Why is
TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();
generating a null pointer exception? The id is correct, testTextView is correctly declared in my XML layout file.
The only reason for findViewById to return null if you are passing a valid id is that you are either setting the wrong content view (with setContentView) or not setting a content view at all.
I think you might have written setContentView(..) after defining the TextView. Reverse these, and it should work.
Change:
TextView test = (TextView) findViewById(R.id.testTextView);
.
.
setContetView(..)
To:
setContetView(..)
.
.
TextView test = (TextView) findViewById(R.id.testTextView);
You probably haven't called setContentView. You can only use findViewById to get elements of views that have already been inflated.
You could also use a layoutinflater to inflate the view, but that's probably not what you want.
Are you sure the TextView is set on the right XML?
For example if you're creating a Dialog that loads a custom XML, to get an element from that xml you have to mention it in dialog.findViewById(R.id.testTextView);
Like Falmarri said, the view has to be inflated.
I understand you solved it by creating a new project, but still thought to mention it for future users.
It can also be that you defined the activity in two files. For example layout and layout-v21 and some information like id is missing on one of them. So check all the activity's layouts
In my case, the layout was not finished inflating. Solved by adding a small delay before trying to access the TextView.
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();
}
}, 100);
I struggled with this for a while and what I realized was, that, if you have more than one layout file version like:
"activity_one.xml" in "layout" folder and one in "layout - small" folder
Which I used for multiple phone layout support, the problem was that one of the TextViews was in both, with the exact same ID and everything, however the difference was that one was higher up in the hierarchy of views in the layout.
When I changed them to be in the same spot it worked.
(I know this is already answered, but maybe this helps someone out there. Very rare though.)