Sorry for the noob question but I did not come across any situations that would work for me. Im sure this is super simple, I just need to be pointed in the right direction.
Objective:
I want to be able to take the name of what the user specified and add it to the markers title.
What I know:
I am unable to inflate the xml and grab it via that way cause the method is being override and if I inflate to my knowledge will no longer allow me to override the method. How should I approach this?
MapsAcitivy
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, addPlace.Communicator {
private GoogleMap mMap;
public static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 0;
int mStackLevel;
int YES_NO_CALL;
FragmentManager manager = getSupportFragmentManager();
final Context context = this;
#InjectView(R.id.btn_login) Button _loginButton;
ClusterManager<ClusterRequest> mClusterManager;
class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
// These are both viewgroups containing an ImageView with id "badge" and two TextViews with id
// "title" and "snippet".
private final View mWindow;
//private final View mContents;
CustomInfoWindowAdapter() {
mWindow = getLayoutInflater().inflate(R.layout.info_window_layout, null);
//mContents = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
}
#Override
public View getInfoWindow(Marker marker) {
render(marker, mWindow);
return mWindow;
}
#Override
public View getInfoContents(Marker marker) {
// Getting view from the layout file info_window_layout
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
// Getting the position from the marker
LatLng latLng = marker.getPosition();
// Getting reference to the TextView to set latitude
TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
// Getting reference to the TextView to set longitude
TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
// Setting the latitude
tvLat.setText("Latitude:" + latLng.latitude);
// Setting the longitude
tvLng.setText("Longitude:"+ latLng.longitude);
// Returning the view containing InfoWindow contents
return v;
//render(marker, mContents);
//return mContents;
}
private void render(Marker marker, View view) {
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
#TargetApi(23)
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
// MY_PERMISSION_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
LocationManager initialLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String initialProvider = initialLocationManager.getBestProvider(criteria, true);
Location initialLocation = initialLocationManager.getLastKnownLocation(initialProvider);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
double initialLatitude = initialLocation.getLatitude();
double initialLongitude = initialLocation.getLongitude();
LatLng initialLatLng = new LatLng(initialLatitude, initialLongitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLatLng, 14.9f));
//mMap.setOnInfoWindowClickListener(this);
mClusterManager = new ClusterManager<ClusterRequest>(getApplicationContext(), mMap);
mMap.setOnCameraChangeListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
}
}
public void onButtonClick(View view){
//do something when button is clicked.
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_place);
dialog.setTitle("Add Place");
Button btnLogin = (Button) dialog.findViewById(R.id.addPlacebtnSubmit);
Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
final EditText txtUsername = (EditText)dialog.findViewById(R.id.txtStopName);
// Attached listener for add place GUI button
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String stopName = txtUsername.getText().toString();
if(stopName.length() > 4){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MapsActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
// MY_PERMISSION_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
LocationManager currentLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria currentCriteria = new Criteria();
String currentProvider = currentLocationManager.getBestProvider(currentCriteria, true);
Location currentLocation = currentLocationManager.getLastKnownLocation(currentProvider);
double currentLatitude = currentLocation.getLatitude();
double currentLongitude = currentLocation.getLongitude();
LatLng currentLatLng = new LatLng(currentLatitude, currentLongitude);
ClusterRequest testCluster = new ClusterRequest(currentLatitude, currentLongitude);
mClusterManager.addItem(testCluster);
mClusterManager.setRenderer(new MapIconRender(getApplicationContext(), mMap, mClusterManager));
dialog.dismiss();
}
else{
txtUsername.setError("Stop name must be at least 5 characters long.");
}
}
});
}
MapIconRender.java
public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> {
public MapIconRender(Context context, GoogleMap map,
ClusterManager<ClusterRequest> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions){
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure));
markerOptions.snippet("Status: ");
markerOptions.title(stopName); //<------ THIS IS WHERE I WANT THE STOP NAME TO BE WHAT THE USER ENTERED.
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
add_place.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="match_parent"
android:paddingLeft="10dp" android:paddingRight="10dp"
android:background="#fff" android:layout_width="300dp"
android:paddingTop="10dp">
<View
android:id="#+id/HorizontalLine"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#aaa" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name:"
android:paddingTop="20dp"
android:id="#+id/name" />
<EditText android:layout_height="wrap_content"
android:id="#+id/txtStopName"
android:maxLength="100"
android:singleLine="true"
android:hint="Name of this stop (5-100 characters)"
android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type:"
android:paddingTop="20dp"
android:layout_marginBottom="10dp"
android:id="#+id/type" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:entries="#array/type_arrays"
android:prompt="#string/type_prompt"
android:layout_marginBottom="10dp"/>
<View
android:id="#+id/HorizontalLine2"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#aaa" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/linearLayout1"
android:paddingTop="5dp">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="0.5"
android:id="#+id/btnCancel" android:text="Cancel"
android:onClick="addPlaceCancel"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="0.5"
android:id="#+id/addPlacebtnSubmit" android:text="Submit"
android:onClick="addPlaceSubmit"
android:layout_marginBottom="10dp"/>
</LinearLayout>
</LinearLayout>
ClusterRequest.java
public class ClusterRequest implements ClusterItem {
private final LatLng mPosition;
public ClusterRequest(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}
#Override
public LatLng getPosition() {
return mPosition;
}
}
Can't you override onClusterItemRendered() method?
I think you can save your marker name in onBeforeClusterItemRendered() to any class-global variable String, and set the marker name in onClusterItemRendered() with Marker.setTitle(yourSavedTitle); or something.
Something like this:
public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> {
private String mMarkerTitle;
public MapIconRender(Context context, GoogleMap map,
ClusterManager<ClusterRequest> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions){
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure));
markerOptions.snippet("Status: ");
markerOptions.title(stopName); //<------ THIS IS WHERE I WANT THE STOP NAME TO BE WHAT THE USER ENTERED.
mMarkerTitle = stopName;
super.onBeforeClusterItemRendered(item, markerOptions);
}
#Override
protected void onClusterItemRendered (ClusterRequest item, Marker marker){
marker.setTitle(mMarkerTitle);
}
}
#SuhyeonLee Not sure if this is what you meant (please let me know) but I did find the answer surprisingly enough in the demo that google released for their customize cluster demo. Heres what I learned. When calling the MapIconRender.java class via
mClusterManager.setRenderer(new MapIconRender(getApplicationContext(), mMap, mClusterManager));
Which will run this method in the MapIconRender.java class
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions)
Which means whatever variables in the ClusterRequest class will be accessible here in the MapIconRender.java class and can be accessed via (item.variableName). I apologize for not posting the code for the file ClusterRequest, I didnt think it was needed. ill go back and add that snippet to my original question.
Anyway the new code I needed to add was as follows:
In the MapsActivity class I needed to pass the ClusterRequest class the name of the marker(or title of the marker) via:
ClusterRequest testCluster = new ClusterRequest(currentLatitude, currentLongitude, stopName); //stopName was added here.
And in the ClusterRequest class I needed to add the "global variable" of the marker name (aka stopName) via:
public class ClusterRequest implements ClusterItem {
private final LatLng mPosition;
public final String stopName; //Add a "global variable" here so it can be accessible in the MapIconRender class.
public ClusterRequest(double lat, double lng, String _stopName) {
mPosition = new LatLng(lat, lng);
stopName = _stopName;
}
#Override
public LatLng getPosition() {
return mPosition;
}
}
And finally in my MapIconRender.java class I can add the title name of the marker via:
public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> {
public MapIconRender(Context context, GoogleMap map,
ClusterManager<ClusterRequest> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions){
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure));
markerOptions.snippet("Status: ");
markerOptions.title("Title: " + item.stopName); //Now we can access the title of the marker by calling item.variableName
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
Im really sorry about the noob question here but I hope this helps someone out that may have the same question as I did and thank you for your answers! "SO" community is awesome!
Related
I am very new to Android development. This is my first attempt at creating an app. I have created a tabbed navigation app, with 4 fragments to navigate between: Home, Search, Map and Account fragments from the MainActivity. I have used a SupportMapFragment within my MapsFragment to display the map. Upon visiting the MapsFragment the map displays and zooms in on my Current location and highlights certain important locations, retrieved from a Firebase instance, with markers.
Everything works as expected when I first launch the app and visit the Map Fragment. I am zoomed into my current location and I see all required markers. The problem arises when I switch to a different fragment from that point. When I navigate to the Home, Search or Account fragment and then return to the Map fragment, none of the location markers are visible anymore. I can still see my current location and get zoomed into it on opening the Map fragment but the other markers disappear.
I originally thought it was to do with the fragment being recreated everytime I navigate to another fragment as I use .replace(container, fragment) to change the fragment based on which button was clicked from the bottom navigation bar. But I observed that the Map fragment would load my current location and zoom into it correctly, so the loadMap() function gets executed but somehow the markers don't appear. If i restart the app, the markers behave correctly the first time I open the Map fragment.
Any help on how to keep the markers showing on the Map when I return to it after navigating to other fragments will be greatly appreciated. Thank you!
Here is a preview of my MapsFragment.java:
public class MapsFragment extends Fragment {
private SupportMapFragment supportMapFragment;
private AutocompleteSupportFragment autocompleteSupportFragment;
private FusedLocationProviderClient client;
private Geocoder geocoder;
private ArrayList<String> mPostCodes;
private ArrayList<Marker> searchMarker;
private DatabaseReference locationRef;
private DatabaseReference businessRef;
private String apiKey;
public MapsFragment() {
// Required empty public constructor
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Initialize Map fragment
supportMapFragment = (SupportMapFragment)
getChildFragmentManager().findFragmentById(R.id.google_map);
// initialize Places client
apiKey = getString(R.string.map_key);
if(!Places.isInitialized()){
Places.initialize(requireActivity(), apiKey);
}
PlacesClient placesClient = Places.createClient(requireActivity());
// Initialize AutoComplete search bar and set autocomplete parameters
autocompleteSupportFragment = (AutocompleteSupportFragment)
getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteSupportFragment.setTypeFilter(TypeFilter.ADDRESS);
autocompleteSupportFragment.setLocationBias(RectangularBounds.newInstance(
new LatLng(55.836229, -4.252612),
new LatLng(55.897463, -4.325364)));
autocompleteSupportFragment.setCountries("UK");
autocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));
// initialize search marker list
searchMarker = new ArrayList<Marker>();
// Initialize client to get user's last location on device
client = LocationServices.getFusedLocationProviderClient(requireActivity());
// Initialize geocoder to convert business postcodes to latlng coordinates
mPostCodes = new ArrayList<>();
geocoder = new Geocoder(requireActivity());
// Get business locations
locationRef = FirebaseDatabase.getInstance().getReference("Business Locations");
locationRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
for(DataSnapshot locationSnapshot : snapshot.getChildren()){
BusinessLocation location = locationSnapshot.getValue(BusinessLocation.class);
mPostCodes.add(location.getPostCode());
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Log.i("Location error", "Error retrieving location: ", error.toException().getCause());
}
});
// initialize reference to business table
businessRef = FirebaseDatabase.getInstance().getReference("Businesses");
// render the map
loadMap();
}
private void loadMap() {
if (ActivityCompat.checkSelfPermission(requireActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Task<Location> task = client.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if(location != null){
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(#NonNull GoogleMap googleMap) {
// autocomplete place search
autocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onError(#NonNull Status status) {
Log.i("AutoComplete error", "error status: " + status);
}
#Override
public void onPlaceSelected(#NonNull Place place) {
LatLng placeLatLng = place.getLatLng();
MarkerOptions placeOptions = new MarkerOptions().position(placeLatLng)
.title("search")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
if(!searchMarker.isEmpty()){
Marker searchedMarker = searchMarker.get(0);
searchMarker.remove(searchedMarker);
searchedMarker.remove();
}
final Marker marker = googleMap.addMarker(placeOptions);
searchMarker.add(marker);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(placeLatLng, 10));
}
});
LatLng myLatLng = new LatLng(location.getLatitude(),
location.getLongitude());
// show current location
if (ActivityCompat.checkSelfPermission(requireActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
}
// show markers for all businesses on database
for(String code : mPostCodes){
try{
Address address = geocoder.getFromLocationName(code, 1).get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
MarkerOptions options = new MarkerOptions().position(latLng);
googleMap.addMarker(options);
}catch (IOException e){
Toast.makeText(requireActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLatLng, 10));
}
});
}
}
});
}else {
ActivityCompat.requestPermissions(requireActivity(),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
}
}
private ActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
result -> {
if(result){
loadMap();
}
}
);
}
Here is my fragment_maps.xml file:
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragments.MapsFragment">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/google_map"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<fragment
android:id="#+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"/>
</RelativeLayout>
And here is my MainActivity.kt file:
class MainActivity : AppCompatActivity() {
private val homeFragment = HomeFragment()
private val searchFragment = SearchFragment()
private val mapFragment = MapsFragment()
private val accountFragment = AccountFragment()
private val guestAccountFragment = GuestAccountFragment()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
replaceFragment(homeFragment)
bottom_navigation.setOnItemSelectedListener {
when (it.itemId) {
R.id.id_home -> replaceFragment(homeFragment)
R.id.id_search -> replaceFragment(searchFragment)
R.id.id_map -> replaceFragment(mapFragment)
R.id.id_account -> {
if(FirebaseAuth.getInstance().currentUser == null){
replaceFragment(guestAccountFragment)
}else {
replaceFragment(accountFragment)
}
}
}
true
}
}
private fun replaceFragment(fragment : Fragment){
if(fragment != null){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
}
}
It happening beacuse your map fragment is "dying" after replacing on new fragment. Try this solution:
First of all you need add in your main activity all required fragments which can be called:
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_view, fragment1)
.add(R.id.fragment_container_view, fragment2)
.add(R.id.fragment_container_view, fragment3)
.commit();
After adding you can change them by calling this code:
getSupportFragmentManager().beginTransaction()
.show(fragment2)
.hide(fragment1)
.commit();
Code was writen on Java but I think there is no problem
How can I display a message at a specific location in a Google Maps Activity on Android without adding a marker? I just want to display a small pop up animation containing a message at specific coordinates.
Are there any GitHub libraries that can perform this specific task?
You can place any View over MapFragment (or MapView) and animate it as you wish with Animations: animate size, position, transparency etc., use various interpolators and merge several animations into one via AnimatorSet. To get screen position of the View for specific LatLng coordinates you can use Projection.toScreenLocation() method.
For example, for "Hello World" TextView with "popup" animation over the map, you can use code like that:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="activities.MainActivity">
<fragment class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/popup_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:visibility="invisible"
android:text="Hello World!"/>
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mGoogleMap;
private SupportMapFragment mMapSupportedFragment;
private TextView mPopUpTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPopUpTextView = (TextView) findViewById(R.id.popup_view);
mMapSupportedFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
mMapSupportedFragment.getMapAsync(MainActivity.this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
// get screen position for latLng
Projection projection = mGoogleMap.getProjection();
Point origin = projection.toScreenLocation(latLng);
// popup TextView
popUpView(origin, mPopUpTextView, 500);
}
});
}
public void popUpView(final Point origin, final View view, long duration){
view.setX(origin.x);
view.setY(origin.y);
view.setVisibility(View.VISIBLE);
ValueAnimator sizeAnimation = ValueAnimator.ofInt(0, view.getMeasuredHeight());
sizeAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
sizeAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = val;
view.setLayoutParams(layoutParams);
}
});
sizeAnimation.setDuration(duration);
ValueAnimator positionAnimation = ValueAnimator.ofInt(0, view.getMeasuredHeight());
positionAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
positionAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
view.setY(origin.y - val);
}
});
positionAnimation.setDuration(duration);
AnimatorSet popUpSet = new AnimatorSet();
popUpSet.play(sizeAnimation).with(positionAnimation);
popUpSet.start();
}
}
Result:
I would suggest you go with marker only.
As marker it self gives some features like animation and all.
You can also customize the marker in different way to show the text and not the image and also you can user the customized text like html text show more decorated text backed by html and css.
This one is simple and great solution as i have also used the same in of my project.
I know that there are a lot of answers to this question already, but I'm still having trouble dealing with this concept.
An answer found here shows:
If you didn't want to use a global variable you could always create a method in your activity to return your string.
public String getMyString(){
return item; }
Then in your current activity you could call:
String myValue = LoginScreen.getMyString();
When I try this method, I am returned an error saying "Non-Static method can not be referenced from a static context". However if I make the method static it says that my variable needs to be static, but I need to update the variable. I'll include my code below.
First Activity -
btnSEARCH.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (editTextSearch.getText().toString() != null) {
SearchQueryTerm = editTextSearch.getText().toString();
editTextSearch.setText("");
}
}
});
public String getMyString(){
return SearchQueryTerm;
}
Second Activity-
String SearchQueryTerm = MainActivity.getMyString();
I truly appreciate any help you can give me in this. Thanks so much!! <3
This is my updated code - however it still crashes :(
Activity 1
public void sendMessageIntent(View view) {
Intent search_intent = new Intent(this, SearchActivity.class);
api_intent.putExtra("my_variable", SearchQueryTerm);
startActivity(search_intent);
}
xml file
<Button
android:id="#+id/button_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="View"
android:onClick="sendMessageIntent"
/>
Activity 2 -
public String SearchQueryTerm
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_variable);
if (extras != null) {
SearchQueryTerm = extras.getString("my_variable");
}
}
Use putExtra method of an object Intent.
In your first activity create for example :
String value = "value";
Intent i = new Intent(getApplicationContext(), MyActivity.class);
i.putExtra("my_variable",value);
startActivity(i);
In your second activity you can retrieve your variable :
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("my_variable");
}
It's the best method to pass a variable between activities.
To show you how it works, i have done this code :
XML of first activity :
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editTextSearch"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/buttonSearch"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
</RelativeLayout>
First Activity :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSEARCH = (Button) findViewById(R.id.buttonSearch);
btnSEARCH.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editTextSearch = (EditText) findViewById(R.id.editTextSearch);
if (editTextSearch.getText().toString() != null) {
String value = "value";
Intent i = new Intent(getApplicationContext(), Main2Activity.class);
i.putExtra("SearchQueryTerm",editTextSearch.getText().toString());
startActivity(i);
editTextSearch.setText("");
}
}
});
}
}
In this first activity we pass the value of the editText in the putExtra method with the key = "SearchQueryTerm".
To retrieve the value of editText do this in your second Activity :
public class Main2Activity extends AppCompatActivity {
String SearchQueryTerm = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle extras = getIntent().getExtras();
if (extras != null) {
SearchQueryTerm = extras.getString("SearchQueryTerm");
}
System.out.println(SearchQueryTerm);
}
}
Now you have the value of your editText in the variable SearchQueryTerm ( Second Activity ).
It works for me, so there is no reason it dont work for you.
Hope it helps.
a good solution is to add a "model" singleton class, where you store data that have to be shared between your activities
example :
public class Model {
private static Model __instance == null;
private Model() {
}
public static Model instance() {
if (__instance == null) {
__instance = new Model();
}
return __instance;
}
private Object mydataToShare = null;
public void setMyDataToShare(Object mydataToShare) {
this.mydataToShare = mydataToShare;
}
public Object getMyDataToShare() {
return mydataToShare;
}
}
to store data :
Model.instance().setMyDataToShare(<value to store>);
to retrieve data :
Object valueToRetrieve = Model.instance().getMyDataToShare();
it allow you to transfer complex data and separate completely logic part from UI
Imagine a number 10, then after user clicks a button it changes to 100. But how to make an efficient transition
10 -> 100,
that will display values like
12, 15, 18, ..., 97, 100 over 1 second.
I've seen something like that in "Cookie clicker" but couldn't find anything about that kind of transition in the source code.
I had an idea of a loop (for number1 < number2, do number1++), it will work fine for small numbers, but if 10 changes to 1 billion, then the loop will probably freeze the whole app.
Second idea is to get added value (100-10=90) and divide by 30 or 60 frames, and add this value with each frame. But what will happen if frame is dropped? - Probably value will not be added. What if user makes double click or the system adds values automatically?
Hope it gives an idea of what kind of number transition I need.
Maybe I overlooked and there is a simple approach? Any help is appreciated.
Hope this little demo using a ValueAnimator will inspire you to find an appropriate solution.
You can specify the duration of the animation (see code) and even adjust the frame-rate by saying mAnimator.setFrameDelay(frameDelay);.
By using animator.isRunning() or animator.isStarted() you can prevent double-click malfunction or other unwanted behaviour while the current animation is runnning.
The Main Activity:
/** ValueAnimator demo */
public class MainActivity extends Activity {
ValueAnimator mAnimator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.textview);
mAnimator = ValueAnimator.ofInt(1, 100).setDuration(1000);
mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
mAnimator.addUpdateListener(new AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(final ValueAnimator animator) {
final Integer value = (Integer) animator.getAnimatedValue();
tv.setText(String.format("%04d", value));
}
});
mAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animator) {
super.onAnimationEnd(animator);
final int endValue = Integer.parseInt((String) tv.getText());
mAnimator.setIntValues(endValue, endValue + 100);
}
});
}
/** Button callback */
public void onClick(final View view) {
if (!mAnimator.isStarted() && !mAnimator.isRunning()) {
mAnimator.start();
}
}
}
Simple demo layout:
<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:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:typeface="monospace"
android:text="0001" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gimme +100"
android:onClick="onClick">
</Button>
Here's another demo (hope this answers your 2. question), which implements different behaviour dependent on single click or double-click on the button. Just experiment with it, you now have the basic building blocks to construct own behavour ...
/** ValueAnimator demo */
public class MainActivity extends Activity {
ValueAnimator mAnimator;
TextView mTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv = (TextView) findViewById(R.id.textview);
mAnimator = ValueAnimator.ofInt(1, 100).setDuration(1000);
mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
mAnimator.addUpdateListener(new AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(final ValueAnimator animator) {
final Integer value = (Integer) animator.getAnimatedValue();
mTv.setText(String.format("%04d", value));
}
});
final Button button = (Button) findViewById(R.id.button);
final GestureDetector gestureDetector = new GestureDetector(this,
new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
performAnimation(100);
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
performAnimation(0);
return true;
}
});
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
/** starts animation */
private void performAnimation(final int offset) {
if (!mAnimator.isStarted() && !mAnimator.isRunning()) {
final int endValue = Integer.parseInt((String) mTv.getText());
mAnimator.setIntValues(endValue + offset, endValue + 100 + offset);
mAnimator.start();
}
}
}
Don't forget to replace your layout file, since the click-attribute of the button has been removed:
<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:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:typeface="monospace"
android:text="0001" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gimme +100" >
</Button>
</LinearLayout>
I guess you can do it by using different threads. Only main thread works with UI so you can divide the interval into small intervals and make a transitions in different threads.After send them to main thread and print. Hope it will help.
I'm new to the java programming language and I'm trying to build an app. The app has to make a list of worked hours that have been saved in an MySQL database. I found an example app, that helped me retrieving the data from the database and putting it in a ListView.
But now we get to my problem. I want to put separators in the listview.
Now, the date of the worked hours is in every item of the ListView. I want the date only above the first item.
I've searched the internet for a way to do this, but it didn't help me.
This it the code that gets the data and puts it in a ListView:
public class AllUrenActivity extends ListActivity {
String url_all_uren;
String ip;
String proid;
String uid = MainScreenActivity.uid;
String datum;
String datum1;
ImageView btntoevoegen;
// Progress Dialog
private ProgressDialog pDialog;
TextView tvDatum;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>>urenList;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_UREN = "uren";
private static final String TAG_TRID = "trid";
private static final String TAG_PROID = "proid";
private static final String TAG_WERKZAAMHEID = "werkzaamheid";
private static final String TAG_TIJD = "tijd";
private static final String TAG_DATUM = "datum";
// products JSONArray
JSONArray uren = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_uren);
SharedPreferences settings = getSharedPreferences("databaseIP", 0);
ip = settings.getString("ip", "").toString();
url_all_uren = ("http://"+ip+"/android_connect/get_all_uren.php");
// Hashmap for ListView
urenList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllUren().execute();
// Get listview
ListView lv = getListView();
// on selecting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String proid = ((TextView) view.findViewById(R.id.tvProid)).getText()
.toString();
String werkzaamheid = ((TextView) view.findViewById(R.id.tvWerkzaamheid)).getText()
.toString();
String trid = ((TextView) view.findViewById(R.id.tvTrid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
AllProjectsActivity.class);
// sending pid to next activity
in.putExtra(TAG_PROID, proid);
in.putExtra(TAG_TRID, trid);
in.putExtra(TAG_WERKZAAMHEID, werkzaamheid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllUren extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllUrenActivity.this);
pDialog.setMessage("Uren laden...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uid", uid));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_uren, "GET", params);
// Check your log cat for JSON reponse
Log.d("Uren: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
uren = json.getJSONArray(TAG_UREN);
// looping through All Products
for (int i = 0; i < uren.length(); i++) {
JSONObject c = uren.getJSONObject(i);
// Storing each json item in variable
String trid = c.getString(TAG_TRID);
String proid = c.getString(TAG_PROID);
String werkzaamheid = c.getString(TAG_WERKZAAMHEID);
String datum = c.getString(TAG_DATUM);
String tijd = c.getString(TAG_TIJD);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_TRID, trid);
map.put(TAG_WERKZAAMHEID, werkzaamheid);
map.put(TAG_PROID, proid);
map.put(TAG_TIJD, tijd);
map.put(TAG_DATUM, datum);
// adding HashList to ArrayList
urenList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllUrenActivity.this, urenList,
R.layout.list_uren, new String[] { TAG_TRID, TAG_PROID, TAG_WERKZAAMHEID, TAG_TIJD, TAG_DATUM},
new int[] { R.id.tvTrid, R.id.tvProid, R.id.tvWerkzaamheid, R.id.tvTijd, R.id.tvDatum });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
list_uren.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvDatum"
android:layout_width="269dp"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="17sp"
android:visibility="visible" />
<ImageView
android:id="#+id/toevoegen"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="#android:drawable/ic_menu_add" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvTijd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:text="8:00-12:00"
android:textSize="17sp"
android:textStyle="bold"
android:visibility="visible" />
<TextView
android:id="#+id/tvProid"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:paddingLeft="6dip"
android:text="Project"
android:textSize="17sp" />
</LinearLayout>
<TextView
android:id="#+id/tvWerkzaamheid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:text="Werkzaamheid"
android:textSize="17sp" />
</LinearLayout>
all_uren.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
What is the best way to get the date separators? Thank you.
Add this at the end of list_uren.xml before closing the LinearLayout
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#666666" />
You can change the background to whatever you want.