0% found this document useful (0 votes)
42 views6 pages

Android App Development Examples

The document contains code snippets from 7 Android apps covering topics like broadcasts, playing videos, activity lifecycle, placing orders, listviews, and fragments. The Broadcast app code handles receiving shared images. The Videoapp code plays a video file. The Activitylifecycle code demonstrates the activity lifecycle methods. The Orderapp code handles placing orders and passing data between activities. The Listview code populates and handles clicks on a grocery list. And the Fragment code replaces fragments on button clicks.

Uploaded by

M.Adil Mushtaq
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)
42 views6 pages

Android App Development Examples

The document contains code snippets from 7 Android apps covering topics like broadcasts, playing videos, activity lifecycle, placing orders, listviews, and fragments. The Broadcast app code handles receiving shared images. The Videoapp code plays a video file. The Activitylifecycle code demonstrates the activity lifecycle methods. The Orderapp code handles placing orders and passing data between activities. The Listview code populates and handles clicks on a grocery list. And the Fragment code replaces fragments on button clicks.

Uploaded by

M.Adil Mushtaq
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/ 6

Broadcast

Main
package com.example.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView=findViewById(R.id.imageView);
Intent intent=getIntent();
String action=intent.getAction();
String type=intent.getType();
if(Intent.ACTION_SEND.equals(action)&& type!=null){

imageView.setImageURI(intent.getParcelableExtra(intent.EXTRA_STREAM));
}
}
}

AndroidManifest.xml
<action android:name="android.intent.action.SEND"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<data android:mimeType="image/*"></data>

Videoapp
Main
package com.example.videoapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView=findViewById(R.id.videoView);

videoView.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.v1);
videoView.start();
}
}

Activitylifecycle
Main
package com.example.lifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Second.class);
startActivity(intent);

}
});
Toast.makeText(this, "OnCreate", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "OnStart", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "OnResume", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause(){
super.onPause();
Toast.makeText(this, "OnPause", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop(){
super.onStop();
Toast.makeText(this, "OnStop", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy(){
super.onDestroy();
Toast.makeText(this, "OnDestroy", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart(){
super.onRestart();
Toast.makeText(this, "OnRestart", Toast.LENGTH_SHORT).show();
}
}

Orderapp
Mian
package com.example.orderapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


public static final String msg="com.myactivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void placeorder(View view){
Intent intent=new Intent(this, order.class);
EditText text1=findViewById(R.id.editTextTextPersonName);
EditText text2=findViewById(R.id.editTextTextPersonName2);
String message=text1.getText().toString()+"
"+text2.getText().toString();
//intent.putExtra(msg,message);
//startActivity(intent);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

}
}

order
package com.example.orderapp;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class order extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.msg);
TextView textView=findViewById(R.id.textView3);
textView.setText(message);

}
}

Listview
Main
package com.example.listviewdemof20;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=findViewById(R.id.mylist);
ArrayList<String> grocery=new ArrayList<>();
grocery.add("Bhindi");
grocery.add("karailay");
grocery.add("mango");

ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1,grocery);
listView.setAdapter(arrayAdapter);

listView.setOnItemClickListener((parent, view, position, id) -> {


String text=((TextView)view).getText().toString();
Toast.makeText(MainActivity.this,text,
Toast.LENGTH_SHORT).show();
});
}
}

Fragment
main
package com.example.fragmentdemof20;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1=findViewById(R.id.button);
Button btn2=findViewById(R.id.button2);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirstFragment firstFragment=new FirstFragment();
FragmentTransaction
transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainerView,firstFragment);
transaction.commit();
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SecondFragment secondFragment=new SecondFragment();
FragmentTransaction
transaction=getSupportFragmentManager().beginTransaction();

transaction.replace(R.id.fragmentContainerView,secondFragment);
transaction.commit();
}
});
}
}

You might also like