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:

Friday, 15 December 2017

shared preferences in android

package com.example.viraj.sharedpreferences;

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

public class MainActivity extends AppCompatActivity {
    private Button savebutton;
    private EditText entertext;
    private TextView resultdisplay;
    private SharedPreferences myPref;
//information isme store kart he iss liy create kiya
    private static final String PREF_NAME = "MY_PREF_FILE";

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

        savebutton = (Button) findViewById(R.id.id_savedata);
        entertext = (EditText) findViewById(R.id.id_entersomething);
        resultdisplay = (TextView) findViewById(R.id.id_resulttextview);

        savebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myPref = getSharedPreferences(PREF_NAME, 0);
                SharedPreferences.Editor editor = myPref.edit();
//editor liya our save kiya
                editor.putString("key_messgae_ki_he", entertext.getText().toString());
                //save commit
                editor.commit();

            }

        });
//get data back
        SharedPreferences pref = getSharedPreferences(PREF_NAME, 0);//level o he
        if (pref.contains("key_messgae_ki_he")) {
            String message = pref.getString("key_messgae_ki_he", "not found");
            resultdisplay.setText("MESSAGE" + message);
        }

    }
}

<?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.sharedpreferences.MainActivity">

    <EditText
        android:id="@+id/id_entersomething"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="48dp"
        android:ems="10"
        android:hint="ENTER SOMETHING"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/id_savedata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="52dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_entersomething" />

    <TextView
        android:id="@+id/id_resulttextview"
        android:layout_width="252dp"
        android:layout_height="28dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="60dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_savedata" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>


Share:

Sunday, 10 December 2017

MUSIC PLAYER APP


Share:

basic play mp3 part 3

package com.example.viraj.mediaplayerapp;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

//import static com.example.viraj.mediaplayerapp.R.raw.ashique;

public class MainActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    private Button playbutton;
    private SeekBar M_seekBar;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        M_seekBar = (SeekBar) findViewById(R.id.id_seekbar);
        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.mercy);
        M_seekBar.setMax(mediaPlayer.getDuration());
//-------seekbar        M_seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override            public void onProgressChanged(SeekBar seekBar, int i_879, boolean b_123) {
                if (b_123) {
                    mediaPlayer.seekTo(i_879);
                }
            }

            @Override            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //----------------seekbar        //set on completion method-----------------        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override            public void onCompletion(MediaPlayer mediaPlayer_mp) {
                int duration = mediaPlayer_mp.getDuration();
                String mduration = String.valueOf(duration / 1000);
                Toast.makeText(getApplicationContext(), "Duration" + mduration, Toast.LENGTH_LONG).show();
            }
        });

        //----------------------------        playbutton = (Button) findViewById(R.id.MusicPlay);
        playbutton.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                    //stop music and give option                    pausemusic();
                } else {
                    startmusic();
                }
            }
        });

    }

    //create method in side of our class    public void pausemusic() {
        if (mediaPlayer != null)
            mediaPlayer.pause();
        playbutton.setText("PLAY MUSIC");
    }

    public void startmusic()

    {
        if (mediaPlayer != null) {
            mediaPlayer.start();
            playbutton.setText("PAUSE MUSIC");
        }

    }

    //memory consume karta he iss liye    @Override    protected void onDestroy() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        super.onDestroy();
    }
}
Share:

basic play mp3 part 2

package com.example.viraj.mediaplayerapp;
import android.media.MediaPlayer;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;
//import static com.example.viraj.mediaplayerapp.R.raw.ashique;

public class MainActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    private Button playbutton;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.mercy);

       //set on completion method-----------------        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override    public void onCompletion(MediaPlayer mediaPlayer_mp) {
        int duration =mediaPlayer_mp.getDuration();
        String mduration=String.valueOf(duration/1000);
        Toast.makeText(getApplicationContext(),"Duration"+mduration,Toast.LENGTH_LONG).show();
    }
});

        //----------------------------
        playbutton = (Button) findViewById(R.id.MusicPlay);
        playbutton.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                    //stop music and give option                    pausemusic();
                }
                else                {
                    startmusic();
                }
            }
        });

    }
    //create method in side of our class    public void pausemusic() {
        if (mediaPlayer != null)
            mediaPlayer.pause();
        playbutton.setText("PLAY MUSIC");
    }

    public void startmusic()

    {
        if (mediaPlayer != null) {
            mediaPlayer.start();
            playbutton.setText("PAUSE MUSIC");
        }

    }//memory consume karta he iss liye    @Override    protected void onDestroy() {
       if (mediaPlayer!=null && mediaPlayer.isPlaying())
       {
           mediaPlayer.stop();
           mediaPlayer.release();
           mediaPlayer=null;
       }
        super.onDestroy();
    }
}
Share:

BASIC play mp3 android studio

create raw folder  value resource type raw select kare

package com.example.viraj.mediaplayerapp;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

//import static com.example.viraj.mediaplayerapp.R.raw.ashique;


public class MainActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    private Button playbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.haa);

        playbutton = (Button) findViewById(R.id.MusicPlay);
        playbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                    //stop music and give option
                    pausemusic();
                }
                else
                {
                    startmusic();
                }
            }
        });

    }
    //create method in side of our class
    public void pausemusic() {
        if (mediaPlayer != null)
            mediaPlayer.pause();
        playbutton.setText("PLAY MUSIC");
    }

    public void startmusic()

    {
        if (mediaPlayer != null) {
            mediaPlayer.start();
            playbutton.setText("PAUSE MUSIC");
        }

    }
}



<?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.mediaplayerapp.MainActivity">

    <Button
        android:id="@+id/MusicPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="PLAY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Share:

Blogger Tutorials

Blogger Templates

Sample Text

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