Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Practical (Greet Application)
- //Create an Android App that demonstrates working with one EditText and one TextView. User will enter his/her name in EditText and “Good morning, username” should be displayed in TextView as well as show message in Toast.
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/btnShow"
- android:layout_width="233dp"
- android:layout_height="52dp"
- android:layout_alignParentTop="true"
- android:layout_marginStart="89dp"
- android:layout_marginLeft="89dp"
- android:layout_marginEnd="89dp"
- android:layout_marginRight="89dp"
- android:layout_marginBottom="136dp"
- android:text="Click me!"
- android:onClick="Greet"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="1.0"
- app:layout_constraintStart_toStartOf="parent" />
- <TextView
- android:id="@+id/textView"
- android:layout_width="109dp"
- android:layout_height="27dp"
- android:layout_marginStart="16dp"
- android:layout_marginLeft="16dp"
- android:layout_marginEnd="21dp"
- android:layout_marginRight="21dp"
- android:layout_marginBottom="100dp"
- android:text="Enter Name : "
- android:textAppearance="@style/TextAppearance.AppCompat.Body2"
- app:layout_constraintBottom_toTopOf="@+id/btnShow"
- app:layout_constraintEnd_toStartOf="@+id/edName"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.564" />
- <EditText
- android:id="@+id/edName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="30dp"
- android:layout_marginLeft="30dp"
- android:layout_marginEnd="43dp"
- android:layout_marginRight="43dp"
- android:layout_marginBottom="100dp"
- android:ems="10"
- android:hint="Enter your name"
- android:inputType="textPersonName"
- app:layout_constraintBottom_toTopOf="@+id/btnShow"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/textView"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.564" />
- <TextView
- android:id="@+id/txtName"
- android:layout_width="192dp"
- android:layout_height="33dp"
- android:layout_marginStart="109dp"
- android:layout_marginLeft="109dp"
- android:layout_marginTop="128dp"
- android:layout_marginEnd="110dp"
- android:layout_marginRight="110dp"
- android:layout_marginBottom="113dp"
- android:text="TextView"
- app:layout_constraintBottom_toTopOf="@+id/btnShow"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/edName" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java File
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- protected EditText ed1;
- protected TextView tv1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ed1 = (EditText) findViewById(R.id.edName);
- tv1 = (TextView) findViewById(R.id.txtName);
- }
- public void Greet(View view){
- String s,m;
- s = ed1.getText().toString();
- m = "Good Morning " + s;
- Toast.makeText(getApplicationContext(), m, Toast.LENGTH_SHORT).show();
- tv1.setText(m);
- }
- }
- //Practical (Create an Android App that demonstrates Activity lifecycle and instance state.)
- acttivity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Activity LifeCycle"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java File
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.d("Activity Lifecycle","onCreate invoked");
- }
- @Override
- protected void onStart() {
- super.onStart();
- Log.d("Activity Lifecycle","onStart invoked");
- }
- @Override
- protected void onResume() {
- super.onResume();
- Log.d("Activity Lifecycle","onResume invoked");
- }
- @Override
- protected void onPause() {
- super.onPause();
- Log.d("Activity Lifecycle","onPause invoked");
- }
- @Override
- protected void onStop() {
- super.onStop();
- Log.d("Activity Lifecycle","onStop invoked");
- }
- @Override
- protected void onRestart() {
- super.onRestart();
- Log.d("Activity Lifecycle","onRestart invoked");
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- Log.d("Activity Lifecycle","onDestroy invoked");
- }
- }
- AndroidManifest.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.activitylifecycle"> //Ye line change krna apne application ke naam se
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="Activity LifeCycle"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- //Practical(Create an Android App that demonstrates use of Option menu.)
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/relativeLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Android Option Menu"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/text1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/text" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- menu_main.xml File(Create a folder with name "menu" and create a xml file named "menu_main")
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/notification"
- android:title="Notification" />
- <item
- android:id="@+id/help"
- android:title="Help" />
- <item
- android:id="@+id/setting"
- android:title="Setting" />
- <item
- android:id="@+id/logout"
- android:title="Logout" />
- </menu>
- MainActivity.java File
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- public class MainActivity extends AppCompatActivity {
- TextView text;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- text=(TextView)findViewById(R.id.text);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu){
- MenuInflater menuInflater=getMenuInflater();
- menuInflater.inflate(R.menu.menu_main,menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item){
- switch(item.getItemId()){
- case R.id.notification:
- Toast.makeText(this,"Notification",Toast.LENGTH_SHORT).show();
- break;
- case R.id.help:
- Toast.makeText(this,"Help",Toast.LENGTH_SHORT).show();
- break;
- case R.id.setting:
- Toast.makeText(this,"Setting",Toast.LENGTH_SHORT).show();
- break;
- case R.id.logout:
- Toast.makeText(this,"Logout",Toast.LENGTH_SHORT).show();
- break;
- default:
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- //Practical (Create an Android App that demonstrates Screen Navigation using App Bar and Tabs)
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <com.google.android.material.tabs.TabLayout
- android:id="@+id/tablayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <com.google.android.material.tabs.TabItem
- android:id="@+id/item1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Tab 1" />
- <com.google.android.material.tabs.TabItem
- android:id="@+id/item2"
- android:nextFocusRight="@id/item3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Tab 2" />
- <com.google.android.material.tabs.TabItem
- android:id="@+id/item3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Tab 3" />
- </com.google.android.material.tabs.TabLayout>
- <androidx.viewpager.widget.ViewPager
- android:id="@+id/viewpager"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </RelativeLayout>
- fragment_page1.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <RelativeLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center">
- <TextView
- android:id="@+id/displayContent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Title" />
- <TextView
- android:id="@+id/description"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/displayContent"
- android:text="Description" />
- </RelativeLayout>
- </FrameLayout>
- VPAdapter.java File
- import androidx.annotation.Nullable;
- import androidx.fragment.app.Fragment;
- import androidx.fragment.app.FragmentManager;
- import androidx.fragment.app.FragmentPagerAdapter;
- import java.util.ArrayList;
- public class VPAdapter extends FragmentPagerAdapter {
- private final ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
- private final ArrayList<String> fragmentTitle = new ArrayList<>();
- public VPAdapter(FragmentManager fm)
- {
- super(fm);
- }
- @Override
- public Fragment getItem(int position)
- {
- return fragmentArrayList.get(position);
- }
- @Override
- public int getCount()
- {
- return fragmentArrayList.size();
- }
- public void addFragment(Fragment fragment, String title)
- {
- fragmentArrayList.add(fragment);
- fragmentTitle.add(title);
- }
- @Nullable
- @Override
- public CharSequence getPageTitle(int position)
- { return fragmentTitle.get(position); }
- }
- customFragment.java File
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import androidx.fragment.app.Fragment;
- public class customFragment extends Fragment {
- String displayContent,description;
- customFragment(String name,String desc)
- {
- this.displayContent = name;
- this.description = desc;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View customView = inflater.inflate(R.layout.fragment_page1,container,false);
- ((TextView)customView.findViewById(R.id.displayContent)).setText(this.displayContent);
- ((TextView)customView.findViewById(R.id.description)).setText(this.description);
- return customView;
- }
- }
- MainActivity.java File
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.viewpager.widget.ViewPager;
- import com.google.android.material.tabs.TabLayout;
- public class MainActivity extends AppCompatActivity {
- private TabLayout tabLayout;
- private ViewPager viewPager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tabLayout = findViewById(R.id.tablayout);
- viewPager = findViewById(R.id.viewpager);
- tabLayout.setupWithViewPager(viewPager);
- VPAdapter vpAdapter = new VPAdapter(getSupportFragmentManager());
- vpAdapter.addFragment(new customFragment("Roll number: 1133","You are on the First page."),"Page 1");
- vpAdapter.addFragment(new customFragment("Name: Dhananjay","You are on the Second page."),"Page 2");
- vpAdapter.addFragment(new customFragment("Class: SYBSc Computer Science - A","You are on the Thirdpage."),"Page3");
- viewPager.setAdapter(vpAdapter);
- }
- }
- //Practical (Create an Android App that demonstrates navigation between three Activities.)
- AndroidManifest.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.twoactivity">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="First Activity"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".SecondActivity"
- android:label="Second Activity"
- android:parentActivityName=".MainActivity">
- <meta-data
- android:name = "android.support.PARENT_ACTIVITY"
- android:value = ".MainActivity"></meta-data>
- </activity>
- <activity android:name=".ThirdActivity"
- android:label="Third Activity"
- android:parentActivityName=".SecondActivity">
- <meta-data
- android:name = "android.support.PARENT_ACTIVITY"
- android:value = ".SecondActivity"></meta-data>
- </activity>
- </application>
- </manifest>
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="First Activity"
- android:textSize="16sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.498"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.186" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="496dp"
- android:onClick="callSecondActivity"
- android:text="CallSecondActivity"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.497"
- app:layout_constraintStart_toStartOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- activity_second.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".SecondActivity">
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="124dp"
- android:text="Second Activity"
- android:textSize="16sp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="504dp"
- android:onClick="callThirdActivity"
- android:text="CallThirdActivity"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- activity_third.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".ThirdActivity">
- <TextView
- android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="128dp"
- android:text="Third Activity"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.498"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="500dp"
- android:onClick="callFirstActivity"
- android:text="CallFirstActivity"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.497"
- app:layout_constraintStart_toStartOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java File
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void callSecondActivity(View view){
- Intent i = new
- Intent(getApplicationContext(),SecondActivity.class);
- i.putExtra("Value1","Traversed to Second Activity");
- i.putExtra("Value2","Three Activities Example");
- startActivity(i);
- }
- }
- SecondActivity.java File
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- public class SecondActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- Bundle extras = getIntent().getExtras();
- String value1 = extras.getString("Value1");
- String value2 = extras.getString("Value2");
- Toast.makeText(this,"Value1: "+value1+"\n"+"Value2: "+value2,Toast.LENGTH_LONG).show();
- }
- public void callThirdActivity(View view){
- Intent i = new Intent(getApplicationContext(),ThirdActivity.class);
- startActivity(i);
- }
- }
- ThirdActivity.java File
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- public class ThirdActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_third);
- }
- public void callFirstActivity(View view){
- Intent i = new Intent(getApplicationContext(),MainActivity.class);
- startActivity(i);
- }
- }
- //Practical (Database)
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation = "vertical"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:text="UserName" />
- <EditText
- android:id="@+id/userNameValue"
- android:textSize="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:text="Password" />
- <EditText
- android:id="@+id/passwordValue"
- android:textSize="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:inputType="textPassword"/>
- <Button
- android:id="@+id/button"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="addUser"
- android:text="Add User" />
- <Button
- android:id="@+id/button2"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="viewDetails"
- android:text="View Details" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text = "Enter Username"
- android:textSize = "20dp">
- </TextView>
- <EditText
- android:id="@+id/name"
- android:textSize="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop = "10dp"
- android:text = "NewUserName to Update"
- android:textSize = "20dp">
- </TextView>
- <EditText
- android:id = "@+id/newName"
- android:layout_width = "wrap_content"
- android:layout_height= "wrap_content"
- android:textSize = "20dp">
- </EditText>
- <Button
- android:id="@+id/button4"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="update"
- android:text="Update" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop = "10dp"
- android:text = "Delete User"
- android:textSize = "20dp">
- </TextView>
- <EditText
- android:id = "@+id/delName"
- android:layout_width = "wrap_content"
- android:layout_height= "wrap_content"
- android:textSize = "20dp">
- </EditText>
- <Button
- android:id="@+id/button5"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="delete"
- android:text="Delete" />
- </LinearLayout>
- Message.java File
- import android.widget.Toast;
- import android.content.Context;
- public class Message {
- public static void message(Context context,String message){
- Toast.makeText(context,message,Toast.LENGTH_LONG).show();
- }}
- DCSDatabaseAdapter.java File
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.SQLException;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.content.Context;
- public class DCSDatabaseAdapter {
- private Context context;
- private SQLiteDatabase db;
- private DCSHelper helper;
- public DCSDatabaseAdapter(Context c){
- context = c;
- }
- //Open Connection Database.
- public DCSDatabaseAdapter open() throws SQLException{
- helper = new DCSHelper(context);
- db = helper.getWritableDatabase();
- return this;
- }
- //Insert Data
- public long insertData(String name,String password){
- //SQLiteDatabase db = helper.getWritableDatabase(); //pen
- ContentValues contentValues = new ContentValues();
- contentValues.put(DCSHelper.NAME,name);
- contentValues.put(DCSHelper.PASSWORD,password);
- long id = db.insert(DCSHelper.TABLE_NAME,null,contentValues);
- //Message.message(context,""+id);
- return id;
- }
- //GetAll the Records
- public String getAllData(){
- //SQLiteDatabase db = helper.getWritableDatabase();
- String[] columns = {DCSHelper.NAME,DCSHelper.PASSWORD};
- Cursor cursor = db.query(DCSHelper.TABLE_NAME,columns,null,null,null,null,null,null);
- StringBuffer buffer = new StringBuffer();
- while(cursor.moveToNext()){
- String name = cursor.getString(0);
- String password = cursor.getString(1);
- buffer.append(name+ " " +password+ "\n");
- }
- return buffer.toString();
- }
- //Get Particular Data
- public String getData(String name){
- //SQLiteDatabase db = helper.getWritableDatabase();
- String[] columns = {DCSHelper.NAME,DCSHelper.PASSWORD};
- String uname = name;
- Cursor cursor = db.query(DCSHelper.TABLE_NAME,columns,null,null,null,null,null,null);
- StringBuffer buffer = new StringBuffer();
- while(cursor.moveToNext()){
- String naam = cursor.getString(0);
- String pass = cursor.getString(1);
- if(naam == uname){
- buffer.append(naam+ " " +pass);
- break;
- }
- }
- return buffer.toString();
- }
- //Update Names
- public int updateName(String oldName,String newName){
- //SQLiteDatabase db = helper.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(DCSHelper.NAME,newName);
- String[] whereArgs = {oldName};
- int count = db.update(DCSHelper.TABLE_NAME,contentValues,DCSHelper.NAME+"=?",whereArgs);
- return count;
- }
- public int deleteRow(String name){
- //SQLiteDatabase db = helper.getWritableDatabase();
- String[] whereArgs = {name};
- int count = db.delete(DCSHelper.TABLE_NAME,DCSHelper.NAME+"=?",whereArgs);
- return count;
- }
- static class DCSHelper extends SQLiteOpenHelper{
- private static final String DATABASE_NAME = "Dcsdatabase10";
- private static final String TABLE_NAME = "DCSTABLE";
- private static final int DATABASE_VERSION = 1;
- private static final String UID = "_id";
- private static final String NAME = "Name";
- private static final String PASSWORD = "Password";
- private static final String CREATE_TABLE = "CREATE TABLE " +TABLE_NAME + "("
- + NAME + " VARCHAR(255)," +PASSWORD+ " VARCHAR(255));";
- private static final String DROP_TABLE = "DROP TABLE IF EXISTS " +TABLE_NAME+";";
- private Context context;
- public DCSHelper(Context context){
- super(context,DATABASE_NAME,null,DATABASE_VERSION);
- this.context = context;
- Message.message(context,"DCSHelper Called..");
- }
- @Override
- public void onCreate(SQLiteDatabase db){
- try{
- db.execSQL(CREATE_TABLE);
- Message.message(context,"OnCreate() Called..");
- }catch(SQLException e){
- Message.message(context,""+e);
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
- try{
- db.execSQL(DROP_TABLE);
- onCreate(db);
- }catch(SQLException e){
- Message.message(context,""+e);
- }
- }
- }
- }
- MainActivity.java File
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import androidx.appcompat.app.AppCompatActivity;
- import android.widget.EditText;
- import android.view.View;
- import android.os.Bundle;
- public class MainActivity extends AppCompatActivity {
- EditText userName,password,name,newName,delName;
- DCSDatabaseAdapter dcsHelper;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- userName = (EditText) findViewById(R.id.userNameValue);
- password = (EditText) findViewById(R.id.passwordValue);
- name = (EditText) findViewById(R.id.name);
- newName = (EditText) findViewById(R.id.newName);
- delName = (EditText) findViewById(R.id.delName);
- dcsHelper = new DCSDatabaseAdapter(this);
- dcsHelper.open();
- }
- public void addUser(View view){
- String user = userName.getText().toString();
- String pass = password.getText().toString();
- long id = dcsHelper.insertData(user,pass);
- if(id < 0){
- Message.message(this,"Unsuccessful..");
- }
- else{
- Message.message(this,"Successfull...");
- }
- }
- public void viewDetails(View view){
- String data = dcsHelper.getAllData();
- Message.message(this,data);
- }
- /*
- public void getDetails(View view){
- String s1 = name.getText().toString();
- String data = dcsHelper.getData(s1);
- Message.message(this,data);
- }*/
- public void update(View view){
- String naam = name.getText().toString();
- String nnaam = newName.getText().toString();
- dcsHelper.updateName(naam,nnaam);
- }
- public void delete(View view){
- String naam = delName.getText().toString();
- int count = dcsHelper.deleteRow(naam);
- Message.message(this,""+count);
- }
- }
- //Practical (Calculator)
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <RelativeLayout
- android:id = "@+id/relativeId1"
- android:layout_width = "312dp"
- android:layout_height = "wrap_content"
- android:layout_gravity = "center"
- android:layout_marginLeft = "5dp">
- <TextView
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:id = "@+id/textView1"
- android:layout_marginTop = "300dp"
- android:layout_marginLeft = "25dp"
- android:textSize = "30dp">
- </TextView>
- <TextView
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:id = "@+id/textView2"
- android:layout_marginTop = "250dp"
- android:layout_marginLeft = "25dp"
- android:textSize = "20dp">
- </TextView>
- <Button
- android:id = "@+id/buttonClear"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignParentLeft = "true"
- android:layout_marginTop = "350dp"
- android:padding = "20px"
- android:text = "Clear">
- </Button>
- <Button
- android:id = "@+id/buttonBack"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/buttonClear"
- android:layout_toRightOf = "@+id/buttonClear"
- android:padding = "20px"
- android:text = "bk">
- </Button>
- <Button
- android:id = "@+id/buttonMod"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/buttonClear"
- android:layout_toRightOf = "@+id/buttonBack"
- android:padding = "20px"
- android:text = "%">
- </Button>
- <Button
- android:id = "@+id/buttonDiv"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/buttonClear"
- android:layout_toRightOf = "@+id/buttonMod"
- android:padding = "20px"
- android:text = "/">
- </Button>
- <Button
- android:id = "@+id/button7"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignParentLeft = "true"
- android:layout_below = "@+id/buttonClear"
- android:padding = "20px"
- android:text = "7">
- </Button>
- <Button
- android:id = "@+id/button8"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button7"
- android:layout_toRightOf = "@+id/button7"
- android:padding = "20px"
- android:text = "8">
- </Button>
- <Button
- android:id = "@+id/button9"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button7"
- android:layout_toRightOf = "@+id/button8"
- android:padding = "20px"
- android:text = "9">
- </Button>
- <Button
- android:id = "@+id/buttonMul"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button7"
- android:layout_toRightOf = "@+id/button9"
- android:padding = "20px"
- android:text = "X">
- </Button>
- <Button
- android:id = "@+id/button4"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignParentLeft = "true"
- android:layout_below = "@+id/button7"
- android:padding = "20px"
- android:text = "4">
- </Button>
- <Button
- android:id = "@+id/button5"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button4"
- android:layout_toRightOf = "@+id/button4"
- android:padding = "20px"
- android:text = "5">
- </Button>
- <Button
- android:id = "@+id/button6"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button4"
- android:layout_toRightOf = "@+id/button5"
- android:padding = "20px"
- android:text = "6">
- </Button>
- <Button
- android:id = "@+id/buttonSub"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button4"
- android:layout_toRightOf = "@+id/button6"
- android:padding = "20px"
- android:text = "-">
- </Button>
- <Button
- android:id = "@+id/button1"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignParentLeft = "true"
- android:layout_below = "@+id/button4"
- android:padding = "20px"
- android:text = "1">
- </Button>
- <Button
- android:id = "@+id/button2"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button1"
- android:layout_toRightOf = "@+id/button1"
- android:padding = "20px"
- android:text = "2">
- </Button>
- <Button
- android:id = "@+id/button3"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button1"
- android:layout_toRightOf = "@+id/button2"
- android:padding = "20px"
- android:text = "3">
- </Button>
- <Button
- android:id = "@+id/buttonAdd"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/button1"
- android:layout_toRightOf = "@+id/button3"
- android:padding = "20px"
- android:text = "+">
- </Button>
- <Button
- android:id = "@+id/buttonNull"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignParentLeft = "true"
- android:layout_below = "@+id/button1"
- android:padding = "20px"
- android:text = "NA">
- </Button>
- <Button
- android:id = "@+id/button0"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/buttonNull"
- android:layout_toRightOf = "@+id/buttonNull"
- android:padding = "20px"
- android:text = "0">
- </Button>
- <Button
- android:id = "@+id/buttonDot"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/buttonNull"
- android:layout_toRightOf = "@+id/button0"
- android:padding = "20px"
- android:text = ".">
- </Button>
- <Button
- android:id = "@+id/buttonAns"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_alignTop = "@+id/buttonNull"
- android:layout_toRightOf = "@+id/buttonDot"
- android:padding = "20px"
- android:text = "=">
- </Button>
- </RelativeLayout>
- </LinearLayout>
- MainActivity.java File
- import androidx.appcompat.app.AppCompatActivity;
- import android.widget.TextView;
- import android.widget.Button;
- import android.view.View;
- import android.os.Bundle;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- private TextView tv1,tv2;
- private boolean ans=true;
- private Integer b,dotHelp=1,ansPress=1;
- private double num1,num2,result;
- private String a = "",aa ="";
- private Button
- b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bDot,bAns,bAdd,bSub,bMul,bDiv,bMod,bBack,
- bClear;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv1 = (TextView) findViewById(R.id.textView1);
- tv2 = (TextView) findViewById(R.id.textView2);
- b1 = (Button) findViewById(R.id.button1);
- b2 = (Button) findViewById(R.id.button2);
- b3 = (Button) findViewById(R.id.button3);
- b4 = (Button) findViewById(R.id.button4);
- b5 = (Button) findViewById(R.id.button5);
- b6 = (Button) findViewById(R.id.button6);
- b7 = (Button) findViewById(R.id.button7);
- b8 = (Button) findViewById(R.id.button8);
- b9 = (Button) findViewById(R.id.button9);
- b0 = (Button) findViewById(R.id.button0);
- bDot = (Button) findViewById(R.id.buttonDot);
- bAns = (Button) findViewById(R.id.buttonAns);
- bAdd = (Button) findViewById(R.id.buttonAdd);
- bSub = (Button) findViewById(R.id.buttonSub);
- bMul = (Button) findViewById(R.id.buttonMul);
- bMod = (Button) findViewById(R.id.buttonMod);
- bDiv = (Button) findViewById(R.id.buttonDiv);
- bClear = (Button) findViewById(R.id.buttonClear);
- b1.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "1";
- tv1.setText(a);
- }
- });
- b2.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "2";
- tv1.setText(a);
- }
- });
- b3.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "3";
- tv1.setText(a);
- }
- });
- b4.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "4";
- tv1.setText(a);
- }
- });
- b5.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "5";
- tv1.setText(a);
- }
- });
- b6.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "6";
- tv1.setText(a);
- }
- });
- b7.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "7";
- tv1.setText(a);
- }
- });
- b8.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "8";
- tv1.setText(a);
- }
- });
- b9.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "9";
- tv1.setText(a);
- }
- });
- b0.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- a = tv1.getText().toString();
- a = a + "0";
- tv1.setText(a);
- }
- });
- bAdd.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- aa = a;
- b = 1;
- a = "";
- dotHelp=1;
- if(a=="" && aa=="")
- {
- tv2.setText("");
- tv1.setText("");
- }
- else
- {
- tv2.setText(aa+ "+");
- tv1.setText("");
- }
- }
- });
- bSub.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- aa = a;
- b = 2;
- a = "";
- dotHelp=1;
- if(a=="" && aa=="") {
- tv1.setText("");
- tv2.setText("");
- }
- else
- {
- tv2.setText(aa+ "-");
- tv1.setText("");
- }
- }
- });
- bMul.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- aa = a;
- b = 3;
- a = "";
- dotHelp=1;
- if(a=="" && aa=="") {
- tv1.setText("");
- tv2.setText("");
- }
- else
- {
- tv2.setText(aa+ "*");
- tv1.setText("");
- }
- }
- });
- bDiv.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- aa = a;
- b = 4;
- a = "";
- dotHelp=1;
- if(a=="" && aa=="") {
- tv1.setText("");
- tv2.setText("");
- }
- else
- {
- tv2.setText(aa+ "/");
- tv1.setText("");
- }
- }
- });
- bMod.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- aa = a;
- b = 5;
- a = "";
- dotHelp=1;
- if(a=="" && aa=="") {
- tv1.setText("");
- tv2.setText("");
- }
- else
- {
- tv2.setText(aa+ "%");
- tv1.setText("");
- }
- }
- });
- bDot.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- if(dotHelp==1){
- a = tv1.getText().toString();
- a = a + ".";
- tv1.setText(a);
- }
- else{
- a = tv1.getText().toString();
- a = a + "";
- tv1.setText(a);
- }
- dotHelp++;
- }
- });
- bAns.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- String op="";
- if(ansPress == 1)
- {
- if(b==1)
- {
- try {
- num1 = Double.parseDouble(aa);
- num2 = Double.parseDouble(a);
- result = num1 + num2;
- }catch(Exception e){
- ans = false;
- };
- aa = "";
- a = "";
- op = "+";
- }
- else if(b==2)
- {
- try {
- num1 = Double.parseDouble(aa);
- num2 = Double.parseDouble(a);
- result = num1 - num2;
- }catch(Exception e)
- {
- ans = false;
- }
- aa = "";
- a = "";
- op = "-";
- }
- else if(b==3)
- {
- try {
- num1 = Double.parseDouble(aa);
- num2 = Double.parseDouble(a);
- result = num1 * num2;
- }catch(Exception e){
- ans = false;
- }
- aa = "";
- a = "";
- op = "*";
- }
- else if(b==4)
- {
- num1 = Double.parseDouble(aa);
- num2 = Double.parseDouble(a);
- aa = "";
- a = "";
- op = "/";
- try{
- result = num1/num2;
- }catch(Exception e)
- {
- ans = false;
- }
- }
- else if(b==5)
- {
- num1 = Double.parseDouble(aa);
- num2 = Double.parseDouble(a);
- op = "%";
- try{
- result = num1%num2;
- }catch(Exception e)
- {
- ans = false;
- }
- }
- ansPress++;
- }
- else
- {
- tv1.setText(Double.toString(result));
- }
- if(ans)
- {
- tv2.setText(num1 + op + num2);
- tv1.setText(Double.toString(result));
- String ans = num1 + op + num2 + " = " +result;
- Toast.makeText(MainActivity.this,ans,Toast.LENGTH_LONG).show();
- }
- else
- {
- tv2.setText(num1 + op + num2);
- tv1.setText("Invalid Expression!!");
- ans = true;
- }
- }
- });
- bClear.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view)
- {
- tv1.setText("");
- tv2.setText("");
- dotHelp=1;
- ansPress=1;
- }
- });
- }
- }
- //Practical (Create SYBSC database in SQLite. Create table SYCS with following columns
- ROLLNO int
- Name varchar(100)
- Insert 3 records in it.)
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation = "vertical"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:text="Name" />
- <EditText
- android:id="@+id/userNameValue"
- android:textSize="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:text="ROLLNO" />
- <EditText
- android:id="@+id/passwordValue"
- android:textSize="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:inputType="textPassword"/>
- <Button
- android:id="@+id/button"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="addUser"
- android:text="Add User" />
- <Button
- android:id="@+id/button2"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="viewDetails"
- android:text="View Details" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text = "Enter Name to Update"
- android:textSize = "20dp">
- </TextView>
- <EditText
- android:id="@+id/name"
- android:textSize="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop = "10dp"
- android:text = "New Name to Update"
- android:textSize = "20dp">
- </TextView>
- <EditText
- android:id = "@+id/newName"
- android:layout_width = "wrap_content"
- android:layout_height= "wrap_content"
- android:textSize = "20dp">
- </EditText>
- <Button
- android:id="@+id/button4"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="update"
- android:text="Update" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop = "10dp"
- android:text = "Delete User"
- android:textSize = "20dp">
- </TextView>
- <EditText
- android:id = "@+id/delName"
- android:layout_width = "wrap_content"
- android:layout_height= "wrap_content"
- android:textSize = "20dp">
- </EditText>
- <Button
- android:id="@+id/button5"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="delete"
- android:text="Delete" />
- </LinearLayout>
- Message.java File
- package com.example.database;
- import android.widget.Toast;
- import android.content.Context;
- public class Message {
- public static void message(Context context,String message){
- Toast.makeText(context,message,Toast.LENGTH_LONG).show();
- }}
- DCSDatabaseAdapter.java File
- package com.example.database;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.SQLException;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.content.Context;
- public class DCSDatabaseAdapter {
- private Context context;
- private SQLiteDatabase db;
- private DCSHelper helper;
- public DCSDatabaseAdapter(Context c){
- context = c;
- }
- //Open Connection Database.
- public DCSDatabaseAdapter open() throws SQLException{
- helper = new DCSHelper(context);
- db = helper.getWritableDatabase();
- return this;
- }
- //Insert Data
- public long insertData(String name,String password){
- //SQLiteDatabase db = helper.getWritableDatabase(); //pen
- ContentValues contentValues = new ContentValues();
- contentValues.put(DCSHelper.NAME,name);
- contentValues.put(DCSHelper.ROLL,password);
- long id = db.insert(DCSHelper.TABLE_NAME,null,contentValues);
- //Message.message(context,""+id);
- return id;
- }
- //GetAll the Records
- public String getAllData(){
- //SQLiteDatabase db = helper.getWritableDatabase();
- String[] columns = {DCSHelper.NAME,DCSHelper.ROLL};
- Cursor cursor = db.query(DCSHelper.TABLE_NAME,columns,null,null,null,null,null,null);
- StringBuffer buffer = new StringBuffer();
- while(cursor.moveToNext()){
- String name = cursor.getString(0);
- String password = cursor.getString(1);
- buffer.append(name+ " " +password+ "\n");
- }
- return buffer.toString();
- }
- //Get Particular Data
- public String getData(String name){
- //SQLiteDatabase db = helper.getWritableDatabase();
- String[] columns = {DCSHelper.NAME,DCSHelper.ROLL};
- String uname = name;
- Cursor cursor = db.query(DCSHelper.TABLE_NAME,columns,null,null,null,null,null,null);
- StringBuffer buffer = new StringBuffer();
- while(cursor.moveToNext()){
- String naam = cursor.getString(0);
- String pass = cursor.getString(1);
- if(naam == uname){
- buffer.append(naam+ " " +pass);
- break;
- }
- }
- return buffer.toString();
- }
- //Update Names
- public int updateName(String oldName,String newName){
- //SQLiteDatabase db = helper.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(DCSHelper.NAME,newName);
- String[] whereArgs = {oldName};
- int count = db.update(DCSHelper.TABLE_NAME,contentValues,DCSHelper.NAME+"=?",whereArgs);
- return count;
- }
- public int deleteRow(String name){
- //SQLiteDatabase db = helper.getWritableDatabase();
- String[] whereArgs = {name};
- int count = db.delete(DCSHelper.TABLE_NAME,DCSHelper.NAME+"=?",whereArgs);
- return count;
- }
- static class DCSHelper extends SQLiteOpenHelper{
- private static final String DATABASE_NAME = "SYCS";
- private static final String TABLE_NAME = "STUDENTTABLE";
- private static final int DATABASE_VERSION = 1;
- private static final String UID = "_id";
- private static final String NAME = "Name";
- private static final String ROLL = "ROllno";
- private static final String CREATE_TABLE = "CREATE TABLE " +TABLE_NAME + "("
- + NAME + " VARCHAR(255)," +ROLL+ " INT(255));";
- private static final String DROP_TABLE = "DROP TABLE IF EXISTS " +TABLE_NAME+";";
- private Context context;
- public DCSHelper(Context context){
- super(context,DATABASE_NAME,null,DATABASE_VERSION);
- this.context = context;
- Message.message(context,"DCSHelper Called..");
- }
- @Override
- public void onCreate(SQLiteDatabase db){
- try{
- db.execSQL(CREATE_TABLE);
- Message.message(context,"OnCreate() Called..");
- }catch(SQLException e){
- Message.message(context,""+e);
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
- try{
- db.execSQL(DROP_TABLE);
- onCreate(db);
- }catch(SQLException e){
- Message.message(context,""+e);
- }
- }
- }
- }
- MainActivity.java File
- package com.example.database;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import androidx.appcompat.app.AppCompatActivity;
- import android.widget.EditText;
- import android.view.View;
- import android.os.Bundle;
- public class MainActivity extends AppCompatActivity {
- EditText userName,password,name,newName,delName;
- DCSDatabaseAdapter dcsHelper;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- userName = (EditText) findViewById(R.id.userNameValue);
- password = (EditText) findViewById(R.id.passwordValue);
- name = (EditText) findViewById(R.id.name);
- newName = (EditText) findViewById(R.id.newName);
- delName = (EditText) findViewById(R.id.delName);
- dcsHelper = new DCSDatabaseAdapter(this);
- dcsHelper.open();
- }
- public void addUser(View view){
- String user = userName.getText().toString();
- String pass = password.getText().toString();
- long id = dcsHelper.insertData(user,pass);
- if(id < 0){
- Message.message(this,"Unsuccessful..");
- }
- else{
- Message.message(this,"Successfull...");
- }
- }
- public void viewDetails(View view){
- String data = dcsHelper.getAllData();
- Message.message(this,data);
- }
- /*
- public void getDetails(View view){
- String s1 = name.getText().toString();
- String data = dcsHelper.getData(s1);
- Message.message(this,data);
- }*/
- public void update(View view){
- String naam = name.getText().toString();
- String nnaam = newName.getText().toString();
- dcsHelper.updateName(naam,nnaam);
- }
- public void delete(View view){
- String naam = delName.getText().toString();
- int count = dcsHelper.deleteRow(naam);
- Message.message(this,"Deleted "+naam);
- }
- }
- //Practical (Notification & Alert)
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/button_show_notification"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="123dp"
- android:layout_marginLeft="123dp"
- android:layout_marginTop="341dp"
- android:layout_marginEnd="124dp"
- android:layout_marginRight="124dp"
- android:layout_marginBottom="342dp"
- android:text="Notification Demo"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java File
- package com.example.notificationdemo;
- import androidx.annotation.RequiresApi;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.NotificationCompat;
- import androidx.core.app.NotificationManagerCompat;
- import android.app.AlarmManager;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Build;
- import android.os.Bundle;
- public class MainActivity extends AppCompatActivity {
- @RequiresApi(api = Build.VERSION_CODES.O)
- @Override
- protected void onCreate(Bundle xyz) {
- super.onCreate(xyz);
- setContentView(R.layout.activity_main);
- Intent i = new Intent(this,MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 234324243, i, PendingIntent.FLAG_UPDATE_CURRENT);
- AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
- am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
- //Toast.makeText(this,"huehue",Toast.LENGTH_LONG).show();
- NotificationChannel channel = new NotificationChannel("My Notification","My Notification", NotificationManager.IMPORTANCE_DEFAULT);
- NotificationManager manager = getSystemService(NotificationManager.class);
- manager.createNotificationChannel(channel);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "My Notification");
- builder.setContentTitle("Alert!!");
- builder.setContentText("Startup notification..");
- builder.setSmallIcon(R.drawable.ic_launcher_background);
- builder.setAutoCancel(false);
- NotificationManagerCompat compatManager = NotificationManagerCompat.from(this);
- compatManager.notify(1, builder.build());
- }
- }
- //Practical (Two Activity) Explicit Intent Pucha tho Two activity ya Three Activity vala code kr dena
- activity_main.xml file
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation = "vertical"
- tools:context=".MainActivity">
- <EditText
- android:hint="Enter Your Name"
- android:id="@+id/editText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginTop="100dp">
- </EditText>
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="Submit to Second Activity"
- android:layout_margin="30dp">
- </Button>
- </LinearLayout>
- activity_second.xml file
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation = "vertical"
- tools:context=".SecondActivity">
- <TextView
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:id = "@+id/textView1"
- android:layout_gravity = "center"
- android:textSize = "30sp"
- android:layout_marginTop = "250dp">
- </TextView>
- </LinearLayout>
- AndroidManifest.xml file
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.twoactivity">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="First Activity (1108 Arpit)"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".SecondActivity"
- android:label="Second Activity (1108 Arpit)"
- android:parentActivityName=".MainActivity">
- <meta-data
- android:name = "android.support.PARENT_ACTIVITY"
- android:value = ".MainActivity"></meta-data>
- </activity>
- </application>
- </manifest>
- MainActivity.java file
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.widget.EditText;
- import android.widget.Button;
- import android.widget.TextView;
- import android.view.View;
- import android.os.Bundle;
- public class MainActivity extends AppCompatActivity {
- private EditText ed1;
- private TextView tv1;
- private Button b1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ed1 = (EditText) findViewById(R.id.editText1);
- b1 = (Button) findViewById(R.id.button1);
- tv1 = (TextView) findViewById(R.id.textView1);
- b1.setOnClickListener(new View.OnClickListener()
- {
- public void onClick(View view)
- {
- Intent i = new Intent(getApplicationContext(),SecondActivity.class);
- String s;
- s = ed1.getText().toString();
- i.putExtra("Name",s);
- startActivity(i);
- }
- });
- }
- }
- SecondActivity.java file
- import androidx.appcompat.app.AppCompatActivity;
- import android.widget.TextView;
- import android.os.Bundle;
- public class SecondActivity extends AppCompatActivity {
- private TextView tv1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- tv1 = (TextView) findViewById(R.id.textView1);
- Bundle b = getIntent().getExtras();
- String result = b.getString("Name");
- tv1.setText("Nice meeting you "+result);
- }
- }
- //Practical (Create an Android app to display list of friends’ name with their photo.)
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:orientation="vertical"
- tools:context=".MainActivity"
- tools:ignore="ExtraText">
- <ListView
- android:id="@+id/simpleListView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:divider="@color/material_blue_grey_800"
- android:dividerHeight="1dp"
- android:footerDividersEnabled="false" />
- </LinearLayout>
- listview.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:id="@+id/icon"
- android:layout_width="120dp"
- android:layout_height="123dp"
- android:src="@drawable/ic_launcher_foreground" />
- <TextView
- android:id="@+id/textView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:padding="10dp" />
- </LinearLayout>
- CustomAdapter.java File
- package com.example.listview;
- import android.content.Context;
- import android.media.Image;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.util.zip.Inflater;
- public class CustomAdapter extends BaseAdapter {
- Context context;
- String countryList[];
- int flags[];
- LayoutInflater inflater;
- public CustomAdapter(Context applicationContext, String[] countryList, int[] flags) {
- this.context = context;
- this.countryList = countryList;
- this.flags = flags;
- inflater = (LayoutInflater.from(applicationContext));
- }
- @Override
- public int getCount() {
- return countryList.length;
- }
- @Override
- public Object getItem(int i) {
- return null;
- }
- @Override
- public long getItemId(int i) {
- return 0;
- }
- @Override
- public View getView(int i,View view,ViewGroup viewGroup) {
- view = inflater.inflate(R.layout.listview,null);
- TextView country = (TextView) view.findViewById(R.id.textView);
- ImageView icon = (ImageView) view.findViewById(R.id.icon);
- country.setText(countryList[i]);
- icon.setImageResource(flags[i]);
- return view;
- }}
- MainActivity.java File
- package com.example.listview;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class MainActivity extends Activity {
- // Array of strings...
- ListView simpleList;
- String countryList[] = {"Rakesh", "Mukesh", "Kamlesh"};
- int flags[] = {R.drawable.rakesh,R.drawable.mukesh,R.drawable.kamlesh};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- simpleList = (ListView) findViewById(R.id.simpleListView);
- CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(),countryList,flags);
- // ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.listview, R.id.textView, countryList);
- simpleList.setAdapter(customAdapter);
- }
- }
- //Practical (Popup Menu)
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="PopupMenu"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.472" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- popup_menu.xml File (create in menu folder in res and create this file)
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/upload"
- android:title="Upload"/>
- <item
- android:id="@+id/copy"
- android:title="Copy"/>
- <item
- android:id="@+id/print"
- android:title="Print"/>
- <item
- android:id="@+id/paste"
- android:title="Paste"/>
- </menu>
- MainActivity.java File
- package com.example.popup;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.PopupMenu;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final Button btn = (Button)findViewById(R.id.button);
- btn.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v){
- // Creating Instance of Popup menu
- PopupMenu popup = new PopupMenu(MainActivity.this,btn);
- //Inflating the Popup using xml file
- popup.getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu());
- //registering popup with OnMenuItemClickListener
- popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
- public boolean onMenuItemClick(MenuItem item){
- Toast.makeText(MainActivity.this,"Selected Item is: "+item.getTitle(),Toast.LENGTH_SHORT).show();
- switch(item.getItemId()){
- case R.id.upload:
- return true;
- case R.id.copy:
- return true;
- case R.id.print:
- return true;
- case R.id.paste:
- return true;
- default:
- return false;
- }
- }
- });
- popup.show();
- }
- });
- }
- }
- //Practical (Develop a form to accept your name, mobile no. When click on submit button display values in toast as well as display values in second activity. )
- activity_main.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Name:"
- android:textSize="16sp"
- android:textStyle="bold"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.131"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.169" />
- <EditText
- android:id="@+id/editTextTextPersonName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="64dp"
- android:ems="10"
- android:inputType="textPersonName"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.151" />
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Mobile No:"
- android:textSize="16sp"
- android:textStyle="bold"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.144"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.282" />
- <EditText
- android:id="@+id/editTextTextPersonName2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="phone"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.781"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.268" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Submit"
- android:onClick="show"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.407" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- activity_second.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".SecondActivity">
- <TextView
- android:id="@+id/textView2"
- android:layout_width="191dp"
- android:layout_height="136dp"
- android:text=""
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.381" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java File
- package com.example.formtwoactivity;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- TextView tv1,tv2;
- EditText et1,et2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv1 = (TextView)findViewById(R.id.tv);
- et1 = (EditText)findViewById(R.id.editTextTextPersonName);
- tv2 = (TextView)findViewById(R.id.textView);
- et2 = (EditText)findViewById(R.id.editTextTextPersonName2);
- }
- public void show(View view){
- String name,mobile;
- name = et1.getText().toString();
- mobile = et2.getText().toString();
- Toast.makeText(this,"Employee Information: \n"+name+"\n"+mobile,Toast.LENGTH_SHORT).show();
- Intent i = new Intent(getApplicationContext(),SecondActivity.class);
- i.putExtra("me",name);
- i.putExtra("you",mobile);
- startActivity(i);
- }
- }
- SecondActivity.java File
- package com.example.formtwoactivity;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class SecondActivity extends AppCompatActivity {
- TextView tv1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- Bundle extra = getIntent().getExtras();
- String un = extra.getString("me");
- String m = extra.getString("you");
- String print = "Employee Details: \n"+un+"\n"+m;
- tv1 = (TextView)findViewById(R.id.textView2);
- tv1.setText(print);
- }
- }
- AndroidManifest.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.formtwoactivity">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".SecondActivity"
- android:label="Second Activity"
- android:parentActivityName=".MainActivity">
- <meta-data
- android:name = "android.support.PARENT_ACTIVITY"
- android:value = ".MainActivity"></meta-data>
- </activity>
- </application>
- </manifest>
- //Edit Text
- import android.graphics.Color;
- import android.graphics.Typeface;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- EditText et;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- et = (EditText)findViewById(R.id.editTextTextPersonName);
- }
- public void show(View view){
- /*String user;
- user = et.getText().toString();
- Toast toast = new Toast(getApplicationContext());
- toast.setGravity(Gravity.CENTER,0,0);
- EditText et1 = new EditText(MainActivity.this);
- et1.setTextColor(Color.RED);
- Typeface tf = Typeface.create("default",Typeface.BOLD);
- et1.setTypeface(tf);
- et1.setText(user);
- toast.setView(et1);
- toast.show();*/
- Toast.makeText(this,"Image button is clicked",Toast.LENGTH_SHORT).show();
- }
- }
- //Practical (Implicit Activity)
- AmndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.implicitintentapplication">
- <uses-permission android:name="android.permission.INTERNET" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/LaunchMap"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:layout_alignParentRight="true"
- android:layout_marginTop="100dp"
- android:layout_marginBottom="4dp"
- android:background="#ffee00"
- android:onClick="process"
- android:text="Launch Map" />
- <Button
- android:id="@+id/LaunchMarket"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/LaunchMap"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_marginTop="4dp"
- android:layout_marginBottom="4dp"
- android:background="#ffee00"
- android:onClick="process"
- android:text="Launch Market" />
- <Button
- android:id="@+id/SendEmail"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/LaunchMarket"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_marginTop="4dp"
- android:layout_marginBottom="4dp"
- android:background="#ffee00"
- android:onClick="process"
- android:text="Send Email" />
- </RelativeLayout>
- MainActivity.java
- package com.example.implicitintentapplication;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- public class MainActivity extends AppCompatActivity {
- //geo:19.2060, 72.8746
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void process(View view) {
- Intent intent = null, chooser = null;
- if (view.getId() == R.id.LaunchMap) {
- intent = new Intent(android.content.Intent.ACTION_VIEW);
- intent.setData(Uri.parse("geo:19.2060, 72.8746"));
- chooser = Intent.createChooser(intent, "Launch Map:");
- startActivity(chooser);
- }
- if (view.getId() == R.id.LaunchMarket) {
- intent = new Intent(android.content.Intent.ACTION_VIEW);
- intent.setData(Uri.parse("market://details?id=dolphin.developers.com"));
- chooser = Intent.createChooser(intent, "Launch Market:");
- startActivity(chooser);
- }
- if (view.getId() == R.id.SendEmail) {
- intent = new Intent(android.content.Intent.ACTION_SEND);
- intent.setData(Uri.parse("mailto:"));
- String[] to = {"tcsc101@gmail.com", "gishawork@gmail.com"};
- intent.putExtra(Intent.EXTRA_EMAIL, to);
- intent.putExtra(Intent.EXTRA_SUBJECT, "Greetings");
- intent.putExtra(Intent.EXTRA_TEXT, "Hi! How are you?");
- intent.setType("message/rfc822");
- chooser = Intent.createChooser(intent, "Send Email:");
- startActivity(chooser);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement