0% found this document useful (0 votes)
59 views69 pages

Mobile App Development Notes PDF

Uploaded by

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

Mobile App Development Notes PDF

Uploaded by

Barathkumar K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

GUI COMPONENTS, FONT AND COLORS


Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</LinearLayout>

[Link]:
package [Link].exno1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
int ch = 1;
float font = 30;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
final TextView t = (TextView) findViewById([Link]);
Button b1 = (Button) findViewById([Link].button1);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link](font);
font += 5;
if (font >= 50)
font = 30;
}
});
Button b2 = (Button) findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View v) {
switch (ch) {
case 1:
[Link]([Link]);
break;
case 2:
[Link]([Link]);
break;
case 3:
[Link]([Link]);
break;
case 4:
[Link]([Link]);
break;
case 5:
[Link]([Link]);
break;
case 6:
[Link]([Link]);
break;
}
ch++;
if (ch == 7)
ch = 1;
}
});
}
}
OUTPUT:-
[Link] AND ACTIVITY
[Link]:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="[Link]
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="70dp"
android:layout_y="64dp"
android:text="Intent and Activity"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/explicit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="104dp"
android:layout_y="152dp"
android:text="Explicit Intent"/>
<Button
android:id="@+id/implicit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="104dp"
android:layout_y="219dp"
android:text="Implicit Intent"/>
</AbsoluteLayout>

[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
Button explicit_btn, implicit_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
explicit_btn = (Button) findViewById([Link]);
implicit_btn = (Button) findViewById([Link]);
explicit_btn.setOnClickListener(new [Link]() {
public void onClick(View arg0) {
Intent intent = new Intent([Link],
[Link]);
startActivity(intent);
}
});
}}

[Link]:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="[Link]
android:id="@+id/AbsoluteLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="86dp"
android:layout_y="202dp"
android:textColor="#000000"
android:text="Explicit Activity"
android:textAppearance="?android:attr/textAppearanceLarge" />
</AbsoluteLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];

public class SecondActivity extends Activity {


protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
[Link](this, "This is an Explicit Activity",
Toast.LENGTH_SHORT).show();
}
}
implicit_btn.setOnClickListener(new [Link]() {
public void onClick(View arg0) {
[Link](getApplicationContext(), "This is an Implicit
Activity", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_VIEW);
[Link]([Link]("[Link]
startActivity(intent);
}
});
}
}
OUTPUT :-
3. LAYOUT MANAGER AND EVENT LISTENERS
Activity_main.xml:
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="LAYOUT MANAGER AND ACTION LISTENER"

android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="64dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
</LinearLayout>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout1"
android:layout_alignLeft="@+id/textView1"
android:text="ENTER THE FIRST NUMBER"

android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignTop="@+id/linearLayout1"
android:layout_marginLeft="20dp"
android:ems="10">
<requestFocus />
</EditText>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="20dp"
android:text="ENTER THE SECOND NUMBER"

android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_marginTop="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="ADD" />
</RelativeLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
final EditText firstNum = (EditText) findViewById([Link].editText1);
final EditText secNum = (EditText) findViewById([Link].editText2);
Button btnAdd = (Button) findViewById([Link].button1);
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
int num1 = [Link]([Link]().toString());
int num2 = [Link]([Link]().toString());
[Link](getApplicationContext(), "Sum = " + (num1
+ num2), Toast.LENGTH_LONG).show();
}
});
}
}
Output :-
4. GRAPHICAL PRIMITIVES
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView(new MyView(this));
}
private class MyView extends View {
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
[Link](canvas);
Paint paint = new Paint(); // Corrected case here
[Link](40);
[Link]([Link]);
[Link]("Circle", 55, 30, paint);
[Link]([Link]);
[Link](100, 150, 100, paint);
[Link]([Link]);
[Link]("Rectangle", 255, 30, paint);
[Link]([Link]);
[Link](250, 50, 400, 350, paint);
[Link]([Link]);
[Link]("Square", 55, 430, paint);
[Link]([Link]);
[Link](50, 450, 150, 550, paint);
[Link]([Link]);
[Link]("Line", 155, 430, paint);
[Link]([Link]);
[Link](250, 500, 350, 500, paint);
}
}}
Output for this program:
[Link] – RICH SITE SUMMARY
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="[Link]
android:layout_width="fill_parent"
android:id="@+id/fragment_container"
android:layout_height="fill_parent" />

fragement_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@+id/listView"
android:layout_width="fill_pare nt"
android:layout_height="fill_parent" >
</ListView>
<ProgressBar android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
rss_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="[Link]
xmlns:tools="[Link]
android:id="@+id/itemTitle" android:layout_width="match_p
arent" android:layout_height="wrap_co
ntent"android:textSize="18dp" tools:ignore="SpUsage" />

[Link]:
package [Link].ex_no_5;
import [Link];
import [Link]; import
[Link];
[Link]; public class
MainActivity extends FragmentActivity { @Override
public void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main); if (savedInstanceState ==
null) { addRssFragment();
}
}
private void addRssFragment() {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = [Link]();
RssFragment fragment = new RssFragment();
[Link]([Link].fragment_container, fragment);
[Link]();
}
@Override
protected void onSaveInstanceState(Bundle outState)
{[Link](outState);
[Link]("fragment_added", true);
}
}

[Link]:
package [Link].ex_no_5;
public class Constants {
public static final String TAG = "RssApp";
}
[Link] package [Link].ex_no_; import
[Link]; import [Link]; import
[Link]; import [Link];
import [Link];
import [Link];
import [Link];
public class PcWorldRssParser {
// We don't use namespaces
private final String ns = null;
public List<RssItem> parse(InputStream inputStream) throws
XmlPullParserException,IOException {
try {
XmlPullParser parser = [Link]();
[Link](XmlPullParser.FEATURE_PROCESS_NAMESPACES,
false); [Link](inputStream, null);
[Link]();
return readFeed(parser);
} finally { [Link]();
}
}
private List<RssItem> readFeed(XmlPullParser parser) throws
XmlPullParserException,IOException
{ [Link](XmlPullParser.START_TAG, null, "rss");
String title = null; String link = null;
List<RssItem> items = new ArrayList<RssItem>();
while ([Link]() != XmlPullParser.END_DOCUMENT) { if
([Link]() != XmlPullParser.START_TAG) { continue;
}
String name = [Link](); if ([Link]("title")) { title =
readTitle(parser);
} else if ([Link]("link")) {link = readLink(parser);
}
if (title != null && link != null) {
RssItem item = new RssItem(title, link);[Link](item); title =
null;link = null;
}
}
return items;
}
private String readLink(XmlPullParser parser) throws
XmlPullParserException, IOException
{
[Link](XmlPullParser.START_TAG, ns, "link"); String link =
readText(parser); [Link](XmlPullParser.END_TAG, ns, "link");
return link;
}
private String readTitle(XmlPullParser parser) throws
XmlPullParserException,IOException
{ [Link](XmlPullParser.START_TAG, ns, "title"); String title =
readText(parser); [Link](XmlPullParser.END_TAG, ns, "title");
return title;
}
// For the tags title and link, extract their text values.
private String readText(XmlPullParser parser) throws IOException,
XmlPullParserException
{
String result = "";
if ([Link]() == [Link]) { result = [Link]();
[Link]();
}
return result;
}
}

[Link]:
package [Link].ex_no_5;
import [Link];
import [Link]; import [Link]; import
[Link];
import [Link]; import
[Link]; public class RssAdapter extends
BaseAdapter {private final List<RssItem> items; private final Context
context;
public RssAdapter(Context context, List<RssItem> items) {
[Link] = items;
[Link] = context;
}
@Override
public int getCount() {
return [Link]();
}
@Override
public Object getItem(int position) {
return [Link](position);
}
@Override
public long getItemId(int id) {
return id;
}
@Override
public View getView(int position, View convertView, ViewGroup
parent) {ViewHolder holder;
if (convertView == null) {
convertView = [Link](context, [Link].rss_item, null);holder =
new
ViewHolder();
[Link] = (TextView)
[Link]([Link]);
[Link](holder);
} else {
holder = (ViewHolder) [Link]();
}
[Link]([Link](position).getTitle());
return convertView;
}
static class ViewHolder { TextView itemTitle;
}
}

[Link]:
package [Link].ex_no_5;
import [Link];
import [Link]; import [Link]; import
[Link]; import [Link]; import
[Link];
import [Link]; import
[Link]; import [Link];
import [Link];
import [Link];
import [Link];
import [Link]; import [Link];
import [Link];
public class RssFragment extends Fragment implements
OnItemClickListener {
private ProgressBar progressBar;private ListView listView; private
View view;
@Override
public void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState); setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, BundlesavedInstanceState) {
if (view == null) {
view = [Link]([Link].fragment_layout, container,
false);progressBar = (ProgressBar)
[Link]([Link]); listView = (ListView)
[Link]([Link]);
[Link](this); startService();
} else {
ViewGroup parent = (ViewGroup) [Link]();
[Link](view);
}
return view;
}
private void startService() {
Intent intent = new Intent(getActivity(), [Link]);
[Link]([Link], resultReceiver);
getActivity().startService(intent);
}
private final ResultReceiver resultReceiver = new ResultReceiver(new
Handler())
{@SuppressWarnings("unchecked") @Override
protected void onReceiveResult(int resultCode, Bundle resultData)
{ [Link]([Link]);
List<RssItem> items = (List<RssItem>)
[Link]([Link]); if (items != null) {
RssAdapter adapter = new RssAdapter(getActivity(), items);
[Link](adapter);
} else {
[Link](getActivity(), "An error occured while downloading
the rss feed.",
}
};
};
@Override
Toast.LENGTH_LONG).show();
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) { RssAdapter adapter = (RssAdapter)
[Link]();
RssItem item = (RssItem) [Link](position);Uri uri =
[Link]([Link]());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
[Link]
package [Link].ex_no_8;
public class RssItem { private final String title; private final String link;
public RssItem(String title, String link) {
[Link] = title;
[Link] = link;
}
public String getTitle() {
return title;
}
public String getLink() {
return link;
}
}

[Link]:
package [Link].ex_no_5;
import [Link]; import [Link]; import
[Link]; import [Link]; import [Link];
import [Link];
import [Link]; import [Link];
import [Link]; import [Link]; import
[Link];
public class RssService extends IntentService {
private static final String RSS_LINK =
"[Link]
public static final String ITEMS = "items"; public static final String
RECEIVER = "receiver";public RssService() {
super("RssService");
}
@Override
protected void onHandleIntent(Intent intent) { Log.d([Link],
"Service started"); List<RssItem> rssItems = null;
try {
PcWorldRssParser parser = new PcWorldRssParser(); rssItems =
[Link](getInputStream(RSS_LINK));
} catch (XmlPullParserException e) { Log.w([Link](), e);
} catch (IOException e) { Log.w([Link](), e);
}
Bundle bundle = new Bundle(); [Link](ITEMS,
(Serializable) rssItems);
ResultReceiver receiver = [Link](RECEIVER);
[Link](0, bundle);
}
public InputStream getInputStream(String link) {
try {
URL url = new URL(link);
return [Link]().getInputStream();
} catch (IOException e) {
Log.w([Link], "Exception while retrieving the input stream",
e);
return null;
}
}
Output :-

6. MULTI-THREADING
Activity_main.xml:
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="0"

android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:text="Thread 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/button1"
android:layout_below="@id/button1"
android:layout_marginTop="32dp"
android:text="Thread 2" />
</RelativeLayout>

[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
TextView tv;
Button b1, b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
tv = (TextView) findViewById([Link].textView1);
b1 = (Button) findViewById([Link].button1);
b2 = (Button) findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
Runnable r = new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10; i++) {
final int n = i;
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
[Link](new Runnable() {
@Override
public void run() {
[Link]([Link](n));
}
});
}
}
};
new Thread(r).start();
}
});
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
[Link]("MultiThread Example");
}
});
} }

Output :-
7. CREATE ALARAM CLOCK
Activity_main.xml:
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/bts" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginLeft="18dp"
android:layout_marginTop="14dp"
android:scaleX="0.80"
android:scaleY="0.80" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentBottom="true"
android:scaleX="0.50"
android:scaleY="0.50" />
</RelativeLayout>

[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
private static MainActivity inst;
public static MainActivity instance()
{
return inst;
}
public void onStart()
{
[Link]();
inst=this;
}
NotificationManager nm;
Notification n;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
final TimePicker tp=(TimePicker)findViewById([Link].timePicker1);
final DatePicker dp=(DatePicker)findViewById([Link].datePicker1);
Button b=(Button)findViewById([Link].button1);
nm=(NotificationManager)getSystemService([Link]
_SERVICE);
n=newNotification([Link].ic_launcher,"ALARM",[Link]
TimeMillis());
tp.setIs24HourView(false);
Calendar now=[Link]();
[Link]([Link]([Link]),[Link]([Link]),[Link](
Calendar.DAY_OF_MONTH),null);
[Link]([Link](Calendar.HOUR_OF_DAY));
[Link]([Link]([Link]));
[Link](new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Calendar current=[Link]();
Calendar alarm=[Link]();
[Link]([Link](),[Link](),[Link](),[Link]
rentHour(),[Link](),00);
if([Link](current)<=0)
[Link](getApplicationContext(), "Invalid Data and Time!!!",
Toast.LENGTH_LONG).show();
else
{
Intent i=new Intent([Link],[Link]);
PendingIntent pi=[Link]([Link],
123,i,0);
AlarmManageram=(AlarmManager)getSystemService(ALARM_SERVI
CE);

[Link](AlarmManager.RTC_WAKEUP,[Link](),pi);
[Link](getApplicationContext(),"Alarm is Set
ON!!!",Toast.LENGTH_LONG).show();
}
}
});
}
@SuppressWarnings("deprecation")
public void update_notification(String no,String msg)
{
[Link](getBaseContext(), no, msg, null);
[Link](1337,n);
}
}

[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
MainActivity inst = [Link]();
inst.update_notification("Alarm","Wake up! Wake up!!");
}
}
Output :-
[Link]
Activity_main.xml:
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="80dp"/>
</RelativeLayout>
[Link]:
package [Link].widgets1;
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
SeekBar simpleSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
simpleSeekBar = (SeekBar) findViewById([Link].seekBar1);
[Link](new
[Link]() {
int ProgressChangeValue = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
ProgressChangeValue = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Optional: Do something when the user starts interacting with
the SeekBar
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
[Link]([Link], "SeekBar progress is: " +
ProgressChangeValue, Toast.LENGTH_SHORT).show();
}
});
}
}
Output :-
[Link] DATA TO SD CARD
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DDFFD2f"
android:orientation="vertical">
<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus />
</EditText>
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/save_data" />
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/show_data" />
</LinearLayout>

[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
Button save, load;
EditText message;
String Message1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Correctly cast the Views to their corresponding types
save = (Button) findViewById([Link].b1);
load = (Button) findViewById([Link].b2);
message = (EditText) findViewById([Link].e1);
// Set OnClickListener for the Save button
[Link](new [Link]() {
public void onClick(View v) {
// Get message from user
Message1 = [Link]().toString();
try {
// Check if external storage is available
if
([Link]().equals([Link]
_MOUNTED)) {
// Create a new folder called MyDirectory in SDCard
File sdcard =
[Link]();
File directory = new File([Link]() +
"/MyDirectory");
if (![Link]()) {
[Link](); // Create directory if it doesn't
exist
}
// Create a new file named [Link] inside
MyDirectory
File file = new File(directory, "[Link]");
FileOutputStream fou = new FileOutputStream(file);
OutputStreamWriter osw = new
OutputStreamWriter(fou);
// Write user data to file
[Link](Message1);
[Link]();
[Link]();
[Link](getBaseContext(), "Data Saved",
Toast.LENGTH_LONG).show();
} else {
[Link](getBaseContext(), "SD Card not
available", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
[Link]();
}
}
});
// Set OnClickListener for the Load button
[Link](new [Link]() {
public void onClick(View v) {
try {
// Check if external storage is available
if
([Link]().equals([Link]
_MOUNTED)) {
File sdcard =
[Link]();
File directory = new File([Link]() +
"/MyDirectory");
File file = new File(directory, "[Link]");
if ([Link]()) {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
char[] data = new char[100];
StringBuilder final_data = new StringBuilder();
int size;
while ((size = [Link](data)) > 0) {
// Read data from file
final_data.append(data, 0, size);
}
[Link]();
[Link]();
// Display the data in output
[Link](getBaseContext(), "Message: " +
final_data.toString(), Toast.LENGTH_LONG).show();
} else {
[Link](getBaseContext(), "File not found",
Toast.LENGTH_LONG).show();
}
} else {
[Link](getBaseContext(), "SD Card not
available", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
[Link]();
}
}
});
}
}
[Link]:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SDCardExample</string>
<string name="save_data">Save Data</string>
<string name="show_data">Show Data</string>
<string name="action_settings">Settings</string> <!-- Add this -->
</resources>

[Link]:

<menu
xmlns:android="[Link]
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
Output :-
10. ALERT UP ON RECEIVING MESSAGE
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView android:layout_width="wrap_conten t"
android:layout_height="wrap_conten t"android:text="Message"
android:textSize="30sp" />
<EditText android:id="@+id/edit Text"
android:layout_width="match_parent"
android:layout_height="wrap_conten t"android:singleLine="true"
android:textSize="30sp" />
<Button android:id="@+id/button"
android:layout_width="wrap_conten t"
android:layout_height="wrap_conten
t"android:layout_margin="30dp" android:layout_gravity="center"
android:text="Notify" android:textSize="30sp"/>
</LinearLayout>

[Link]:
package [Link];
import [Link];
import [Link]; import
[Link]; import [Link];
import [Link];
import [Link]; import
[Link];
import [Link]; import [Link];
public class MainActivity extends AppCompatActivity
{
Button notify; EditText e; @Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main); notify= (Button)
findViewById([Link]); e= (EditText) findViewById([Link]);
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent([Link], [Link]);
PendingIntentpending=[Link]([Link],0,
intent, 0);
Notificationnotion=[Link]([Link]).setCon
tentTitle("NewMessage").setContentText([Link]().toString()).setS
mallIcon([Link].ic_launcher).setContentIntent(pending). build();
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
[Link] |= Notification.FLAG_AUTO_CANCEL;
[Link](0, noti);
}
} );
}
}

Output :-
11. DATABASE
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:background="#FFFFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textColor="#000000" android:textAppearance="?
android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retrieve" />
</LinearLayout>

[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
EditText name, email;
Button Save, Clear, Get;
public static final String mypreference = "mypref";
public static final String Name = "namekey";
public static final String Email = "emailkey";
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize views
name = (EditText) findViewById([Link]);
email = (EditText) findViewById([Link]);
Save = (Button) findViewById([Link].button1);
Clear = (Button) findViewById([Link].button2);
Get = (Button) findViewById([Link].button3);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
// Save data
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
String n = [Link]().toString();
String e = [Link]().toString();
[Link] editor = [Link]();
[Link](Name, n);
[Link](Email, e);
[Link](); // Apply the changes
[Link](getApplicationContext(), "Data Saved",
Toast.LENGTH_SHORT).show();
}
});
// Clear data
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
[Link]("");
[Link]("");
[Link](getApplicationContext(), "Data Cleared",
Toast.LENGTH_SHORT).show();
}
});
// Retrieve data
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
if ([Link](Name)) {
[Link]([Link](Name, ""));
}
if ([Link](Email)) {
[Link]([Link](Email, ""));
}
[Link](getApplicationContext(), "Data Retrieved",
Toast.LENGTH_SHORT).show();
}
});
}
}
Output for this program:

You might also like