Button OneClick Android Studio 3.5 Aplikasi Counter

Kali ini saya akan memberi koding bagaimana membuat applikasi counter pada android studio ini bisa.

MainActivity yang berada di folder java
package com.masbudicahyono.hellotoast;

import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button mybutton;
    private String myString = "MasBudiCahyono.blogspot.com";

    private int mCount = 0;
    private TextView mShowCount;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// ngambil show_count dari layout        mShowCount = (TextView) findViewById(R.id.show_count);

        mybutton = (Button) findViewById(R.id.button_toast);
        mybutton.setOnClickListener(new View.OnClickListener() {

            @Override            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), myString, Toast.LENGTH_LONG).show();

            }
        });

    }

    //menampilkan count    public void countUp(View view) {
        mCount++;
        mShowCount.setText(Integer.toString(mCount));
    }

    public void countReset(View view) {
        mCount = 0;
        mShowCount.setText(Integer.toString(mCount));

    }

}

Activity_main.xml yang ada di folder res>layout
<?xml version="1.0" encoding="utf-8"?><androidx.appcompat.widget.LinearLayoutCompat 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"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity">


    <Button        android:id="@+id/button_toast"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/colorPrimary"
        android:text="@string/button_label_toast"        android:textColor="@color/white" />

    <TextView        android:id="@+id/show_count"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/mybackground"        android:paddingTop="150dp"        android:paddingBottom="150dp"        android:text="@string/count_initial_value"        android:textAlignment="center"        android:textSize="@dimen/count_text_size"        android:textStyle="bold"        />

    <Button        android:id="@+id/button_count"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/biru"        android:onClick="countUp"        android:text="@string/button_label_count"        android:textColor="@color/white"        />

    <Button        android:id="@+id/button_reset"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/orange"
        android:onClick="countReset"        android:text="@string/button_label_reset"        android:textColor="@color/white"
        />
</androidx.appcompat.widget.LinearLayoutCompat>

Color.xml di folder res>values
<?xml version="1.0" encoding="utf-8"?><resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="white">#FFFFFF</color>
    <color name="orange">#00C853</color>
    <color name="biru">#2962FF</color>
    <color name="mybackground">#FFF043</color>
</resources>

dimens.xml di folder res>values
<?xml version="1.0" encoding="utf-8"?><resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="count_text_size">50sp</dimen>

</resources>

Strings.xml di folder res>values
<resources>
    <string name="app_name">Aplikasi Counter</string>
    <string name="button_label_toast">© Budi Cahyono ©</string>
    <string name="count_initial_value">0</string>
    <string name="button_label_count">Count</string>
    <string name="button_label_reset">Reset</string>
    <string name="toast_message">Hello Toast</string>
</resources>

Styles.xml di folder res>values
<resources>

    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

No comments:

Powered by Blogger.