user2870
user2870

Reputation: 487

How to change layout by pressing the button?

I am trying to bring a new layout by pressing a button in my android app. Here is the code piece:

Button button = (Button)findViewById(R.id.btnAccept);
button.setOnClickListener(new View.OnClickListener() {
    LayoutInflater layoutInflater = LayoutInflater.from(getBaseContext());
    View promptView = layoutInflater.inflate(R.layout.empty_layout, null);

    public void onClick(View v) {

        // What should I write here to prompt empty_layout?
    }
}

I dont know how to use "promptView". Can you give me some insight please?

Thank you guys!

Upvotes: 0

Views: 105

Answers (3)

digiwebguy
digiwebguy

Reputation: 480

I don't know if i understand your question correctly but what I would do to add a layout when a button is pressed is to do it programmatically like this:

    LinearLayout mainLayout;
    mainLayout = (LinearLayout)findViewById(R.id.mainLayout)//assuming u gave the main layout an id of mainLayout in your XML file
    public void onClick(View v) {
          LinearLayout layout = new LinearLayout(this);
          LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);//create variable to store layout parameters
          layout.setOrientation(LinearLayout.VERTICAL);//set orientation of layout
          layout.setLayoutParams(params);//set layout parameters
          mainLayout.addView(layout);//add the newly created layout to the already existing layout
}

Upvotes: 0

IronBlossom
IronBlossom

Reputation: 3917

If its the whole view of your screen then use

setContentView(prompView);

If its only a portion of a view then use

yourPortionViewContainer.addView(prompView);

Or may be you could use Fragment.

Upvotes: 2

robotoaster
robotoaster

Reputation: 3122

you should create Intent to open new Activity or create Fragment and let FragmentManager show it.

Upvotes: 1

Related Questions