Show Listview on googlemaps activity - android

I'm trying to show list view on the bottom side of the google maps activity (only when the button is clicked), but it doesn't work, just showing app stop message. How can I show listview on google maps activity?
Here is my java and xml code
package com.example.dong_gyo.project;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
public class MapFind extends ActionBarActivity {
GoogleMap mMap; // Might be null if Google Play services APK is not available.
LocationManager lm;
String locationProvider;
Location location;
Button mapListbut;
ListView mapList;
ArrayList list;
ButtonListener listener;
ArrayAdapter mapadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_find);
setUpMapIfNeeded();
mapListbut = (Button)findViewById(R.id.showMapList);
mapadapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
mapList = (ListView)findViewById(R.id.mapList);
mapList.setAdapter(mapadapter);
mapList.setVisibility(View.INVISIBLE);
mapListbut.setOnClickListener(listener);
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationProvider = lm.getBestProvider(new Criteria(), true);
location = lm.getLastKnownLocation(locationProvider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
final LatLng LOC = new LatLng(latitude, longitude);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LOC, 16));
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap(LOC);
}
}
}
private void setUpMap(LatLng LOC) {
mMap.addMarker(new MarkerOptions().position(LOC));
}
class ButtonListener implements View.OnClickListener {
#Override
public void onClick(View v) { // 목록 토글 버튼 리스너
// TODO Auto-generated method stub
switch (v.getId()) { //
case R.id.showMapList:
if (!mapList.isShown()) {
mapList.setVisibility(View.VISIBLE);
mapadapter.notifyDataSetChanged();
mapListbut.setSelected(true);
} else {
mapList.setVisibility(View.INVISIBLE);
mapListbut.setSelected(false);
}
break;
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
startActivity(new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<fragment 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"
android:id="#+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<Button
android:id="#+id/showMapList"
android:text = "목록보기"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"/>
<ListView
android:id="#+id/mapList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#eeeeee" >
</ListView>
</RelativeLayout>

I ran your code and it said it gets a null pointer. Looking at your code, location = lm.getLastKnownLocation(locationProvider); makes location to be null. So, instead of the getBestProvider, try:
location = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
I tried it and the maps load.

Related

Adding Google Maps Activity to button using Android Studio

I am trying to add new Google Maps Activity to a button in my main activity here is the code.
Code for activity_main.xml
<LinearLayout 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"
android:layout_marginTop="20dp"
android:id="#+id/layout_1"
android:orientation="vertical"
android:nestedScrollingEnabled="false"
android:weightSum="1">
<ImageView
android:id="#+id/test_image"
android:src="#drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SITbus"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:textColor="#ffff1b37"
android:textColorHighlight="#ff3a57ff"
android:textSize="#android:dimen/app_icon_size"
android:theme="#android:style/Animation"
android:typeface="monospace"
android:textIsSelectable="false" />
<Button
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_marginTop="10dp"
android:text="#string/button_send1"
android:onClick="sendMessageR"
android:layout_gravity="center_horizontal"
android:background="#ffff2605"
android:visibility="visible" />
</LinearLayout>
MainActivity.java
package com.example.harish.mysampleapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.harish.mysampleapplication.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessageR(View view) {
Intent intent = new Intent(this, MapsRedActivity.class);
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MapsRedActivity.java
package com.example.harish.mysampleapplication;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsRedActivity extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_red);
setUpMapIfNeeded();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
I am able to open my main activity but not able to open the Google Map Activity when I click the button. The program is not showing any errors.
Can anyone tell me where I have done wrong? and can anyone suggest me solution for this problem
After you create intent you have to start new activity with that intent or nothing will happen
public void sendMessageR(View view) {
Intent intent = new Intent(this, MapsRedActivity.class);
startActivity(intent);
}
Starting Another Activity

Android: Google Maps app gives black screen after single tap, no error

Currently I just made an app with the API key to setup a Google Maps application.
It launches without an error, but I can't figure out why it gives a black screen after tapping the screen once. If I tap it twice it zooms in.
Even if it gives a black screen, it doesn't crash, since I am still able to open the onOptionsItemSelected menu.
When its black, I cant return back to the main map
Please proceed
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
GoogleMap m_googleMap;
StreetViewPanorama m_StreetView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createMapView();
createStreetView();
m_googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
if (m_StreetView != null) {
/**
* Hide the map view to expose the street view.
*/
Fragment mapView = getFragmentManager().findFragmentById(R.id.mapView);
getFragmentManager().beginTransaction().hide(mapView).commit();
m_StreetView.setPosition(latLng);
}
}
});
}
private void createStreetView() {
m_StreetView = ((StreetViewPanoramaFragment)
getFragmentManager().findFragmentById(R.id.streetView))
.getStreetViewPanorama();
}
private void createMapView(){
try {
if(null == m_googleMap){
m_googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
if(null == m_googleMap) {
Toast.makeText(getApplicationContext(),
"Error creating map",Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){
Log.e("mapApp", exception.toString());
}
}
private void addMarker(){
if(null != m_googleMap){
m_googleMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker")
.draggable(true)
);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Edit 1
Added activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/streetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"/>
<fragment
android:id="#+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Google maps failing to load from clicking a button

I want to load a google maps activity by clicking a button in the main activity but the map does not load, all that loads is a blank activity with the zoom controls. Below is my code:
MainActivity
package com.maplocator.shareplaces;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
startActivity(new Intent("com.maplocator1.ToMap"));
}
public void Map(final View view) {
Intent intent = new Intent(MainActivity.this,ToMap.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MapActivity
package com.maplocator.shareplaces;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;
public class ToMap extends FragmentActivity {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
Toast.makeText(this, "Ready to Map", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_map );
}
else {
setContentView(R.layout.activity_main);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else {
Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
}
return false;
}
public static int getGpsErrordialogRequest() {
return GPS_ERRORDIALOG_REQUEST;
}
}
ActivityMain
<RelativeLayout 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"
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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="153dp"
android:contentDescription="#string/share_p"
android:src="#drawable/shareplaces" />
<Button
android:id="#+id/button_showmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:text="#string/go_to_map"
android:textAppearance="?android:attr/textAppearanceLarge"
android:onClick="onClick"/>
</RelativeLayout>
MapActivity
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
Okay the fix to this is actually simple. I was usin an API Key I had made in another application, so to fix this issue all I had to do was go to the Google Apis console and get a key for this new application I was developing, so make sure that you generate a new key for a new application.

Android and Google map inside fragment with other controls and viewpager

I'm new in android programming;
I have that small app with 3 pages (fragments), swiping between them using pageradapter and viewpager;
one of these pages contains checkbox [and other controls] and a map;
my problem is that the program is crashing on start.
Fragment_compass.java
package com.ibdiab.emadaldien;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
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.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class Fragment_Compass extends Fragment {
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.activity_compass, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.eamap);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
//map.getUiSettings().setMyLocationButtonEnabled(false);
//map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(new LatLng(50.167003,19.383262)));
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
return v;
}
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
activity_compass.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/autolocate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="view live map" />
<fragment
android:id="#+id/eamap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
MainScr.java
public class MainScr extends FragmentActivity {
private PagerAdapter el_pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
initialisePaging();
}
private void initialisePaging() {
// TODO Auto-generated method stub
List<Fragment> eafragments = new Vector<Fragment>();
eafragments.add(Fragment.instantiate(this, Fragment_PraysTable.class.getName()));
eafragments.add(Fragment.instantiate(this, Fragment_Main.class.getName()));
eafragments.add(Fragment.instantiate(this, Fragment_Compass.class.getName()));
el_pagerAdapter = new PagerAdapter(this.getSupportFragmentManager() , eafragments);
ViewPager pager = (ViewPager) findViewById(R.id.mainviewpager);
pager.setAdapter(el_pagerAdapter);
pager.setCurrentItem(1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_scr, menu);
return true;
}
}

Start Fragment from another -> might have started the wrong way my app?

I have started building an application but I think I'm might be doing something wrong in the main structure. My application is 3 fragments, communicating with each other. The first fragment is about settings, the second one is an exandable array list. The third is a Gmap.
Clicking on a specific setting in the first fragment (that is a location) would make the gmap opens at the good spot... Same thing for child item in the second fragment.
I also would like to have a thread that update the current location of all fragments.
The this is, I don't know how I should structure my app. Is it good to have 3 fragments, should I use 3 activities? And how the communication should be made within fragments? I read that fragments should not communicate directly together, but in my case I don't see how the child of an exandable array list can not directly open the fragment containing GMap...
Here is a picture, just to figure out what my app would look like:
http://s14.postimg.org/ywj7yu8qp/Untitled.png
Here's the code so far:
MainActivity:
package com.stylingandroid.basicactionbar;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity
{
private class MyTabListener implements ActionBar.TabListener
{
private Fragment mFragment;
private final Activity mActivity;
private final String mFragName;
public MyTabListener( Activity activity, String fragName )
{
mActivity = activity;
mFragName = fragName;
}
#Override
public void onTabReselected( Tab tab, FragmentTransaction ft )
{
// TODO Auto-generated method stub
}
#Override
public void onTabSelected( Tab tab, FragmentTransaction ft )
{
mFragment = Fragment.instantiate( mActivity, mFragName );
ft.add( android.R.id.content, mFragment );
}
#Override
public void onTabUnselected( Tab tab, FragmentTransaction ft )
{
ft.remove( mFragment );
mFragment = null;
}
}
#Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
// Intent intent;
ActionBar ab = getActionBar();
ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
Tab tab = ab.newTab()
.setText( R.string.title_param )
.setTabListener(
new MyTabListener( this,
Param.class.getName() ) );
ab.addTab( tab );
// intent = new Intent().setClass(this, Friends.class);
tab = ab.newTab()
.setText( R.string.title_friends )
.setTabListener(
new MyTabListener( this,
Friends.class.getName() ) );
ab.addTab( tab );
tab = ab.newTab()
.setText( R.string.title_maps )
.setTabListener(
new MyTabListener( this,
Maps.class.getName() ) );
ab.addTab( tab );
}
#Override
public boolean onCreateOptionsMenu( Menu menu )
{
getMenuInflater().inflate( R.menu.main, menu );
return true;
}
#Override
public boolean onOptionsItemSelected( MenuItem item )
{
boolean ret;
if (item.getItemId() == R.id.menu_settings)
{
// Handle Settings
ret = true;
} else
{
ret = super.onOptionsItemSelected( item );
}
return ret;
}
}
Tab1:
package com.stylingandroid.basicactionbar;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Fragment;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Param extends Fragment
{
Button btnShowLocation;
private View V;
TextView text;
public static double latitude;
public static double longitude;
// GPSTracker class
GPSTracker gps;
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState )
{
V = inflater.inflate( R.layout.param, container, false );
gps = new GPSTracker(getActivity());
if(gps.canGetLocation()){
text = (TextView) V.findViewById(R.id.widget35);
latitude = gps.getLatitude();
longitude = gps.getLongitude();
String cityName=null;
Geocoder gcd = new Geocoder(getActivity(), Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
cityName=addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
// create class object
// check if GPS enabled
text.setText(cityName);
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
return V;
}
}
Tab2:
package com.stylingandroid.basicactionbar;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
public class Friends extends Fragment implements OnChildClickListener
{
private static List<Country> Countries;
private ExpandableListView expandableListView;
private CountryAdapter adapter;
private View V;
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState )
{
V = inflater.inflate( R.layout.frag2, container, false );
expandableListView = (ExpandableListView) V.findViewById(R.id.expandableListView);
expandableListView.setOnChildClickListener(this);
LoadCountries();
return V;
}
private void LoadCountries() {
Countries = new ArrayList<Country>();
ArrayList<String> citiesAustralia = new ArrayList<String>(
Arrays.asList("Brisbanea", "Hobart", "Melbourne", "Sydney"));
Countries.add(new Country("Australia", citiesAustralia));
ArrayList<String> citiesChina = new ArrayList<String>(
Arrays.asList("Beijing", "Chuzhou", "Dongguan", "Shangzhou"));
Countries.add(new Country("China", citiesChina));
adapter = new CountryAdapter(getActivity(), Countries);
expandableListView.setAdapter(adapter);
}
#Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
int arg3, long arg4) {
/* Fragment param = new Param();
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frag2, param);
ft.addToBackStack(null);
ft.commit();
*/
Toast.makeText(getActivity(), "Clicked on Detail " , Toast.LENGTH_LONG).show();
return true;
}
}
Tab3:
package com.stylingandroid.basicactionbar;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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 android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Maps extends Fragment
{
static final LatLng HAMBURG = new LatLng(Param.latitude, Param.longitude);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
View V;
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState )
{
V = inflater.inflate( R.layout.frag1, container, false );
map = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
GoogleMap gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gMap.setMyLocationEnabled(true);
gMap.getUiSettings().setCompassEnabled(true);
Log.e("Maps", "------EOC-------");
return V;
}
}
param.xml (tab1):
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ToggleButton
android:id="#+id/widget33"
android:layout_width="125dp"
android:layout_height="59dp"
android:textOn="I&apos;m free!"
android:textOff="I&apos;m free!"
android:layout_x="78dp"
android:layout_y="29dp" />
<TextView
android:id="#+id/widget34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lieu actuel"
android:layout_x="87dp"
android:layout_y="196dp" />
<TextView
android:id="#+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Indisponible"
android:layout_x="101dp"
android:layout_y="247dp" />
<TextView
android:id="#+id/widget36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GPS"
android:layout_x="89dp"
android:layout_y="108dp" />
<ToggleButton
android:id="#+id/widget37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="On"
android:textOff="Off"
android:layout_x="158dp"
android:layout_y="99dp" />
</AbsoluteLayout>
frag2.xml (tab2):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
frag1.xml (tab3):
<RelativeLayout 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"
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" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
Here are only the principal fragments, I can also provide the full code of all classes if needed...
Questions:
Is it okay of what I began to do about how fragments work? Should I instead use activities? Is it okay how I proceeded to create fragment? Do I respect programming standard?
How to communicate from fragments to another? I use global variables so when something is updated, gmap (tab3) is also updated, but I don't see how to open the gmap fragment on a click.
The gmap is making the app crashing when I click two times on the gmap tab...?
Thanks a lot guys.
You can use all the fragments you want :)
I think the best way i to make an abstract Class to save and handle all you data and events.
In an abstract Class declare the method Static so you can call them directly on the Class.

Categories

Resources