Monday, July 23, 2018

Counter App

Design for Counter App (Layout File)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
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:orientation="vertical"    
android:padding="16dp"
android:layout_height="match_parent"    
tools:context="net.samaysoftware.sampleapplication12345.MainActivity">
    <TextView
        android:id="@+id/tvNumber"        
        android:layout_width="match_parent"        
        android:layout_height="wrap_content"
        android:textSize="80sp"        
        android:gravity="center"        
        android:text="1" />


    <Button        
        android:id="@+id/btnIncrement"        
        android:layout_width="match_parent"        
        android:layout_height="wrap_content"        
        android:text="Increment" />


    <Button        
        android:id="@+id/btnDecrement"        
        android:layout_width="match_parent"        
        android:layout_height="wrap_content"        
        android:text="Decrement" />


</LinearLayout>


Code for Counter App (Java File)

package net.samaysoftware.sampleapplication12345;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    TextView tvNumber;
    Button btnIncrement, btnDecrement;
    @Override    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvNumber = (TextView)findViewById(R.id.tvNumber);
        btnIncrement = (Button) findViewById(R.id.btnIncrement);
        btnDecrement = (Button) findViewById(R.id.btnDecrement);
        tvNumber.setText("0");
        btnIncrement.setOnClickListener(this);
        btnDecrement.setOnClickListener(this);
        tvNumber.setOnClickListener(this);
    }
    @Override    
    public void onClick(View view) {
        if(view==btnIncrement) {
            String number = tvNumber.getText().toString();
            int no = Integer.parseInt(number);
            no++;
            String newnumber = String.valueOf(no);
            tvNumber.setText(newnumber);
        }else if(view== btnDecrement){
            String number = tvNumber.getText().toString();
            int no = Integer.parseInt(number);
            no--;
            String newnumber = String.valueOf(no);
            tvNumber.setText(newnumber);
        }
        else if(view==tvNumber){
            tvNumber.setText("100");

        }
    }
}

No comments:

Post a Comment

Near by App

https://drive.google.com/file/d/0B2ag35s4X53Eb2pSQVI1SzNudE0/view