1. Are you ready for the Galaxy S20? Here is everything we know so far!

Implementing Arrange Activity in Battleships

Discussion in 'Android Development' started by Marijan Novak, Apr 15, 2016.

  1. Marijan Novak

    Marijan Novak Lurker
    Thread Starter

    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"
    android:eek:rientation="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.
     


  2. Deleted User

    Deleted User Guest

    Wouldn't a GridView be ideal for this?
     
  3. Marijan Novak

    Marijan Novak Lurker
    Thread Starter

    Any reference/tutorial to this?
     
  4. Deleted User

    Deleted User Guest

    Actually you're probably better off using a Canvas approach for this. Draw the grid with lines and handle touch events to detect selection of the squares.
     
  5. Marijan Novak

    Marijan Novak Lurker
    Thread Starter

    Is it possible to change more ImageViews stored inside a GridView by clicking a single cell? This is a part of Main Activity that troubles me.

    tablicaBrodova.setOnItemClickListener(new OnItemClickListener() {
    @override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    ImageView img = (ImageView) view;

    //SOME OTHER CODE

    case 1:
    if ((provjeriStupac(temp) <= 6)) {

    pozicije1[temp + 1] = 1;
    pozicije1[temp + 2] = 1;
    pozicije1[temp + 3] = 1;

    for (int k=0;k<=3;k++)
    {
    position=temp+k;
    img.setImageResource(R.drawable.postavljen);
    }


    This is the part that troubles me (the for loop). I tried changing the position and then changing the ImageView, but it doesn't seem to work. Is this even possible? Any other way to do this? I read something about performItemClick but don't know anything about it.

    Temp is the previous position clicked saved, and pozicije1 is an array saving my data.

    This is my Image Adapter:

    public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public Integer[] celije = new Integer[100];



    public ImageAdapter (Context c){
    mContext = c;

    for (int i=0; i<100; i+=1){
    celije = R.drawable.kockica;
    }
    }


    public int getCount(){
    return celije.length;
    }


    public Object getItem(int position){
    return celije[position];
    }


    public long getItemId(int position){
    return 0;
    }


    public View getView (int position, View convertView, ViewGroup parent){

    ImageView imageView = new ImageView(mContext);

    imageView.setImageResource(android.R.color.darker_gray);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(50,50));

    return imageView;
    }


    }

    And my Layout file:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    tools:context="com.marijannovak.potapanjebrodovazavrsni.Ploca1">

    <ListView
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:id="@+id/listaBrodova"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

    <GridView
    android:layout_width="340dp"
    android:layout_height="340dp"
    android:id="@+id/tablicaBrodova"
    android:numColumns="10"
    android:columnWidth="30dp"
    android:background="@android:color/white"
    android:horizontalSpacing="1dp"
    android:verticalSpacing="1dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/listaBrodova"
    android:layout_toEndOf="@+id/listaBrodova"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="10dp"/>

    <Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="X"
    android:id="@+id/reset"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:eek:nClick="reset"/>

    <Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=">"
    android:id="@+id/dalje"
    android:layout_below="@+id/reset"
    android:layout_alignLeft="@+id/reset"
    android:layout_alignStart="@+id/reset"
    android:eek:nClick="dalje"/>

    </RelativeLayout>
     
Loading...
Similar Threads - Implementing Arrange Activity
  1. AndroidGUIIsComplicated
    Replies:
    1
    Views:
    435
  2. d2idan
    Replies:
    626
    Views:
    2,887
  3. Rascalsailor
    Replies:
    0
    Views:
    350
  4. dagwood90
    Replies:
    0
    Views:
    527
  5. kannanthayumanavan
    Replies:
    5
    Views:
    1,533
  6. nagamothu
    Replies:
    1
    Views:
    608
  7. Hardik Thummar
    Replies:
    2
    Views:
    318
  8. Ashish Sikarwar
    Replies:
    14
    Views:
    1,018
  9. Calvert Wong
    Replies:
    6
    Views:
    468
  10. mark9
    Replies:
    5
    Views:
    495

Share This Page

Loading...