Twitter Facebook Delicious Digg Stumbleupon Favorites More

Saturday, 16 December 2017

to do app

package com.example.viraj.app_honey_do_list_sharedpreference;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private EditText editText_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText_message = (EditText) findViewById(R.id.id_entertext);
        button = (Button) findViewById(R.id.id_button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //logic goes here
                if (!editText_message.getText().toString().equals("")) {
                    String message = editText_message.getText().toString();
                    writetofile(message);
                } else {
                    //do nothing for now
                }

            }
        });
        try {
            //-----------------------------
            if (readfromfile() != null) {
                editText_message.setText(readfromfile());
            }//----------------------------
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //------------------------
    private void writetofile(String message) {
        try {
            //-------------------------itna hi likha he
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("todolist.txt", Context.MODE_PRIVATE));
            outputStreamWriter.write(message);
            outputStreamWriter.close();//alwas close your stream
            //-----------------------------
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String readfromfile() throws IOException {
        String result = "";
        InputStream inputStream = openFileInput("todolist.txt");

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            //we need buffer behav like a bucket
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            //empty string to to do things
            String tempstring = "";
            StringBuilder stringBuilder = new StringBuilder();
            //temp string me read hone jaayega
            while ((tempstring = bufferedReader.readLine()) != null) {
                stringBuilder.append(tempstring);
            }
            inputStream.close();
            result = stringBuilder.toString();
        }

        return result;
    }
}



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.viraj.app_honey_do_list_sharedpreference.MainActivity">

    <EditText
        android:id="@+id/id_entertext"
        android:layout_width="276dp"
        android:layout_height="282dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="ENTER YOUT LIST"
        android:inputType="textMultiLine"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.084" />

    <Button
        android:id="@+id/id_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="406dp" />
</android.support.constraint.ConstraintLayout>

Share:

0 comments:

Post a Comment

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab