Tuesday, 22 January 2019

Students Records App with Source Code

 MainActivity.Java :-
 
package com.irawen.attendance;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    EditText ename,eroll_no,emarks;
    Button add,view,viewall,Show1,delete,modify;
    SQLiteDatabase db;

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

        ename=(EditText)findViewById(R.id.name);
        eroll_no=(EditText)findViewById(R.id.roll_no);
        emarks=(EditText)findViewById(R.id.marks);
        add=(Button)findViewById(R.id.addbtn);
        view=(Button)findViewById(R.id.viewbtn);
        viewall=(Button)findViewById(R.id.viewallbtn);
        delete=(Button)findViewById(R.id.deletebtn);
        Show1=(Button)findViewById(R.id.showbtn);
        modify=(Button)findViewById(R.id.modifybtn);



db=openOrCreateDatabase("Student_manage", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno INTEGER,name VARCHAR,marks INTEGER);");


        add.setOnClickListener(new OnClickListener() {

            @Override            public void onClick(View v) {
                // TODO Auto-generated method stub 
 if(eroll_no.getText().toString().trim().length()==0||
                        ename.getText().toString().trim().length()==0||
                        emarks.getText().toString().trim().length()==0)
                {
                    showMessage("Error", "Please enter all values");
                    return;
                }
                db.execSQL("INSERT INTO student VALUES('"+eroll_no.getText()+"','"+ename.getText()+
                        "','"+emarks.getText()+"');");
                showMessage("Success", "Record added successfully");
                clearText();
            }
        });
        delete.setOnClickListener(new OnClickListener() {

            @Override            public void onClick(View v) {
                // TODO Auto-generated method stub 
 if(eroll_no.getText().toString().trim().length()==0)
                {
                    showMessage("Error", "Please enter Rollno");
                    return;
                }
                Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+eroll_no.getText()+"'", null);
                if(c.moveToFirst())
                {
                    db.execSQL("DELETE FROM student WHERE rollno='"+eroll_no.getText()+"'");
                    showMessage("Success", "Record Deleted");
                }
                else                {
                    showMessage("Error", "Invalid Rollno");
                }
                clearText();
            }
        });
        modify.setOnClickListener(new OnClickListener() {

            @Override            public void onClick(View v) {
                // TODO Auto-generated method stub 
 if(eroll_no.getText().toString().trim().length()==0)
                {
                    showMessage("Error", "Please enter Rollno");
                    return;
                }
 Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+eroll_no.getText()+"'", null);
                if(c.moveToFirst())
                {
     db.execSQL("UPDATE student SET name='"+ename.getText()+"',marks='"+emarks.getText()+
                            "' WHERE rollno='"+eroll_no.getText()+"'");
                    showMessage("Success", "Record Modified");
                }
                else                {
                    showMessage("Error", "Invalid Rollno");
                }
                clearText();
            }
        });
        view.setOnClickListener(new OnClickListener() {




            @Override            public void onClick(View v) {
                // TODO Auto-generated method stub 
 if(eroll_no.getText().toString().trim().length()==0)
                {
                    showMessage("Error", "Please enter Rollno");
                    return;
                }
 Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+eroll_no.getText()+"'", null);
                if(c.moveToFirst())
                {
                    ename.setText(c.getString(1));
                    emarks.setText(c.getString(2));
                }
                else                {
                    showMessage("Error", "Invalid Rollno");
                    clearText();
                }
            }
        });
        viewall.setOnClickListener(new OnClickListener() {

            @Override            public void onClick(View v) {
                // TODO Auto-generated method stub 
 Cursor c=db.rawQuery("SELECT * FROM student", null);
                if(c.getCount()==0)
                {
                    showMessage("Error", "No records found");
                    return;
                }
                StringBuffer buffer=new StringBuffer();
                while(c.moveToNext())
                {

                    buffer.append("Rollno: "+c.getString(0)+"\n");
                    buffer.append("Name: "+c.getString(1)+"\n");
                    buffer.append("Marks: "+c.getString(2)+"\n\n");
                }
                showMessage("Student Details", buffer.toString());
            }
        });
        Show1.setOnClickListener(new OnClickListener() {

            @Override            public void onClick(View v) {
                // TODO Auto-generated method stub 
 showMessage("Student Management Application", "Irawen Education");
            }
        });

    }
    public void showMessage(String title,String message)
    {
        Builder builder=new Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
    public void clearText()
    {


        eroll_no.setText("");
        ename.setText("");
        emarks.setText("");
        eroll_no.requestFocus();
    }
    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present. 
 getMenuInflater().inflate(R.menu.student_main, menu);
        return true;
    }

}


MainActivity.xml :- 

<LinearLayout xmlns: 
android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent"
 android:background="#707777"
 android:orientation="vertical" 
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/roll_no" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:ems="10" 
 android:hint="Enter Roll No." 
 android:inputType="number" >
    <requestFocus />
</EditText>



<EditText 
 android:id="@+id/name" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:ems="10"    android:hint="Enter Your Name" />

<EditText 
 android:id="@+id/marks" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:ems="10" 
 android:hint="Enter Marks" 
 android:inputType="number" />



<LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="98dp" >

    <Button 
 android:id="@+id/addbtn" 
 android:layout_width="140dp" 
 android:layout_height="90dp"
        android:text="Add" />



    <Button         
android:id="@+id/deletebtn" 
 android:layout_width="140dp" 
 android:layout_height="90dp"         
android:layout_marginLeft="50dp" 
 android:text="Delete" />
</LinearLayout>

<LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="98dp" >

    <Button         
android:id="@+id/modifybtn" 
 android:layout_width="140dp" 
 android:layout_height="90dp" 
 android:text="Modify" />

    <Button 
 android:id="@+id/viewbtn" 
 android:layout_width="140dp" 
 android:layout_height="90dp" 
 android:layout_marginLeft="50dp" 
 android:text="View" />
</LinearLayout>



<LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="0.74" >

    <Button 
 android:id="@+id/viewallbtn" 
 android:layout_width="140dp" 
 android:layout_height="90dp" 
 android:text="View all" />




    <Button 
 android:id="@+id/showbtn"         
android:layout_width="140dp" 
 android:layout_height="90dp" 
 android:layout_marginLeft="50dp" 
 android:text="Show" />

</LinearLayout>

</LinearLayout> 

1 comment:

Codecademy Code Foundations

Popular Posts

Categories

Android (23) AngularJS (1) Assembly Language (2) Books (11) C (75) C# (12) C++ (81) Course (3) Data Science (1) Data Strucures (4) Downloads (1) Engineering (13) flutter (1) FPL (17) Hadoop (1) HTML&CSS (40) IS (25) Java (89) Leet Code (4) Pandas (2) PHP (20) Projects (19) Python (435) R (69) Selenium Webdriver (2) Software (14) SQL (27)