Monday, 4 January 2016

Using Assets

00:31

Using  Assests :

Content placed in the assests /directory  is maintained in raw file format ,and  No IDs are generated for these files. To read the content from the assests/ folder in an android application, we use the AssetManager, which reads the content from the external files in the form of a stream of bytes.
We are going to create an example ,we add a text file asset. The content in the text file added to the assests folder is accessed using the AssetManager ..







(1)write code  for  MainActivity.java  file:

package asset.test.om.com.assetmanager;

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

import java.io.IOException;
import java.io.InputStream;


public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        TextView  fileView=(TextView)findViewById(R.id.
file_view);
        InputStream input;
        AssetManager  assetManager=getAssets();
       
try
       
{
            input =assetManager.open(
"info.txt");
           
int fileSize=input.available();
           
byte[] buffer=new byte[fileSize];
            input.read(buffer);
            input.close();
            String  textForm=
new String(buffer);
            fileView.setText(textForm);
        }
catch (IOException e) {
            fileView.setText(
"some exception has occured");
        }
    }


}

(2) write   code for activity_main.xml   file:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    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" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write   code  for    content_main.xml     file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <TextView

        android:id="@+id/file_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="15dip"

        android:textStyle="bold" />



</RelativeLayout>

(4)Go to your project folder   “assetmanager\app\src\main\assets”:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <TextView

        android:id="@+id/file_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="15dip"

        android:textStyle="bold" />



</RelativeLayout>

 output:






Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 ANDROID TRAINING AT VISSICOMP BY OM SIR. All rights resevered. Designed by Templateism

Back To Top