I'm doing an Android Studio project called Android Battleships, from scratch. I ran into a problem. When designing player ship arranging activity I need a table with clickable cells that should return it's row and col number or at least it's ID (so I can save data into boolean array holding ship positions). I tried doing this with 64 (8x8table) ImageButtons but that doesn't seem like a good solution considering I have to initialize all of them and set onClick listener cases for all of them. Also there should be switches that determine how many cells I can choose for the current ship. For instante I click destroyer, choose 4 adjacent cells (changing their colour and saving data to position table) and then the destroyer switch becomes disabled and then I move on to other switches. Any ideas? Or any better solutions? Maybe inputing coordinates and the image view elements in table show ships (different colored cells). I hit a wall. I'm open minded and any suggesions are welcome! Thanks
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
androidrientation="horizontal">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="@+id/switch1"
android:layout_column="0"
android:layout_gravity="left"
android:layout_marginRight="40dp"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/p1btn00"
android:layout_column="1"
android:src="#4d4d4e"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:adjustViewBounds="true"
android:background="@Null"/>
This is how the XML layout looks,there's 8 ImageButtons by row and 8 rows. Also Switches for ships.
@override
publicvoid onClick(View v){
switch(v.getId()){
case R.id.p1btn00:
if(brododabran=true){
a=0;b=0;
b00.setEnabled(false);
b00.setBackgroundColor(Color.YELLOW);
}else{
odaberi();
}
break;
// other code
}
}
This is the Java code, showing only case for one button (out of 64), it gives the Button's ID, disables it and changes it's color. b00 is the name of the variable button. Switches I just added, didn't write a code yet. I tried using buttons for those also but got into problems while trying to activate click listener for the table after clicking the ship you want to add. brododabran is boolean that checks if the ship has been pressed before. Basically I want to press the ship that allows u the choice of choosing cells in table and then go onto the next ship.
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
androidrientation="horizontal">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="@+id/switch1"
android:layout_column="0"
android:layout_gravity="left"
android:layout_marginRight="40dp"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/p1btn00"
android:layout_column="1"
android:src="#4d4d4e"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:adjustViewBounds="true"
android:background="@Null"/>
This is how the XML layout looks,there's 8 ImageButtons by row and 8 rows. Also Switches for ships.
@override
publicvoid onClick(View v){
switch(v.getId()){
case R.id.p1btn00:
if(brododabran=true){
a=0;b=0;
b00.setEnabled(false);
b00.setBackgroundColor(Color.YELLOW);
}else{
odaberi();
}
break;
// other code
}
}
This is the Java code, showing only case for one button (out of 64), it gives the Button's ID, disables it and changes it's color. b00 is the name of the variable button. Switches I just added, didn't write a code yet. I tried using buttons for those also but got into problems while trying to activate click listener for the table after clicking the ship you want to add. brododabran is boolean that checks if the ship has been pressed before. Basically I want to press the ship that allows u the choice of choosing cells in table and then go onto the next ship.