3

I have found lots of question here regarding adding LinearLayout dynamically. I haven't found any reference or any tutorial or books suggested where I could know complete details of the and steps to add a LinearLayout dynamically.

    LinearLayout parentLayout = (LinearLayout) findViewById(R.id.master);
    LinearLayout Linear1 = new LinearLayout(this);
    Linear1.setOrientation(LinearLayout.HORIZONTAL);
    Linear1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    parentLayout.addView(Linear1);

This is the code snippet I am using for adding a Layout. The problem is that the min api required is 11. I have designed my app targeting api 10. So its very bad news for me. Please tell me is there any other way to add LinearLayout dynamically for the lower apis?

Error Message: Call requires API level 11 (current min is 8): new android.app.ActionBar.LayoutParams

1
  • What errors are you getting when you do this? Commented Jun 1, 2013 at 3:10

2 Answers 2

7

You are using the wrong LayoutParams class. Use LinearLayout.LayoutParams instead of ActionBar.LayoutParams.

Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't compute; I don't see any references to ActionBar in the code snippet. OP, you're going to need to show us the actual code. But yeah, Ahmad is right; ActionBar is not available prior to API 11 so don't use it. Your IDE should have flagged this.
@EdwardFalk: I pressed ctrl+shift+o and by default the editor imported the ActionBar.layoutpParams class after seeing @Ahmad 's answer I checked my import statements and found that I have imported wrong class. I have corrected it. Thanks.
0

To be precise, add following line in the import section:

import android.widget.LinearLayout.LayoutParams;

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.