1
TextView tv = new TextView(getActivity());
                LayoutParams tvLp = new LayoutParams((new LayoutParams(
                        0, LayoutParams.WRAP_CONTENT,1f)));
                tvLp.gravity = (Gravity.BOTTOM);
                tv.setLayoutParams(tvLp);

When I run the above code in pre-Kitakat devices I am getting No such method error. I found this solution Android: java.lang.NoSuchMethodError on LinearLayout$LayoutParams.<init> in which the below code is used to avoid such error. But using the below solution, I cannot programmatically set gravity,layout weights etc. Is there any workaround to solve this?

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((ViewGroup.MarginLayoutParams)(new    LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));
2
  • what is TextView tv parent layout ? Commented Mar 4, 2015 at 8:18
  • What do you mean by "cannot programmatically set gravity,layout weights etc", does it crash? Please be more specific Commented Mar 4, 2015 at 8:21

1 Answer 1

2
LayoutParams tvLp = new LayoutParams((new LayoutParams(
                        0, LayoutParams.WRAP_CONTENT,1f)));

the copy constructor, the one that takes a LayoutParams as object, was introduced with api level 19. But you can use this, that is part of the sdk since api 1.

 LayoutParams tvLp = new LayoutParams(
                            0, LayoutParams.WRAP_CONTENT,1f);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.