Post Top Ad

Post Top Ad

Saturday 8 July 2017

Counting Next Strore Shred Prefernce

1 activity_main.xml:
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kiran.task4.MainActivity">

    <TextView
        android:text="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:id="@+id/count_tv"
        android:textSize="56sp"
        android:textStyle="normal|bold" />

    <Button
        android:text="Call Next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/count_button"
        android:layout_alignStart="@+id/count_button"
        android:layout_marginTop="40dp"
        android:id="@+id/callnext_button"
        android:onClick="callNext"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/background_light"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="COUNT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="63dp"
        android:id="@+id/count_button"
        android:layout_below="@+id/count_tv"
        android:onClick="callCount"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/background_light"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>


2 MainActivity.java:

package com.example.kiran.task4;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Intent intent;
    private TextView mCountTV;
    int mCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCountTV = (TextView) findViewById(R.id.count_tv);
        intent = new Intent(MainActivity.this,SecondActivity.class);
    }

    public void callCount(View view) {
        try {
            mCount = Integer.parseInt(mCountTV.getText().toString());
            mCount++;
            mCountTV.setText(""+mCount);
        }catch(Exception e){
            System.out.println("Could not parse "+e);
        }
    }

    public void callNext(View view) {
        try {
            mCount = Integer.parseInt(mCountTV.getText().toString());
            intent.putExtra("count",mCount);
            startActivity(intent);
        }catch(Exception e){
            System.out.println("Could not parse "+e);
        }
    }
}
.
3.activity_secound.xml:
<?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:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kiran.task4.SecondActivity">

    <TextView
        android:text="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="63dp"
        android:id="@+id/count_tv"
        android:textStyle="normal|bold"
        android:textSize="56sp" />

    <TextView
        android:text="You Passed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:id="@+id/textView2"
        android:textStyle="normal|bold"
        android:textSize="18sp" />
</RelativeLayout>
 
4.SecondActivity.java:
package com.example.kiran.task4;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {

    private TextView countTV;
    String mCount;

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

        Intent intent = getIntent();
        countTV = (TextView) findViewById(R.id.count_tv);
        mCount = String.valueOf(intent.getIntExtra("count",0));
        //countTV.setText(""+intent.getIntExtra("count",0));
        countTV.setText(mCount);

    }
}



No comments:

Post a Comment