Mobil Programlama - Uygulama 3

Basket Sayacı

Uygulama arayüzünü yukarıda belirtildiği şekilde hazırlayalım.

"Takım A" yazan TextView'de yazıyı bulunduğu kutu içerisinde ortalamak için android:gravity özelliğini center_horizantally yapalım.

Puan butonlarına basıldığında o puanları gösterecek şekilde Java kodlarını yazıyoruz, Şöyle ki, +1 butonuna basıldığında 0 yazan TextView 1 yazacak, +2 butonuna basıldığında 0 yazan TextView'de 2 yazacak. 

Programımıza int tipinde skorTakimA isminde ve ilk değeri 0 olan bir global değişken tanımlayalım. Bu puan butonlarına bastıkça skor değeri artacak şekilde java kodlarını yazalım.


 Yandaki resimdeki gibi Takım A ve Takım B için skor değerlerini hesaplamak istiyoruz. Bu nedenle yerleşim düzeninde değişiklik yapmak gerekiyor. Bu şekilde bir düzen elde elde edebilmek için Ana layout olarak Yatay (Horizontal) Lineer Layout ekleyip, içerisinde 2 tane Vertical LineerLayout eklemek gerekiyor. Böylece yandaki görünümü elde etmeye başlamış oluruz.












Kodları eklediğinizde yandaki gibi görünümü elde edemediyseniz;

Vertical LineerLayout özellikleri arasına;
android:layout_weight="1"
android:layout_width="0dp"

layout_weight değeri, layout'ların kapsadığı alanı belirlemektedir. width değerinin 0 verilmesi ile mevcut alanda yarıya yarıya kapsamaları sağlamaktadır.
A ve B takımları için butona basıldıkça skorları artıracak kodları da yazınız.

Bu uygulamaya, skorları sıfırlamak için bir buton daha ekliyoruz. Bu buton, ekranın en altında ve ortalanmış şekilde olacak. Sıfırla butonunu ekleyebilmek için layout üzerinde yeniden bir düzenleme yaparak, ana layout olarak Relative Layout ekliyoruz, diğer layoutlar ise içinde kalıyor.


Sonuçta kodlar şu şekilde;

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android.basketsayac.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="Takım A:" />

            <TextView
                android:id="@+id/PuanTextviewA"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="0" />

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="Serbest Atış" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="+2 Puan" />

            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="+3 Puan" />


        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="Takım B:" />

            <TextView
                android:id="@+id/PuanTextviewB"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="0" />

            <Button
                android:id="@+id/button1B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="Serbest Atış" />

            <Button
                android:id="@+id/button2B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="+2 Puan" />

            <Button
                android:id="@+id/button3B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="+3 Puan" />


        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/ResetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Sıfırla" />
</RelativeLayout>


MainActivity.Java

 final TextView puanA=(TextView) findViewById(R.id.PuanTextviewA);
        Button b_A1=(Button) findViewById(R.id.button1);
        Button b_A2=(Button)findViewById(R.id.button2);
        Button b_A3=(Button)findViewById(R.id.button3);

        final TextView puanB=(TextView) findViewById(R.id.PuanTextviewB);
        Button b_B1=(Button) findViewById(R.id.button1B);
        Button b_B2=(Button)findViewById(R.id.button2B);
        Button b_B3=(Button)findViewById(R.id.button3B);

        Button reset=(Button) findViewById(R.id.ResetButton);

        b_A1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                skorTakimA=skorTakimA+1;
                puanA.setText(""+skorTakimA);
            }
        });

        b_A2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                skorTakimA=skorTakimA+2;
                puanA.setText(""+skorTakimA);
            }
        });

        b_A3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                skorTakimA=skorTakimA+3;
                puanA.setText(""+skorTakimA);
            }
        });

        b_B1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                skorTakimB=skorTakimB+1;
                puanB.setText(""+skorTakimB);
            }
        });

        b_B2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                skorTakimB=skorTakimB+2;
                puanB.setText(""+skorTakimB);
            }
        });

        b_B3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                skorTakimB=skorTakimB+3;
                puanB.setText(""+skorTakimB);
            }
        });

        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                skorTakimA=0;
                skorTakimB=0;
                puanA.setText(""+skorTakimA);
                puanB.setText(""+skorTakimB);
            }
        });
    }


Program çalıştığına göre biraz stiller ile oynama yapabiliriz.

Bu hale gelmesi için öncelikle res->values->sytle.xml dosyasında değişiklik yapıyoruz.
<item name="colorPrimary">#EF746F</item>
<item name="colorButtonNormal">#EF746F</item>

colorprimary -> Uygulamanın başlık rengini değiştirir.
colorButtonNormal -> butonların arkaplan rengini değiştirir.

Orta kısımda bulunan ayırıcı ( seperatör - divider) eklemek için Linear Layout (Horizontal) içerisinde bir view ekliyoruz.

<View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray"/>



Uzaklıkları ise yukarıdaki ölçülerde ayarladığınızda uygulama tamamlanmıştır. 

xml son hali ile;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android.basketsayac.MainActivity">

    <LinearLayout
        android:id="@+id/linearLayoutH"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="Takım A:"
                android:textColor="#616161"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/PuanTextviewA"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:layout_marginTop="16dp"
                android:fontFamily="sans-serif"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="0"
                android:textColor="#000000"
                android:textSize="56sp" />

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginTop="8dp"
                android:text="Serbest Atış" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginTop="8dp"
                android:text="+2 Puan" />

            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginTop="8dp"
                android:text="+3 Puan" />


        </LinearLayout>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp"
            android:background="@android:color/darker_gray" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="Takım B:"
                android:textColor="#616161"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/PuanTextviewB"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:layout_marginTop="16dp"
                android:fontFamily="sans-serif"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="0"
                android:textColor="#000000"
                android:textSize="56sp" />

            <Button
                android:id="@+id/button1B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginTop="8dp"
                android:text="Serbest Atış" />

            <Button
                android:id="@+id/button2B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginTop="8dp"
                android:text="+2 Puan" />

            <Button
                android:id="@+id/button3B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginTop="8dp"
                android:text="+3 Puan" />


        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/ResetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="32dp"
        android:text="Sıfırla" />

</RelativeLayout>


Kaynak : Udacity

Yorumlar

Bu blogdaki popüler yayınlar

SQLite Değişken tipleri ve Komutları

Mobil Programlama - Not Hesaplama Uygulaması

Sistem Analizi ve Tasarımı Proje Sonu Bilgilendirme