Java tutorial
package our.isaacmayur.expensemanager; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.database.Cursor; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Environment; import android.os.Vibrator; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.Toast; import com.example.expensemanager.R; import com.itextpdf.text.Anchor; import com.itextpdf.text.BadElementException; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chapter; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Section; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class GenerateReport extends Activity implements OnClickListener { int date1, date2; Cursor c; private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED); private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); private static String FILE; Button bpdf; int counter = 0; static ArrayList<String> category; static ArrayList<String> mode; static ArrayList<Integer> amount; static ArrayList<String> date; View backg; // You can access the static variables from nonstatic methods // ///////////date ka onclick change kr private TextView text_date1, text_date2, tv_date1, tv_date2; private DatePicker date_picker1, date_picker2; private Button bstart_date, bend_date; private int year1, year2; private int month1, month2; private int day1, day2; static final int DATE_DIALOG_ID1 = 100; static final int DATE_DIALOG_ID2 = 200; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.generate_report); backg = findViewById(R.drawable.words_bgred); loadPreference(); if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { // sd card mounted } File direct = new File(Environment.getExternalStorageDirectory() + "/ExpenseManager"); if (!direct.exists()) { if (direct.mkdir()) { // directory is created; } } bpdf = (Button) findViewById(R.id.bpdf_gen); bpdf.setOnClickListener(this); bpdf.setEnabled(false); // FILE = Environment.getExternalStorageDirectory() // + "/Maac/Expense.pdf"; setCurrentDate(); addButtonListener1(); setEndDate(); addButtonListener2(); /* * if (text_date1.getVisibility() == View.VISIBLE && * text_date2.getVisibility() == View.VISIBLE) * * { Toast.makeText(getApplicationContext(), "inside if", * Toast.LENGTH_LONG).show(); bpdf.setEnabled(true); * * * } */ // Toast.makeText(getApplicationContext(),"pdf"+date2,Toast.LENGTH_LONG).show(); // //////////////////////////////////////////////// /* * DbClass dc = new DbClass(this); dc.open(); Cursor * c=dc.showResultInPdf(date1,date2); for (c.moveToFirst(); * !c.isAfterLast(); c.moveToNext()) { * * Log.e("category", c.getString(1));// correct till hereS Log.e("mode", * c.getString(4)); Log.e("amt", "amt" + c.getInt(2)); Log.e("date2", * "date" + c.getInt(3)); * * * } dc.close(); */ } // iText allows to add metadata to the PDF which can be viewed in your Adobe // Reader // under File -> Properties /* * private static void addMetaData(Document document) { * document.addTitle("My first PDF"); document.addSubject("Using iText"); * document.addKeywords("Java, PDF, iText"); * document.addAuthor("Lars Vogel"); document.addCreator("Lars Vogel"); * Log.e("meta", "data"); } */ /* * private static void addTitlePage(Document document) throws * DocumentException { Paragraph preface = new Paragraph(); // We add one * empty line addEmptyLine(preface, 1); // Lets write a big header * preface.add(new Paragraph("Expense Details", catFont)); * * addEmptyLine(preface, 1); // Will create: Report generated by: _name, * _date preface.add(new Paragraph( "Report generated by: isaac and mayur " * + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ * smallBold)); addEmptyLine(preface, 3); */ /* * preface.add(new * Paragraph("This document describes something which is very important " , * smallBold)); * * addEmptyLine(preface, 8); * * preface.add(new Paragraph( * "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-)." * , redFont)); */ /* * document.add(preface); // Start a new page document.newPage(); * Log.e("meta", "title"); * * } */ private static void addContent(Document document) throws DocumentException { Anchor anchor = new Anchor("Expense Report", catFont); anchor.setName("Expense Report"); // Second parameter is the number of the chapter Chapter catPart = new Chapter(new Paragraph(anchor), 1); Paragraph paragraph = new Paragraph(); paragraph.add(new Paragraph("Report generated on " + new Date(), smallBold)); addEmptyLine(paragraph, 3); catPart.add(paragraph); // Paragraph subPara = new Paragraph("Expense Table 1", subFont); // Section subCatPart = catPart.addSection(subPara); // subCatPart.add(new Paragraph("Expense")); // subPara = new Paragraph("Subcategory 2", subFont); // subCatPart = catPart.addSection(subPara); // subCatPart.add(new Paragraph("Paragraph 1")); // subCatPart.add(new Paragraph("Paragraph 2")); // subCatPart.add(new Paragraph("Paragraph 3")); // // add a list // createList(subCatPart); // Paragraph paragraph = new Paragraph(); // addEmptyLine(paragraph, 5); // subCatPart.add(paragraph); // add a table createTable(catPart); // now add all this to the document // document.add(catPart); // Next section // anchor = new Anchor("Second Chapter", catFont); // anchor.setName("Second Chapter"); // Second parameter is the number of the chapter // catPart = new Chapter(new Paragraph(anchor), 1); // subPara = new Paragraph("Subcategory", subFont); // subCatPart = catPart.addSection(subPara); // subCatPart.add(new Paragraph("This is a very important message")); // now add all this to the document document.add(catPart); Log.e("meta", "content"); } @Override public void onBackPressed() { // TODO Auto-generated method stub super.onBackPressed(); finish(); } private void vibrate(int ms) { ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(ms); } private static void createTable(Section catPart) throws BadElementException { PdfPTable table = new PdfPTable(4); // t.setBorderColor(BaseColor.GRAY); // t.setPadding(4); // t.setSpacing(4); // t.setBorderWidth(1); for (int i = 0; i < category.size(); i++) { Log.e("category...", category.get(i)); Log.e("mode...", mode.get(i)); Log.e("amt...", "amt" + amount.get(i)); Log.e("date2...", "date" + date.get(i)); } PdfPCell c1 = new PdfPCell(new Phrase("Category Name")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Amount Spent (Rs)")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Date")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Payment Mode")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); /* * table.addCell("pay"); table.addCell("hell"); table.addCell("to"); * table.addCell("to"); table.addCell("to"); * * table.addCell("2.1"); table.addCell("2.2"); table.addCell("2.3"); */ // code to add rows dynamically...remember the multiple for (int i = 0; i < category.size(); i++) { table.addCell(category.get(i)); table.addCell(amount.get(i) + ""); table.addCell(date.get(i)); table.addCell(mode.get(i)); } catPart.add(table); Log.e("meta", "table"); } /* * private static void createList(Section subCatPart) { List list = new * List(true, false, 10); list.add(new ListItem("First point")); * list.add(new ListItem("Second point")); list.add(new * ListItem("Third point")); subCatPart.add(list); Log.e("meta", * "create list"); * * } */ private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } @Override public void onClick(View arg0) { // vibrate(50); // TODO Auto-generated method stub try { category = new ArrayList<String>(); mode = new ArrayList<String>(); amount = new ArrayList<Integer>(); date = new ArrayList<String>(); if (date1 > date2) { Toast.makeText(getApplicationContext(), "Start Date should be less than End Date", Toast.LENGTH_LONG).show(); Log.e("date1", date1 + " " + date2); } else { DbClass dc = new DbClass(this); dc.open(); c = dc.showResultInPdf(date1, date2); for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { /* * Log.e("category", c.getString(1));// correct till hereS * Log.e("mode", c.getString(4)); Log.e("amt", "amt" + * c.getInt(2)); Log.e("date2", "date" + c.getInt(3)); */ category.add(c.getString(1)); mode.add(c.getString(4)); amount.add(c.getInt(2)); // int d=c.getInt(5); // String da=String.valueOf(d); // Log.e("da",da); String change = c.getString(3); // change.replace("", ""); change = change.substring(0, 8); Log.e("change", change); date.add(change); } /* * for(int i=0;i<category.size();i++){ Log.e("category...", * category.get(i)); Log.e("mode...", mode.get(i)); * Log.e("amt...", "amt" + amount.get(i)); Log.e("date2...", * "date" + date.get(i)); } */ dc.close(); Toast.makeText(getApplicationContext(), "Pdf Report Generated and saved in \n " + Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExpenseManager", Toast.LENGTH_LONG).show(); final Calendar calendar = Calendar.getInstance(); int year_current = calendar.get(Calendar.YEAR); int month_current = calendar.get(Calendar.MONTH) + 1; int day_current = calendar.get(Calendar.DAY_OF_MONTH); String print = day_current + "-" + month_current + "-" + year_current; // String print_date = new // StringBuilder().append(day_current).append("/").append(month_current // + 1).append("/") .append(year_current).toString(); savePreference(); FILE = Environment.getExternalStorageDirectory() + "/ExpenseManager/ExpenseManager" + " " + print + "(" + counter + ")" + ".pdf"; Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); // addMetaData(document); // addTitlePage(document); addContent(document); document.close(); } } catch (Exception e) { e.printStackTrace(); } } private void savePreference() { counter++; SharedPreferences sp = getSharedPreferences("AppSharedPref", 0); Editor editor = sp.edit(); editor.putInt("count", counter); editor.commit(); } private void loadPreference() { SharedPreferences sp = getSharedPreferences("AppSharedPref", 0); counter = sp.getInt("count", 0); } // display current date both on the text view and the Date Picker when the // application starts. public void setCurrentDate() { text_date1 = (TextView) findViewById(R.id.text_date1); tv_date1 = (TextView) findViewById(R.id.date_label1); date_picker1 = (DatePicker) findViewById(R.id.date_picker1); final Calendar calendar = Calendar.getInstance(); year1 = calendar.get(Calendar.YEAR); month1 = calendar.get(Calendar.MONTH); day1 = calendar.get(Calendar.DAY_OF_MONTH); // set current date into textview text_date1.setText(new StringBuilder() // Month is 0 based, so you have to add 1 .append(day1).append("/").append(month1 + 1).append("/").append(year1).append(" ")); // set current date into Date Picker date_picker1.init(year1, month1, day1, null); String s = new StringBuilder().append(year1).append(month1 + 1).append(day1).toString(); // Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); date1 = Integer.parseInt(s); // Toast.makeText(getApplicationContext(),"pdf"+date1,Toast.LENGTH_LONG).show(); } // display current date both on the text view and the Date Picker when the // application starts. public void setEndDate() { text_date2 = (TextView) findViewById(R.id.text_date2); tv_date2 = (TextView) findViewById(R.id.date_label2); date_picker2 = (DatePicker) findViewById(R.id.date_picker2); final Calendar calendar = Calendar.getInstance(); year2 = calendar.get(Calendar.YEAR); month2 = calendar.get(Calendar.MONTH); day2 = calendar.get(Calendar.DAY_OF_MONTH); // set current date into textview text_date2.setText(new StringBuilder() // Month is 0 based, so you have to add 1 .append(day2).append("/").append(month2 + 1).append("/").append(year2).append(" ")); // set current date into Date Picker date_picker2.init(year2, month2, day2, null); String s = new StringBuilder().append(year2).append(month2 + 1).append(day2).toString(); // Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); date2 = Integer.parseInt(s); // Toast.makeText(getApplicationContext(),"pdf"+date2,Toast.LENGTH_LONG).show(); } public void addButtonListener1() { bstart_date = (Button) findViewById(R.id.bstart_date); bstart_date.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // vibrate(50); showDialog(DATE_DIALOG_ID1); } }); } public void addButtonListener2() { bend_date = (Button) findViewById(R.id.bend_date); bend_date.setEnabled(false); bend_date.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // vibrate(50); showDialog(DATE_DIALOG_ID2); } }); } @Override protected Dialog onCreateDialog(int id) { if (id == DATE_DIALOG_ID1) { return new DatePickerDialog(this, datePickerListener1, year1, month1, day1); } else if (id == DATE_DIALOG_ID2) { return new DatePickerDialog(this, datePickerListener2, year2, month2, day2); } return null; } private DatePickerDialog.OnDateSetListener datePickerListener1 = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year1 = selectedYear; month1 = selectedMonth + 1; day1 = selectedDay; // set selected date into Text View text_date1.setText(new StringBuilder().append(day1).append("/").append(month1).append("/").append(year1) .append(" ")); text_date1.setVisibility(View.VISIBLE); tv_date1.setVisibility(View.VISIBLE); // set selected date into Date Picker date_picker1.init(year1, month1, day1, null); String s = year1 + "" + (month1 < 10 ? ("0" + month1) : (month1)) + "" + (day1 < 10 ? ("0" + day1) : (day1)); // String s = new StringBuilder().append(year1).append(month1 + 1) // .append(day1).toString(); // Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); date1 = Integer.parseInt(s); // Toast.makeText(getApplicationContext(), "our" + date1, // Toast.LENGTH_LONG).show(); bend_date.setEnabled(true); // bend_date.setBackgroundResource(R.drawable.calendar_new_end); } }; private DatePickerDialog.OnDateSetListener datePickerListener2 = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year2 = selectedYear; month2 = selectedMonth + 1; day2 = selectedDay; // set selected date into Text View text_date2.setText(new StringBuilder().append(day2).append("/").append(month2).append("/").append(year2) .append(" ")); text_date2.setVisibility(View.VISIBLE); tv_date2.setVisibility(View.VISIBLE); // set selected date into Date Picker date_picker2.init(year2, month2, day2, null); String s = year2 + "" + (month2 < 10 ? ("0" + month2) : (month2)) + "" + (day2 < 10 ? ("0" + day2) : (day2)); // String s = new StringBuilder().append(year2).append(month2 + // 1).append(day2).toString(); // Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); date2 = Integer.parseInt(s); // Toast.makeText(getApplicationContext(), "pdf" + date2, // Toast.LENGTH_LONG).show(); bpdf.setEnabled(true); // bpdf.setBackgroundResource(R.drawable.pdf1); } }; }