3 layouts and an Activity

Asked

Viewed 93 times

0

I have an actyvity_main, containing 3 radiobuttons (each one corresponds to a layout), and a button. After the user selects the option, he clicks on the button, which makes a call to another Activity, where in it, I need to do with come with the selected layout.

public void onClick(View view) {
        int checkeRadioButtonId = escolhaRadio.getCheckedRadioButtonId();
        switch (checkeRadioButtonId) {
            case R.id.rdb1:
                Intent opcao8 = new Intent(this, Activity_Jogo.class);
                setContentView(R.layout.layout_8);
                startActivity(opcao8);
                break;
            case R.id.rdb2:
                Intent opcao10 = new Intent(this, Activity_Jogo.class);
                setContentView(R.layout.layout_10);
                startActivity(opcao10);
                break;
            case R.id.rdb3:
                Intent opcao12 = new Intent(this, Activity_Jogo.class);
                setContentView(R.layout.layout_12);
                startActivity(opcao12);
        }
    }

This way that I’m doing, he calls the layout and, after appearing he instantly calls the Activity, leaving only the Activity with its default layout.

  • You have to explain what you want better. One thing is certain setContentView() shall be called only once in each Active onCreate().

  • Is there any way I can call just one Activity, passing which layout it should display?

  • There is but it becomes complicated to manage each of the Views that make up each of the past layouts. The best solution is to use different Fragments or Activities.

1 answer

1

The simplest way would be to pass the Resource of layout for Extras, and recover in the next Activity:

public void onClick(View view) {
    int checkeRadioButtonId = escolhaRadio.getCheckedRadioButtonId();
    Intent i = new Intent(this, Activity_Jogo.class);

    switch (checkeRadioButtonId) {
        case R.id.rdb1:
            opcao10.putExtra("opcao", R.layout.layout_8);
            break;
        case R.id.rdb2:
            opcao10.putExtra("opcao", R.layout.layout_10);
            break;
        case R.id.rdb3:
            opcao10.putExtra("opcao", R.layout.layout_12);
            break;
    }

    startActivity(i);
}

In the method onCreate of his Activity_Jogo you would recover that value and use as contentView:

@Override
protected onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int layout = getIntent().getIntExtra("opcao", 0);

    // Se ninguem passou um layout como parametro,
    // voce pode usar um padrao ou fechar a Activity
    if(layout == 0) {
        finish();
    }

    setContentView(layout);

    // Restante do seu processamento...
}
  • It’s a possibility but then it becomes complicated to manage each of the Views that make up each of the past layouts. The best solution is to use different Fragments or Activities.

  • @ramaral, yes, of course, I hadn’t thought of it that way. My solution is only great if the layouts have the same elements, but with a different arrangement.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.