Saturday 22 October 2011

Shared Preference

First Class:





import android.os.Bundle;
import android.widget.Toast;


public class SharedActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Preference p=new Preference(this);
        p. Saved pref("KUMAR MLA COIMBATORE");
        Toast.makeText(this, p.Getprif(), Toast.LENGTH_LONG).show();
    }
}




Second Class:



import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class Preference {
private final static String PREF_FILE="pref";
SharedPreferences preference;
public Preference(Context context){
preference=context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
}
public void Savedpref(String str){
Editor et=preference.edit();
et.putString("username",str);
et.commit();
}
public String Getprif(){
return preference.getString("username","Not Exit");
}


}



1 comment:

  1. Creation of PDF file;


    import java.io.FileOutputStream;
    import java.io.IOException;

    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.PdfWriter;

    import android.app.Activity;
    import android.os.Bundle;

    public class LatestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /* class helper {

    *//**
    * Generates a PDF file with the text 'Hello World'
    *
    * @param args
    * no arguments needed here
    *//*
    public void main(String[] args) {
    */
    System.out.println("Hello World");

    // step 1: creation of a document-object
    Document document = new Document();
    try {
    // step 2:
    // we create a writer that listens to the document
    // and directs a PDF-stream to a file
    PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + java.io.File.separator + "droidtext" + java.io.File.separator + "/HelloWorld.pdf"));

    // PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/HelloWorld.pdf"));

    // step 3: we open the document
    document.open();
    // step 4: we add a paragraph to the document
    document.add(new Paragraph("Hello World"));
    } catch (DocumentException de) {
    System.err.println(de.getMessage());
    } catch (IOException ioe) {
    System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();
    }
    }
    //}
    //}

    -----------------------------------------------------------
    Above code not having errors but not creating a pdf file

    jar file for this code can download from the following link :

    http://code.google.com/p/droidtext/downloads/list


    please checkout this code & give me the right solution as soon as possible....

    ReplyDelete