










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An example of creating a toast message in android using java code. It also demonstrates how to handle user input by adding listeners to edittext and button widgets, and performing basic arithmetic operations. The code includes both java and xml components.
Typology: Exercises
1 / 18
This page cannot be seen from the preview
Don't miss anything!











package com.example.androidlifecycle; import android.os.Bundle; import android.app.Activity; import android.view.Menu;import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(getApplicationContext(), "I am create method", Toast.LENGTH_LONG).show(); EXXXXXXXXXXXXX Java Code: package com.example.addition; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText edittext1,edittext2; private Button Btn_Add ; private Button Btn_Sub ; private Button Btn_Mul ; private Button Btn_Div ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_main ); addListenerOnButton(); } public void addListenerOnButton(){ edittext1=(EditText)findViewById(R.id. editText1 ); edittext2=(EditText)findViewById(R.id. editText2 ); Btn_Add=(Button)findViewById(R.id. button1 ); Btn_Sub=(Button)findViewById(R.id. button2 ); Btn_Mul=(Button)findViewById(R.id. button3 ); Btn_Div=(Button)findViewById(R.id. button4 ); Btn_Add.setOnClickListener( new OnClickListener(){ @Override public void onClick(View view) { String value1=edittext1.getText().toString(); String value2=edittext2.getText().toString(); int a=Integer. parseInt (value1); int b=Integer. parseInt (value2); int sum=a+b; Toast. makeText (getApplicationContext(),String. valueOf (sum),Toast. LENGTH_LONG ).show(); REPEAT*************************** public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu. main , menu); return true ;
XML Code: <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= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentLeft= "true" android:layout_alignParentTop= "true" android:layout_marginLeft= "53dp" android:layout_marginTop= "34dp" android:ems= "10" android:inputType= "number" >
android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="20dp" android:ems="10"/> 2222222
EX___6 Student database sqlite MainActivity.java public class MainActivity extends Activity implements OnClickListener { EditText Rollno,Name,Marks; Button Insert,Delete,Update,View,ViewAll; SQLiteDatabase db; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Rollno=(EditText)findViewById(R.id.Rollno); Name=(EditText)findViewById(R.id.Name); Marks=(EditText)findViewById(R.id.Marks); Insert=(Button)findViewById(R.id.Insert); Delete=(Button)findViewById(R.id.Delete); Update=(Button)findViewById(R.id.Update); View=(Button)findViewById(R.id.View); ViewAll=(Button)findViewById(R.id.ViewAll); Insert.setOnClickListener(this); Delete.setOnClickListener(this); Update.setOnClickListener(this); View.setOnClickListener(this); ViewAll.setOnClickListener(this); // Creating database and table db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null); db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);"); }public void onClick(View view) { if(view==Insert) { if(Rollno.getText().toString().trim().length()==0|| Name.getText().toString().trim().length()==0|| Marks.getText().toString().trim().length()==0){ showMessage("Error", "Please enter all values");return; } db.execSQL("INSERT INTO student VALUES('"+Rollno.getText()+"','"+Name.getText()+ "','"+Marks.getText()+"');"); showMessage("Success", "Record added"); clearText(); }if(view==Delete)
{ if(Rollno.getText().toString().trim().length()==0) { showMessage("Error", "Please enter Rollno"); return; } Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+Rollno.getText()+"'", null); if(c.moveToFirst()) { db.execSQL("DELETE FROM student WHERE rollno='"+Rollno.getText()+"'"); showMessage("Success", "Record Deleted"); } else { showMessage("Error", "Invalid Rollno"); } clearText(); } if(view==Update) { if(Rollno.getText().toString().trim().length()==0) { showMessage("Error", "Please enter Rollno"); Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+Rollno.getText()+"'", null); if(c.moveToFirst()) { db.execSQL("UPDATE student SET name='" + Name.getText() + "',marks='" + Marks.getText() + "' WHERE rollno='"+Rollno.getText()+"'"); showMessage("Success", "Record Modified"); } else { showMessage("Error", "Invalid Rollno"); } clearText(); } // Display a record from the Student table if(view==View) { // Checking for empty roll number if(Rollno.getText().toString().trim().length()==0) { showMessage("Error", "Please enter Rollno"); return; } Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+Rollno.getText()+"'", null); if(c.moveToFirst()) { Name.setText(c.getString(1)); Marks.setText(c.getString(2)); } else { showMessage("Error", "Invalid Rollno"); clearText(); } } // Displaying all the records
android:layout_y="100dp" android:inputType="number" android:textSize="20sp" />