0% found this document useful (0 votes)
19 views4 pages

mad_chhet

Uploaded by

RJ Sahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

mad_chhet

Uploaded by

RJ Sahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Types of Android Fragments Android Implicit Intent Example

Let's see the simple example of implicit intent that displays a web page.
1. Single Fragment: Display only one single view on the device screen.
This type of fragment is mostly used for mobile phones. mainActivity.java
2. List Fragment: This Fragment is used to display a list-view from which the
user can select the desired sub-activity. The menu drawer of apps like package example.javatpoint.com.implicitintent
Gmail is the best example of this kind of fragment.
3. Fragment Transaction: This kind of fragments supports the transition from import android.content.Intent;
one fragment to another at run time. Users can switch between multiple
fragments import android.net.Uri;
like switching tabs.
import android.support.v7.app.AppCompatActivity;
Fragment Lifecycle :
Each fragment has it’s own lifecycle but due to the connection with the Activity it
import android.os.Bundle;
belongs to, the fragment lifecycle is influenced by the activity’s lifecycle.
Methods of the Android Fragment import android.view.View;
Description import android.widget.Button;
Methods
import android.widget.EditText;
The very first method to be called when the fragment has been
public class MainActivity extends AppCompatActivity {
associated with the activity. This method executes only once during
onAttach() the lifetime of a fragment.
Button button;

EditText editText;
This method initializes the fragment by adding all the required
onCreate() attributes and components. @Override

protected void onCreate(Bundle savedInstanceState) {


System calls this method to create the user interface of the
fragment. The root of the fragment’s layout is returned as the View super.onCreate(savedInstanceState);
onCreateView() component by this method to draw the UI.
setContentView(R.layout.activity_main);

button = findViewById(R.id.button);
It indicates that the activity has been created in which the fragment
exists. View hierarchy of the fragment also instantiated before this editText = findViewById(R.id.editText);
onViewCreated() function call.
button.setOnClickListener(new View.OnClickListener() {

The system invokes this method to make the fragment visible on the @Override
onStart() user’s device.
public void onClick(View view) {

String url=editText.getText().toString();
onResume() This method is called to make the visible fragment interactive.
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));

It indicates that the user is leaving the fragment. System call this startActivity(intent);
onPause() method to commit the changes made to the fragment.
}

Method to terminate the functioning and visibility of fragment from });


onStop() the user’s screen.
}

}
System calls this method to clean up all kinds of resources as well as
onDestroyView() view hierarchy associated with the fragment.

It is called to perform the final clean up of fragment’s state and its


onDestroy() lifecycle.

The system executes this method to disassociate the fragment from


onDetach() its host activity.

;
Android telephony example switch (phoneType) {
. Now open the MainActivity.java file and write the following code there: case (TelephonyManager.PHONE_TYPE_CDMA):
package com.DataFlair.androidtelephony; PhoneType = "CDMA";
import android.app.Activity; break;
import android.content.Context; case (TelephonyManager.PHONE_TYPE_GSM):
import android.os.Bundle; PhoneType = "GSM";
import android.telephony.TelephonyManager; break;
import android.widget.TextView; case (TelephonyManager.PHONE_TYPE_NONE):
public class MainActivity extends Activity { PhoneType = "NONE";
TextView tv; break;
@Override }
protected void onCreate(Bundle savedInstanceState) { // true or false for roaming or not
super.onCreate(savedInstanceState); boolean checkRoaming = tele_man.isNetworkRoaming();
setContentView(R.layout.activity_main); String data = "Your Mobile Details are enlisted below: \n";
tv = findViewById(R.id.textView); data += "\n Network Country ISO is - " + nwcountryISO;
//instance of TelephonyManager data += "\n SIM Country ISO is - " + SIMCountryISO;
TelephonyManager tele_man = (TelephonyManager) data += "\n Network type is - " + PhoneType;
getSystemService(Context.TELEPHONY_SERVICE);
data += "\n Roaming on is - " + checkRoaming;
String nwcountryISO = tele_man.getNetworkCountryIso();
//Now we'll display the information
String SIMCountryISO = tele_man.getSimCountryIso();
tv.setText(data);}}
String PhoneType = ""; // it'll hold the type of phone i.e CDMA / GSM/ None
}}
int phoneType = tele_man.getPhoneType();
public void addRecord(String name, String
CRUD operations (Example. COURSE table (ID, Name, Duration, str += "\n" + list.get(i).id + " " + list.get(i).name + " " +
duration, String description) {
Description),
list.get(i).duration + " " + list.get(i).description;
SQLiteDatabase database =
perform ADD, UPDATE, DELETE and READ operations)
} this.getWritableDatabase();
MainActivity.java
txt.setText(str); ContentValues values = new ContentValues();
package com.subhdroid.lab_j15;
}); values.put("name", name);
//15. Write an android application using SQLite to create table
} values.put("duration", duration);
and perform

} values.put("description", description);
CRUD operations

CourseModel.java database.insert("course", null, values);


// (Example. COURSE table (ID, Name, Duration, Description),
perform Toast.makeText(context, "Added successfully",
package com.subhdroid.lab_j15;
ADD, UPDATE, Toast.LENGTH_SHORT).show();
public class CourseModel {
// DELETE and READ operations)
String name, duration, description; // database.close();
import androidx.appcompat.app.AppCompatActivity;
int id; }
import android.os.Bundle; public ArrayList<CourseModel> getRecords() {
public CourseModel() {
import android.view.View; SQLiteDatabase db = this.getReadableDatabase();
}
import android.widget.EditText; Cursor cursor = db.rawQuery("SELECT * FROM
}
course", null);
import android.widget.TextView;
MyDBClass.java
ArrayList<CourseModel> recordList = new
import java.util.ArrayList;
package com.subhdroid.lab_j15; ArrayList<>();
public class MainActivity extends AppCompatActivity {
import android.content.ContentValues; while (cursor.moveToNext()) {
@Override
import android.content.Context; CourseModel model = new CourseModel();
protected void onCreate(Bundle savedInstanceState) {
import android.database.Cursor; model.id = cursor.getInt(0);
super.onCreate(savedInstanceState);
import android.database.sqlite.SQLiteDatabase; model.name = cursor.getString(1);
setContentView(R.layout.activity_main);
import android.database.sqlite.SQLiteOpenHelper; model.duration = cursor.getString(2);
MyDBClass mydb = new MyDBClass(this);
import android.widget.Toast; model.description = cursor.getString(3);
EditText name, duration, description;
import androidx.annotation.Nullable; recordList.add(model);
TextView txt = findViewById(R.id.txtView);
import java.util.ArrayList; }
name = findViewById(R.id.name);
public class MyDBClass extends SQLiteOpenHelper { return recordList;
duration = findViewById(R.id.duration);
private static final String DBName = "LabDB"; }
description = findViewById(R.id.description);
private static final int DB_VERSION = 1; public void updateRecord(String duration, String
findViewById(R.id.addBtn).setOnClickListener(view -> name) {
Context context;
mydb.addRecord(name.getText().toString(), SQLiteDatabase db = this.getWritableDatabase();
public MyDBClass(@Nullable Context context) {
duration.getText().toString(),
ContentValues cv = new ContentValues();
super(context, DBName, null, DB_VERSION);
description.getText().toString()));
cv.put("duration", duration);
this.context = context;
findViewById(R.id.updateBtn).setOnClickListener(view ->
db.update("course", cv, "name=?", new
} String[]{name});
mydb.updateRecord(duration.getText().toString(),
@Override Toast.makeText(context, "Updated successfully",
name.getText().toString()));
public void onCreate(SQLiteDatabase sqLiteDatabase) { Toast.LENGTH_SHORT).show();
findViewById(R.id.deleteBtn).setOnClickListener(view ->
sqLiteDatabase.execSQL("CREATE TABLE course(id }
mydb.deleteRecord(name.getText().toString()));
INTEGER
findViewById(R.id.displayBtn).setOnClickListener(view -> { public void deleteRecord(String courseName) {
PRIMARY KEY AUTOINCREMENT,name " +
ArrayList<CourseModel> list = mydb.getRecords(); SQLiteDatabase database =
"TEXT,duration TEXT,description TEXT)"); this.getWritableDatabase();
String str = "ID Name Duration Description";
} database.delete("course", "name=?", new
for (int i = 0; i < list.size(); i++) { String[]{courseName});
@Override
str += "\n" + list.get(i).id + " " + list.get(i).name + " " + Toast.makeText(context, "Deleted successfully",
public void onUpgrade(SQLiteDatabase sqLiteDatabase,
list.get(i).duration + " " + list.get(i).description; int i, int i1) {} Toast.LENGTH_SHORT).show();}
dialogbox 1. });

package example.javatpoint.com.alertdialog; 2. //Creating dialog box

import android.content.DialogInterface; 3. AlertDialog alert = builder.create();

import android.support.v7.app.AppCompatActivity; 4. //Setting the title manually

import android.os.Bundle; 5. alert.setTitle("AlertDialogExample");

import android.view.View; 6. alert.show();

import android.widget.Button; 7. }

import android.app.AlertDialog; 8. });

import android.widget.Toast; 9. }

public class MainActivity extends AppCompatActivity { 10. }

0. Button closeButton;

1. AlertDialog.Builder builder;

2. @Override

3. protected void onCreate(Bundle savedInstanceState) {

4. super.onCreate(savedInstanceState);

5. setContentView(R.layout.activity_main);

6. closeButton = (Button) findViewById(R.id.button);

7. builder = new AlertDialog.Builder(this);

8. closeButton.setOnClickListener(new View.OnClickListener() {

9. @Override

0. public void onClick(View v) {

1. builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);

2. //Setting message manually and performing action on button click

3. builder.setMessage("Do you want to close this application ?")

4. .setCancelable(false)

5. .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

6. public void onClick(DialogInterface dialog, int id) {

7. finish();

8. Toast.makeText(getApplicationContext(),"you choose yes action for al

ertbox",

9. Toast.LENGTH_SHORT).show();

0. }

1. })

2. .setNegativeButton("No", new DialogInterface.OnClickListener() {

3. public void onClick(DialogInterface dialog, int id) {

4. // Action for 'NO' Button

5. dialog.cancel();

6. Toast.makeText(getApplicationContext(),"you choose no action for ale

rtbox",

7. Toast.LENGTH_SHORT).show();

8. }

9. });

You might also like