[go: up one dir, main page]

0% found this document useful (0 votes)
15 views29 pages

Android Lab Submission

The document outlines a series of Android lab assignments completed by Tanishq Sharma, a 3rd-year BTech student. It includes code snippets for various Android components such as XML layouts, Java files for activities, intents, and fragments, demonstrating the use of EditText, TextView, and Buttons. The labs cover topics like layout design, intents, and fragment management in Android development.

Uploaded by

Jay Nahata
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)
15 views29 pages

Android Lab Submission

The document outlines a series of Android lab assignments completed by Tanishq Sharma, a 3rd-year BTech student. It includes code snippets for various Android components such as XML layouts, Java files for activities, intents, and fragments, demonstrating the use of EditText, TextView, and Buttons. The labs cover topics like layout design, intents, and fragment management in Android development.

Uploaded by

Jay Nahata
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/ 29

Tanishq Sharma

0801CS221145
3rd yr Btech
Section - B

Android Lab Assignment

Lab-0 : Android Studios Installation


Lab 1 : Text view and Edit text

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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical"
>
<EditText
android:id="@+id/editText"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#D1DBDC"
android:textSize="25dp"
android:hint="Enter text here"/>
<Button
android:id="@+id/Enter"
android:text="Enter"
android:layout_margin="50dp"
android:layout_width="150dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#D1DBDC"
android:textSize="25dp"
/>
</LinearLayout>
Java File
package com.example.lab1;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

Button enter;
TextView textView;
EditText editText;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
enter = findViewById(R.id.Enter);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);

enter.setOnClickListener(v->{
String text=editText.getText().toString();
textView.setText(text);

});
}
}
Output
Lab 2 : Layout(Linear, Relative and Constraints)

1. Linear Layout XML


<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center"
>

<TextView
android:id="@+id/textView"
android:text="0"
android:textSize="100dp"
android:gravity="center"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#C8BFBF"
android:layout_marginBottom="100dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/Increment"
android:background="@drawable/baseline_arrow_upward_24"
android:layout_width="100dp"
android:layout_height="100dp"/>
<Button
android:id="@+id/reset"
android:background="@drawable/baseline_refresh_24"
android:layout_width="100dp"
android:layout_height="100dp"/>
<Button
android:id="@+id/Decrement"
android:background="@drawable/baseline_arrow_downward_24"
android:layout_width="100dp"
android:layout_height="100dp"/>

</LinearLayout>
</LinearLayout>

Output
2. Relative Layout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
>

<TextView
android:id="@+id/textView"
android:text="0"
android:textSize="100dp"
android:gravity="center"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#C8BFBF"
android:layout_marginLeft="70dp"
android:layout_marginBottom="100dp"
/>
<Button
android:id="@+id/Increment"
android:background="@drawable/baseline_arrow_upward_24"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@id/textView"
/>
<Button
android:id="@+id/reset"
android:background="@drawable/baseline_refresh_24"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@id/textView"
android:layout_marginLeft="100dp"
/>
<Button
android:id="@+id/Decrement"
android:background="@drawable/baseline_arrow_downward_24"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@id/textView"
android:layout_toEndOf="@id/reset"

/>

</RelativeLayout>

Output:
3. Grid Layout XML
<?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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- TextView for displaying the counter -->


<TextView
android:id="@+id/textView"
android:text="0"
android:textSize="100sp"
android:gravity="center"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#C8BFBF"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/buttonsRow"
android:layout_marginBottom="50dp"/>
<!-- Buttons Container (Row for Increment, Reset, and Decrement) -->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/buttonsRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<Button
android:id="@+id/Increment"
android:background="@drawable/baseline_arrow_upward_24"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/reset"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="10dp"/>

<Button
android:id="@+id/reset"
android:background="@drawable/baseline_refresh_24"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toEndOf="@id/Increment"
app:layout_constraintEnd_toStartOf="@id/Decrement"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="10dp"/>

<Button
android:id="@+id/Decrement"
android:background="@drawable/baseline_arrow_downward_24"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toEndOf="@id/reset"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Output:
Lab 3 : Intents(with Toasts)
Explicit Intent
Java code:
Main_Activity
public class MainActivity extends AppCompatActivity {
Button increment, decrement, reset;
int count=0;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
text=findViewById(R.id.text);
Intent intent1=new Intent(this,Increment.class);
Intent intent2=new Intent(this,Decrement.class);
increment=findViewById(R.id.increment);
decrement=findViewById(R.id.decrement);
reset=findViewById(R.id.reset);
increment.setOnClickListener(v -> {
count++;
intent1.putExtra("count",count);
Toast toast=Toast.makeText(MainActivity.this,"Increment",Toast.LENGTH_SHORT);
toast.show();
startActivity(intent1);
});
decrement.setOnClickListener(v -> {
count--;
intent2.putExtra("count",count);
Toast toast=Toast.makeText(MainActivity.this,"Decrement",Toast.LENGTH_SHORT);
toast.show();
startActivity(intent2);

});
reset.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
count=0;
Toast toast=Toast.makeText(MainActivity.this,"Reset",Toast.LENGTH_SHORT);
toast.show();
}
}
);
}
}

Increment Intent
public class Increment extends AppCompatActivity {
TextView text;
int count=0;
Button increment, decrement, reset;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_increment);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
increment=findViewById(R.id.increment);
decrement=findViewById(R.id.decrement);
reset=findViewById(R.id.reset);
text=findViewById(R.id.text);

Intent intent=getIntent();
count=intent.getIntExtra("count",0);

text.setText(String.valueOf(count));
Intent goHome=new Intent(this,MainActivity.class);
Intent goDecrement=new Intent(this,Decrement.class);

reset.setOnClickListener(v->{
startActivity(goHome);
text.setText("0");
});

increment.setOnClickListener(v->{
count++;
Toast toast=Toast.makeText(this,"Increment",Toast.LENGTH_SHORT);
toast.show();
text.setText(String.valueOf(count));
});

decrement.setOnClickListener(v->{
count--;
Toast toast=Toast.makeText(this,"Decrement",Toast.LENGTH_SHORT);
toast.show();
goDecrement.putExtra("count",count);
startActivity(goDecrement);
});

}
}

Decrement Intent
public class Decrement extends AppCompatActivity {
TextView text;
Button increment, decrement, reset;
int count=0;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_decrement);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
text=findViewById(R.id.text);
increment=findViewById(R.id.increment);
decrement=findViewById(R.id.decrement);
reset=findViewById(R.id.reset);
text=findViewById(R.id.text);

Intent intent=getIntent();
count=intent.getIntExtra("count",0);
text.setText(String.valueOf(count));

Intent goHome=new Intent(this,MainActivity.class);


Intent goIncrement=new Intent(this,Increment.class);

reset.setOnClickListener(v->{
startActivity(goHome);
text.setText("0");
});

decrement.setOnClickListener(v->{
count--;
Toast toast=Toast.makeText(this,"Decrement",Toast.LENGTH_SHORT);
toast.show();
text.setText(String.valueOf(count));
});

increment.setOnClickListener(v->{
count++;
Toast toast=Toast.makeText(this,"Increment",Toast.LENGTH_SHORT);
toast.show();
goIncrement.putExtra("count",count);
startActivity(goIncrement);
});

}
}
Output
Implicit Intents
Java Filw
import java.net.URL;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

@SuppressLint({"MissingInflatedId", "LocalSuppress"}) Button btn = findViewById(R.id.youtube);

btn.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com"));
startActivity(intent);
});}}
Output:
Lab 4 : Fragment
Java file
Main_Activity.java
public class MainActivity extends AppCompatActivity {

int counter;
Button increment,decrement,reset;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

replaceFragment(new resetFragment());
increment=findViewById(R.id.incrementButton);
decrement=findViewById(R.id.decrementButton);
reset=findViewById(R.id.resetButton);

increment.setOnClickListener(v -> {
counter++;
replaceFragment(new Incrementfragment());
});
decrement.setOnClickListener(v -> {
counter--;
replaceFragment(new decFragment());
});
reset.setOnClickListener(v -> {
counter=0;
replaceFragment(new resetFragment());
});

}
public void replaceFragment(Fragment fragment){
Bundle bundle=new Bundle();
bundle.putInt("counter",counter);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentView,fragment).commit();
}
}

resetFragment.java
package com.example.fragmentlab;
public class resetFragment extends Fragment {
public resetFragment(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_reset, container, false);
}
public void onViewCreated(View view,Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
text = view.findViewById(R.id.textView);
int counter = 0;
text.setText(String.valueOf(counter));
}
}

Incrementfragment.java
public class Incrementfragment extends Fragment {
public Incrementfragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_incrementfragment, container, false);
}
public void onViewCreated(View view,Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
text=view.findViewById(R.id.textView);
int counter=getArguments().getInt("counter",0);
text.setText(String.valueOf(counter));
}

decFragment.java
public class decFragment extends Fragment {
public decFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_dec, container, false);


}
public void onViewCreated(View view,Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
text = view.findViewById(R.id.textView);
int counter = getArguments().getInt("counter", 0);
text.setText(String.valueOf(counter));
}
}
Output:
Lab 5 : Shared Preference And Content Provider

Shared Preference
Java File
Main_Activity.java
package com.example.lab5;
public class MainActivity extends AppCompatActivity {
Button increment, reset, decrement;
TextView textView;
int value;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
increment = findViewById(R.id.increment);
reset = findViewById(R.id.reset);
decrement = findViewById(R.id.decrement);
textView = findViewById(R.id.text);

increment.setOnClickListener(v -> {
int value = Integer.parseInt(textView.getText().toString());
value++;
textView.setText(String.valueOf(value));
});

decrement.setOnClickListener(v->{
int value=Integer.parseInt(textView.getText().toString());
value--;
textView.setText(String.valueOf(value));
});
reset.setOnClickListener(v->{
value=0;
textView.setText(String.valueOf(value));
});
}

@Override
protected void onResume(){
super.onResume();
SharedPreferences sharedPreferences = getSharedPreferences("UserPreferences",
MODE_PRIVATE);
String value = sharedPreferences.getString("value", "0");
textView.setText(value);
}
@Override
protected void onPause(){
super.onPause();
value=Integer.parseInt(textView.getText().toString());

SharedPreferences sharedPreferences = getSharedPreferences("UserPreferences",


MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("value", String.valueOf(value));
editor.apply();
}
}
Output:
Content Provider
Java File
ContentProvider App
Java File
Main_activity
public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return true;
}
public void onClickAddDetails(View view) {

// class to add values in the database


ContentValues values = new ContentValues();

// fetching text from user


values.put(MyContentProvider.name, ((EditText) findViewById(R.id.editText)).getText().toString());

// inserting into database through content URI


getContentResolver().insert(MyContentProvider.CONTENT_URI, values);

// displaying a toast message


Toast.makeText(getBaseContext(), "New Record Inserted", Toast.LENGTH_LONG).show();
}

@SuppressLint("Range")
public void onClickShowDetails(View view) {
// inserting complete table details in this text field
TextView resultView= (TextView) findViewById(R.id.loadText);
// creating a cursor object of the
// content URI
Cursor cursor = getContentResolver().query(Uri.parse("content://com.demo.user.provider/users"),
null, null, null, null);

// iteration of the cursor


// to print whole table
if(cursor.moveToFirst()) {
StringBuilder strBuild=new StringBuilder();
while (!cursor.isAfterLast()) {
strBuild.append("\n"+cursor.getString(cursor.getColumnIndex("id"))+ "-"+
cursor.getString(cursor.getColumnIndex("name")));
cursor.moveToNext();
}
resultView.setText(strBuild);
}
else {
resultView.setText("No Records Found");
}
}
}
My_contentProvider
public class MyContentProvider extends ContentProvider {
public MyContentProvider() {
}
static final String PROVIDER_NAME = "com.demo.user.provider";
static final String URL = "content://" + PROVIDER_NAME + "/users";
static final Uri CONTENT_URI = Uri.parse(URL);

static final String id = "id";


static final String name = "name";
static final int uriCode = 1;
static final UriMatcher uriMatcher;
private static HashMap<String, String> values;

static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "users", uriCode);
uriMatcher.addURI(PROVIDER_NAME, "users/*", uriCode);
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case uriCode:
return "vnd.android.cursor.dir/users";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
if (db != null) {
return true;
}
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(TABLE_NAME);
switch (uriMatcher.match(uri)) {
case uriCode:
qb.setProjectionMap(values);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (sortOrder == null || sortOrder == "") {
sortOrder = id;
}
Cursor c = qb.query(db, projection, selection, selectionArgs, null,
null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = db.insert(TABLE_NAME, "", values);
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLiteException("Failed to add a record into " + uri);
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case uriCode:
count = db.update(TABLE_NAME, values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case uriCode:
count = db.delete(TABLE_NAME, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
private SQLiteDatabase db;
static final String DATABASE_NAME = "UserDB";
static final String TABLE_NAME = "Users";
static final int DATABASE_VERSION = 1;
static final String CREATE_DB_TABLE = " CREATE TABLE " + TABLE_NAME
+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ " name TEXT NOT NULL);";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); }
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_TABLE)}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}}}
Output:
contentReceiver
Java File
package com.example.contentreceiver;
public class MainActivity extends AppCompatActivity {
Uri uri=Uri.parse("content://com.demo.user.provider/users");
Button load;
TextView loadText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

}
@SuppressLint("Range")
public void onClickShowDetails(View view) {
TextView resultView= (TextView) findViewById(R.id.loadText);

Cursor cursor = getContentResolver().query(Uri.parse("content://com.demo.user.provider/users"),


null, null, null, null);
if(cursor.moveToFirst()) {
StringBuilder strBuild=new StringBuilder();
while (!cursor.isAfterLast()) {
strBuild.append("\n"+cursor.getString(cursor.getColumnIndex("id"))+ "-"+
cursor.getString(cursor.getColumnIndex("name")));
cursor.moveToNext();
}
resultView.setText(strBuild);
}
else {
resultView.setText("No Records Found");
}
}
}
Output:

You might also like