mText1.getLayoutParams().height = NEW VALUE does not take effect in popup window - android

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.

Related

Set Custom layout inside ListView as non-scrollable in android

I have a ListView which contains a custom layout.
Each row of the list View look like this:
The row contains 2 LinearLayouts, one for Date and one for Progress details. The progress Details Layout consits of 2 TextViews(Heading and data below it) and 1 Button (View More).
When the user clicks 'View More' button the data below the heading expands to 10-12 lines.
My problem is that when the TextView expands, a scrollbar comes at the edge and the user has to scroll to read. The width of the row does not change i.e the row does not expand.
I want the row width to expand so that the user does not have to scroll to read the text.
I did read a lot and have already tried the following options but they did not work
1. android:scrollbar="none"
2. View.setOverScrollMode(View.OVER_SCROLL_NEVER);
3. View.setScrollContainer(false);
Please help me with this.
You have to use exapanding listview animation for that you can use this lib for that
https://github.com/nhaarman/ListViewAnimations
Use a expandable List View such that the normal view of your list will have the image that you provided and on clicking that view it will expand to reveal the details that you wanted to show by clicking the view more option(in your image)
Here is a tut link for more info.
Try to use LayoutParams and change the height programatically inside the listener for you Button.
LayoutParams should derive from the type of layout containing your LinearLayout. If for instance it is a RelativeLayout, it will look something like that:
int heightForRow = 100;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.height = heightForRow;
yourLinearLayout.setLayoutParams(params);
Please note that your LinearLayout must then be inside another layout (RelativeLayout in the example)

Duplicate layout IDs returning as -1 after view replacement

Short Story:
I have a layout "layout.xml", which gets replaced by another layout "success.xml" after a successful web request. Both layouts have an ImageView that provides the backgrounds to the layouts. These 2 backgrounds both need to be the same, and both are dependent on a user preference.
Longer Story: This all happens in a Fragmnet with an AsyncTask replacing the contentView with "success.xml" in onPostExecute after the web request. This happens as follows:
View view = getView();
view = null;
view = View.inflate(context, R.layout.success, null);
What I tried to do is give both ImageViews the following android:id="#+id/background_image" and then call
ImageView background = (ImageView)view.findViewById(R.id.background_image);
background.setImageResource(R.drawable.bg1);
This background-setting works for the initial view (layout.xml), but on trying to change to "success.xml", I get a NullPointException because background is null.
I've checked and the View's id is set to -1 while the original view's background_image id is set to something sensible and valid.
I've also tried setting the second view's background id like this: android:id="#id/background_image", i.e. without the '+', but still no luck.
The added complication is that it's not just 2 layouts, but about 5 that I need to do this for, so it would be really handy to recycle view id's.
Any help would be greatly appreciated.
Your code for replacing the fragment's view will not do what you want, the original view will remain the same as you change only a reference to that view and not the actual object.
To replace the view of the fragment with the new layout you could have another ViewGroup(for example a FrameLayout) in the basic layout (layout.xml) wrapping your current content(don't forget to give it an id) of layouts.xml(as I understand this is the basic layout). Then, when it's time to replace the layout you could simply do:
// remove the current content
((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)).removeAllViews();
// add the new content
View.inflate(context, R.layout.success, ((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)));
You could avoid adding an extra layout if, by any chance, all your five layouts have the same type for the root view(like a LinearLayout etc). In this case you would use the same code as above but you'll modify the other layouts file to use a merge tag. Also, you'll be looking for the id of the root in the layout.xml layout into which you'll add the content of the other files.
Then you could have the same ids, but you'll have to reinitialize any reference to the views(meaning that you'll have to search for the view again if you store a reference to the view(like a Button field in the fragment class)).

Layout alert dialog with dynamic content - result too tall

i'm having a hard time squeezing my alert dialog to just wrap my content.
the dialog's layout xml is structured like this:
+ linear layout (main)
++ text view
++ linear layout (placeholder)
i use the placeholder to attach a singe edittext-derived field to it later, during oncreate, by calling addView() on it, so my onCreate basically looks like this:
View v = LayoutInflater.from(context).inflate(input_layout, null);
LinearLayout placeholder = (LinearLayout)view.findViewById(input_placeholder)
SoftEditText text = new SoftEditText(context)
// set text attributes - input type, ime action, watchers and listeners
placeholder.addView(text);
setCancelable(true);
setButton(BUTTON_NEGATIVE, ....);
setTitle(title_label);
// view.forceLayout() - didn't help much
setView(view);
super.onCreate(instance);
// getWindow().getAttributes().height=LayoutParams.WRAP_CONTENT no go either
and the result is: my alert dialog is twice the height it's supposed to be. my linearlayout's are set to WRAP_CONTENT height, and if i paint their background, i see they are of correct size (that is nearly half the dialog's height). the rest of the dialog is black, so there is no component that forces or expects this size.
I set the text's maxLines to 1, but that again hasn't helped at all. this seems to be a trivial layout problem but i can't figure out what more should i call to get the dialog squeeze to simply wrap the content.
thanks bunches for any tip.
set maxLength and you can use ellipsize to indicate the text in continuation..

Remove rule from RelativeLayout

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

how to invisible a Layout control in android

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

Categories

Resources