-7

I tried to send a int value from current activity to the new one, here is the parts in current activity.

            dialog.setPositiveButton("4 players", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "Start a new game!", Toast.LENGTH_SHORT).show();
                    // need send extra value to PlayerBoardActivity to decide how many buttons I should have
                    Intent intent = new Intent(MainActivity.this,
                            PlayBoardActivity.class);
                    intent.putExtra(PLAYER_NO, 4);
                    startActivity(intent);
                }

            });
            dialog.setNegativeButton("2 players", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "Start a new game!", Toast.LENGTH_SHORT).show();
                    // need send extra value to PlayerBoardActivity to decide how many buttons I should have
                    Intent intent = new Intent(MainActivity.this,
                            PlayBoardActivity.class);
                    intent.putExtra(PLAYER_NO, 2);
                    startActivity(intent);
                }

            });

The problem is, I create 2 layout files for the new activity. When I press the negative button in the dialog for example, what I want is let the new activity (PlayerBoardActivity in my case) load the layout file corresponding to the value I have sent by "intent.putExtra(PLAYER_NO, 2); "

The code in the new activity is

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final String PLAYER_NO = "the number of players";
    Bundle b = getIntent().getExtras();
    int a = b.getInt(PLAYER_NO);
    if (b != null) {
        if (a == 2) {
            setContentView(R.layout.two_player);
        }
        if(a == 4){
            setContentView(R.layout.four_player);
        }
    }
}

I do want to know whether I can load different layout file in this way? Or is there any better solution for my problem.

Thank you all in advance.

SonicFancy
  • 311
  • 1
  • 3
  • 11
  • And why don't you just implement two different `Activities`? If you have two different layouts then you should implement them in two different components. If you do that you don't need to send a value along with the `Intent` anymore, you just need to start two different `Activities` based on which `Button` is pressed. – Xaver Kapeller Sep 29 '15 at 06:43
  • Offtopic: you should do the null check before using the `b` variable in `b.getInt`. – Paco Abato Sep 29 '15 at 08:01

5 Answers5

1

If you use

intent.putExtra(PLAYER_NO, 2);

you should call following code to get values (without using "Bundle"):

getIntent().getIntExtra(PLAYER_NO, -1)
Tieru
  • 1,360
  • 1
  • 10
  • 16
1

In your code, the problem is in your second activity to which you are calling. You are trying to fetching the values from intent in incorrect way. Try this in your second activity:

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   Intent intent = getIntent();
   int b = intent.getIntExtra(PLAYER_NO, 0);
        if (b == 2) {
            setContentView(R.layout.two_player);
        } 
        if(b == 4){
            setContentView(R.layout.four_player);
        } 
} 
Er.Rohit Sharma
  • 696
  • 5
  • 21
0

Ji Yang... it is fine..if both the layout content the same kind of structure and dealing with different resources of any layout in the same activity is not so difficult..

suppose layout two_player.xml is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:orientation="vertical">

<TextView
    android:id="@+id/textview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#777370"
    android:textSize="16sp"
    android:paddingLeft="5dp"
    android:text="Dummy Text"
    android:visibility="gone"
    android:textStyle="bold"/>
</RelativeLayout>

and layout four_player.xml is something like that

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:orientation="vertical">

<ImageView
    android:id="@+id/iv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/order_content"
    android:src="@drawable/order_next_sap"
    android:layout_alignLeft="@+id/order_content"/>
<ImageView
    android:id="@+id/iv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/order_content"
    android:src="@drawable/order_next_sap"
    android:layout_alignLeft="@+id/order_content"/>
</RelativeLayout>

means ...both layout of different defination.. than its difficult to use resource of both layout in same activity and its not good too..

The better solution in this case is to create fragment of both layout

class TwoPlayerFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup   container,
            Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.two_player, container, false);
        return v;
    }
}

class FourPlayerFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup   container,
            Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.four_player, container, false);
        return v;
    }
}

and use the fragment according to the intent value pass from dialog..

Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23
0

try this,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final String PLAYER_NO = "the number of players";
    Intent b = getIntent();
    int a = b.getExtras().getInt(PLAYER_NO);

    if (b != null) {
        if (a == 2) {
            setContentView(R.layout.two_player);
        }
        if(a == 4){
            setContentView(R.layout.four_player);
        }
    }
}
Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16
0

You are doing very wrong way. You should use fragment for this. You should create two fragment in which you can inflate different different layout. But this is your call.

From PlayBoardActivity you are sending data like :

intent.putExtra(PLAYER_NO, 4);

So in new activity you need to retreive like:

int b=getIntent.getIntExtra(PLAYER_NO,defaulValue);

you are trying to get value from bundle which is wrong.

Deepak Gupta
  • 979
  • 1
  • 10
  • 18