2

I am new to android. Right now working on small project called School report. I like to get input from user on two different activity and when user click on createpdf button it should create PDF file in my device with all user input from both the activity. The two activity are report and report1 as you can see in screenshot. Issue is i don't know how to get other activity data for example: data from report1. when i input data in report activity and click on save it create pdf file with all data. But i want data should store temporarily in device of both the activity (report, report1) and when user click on createpdf button(MainActivity) it should create pdf with both the activities data.

I am using itextpdf library. If any one can rearrange the java logic part it would be great help. Thank you in advance

MainActivity.java



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


    public void buttonone(View view) {
        Intent intent = new Intent(this, report.class);
        startActivity(intent);
    }
    public void buttontwo(View view) {
        Intent intent = new Intent(this, report1.class);
        startActivity(intent);
    }

report.java



    EditText et1,et2,et3,et4;
    Button Save;
    String edit1,edit2,edit3,edit4;
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell1, cell2,cell3,cell4, cell5,cell6,cell7, cell8,cell9,cell10;
    File cacheDir;
    final Context context = this;

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

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Code Analyser");



        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();

        et1=(EditText)findViewById(R.id.editText1);
        et2=(EditText)findViewById(R.id.editText2);
        et3=(EditText)findViewById(R.id.editText3);
        et4=(EditText)findViewById(R.id.editText4);


        Save=(Button)findViewById(R.id.button1);
        Save.setOnClickListener(reportClickListener);
    }
    OnClickListener reportClickListener= new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            edit1=et1.getText().toString();
            edit2=et2.getText().toString();
            edit3=et3.getText().toString();
            edit4=et4.getText().toString();

            String FILE = Environment.getExternalStorageDirectory().toString() + "/Code Analyser/" + "report.pdf";

            // Create New Blank Document
            Document document = new Document(PageSize.A4);

            // Create Pdf Writer for Writting into New Created Document
            try {
                PdfWriter.getInstance(document, new FileOutputStream(FILE));
                // Open Document for Writting into document
                document.open();
                // User Define Method
                addTitlePage(document);
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Close Document after writting all content
            document.close();

            Toast.makeText(report.this, "PDF File is Created."+FILE, Toast.LENGTH_LONG).show();
        }
    };
    // Set PDF document Properties
    public void addTitlePage(Document document) throws DocumentException
    {
        // Font Style for Document
        Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
        Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD| Font.UNDERLINE, BaseColor.GRAY);
        Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
        Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);

        // Start New Paragraph
        Paragraph prHead = new Paragraph();
        // Set Font in this Paragraph
        prHead.setFont(titleFont);
        // Add item into Paragraph
        prHead.add("Code Analyzer\n");
        //prHead.add("\n");
        prHead.setAlignment(Element.ALIGN_CENTER);

        Paragraph cat = new Paragraph();
        cat.setFont(catFont);
        cat.add("\n");
        cat.add("Report\n");
        cat.add("\n");
        cat.setAlignment(Element.ALIGN_CENTER);

        // Add all above details into Document
        document.add(prHead);
        document.add(cat);
        document.add(table);

            /* Header values*/
        table = new PdfPTable(2);
        cell1 = new PdfPCell(new Phrase("Category"));
        cell2 = new PdfPCell(new Phrase("Values"));
        cell1.setVerticalAlignment(Element.ALIGN_LEFT);
        cell2.setVerticalAlignment(Element.ALIGN_LEFT);

        cell1.setBorder(Rectangle.NO_BORDER);
        cell1.setPadding(5);

        cell2.setBorder(Rectangle.NO_BORDER);
        cell2.setPadding(5);

        cell1.setBackgroundColor(BaseColor.GRAY);
        cell2.setBackgroundColor(BaseColor.GRAY);

            /*Table values*/
        cell3 = new PdfPCell(new Phrase("Name"));
        cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell3.setBorder(Rectangle.NO_BORDER);
        cell3.setPadding(5);

        cell4 = new PdfPCell(new Phrase(edit1));
        cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell4.setBorder(Rectangle.NO_BORDER);
        cell4.setPadding(5);

        cell5 = new PdfPCell(new Phrase("Mobile Number"));
        cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell5.setBorder(Rectangle.NO_BORDER);
        cell5.setPadding(5);

        cell6 = new PdfPCell(new Phrase(edit2));
        cell6.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell6.setBorder(Rectangle.NO_BORDER);
        cell6.setPadding(5);

        cell7 = new PdfPCell(new Phrase("Mail Id"));
        cell7.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell7.setBorder(Rectangle.NO_BORDER);
        cell7.setPadding(5);

        cell8 = new PdfPCell(new Phrase(edit3));
        cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell8.setBorder(Rectangle.NO_BORDER);
        cell8.setPadding(5);

        cell9 = new PdfPCell(new Phrase("City"));
        cell9.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell9.setBorder(Rectangle.NO_BORDER);
        cell9.setPadding(5);

        cell10 = new PdfPCell(new Phrase(edit4));
        cell10.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell10.setBorder(Rectangle.NO_BORDER);
        cell10.setPadding(5);

        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);
        table.addCell(cell5);
        table.addCell(cell6);
        table.addCell(cell7);
        table.addCell(cell8);
        table.addCell(cell9);
        table.addCell(cell10);

        // add table into document
        document.add(table);

        // Create new Page in PDF
        document.newPage();
        //Toast.makeText(this, "PDF File is Created.", Toast.LENGTH_LONG).show();
    }

report1.java



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

activity_main

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Personal Details"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="buttonone"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Student Details"
    android:id="@+id/button2"
    android:layout_below="@+id/button"
    android:layout_alignRight="@+id/button"
    android:layout_alignEnd="@+id/button"
    android:onClick="buttontwo"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Create PDF"
    android:id="@+id/button3"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="createpdf"/>

activity_report

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:hint="Name"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Mobile number"
    android:ems="10" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Mail id"
    android:ems="10" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="10dp"
    android:hint="City"
    android:ems="10" />


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/button9"
    android:layout_alignStart="@+id/button9" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button send"
    android:onClick="sendMessage"
    android:layout_marginBottom="136dp"
    android:id="@+id/button9"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

activity_report1

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:hint="Student Name"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Mobile number"
    android:ems="10" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Mail id"
    android:ems="10" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="10dp"
    android:hint="School name"
    android:ems="10" />


<Button
    android:id="@+id/buttonsave2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

Screenshot

Community
  • 1
  • 1
Vinayak Nadar
  • 47
  • 1
  • 7
  • I am not sure this will exactly solves ur issue. Try this, create a singleton class use an hashtable of type object class in the singleton class, and store the data from the screens and while generating report get the data from hashtable and print it and clear the hashtable. – Raghavendra Sep 07 '16 at 09:21

1 Answers1

0

You can do this a couple of different ways:

1) Using a 'global' data concept Store application global data

2) Pass extras into the Activity using a bundle How do I create an android Intent that carries data?

3) Write to a file and read from a file

4) Create each PDF file individually. Then use iTextPDF to combine them.

Community
  • 1
  • 1
Kafros
  • 166
  • 14
  • is it possible to do with sharedpreferences? like each activity data stored using sharedpreferences and in the end when click on createpdf button it should retrieve all activities data and create pdf – Vinayak Nadar Sep 07 '16 at 16:24
  • You could, but shared preferences is really for different use. For example saving a users settings in some offline game. – Kafros Sep 08 '16 at 08:40