Showing posts with label Android development tutorials. Show all posts
Showing posts with label Android development tutorials. Show all posts

Thursday, 3 November 2016

InternalStorage

Android Internal storage is the capacity of the private information on the gadget memory. Naturally, sparing and stacking documents to the inner stockpiling are private to the application and different applications won't have entry to these records. At the point when the client uninstalls the applications the inward put away documents connected with the application are additionally expelled. Nonetheless, take note of that a few clients root their Android telephones, picking up superuser get to. These clients will have the capacity to peruse and compose whatever records they wish.

For example as shown figure is internal storage in android.

InternalStorage
InternalStorage Figure


code for internal storage in android

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity" 
 android:background="#f3f0f0">

<TextView 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Welcome to internal storage in android" 
 android:id="@+id/textView" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Text to write" 
 android:layout_alignTop="@+id/button2" 
 android:id="@+id/button" 
 android:onClick="click text" 
 android:layout_gravity="center_horizontal" />

<Button
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Read from file" 
 android:onClick="click read" 
 android:id="@+id/button2" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

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

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
    EditText editText;
   // TextView t1;
  //  Button b1,b2;
    static final int READ_BLOCK_SIZE = 100;
@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
// t1=(TextView)findViewById(R.id.textView);
// b1=(Button)findViewById(R.id.button);
// b2=(Button)findViewById(R.id.button2);
 editText=(EditText)findViewById(R.id.editText);
 }

public void WriteBtn(View v) {
try {
FileOutputStream fileout=openFileOutput("file.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(editText.getText().toString());
outputWriter.close();
Toast.makeText(getBaseContext(), "saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void ReadBtn(View v) {
try {
FileInputStream fileIn=openFileInput("file.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
editText.setText(s);
}
catch (Exception e) {
e.printStackTrace();
 }
}
}

Output :-

InternalStorage
InternalStorage Fig :1

















  


InternalStorage
InternalStorage Fig :2


Wednesday, 12 October 2016

CustomDialog

It is  a user defined dialog.
It is android widget.
It is similar to activity or user-interface(UI).
Using Dialog class
And create one more XML file and java file (its not mandatory)

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity">


<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"       
 android:text="customdialog" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>


customdialog :-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent">

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" 
 android:layout_marginTop="30dp" 
 android:hint="enter  name" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="enter" 
 android:id="@+id/okbtn" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="cancel" 
 android:id="@+id/canbtn" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.app.Dialog;
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.Toast;

public class MainActivity extends AppCompatActivity {
 Button okbtn,canbtn;
 Dialog dialog;
 EditText e1;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 okbtn=(Button)findViewById(R.id.button);
 okbtn.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
 dialog=new Dialog(MainActivity.this);
 dialog.setTitle("Custom Dialog");
 dialog.setContentView(R.layout.customdialog);
 dialog.show();
 okbtn=(Button)dialog.findViewById(R.id.okbtn);
 canbtn=(Button)dialog.findViewById(R.id.canbtn);
 e1=(EditText)dialog.findViewById(R.id.editText);
 okbtn.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
 String s1=e1.getText().toString();
Toast.makeText(MainActivity.this, "Name is : "+s1, Toast.LENGTH_SHORT).show();
 }
});
canbtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 dialog.cancel();
 }
});
}
});
}
}


Customdialog.java :-

package com.kiranapp;

/** * Created by Dell on 10/12/2016. */public class customdialog {
}

Output :-

Custom dialog
Custom dialog fig :1





















custom dialog
custom dialog fig :2

Switch

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.kiranapp.MainActivity"
  android:background="#ddd6d6">
<Switch
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Bluetooh"
  android:layout_marginTop="30dp"
  android:id="@+id/switch1"
  android:checked="false"
  android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java
:-


package com.kiranapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  Switch aSwitch;


@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   aSwitch=(Switch) findViewById(R.id.switch1);
  aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   if (isChecked){
   Toast.makeText(MainActivity.this,"TurnOn",Toast.LENGTH_SHORT).show();
    }
  else {
 Toast.makeText(MainActivity.this,"TurnOff",Toast.LENGTH_SHORT).show();
 }
 }
});
}
}

Output :- 


Switch
Switch fig:1




Switch
Switch fig:2