Showing posts with label Android tutorials.. Show all posts
Showing posts with label Android 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


Monday, 31 October 2016

SharedPreferences

The SharedPreferences class gives a general system that permits you to spare and recover determined key-esteem sets of primitive information sorts. You can utilize SharedPreferences to spare any primitive information: booleans, coasts, ints, yearns, and strings. This information will continue crosswise over client sessions (regardless of the possibility that your application is executed).

There are two methods of SharedPreferences.

1) get SharedPreferences()
2)get Preferences()

 Example Code for  SharedPreferences.

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="#be2f2f">

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_marginTop="20dp" 
 android:background="#f3e8e8" 
 android:allowUndo="false" 
 android:hint="Name" />

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText2" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" 
 android:background="#e8dfdf" 
 android:hint="Location" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Save" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Show" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button2" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

showactivity.xml :-

<?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">

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:id="@+id/textView" 
 android:text="Large Text" 
 android:layout_gravity="center_horizontal" />

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:id="@+id/textView2" 
 android:text="Large Text" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>
 
MainActivity.java :-

package com.kiranapp;

import android.content.Intent;
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.Toast;

public class MainActivity extends AppCompatActivity {
    Button savebtn,showbtn;
    EditText uname,uloc;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 savebtn=(Button)findViewById(R.id.button);
 uname=(EditText)findViewById(R.id.editText);
 uloc=(EditText)findViewById(R.id.editText2);
 showbtn=(Button)findViewById(R.id.button2);
 savebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 String s1=uname.getText().toString();
 String s2=uloc.getText().toString();
 SharedPreferences sp=getSharedPreferences("myfile",MODE_PRIVATE);
 SharedPreferences.Editor e=sp.edit();
 e.putString("name",s1);
 e.putString("loc",s2);
 e.commit();
 Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
 }
 });
 showbtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 Intent in=new Intent(MainActivity.this,ShowActivity.class);
 startActivity(in);
 }
 });
}
}

ShowActivity.java :-

package com.kiranapp;

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

/** * Created by Dell on 10/31/2016. */
 public class ShowActivity extends AppCompatActivity {

    TextView t1, t2;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.showactivity);
 t1=(TextView)findViewById(R.id.textView);
 t2=(TextView)findViewById(R.id.textView2);
 SharedPreferences sp=getSharedPreferences("myfile",MODE_PRIVATE);
 String s1=sp.getString("name",null);
 String s2=sp.getString("loc",null);
 t1.setText(s1);
 t2.setText(s2);
 }
}
 

Output :-

























Saturday, 15 October 2016

WebView in android

WebView is a view and it displays the web pages inside the applications.
Creating and setting webview client.
Example : Loading the web page like websites etc. (Urls).
                 1)http://androiddevelopersstudio.blogspot.in

acivitymain.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">

<ProgressBar
  android:layout_centerHorizontal="true"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:layout_gravity="center_vertical|center_horizontal"
  android:id="@+id/progressBar1"/>

<TextView
   android:layout_below="@+id/progressBar1"
   android:layout_height="wrap_content"
   android:id="@+id/LoadingText"
   android:layout_width="fill_parent"
   android:text="please wait loading website"
   android:textSize="20sp"
   android:gravity="center_horizontal">
</TextView>
<WebView
  android:id="@+id/activity_main_webview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal" />

</LinearLayout>

How to design the widgets please follow below figure :
or we can select from widgets we can find in side bar.

WebView
XML Figure




















MainActivity.java :-

package com.kiranapp;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

 private WebView mWebView;
  ProgressBar progressBar;

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

  mWebView = (WebView) findViewById(R.id.activity_main_webview);
  progressBar = (ProgressBar) findViewById(R.id.progressBar1);

WebSettings webSettings = mWebView.getSettings();
 webSettings.setJavaScriptEnabled(true);
 mWebView.loadUrl("http://androiddevelopersstudio.blogspot.in/");

    }
}

Output :-

WebView
WebView fig :1




















WebView
WebView fig :2