Académique Documents
Professionnel Documents
Culture Documents
Aim: To develop an Android app that implements Preferences, settings and various menus.
Source Code:
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.ankitj.lab_7.MainActivity"
tools:showIn="@layout/activity_main">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_gravity="center_horizontal"
android:visibility="visible"
android:src="@drawable/pug"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/contentPanel"
android:text="PUG"
android:layout_alignParentTop="true"
android:visibility="gone"/>
<TextView
android:id="@+id/cont"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pug"
android:layout_below="@+id/image"
android:layout_marginTop="40dp"
android:visibility="visible"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/user_text"
android:text="Hello User!"
android:layout_above="@+id/tv"
android:textSize="30dp"/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="55dp"
android:textColor="#ffffff"
android:padding="20dp"
android:text="LIKE IT!?"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimaryDark"/>
</RelativeLayout>
content_menu.xml
</RelativeLayout>
activity_settings.xml
?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ankitj.lab_7.Settings">
</android.support.constraint.ConstraintLayout>
ssettings_frag.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ankitj.lab_7.Settings_frag">
</FrameLayout>
layout/activity_about.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ankitj.lab_7.about">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_about" />
</android.support.design.widget.CoordinatorLayout>
content_about.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.ankitj.lab_7.about"
tools:showIn="@layout/activity_about">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="25sp"
android:textColor="#000000"/>
</RelativeLayout>
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:defaultValue="false"
android:key="italiac"
android:title="Font-Italic"
/>
<ListPreference
android:defaultValue="image"
android:entries="@array/theme_labels"
android:entryValues="@array/theme_values"
android:key="display"
android:title="Display"/>
<EditTextPreference
android:defaultValue="User"
android:key="name"
android:title="Your Name"/>
</PreferenceScreen>
array.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="theme_labels" >
<item>Image_display</item>
<item>Content_display</item>
</array>
<array name="theme_values">
<item>image</item>
<item>content</item>
</array>
</resources>
strings.xml:
<resources>
<string name="app_name">PETS</string>
<string name="action_settings">Settings</string>
<string name="pug">age : 10
cost: 100$</string>
colors.xml:
<resources>
<string name="app_name">PETS</string>
<string name="action_settings">Settings</string>
<string name="pug">age : 10
cost: 100$</string>
styles.xml:
<resources>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay"
parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
</resources>
MainActivity.java:
package com.example.ankitj.lab_7;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.ActionMode;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
setUpSharedPreferences();
TextView textView = (TextView) findViewById(R.id.tv);
textView.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
mActionMode =
MainActivity.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem
menuItem) {
switch (menuItem.getItemId()) {
case R.id.yes:
Toast.makeText(getApplicationContext(), "Thanks for
liking!", Toast.LENGTH_SHORT).show();
actionMode.finish(); // Action picked, so close the CAB
return true;
case R.id.no:
Toast.makeText(getApplicationContext(), "We'll try to
improve", Toast.LENGTH_SHORT).show();
actionMode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
mActionMode = null;
}
};
setItalic(sharedPreferences.getBoolean("italiac",true));
setMydisplay(sharedPreferences.getString("display","image"));
setMyName(sharedPreferences.getString("name","User"));
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
if(b)
spannableString.setSpan(new
StyleSpan(Typeface.ITALIC),0,spannableString.length(),0);
else
spannableString.setSpan(new
StyleSpan(Typeface.NORMAL),0,spannableString.length(),0);
cont.setText(spannableString);
}
}
public void setMyName(String myName){
tv_hello.setText("Hello "+myName+ "!");
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String s) {
if(s.equals("italiac"))
{
setItalic(sharedPreferences.getBoolean("italiac",true));
}
if(s.equals("display")){
setMydisplay(sharedPreferences.getString("display","image"));
}
if(s.equals("name")){
setMyName(sharedPreferences.getString("name","User"));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id)
{
case R.id.about:
openabout();
return true;
case R.id.popupmenu:
popupmenu_implementation(id);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
popupMenu.getMenuInflater().inflate(R.menu.popupmenu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new
PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getTitle().toString().equals("Pug")){
imageView.setImageResource(R.drawable.pug);
content_panel.setText("PUG");
}
else if (item.getTitle().toString().equals("Poodle")){
imageView.setImageResource(R.drawable.poodle);
content_panel.setText("POODLE");
}
Toast.makeText(MainActivity.this,"Chose"+item.getTitle(),Toast.LENGTH_LONG).show
();
return true;
}
});
popupMenu.show();
}
}
About.java:
package com.example.ankitj.lab_7;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
Settings.java:
package com.example.ankitj.lab_7;
import android.content.SharedPreferences;
import android.preference.PreferenceActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.SharedPreferencesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeL
istener(this);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(android.R.id.content,new Settings_frag());
ft.commit();
}
Settings_frag.java:
package com.example.ankitj.lab_7;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeLis
tener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeL
istener(this);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
PreferenceScreen ps = getPreferenceScreen();
SharedPreferences sp = ps.getSharedPreferences();
int i;
int count = ps.getPreferenceCount();
for(i=0;i<count;i++){
Preference p = ps.getPreference(i);
if(!(p instanceof CheckBoxPreference)){
String val = sp.getString(p.getKey(),"");
setPreference(val,p);
}
}
}
private void setPreference(String val,Preference p){
if(p instanceof ListPreference){
ListPreference lp = (ListPreference) p;
int prefindex = lp.findIndexOfValue(val);
if(prefindex>=0){
lp.setSummary(lp.getEntries()[prefindex]);
}
}
else if(p instanceof EditTextPreference){
p.setSummary(val);
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String s) {
Preference p = findPreference(s);
if(p!=null){
if(!(p instanceof CheckBoxPreference)){
String val = sharedPreferences.getString(p.getKey(),"");
setPreference(val,p);
}
}
}
}
Output:
Result:
Android app that implements preferences and menus is successfully implemented is created.
Ex. No.: 8 Application Using Google Maps API
Date: 05-03-2018
Aim: To develop an Android app that using Google Maps API to search for a location and get its
address details.
Source Code:
activity_front.xml
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<fragment
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
</LinearLayout>
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
MainActivity.java
package com.ankitj5014;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
/**
* An activity that displays a Google map with a marker (pin) to indicate a
particular location.
*/
public class MapsMarkerActivity extends AppCompatActivity
implements OnMapReadyCallback, GoogleMap.OnMapClickListener {
/**
* Manipulates the map when it's available.
* The API invokes this callback when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the
camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user receives
a prompt to install
* Play services inside the SupportMapFragment. The API invokes this method
after the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
this.googleMap = googleMap;
this.googleMap.setOnMapClickListener(this);
}
@Override
public void onMapClick(LatLng latLng) {
getAddress(latLng.latitude, latLng.longitude, latLng);
}
if(marker!=null){
marker.remove();
}
marker = googleMap.addMarker(new MarkerOptions().position(latLng)
.title(location));
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
GradleProperties:
# Project-wide Gradle settings.
# Replace the value below with your own Google API key for Android app.
# You can get an API key by clicking "Get a Key" on the Google Maps Android API
developer's site.
# https://developers.google.com/maps/documentation/android-api/start
GOOGLE_MAPS_API_KEY=AIzaSyCl8MvfD6YhmeXmLuURDPU363ZUB2PWaSY
Output:
Result:
Android app that implements preferences and menus is successfully implemented is created.