Android Programming
1. Create an android application to display internal storage data in vertical format by
using Custom Adapter.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
[Link]
(right click on layout then select Layout Resource File, name it as indiview and, root name as
LinearLayout and save it)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/iview"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Name"
android:textSize="18sp"/>
SVFGC CTA 1
Android Programming
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Size"/>
</LinearLayout>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"/>
</LinearLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{ ListView lview;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
lview = findViewById([Link]);
try {
openFileOutput("[Link]",
MODE_PRIVATE).close();
openFileOutput("[Link]",
MODE_PRIVATE).close();
openFileOutput("[Link]", MODE_PRIVATE).close();
}
catch(Exception e)
{
[Link]();
}
SVFGC CTA 2
Android Programming
File dir = getFilesDir();
File[] files = [Link]();
MyAdapter adapter = new MyAdapter(this, files);
[Link](adapter);
}
}
[Link](create new java class file, name it as MyAdapter and save it)
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MyAdapter extends BaseAdapter
{ Activity activity;
File[] files;
public MyAdapter(Activity activity, File[] files)
{ [Link] = activity;
[Link] = files;
}
@Override
public int getCount()
{ return [Link];
}
@Override
public Object getItem(int position)
{ return files[position];
}
@Override
public long getItemId(int position)
{ return position;
}
SVFGC CTA 3
Android Programming
@Override
public View getView(final int position,
View convertView,
ViewGroup parent) {
LayoutInflater inflater =
[Link]();
View v =
[Link](
[Link],
null);
ImageView iv =
[Link]([Link]);
TextView tv1 =
[Link]([Link].tv1);
TextView tv2 =
[Link]([Link].tv2);
Button b1 =
[Link]([Link].b1);
[Link](files[position].getName());
[Link](
files[position].length()
+ " bytes");
[Link](view -> {
files[position].delete();
File dir =
[Link]();
files = [Link]();
notifyDataSetChanged();
});
return v;
}
}
SVFGC CTA 4
Android Programming
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=
"[Link]
<application
android:allowBackup="true"
android:label="CustomAdapterApp"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action
android:name=
"[Link]"/>
<category
android:name=
"[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
Output:
Click on DELETE Button to delete the file, it will automatically delete file at run time
For example: we deleted [Link] file then it displays only first Two files.
SVFGC CTA 5
Android Programming
1. Create an android application to display WhatsApp videos in grid view by using
Custom Adapter.
Step1: create below path inside Device Explorer folder
/storage/emulated/0/Android/media/[Link]/WhatsApp/Media/WhatsApp Video/
View
Tool Windows Device Explorer create above path
Steo2: copy .mp4 vodep file inside WhatsApp Video
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
item_video.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="[Link]
android:id="@+id/imgVideo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="100dp"
android:scaleType="centerCrop"/>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
SVFGC CTA 6
Android Programming
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import
[Link];
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<File> videoList = new ArrayList<>();
private static final int REQUEST_CODE = 111;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
recyclerView = findViewById([Link]);
[Link](new GridLayoutManager(this, 2));
checkPermission();
}
private void checkPermission()
{ String permission;
if ([Link].SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permission = [Link].READ_MEDIA_VIDEO;
SVFGC CTA 7
Android Programming
} else {
permission = [Link].READ_EXTERNAL_STORAGE;
}
if ([Link](this, permission)
== PackageManager.PERMISSION_GRANTED) {
readFiles();
} else {
[Link](
this,
new String[]{permission},
REQUEST_CODE);
}
}
@Override
public void
onRequestPermissionsResult( int
requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
[Link]( req
uestCode,
permissions,
grantResults);
if (requestCode == REQUEST_CODE
&& [Link] > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SVFGC CTA 8
Android Programming
readFiles();
}
}
private void readFiles() {
String path =
"/storage/emulated/0/Android/media/[Link]/WhatsApp/Media/WhatsApp Video/";
File folder = new File(path);
if (![Link]())
return;
File[] files = [Link]();
if (files == null)
return;
[Link]();
for (File file : files) {
if ([Link]()
&& ([Link]().endsWith(".mp4")
|| [Link]().endsWith(".3gp"))) {
[Link](file);
}
}
[Link](
new VideoAdapter(this, videoList));
SVFGC CTA 9
Android Programming
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
<uses-permission android:name="[Link].READ_EXTERNAL_STORAGE" />
<uses-permission android:name="[Link].READ_MEDIA_VIDEO" />
<application
android:allowBackup="true"
android:label="WhatsApp Videos"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
[Link]
package [Link];
import [Link];
import [Link];
SVFGC CTA 10
Android Programming
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class VideoAdapter
extends [Link]<[Link]> {
Context context;
ArrayList<File> files;
public VideoAdapter(Context context,
ArrayList<File> files) {
[Link] = context;
[Link] = files;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(
@NonNull ViewGroup parent,
int viewType) {
View view = [Link](context)
SVFGC CTA 11
Android Programming
.inflate(
[Link].item_video,
parent,
false);
return new ViewHolder(view);
}
@Override
public void
onBindViewHolder( @NonNull
ViewHolder holder, int position)
{
File file = [Link](position);
Bitmap bitmap = [Link](
[Link](),
[Link].MINI_KIND);
[Link](bitmap);
}
@Override
public int getItemCount()
{ return [Link]();
}
static class ViewHolder extends [Link]
{ ImageView imgVideo;
public ViewHolder(@NonNull View itemView) {
super(itemView);
SVFGC CTA 12
Android Programming
imgVideo = [Link]([Link]);
}
}
}
Output:
SVFGC CTA 13
Android Programming
SVFGC CTA 14