MapView inside RecyclerView only displaying map on clicks - android

This has to be the most absurd thing ever.
I have a RecyclerView with repeated custom items. Inside these items, there are a few textfields, buttons and a single MapView.
The issue is that when the list loads, the MapView only displays the Google logo and no other tile or detail (or marker). However, when I tap once on the map, it shows the marker I added. On the next tap, it loads a pixellated map. On another tap, it loads a better quality map. On further clicks it adds the text labels for nearby locations. LatLngBounds are also not working but that's a secondary problem.
Why is this happening?
My code is as follows:
JobAdapter.java
public class JobAdapter extends RecyclerView.Adapter<JobAdapter.ViewHolder>
{
private Context context;
private static List<Job> jobList;
private HashSet<MapView> mapViews = new HashSet<>();
private GoogleMap googleMap;
public JobAdapter(Context con, List<Job> jobs)
{
context = con;
jobList = jobs;
}
#Override
public int getItemCount()
{
return jobList.size();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.listitem_booking, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
mapViews.add(viewHolder.mapView);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
Job job = jobList.get(position);
holder.mapView.setClickable(false);
if(job.getJobType().equalsIgnoreCase("Now"))
{
holder.pickup.setBackgroundColor(ContextCompat.getColor(context, R.color.lightRed));
}
holder.pickup.setText(job.getPickupAddress());
if(job.getDestinationAddress() != null && !job.getDestinationAddress().equalsIgnoreCase(""))
{
holder.destination.setText(job.getDestinationAddress());
}
else
{
holder.destination.setVisibility(View.GONE);
}
holder.person.setText(job.getContact());
holder.datetime.setText(job.getDate() + " at " + job.getTime());
}
class ViewHolder extends RecyclerView.ViewHolder implements /*View.OnClickListener,*/ OnMapReadyCallback
{
#BindView(R.id.pickup)
TextView pickup;
#BindView(R.id.destination)
TextView destination;
#BindView(R.id.person)
TextView person;
#BindView(R.id.datetime)
TextView datetime;
#BindView(R.id.map_listitem)
MapView mapView;
#BindView(R.id.acceptJob)
Button acceptJob;
#BindView(R.id.declineJob)
Button declineJob;
#BindView(R.id.buttonLayout)
LinearLayout buttonLayout;
private ViewHolder(View itemView)
{
super(itemView);
ButterKnife.bind(this, itemView);
// itemView.setOnClickListener(this);
mapView.onCreate(null);
mapView.getMapAsync(this);
}
private void addMarkers(Job job)
{
googleMap.clear();
boolean hasDestination = true;
String[] destinationLatlng = null;
LatLng destination = null;
if(job.getDestinationAddress() == null || job.getDestinationAddress().equalsIgnoreCase(""))
{
hasDestination = false;
}
else
{
destinationLatlng = job.getDestinationLatLong().split(",");
destination = new LatLng(Double.valueOf(destinationLatlng[0]), Double.parseDouble(destinationLatlng[1]));
}
final String[] pickupLatlng = job.getPickupLatLong().split(",");
final LatLng pickup = new LatLng(Double.valueOf(pickupLatlng[0]), Double.parseDouble(pickupLatlng[1]));
if(hasDestination)
{
googleMap.addMarker(new MarkerOptions()
.position(pickup)
.title(job.getPickupAddress()));
googleMap.addMarker(new MarkerOptions()
.position(destination)
.title(job.getDestinationAddress()));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pickup);
builder.include(destination);
LatLngBounds bounds = builder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 5);
googleMap.animateCamera(cameraUpdate);
}
else
{
googleMap.addMarker(new MarkerOptions()
.position(pickup)
.title(job.getPickupAddress()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(pickup, 15);
googleMap.animateCamera(cameraUpdate);
}
}
/*#Override
public void onClick(View view)
{
final Job job = jobList.get(getAdapterPosition());
}*/
#Override
public void onMapReady(GoogleMap gMap)
{
googleMap = gMap;
addMarkers(jobList.get(getAdapterPosition()));
}
}
}
listitem_booking
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/tools"
android:orientation="vertical"
app:cardElevation="2dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/pickup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:textSize="16sp"
android:text="Complex"
android:background="#color/lessLightGreen"
android:gravity="center_vertical"
android:drawableStart="#drawable/google_maps"
android:drawablePadding="2dp"
android:textColor="#color/colorPrimaryText"/>
<TextView
android:id="#+id/destination"
android:layout_below="#id/pickup"
android:text="Golra"
android:visibility="visible"
android:drawableStart="#drawable/directions"
android:drawablePadding="2dp"
style="#style/listitem_secondary_text"/>
<TextView
android:id="#+id/person"
android:drawablePadding="2dp"
android:layout_below="#id/destination"
android:text="Asfandyar Khan"
android:drawableStart="#drawable/account"
style="#style/listitem_secondary_text"/>
<TextView
android:id="#+id/datetime"
android:layout_below="#id/person"
android:text="7th April 2017 at 9:00am"
android:drawableStart="#drawable/time"
style="#style/listitem_secondary_text"/>
<com.google.android.gms.maps.MapView
android:id="#+id/map_listitem"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_marginTop="2dp"
android:layout_below="#id/datetime"
map:liteMode="true"
android:padding="10dp"/>
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/map_listitem"
android:gravity="end"
android:layout_margin="4dp">
<Button
android:backgroundTint="#color/colorPrimary"
android:textColor="#android:color/white"
android:id="#+id/acceptJob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept"/>
<Button
android:backgroundTint="#color/darkRed"
android:textColor="#android:color/white"
android:id="#+id/declineJob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Decline"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
I've tried various things but nothing seems to be working.

Try adding onResume, like this:
mapView.onCreate(null);
mapView.getMapAsync(this);
mapView.onResume();
Edit:
I've just noticed you are using
map:liteMode="true"
Try removing it (at least to test) or adding the following to the xml:
map:cameraZoom="15"
map:mapType="normal"
Either way, I think the onResume is needed.
When I use liteMode it sometimes takes a few seconds (about 5) for the map to show after the logo.

There might another issue in your code. The "map" should be:
xmlns:map="http://schemas.android.com/apk/res-auto"
but not:
xmlns:map="http://schemas.android.com/tools"

Related

Unable to display MapView in LiteMode inside RecyclerView

I have a list of saved locations coming from the server that I want to display inside a RecyclerView with a map snapshot of the location.
I coded it according to code samples provided for the same by Google but unfortunately the list wouldn't show on the UI.
I am attaching the code to my RecyclerView Adapter & item XML.
public class SavedLocationAdapter extends RecyclerView.Adapter<SavedLocationAdapter.ViewHolder> {
private Context mContext;
private ArrayList<SavedLocationDetails> savedLocationList;
private LoadMoreLocationsInterface loadMoreLocationsInterface;
public SavedLocationAdapter(Context context, LoadMoreLocationsInterface loadMoreLocationsInterface) {
//super(DIFF_CALLBACK);
super();
mContext = context;
this.savedLocationList = new ArrayList<>();
this.loadMoreLocationsInterface = loadMoreLocationsInterface;
}
public void updateList(List<SavedLocationDetails> savedLocationList) {
this.savedLocationList.addAll(savedLocationList);
notifyDataSetChanged();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_saved_location, parent, false));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
SavedLocationDetails savedLocationDetails = savedLocationList.get(position);
if (savedLocationDetails != null)
holder.bindView(savedLocationDetails);
if (position == getItemCount()-1)
loadMoreLocationsInterface.loadMoreLocations(savedLocationDetails.getLid());
}
#Override
public int getItemCount() {
return savedLocationList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
MapView mapView;
TextView locationType;
TextView friendlyName;
TextView fullAddress;
public GoogleMap map;
View layout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
layout = itemView;
locationType = layout.findViewById(R.id.savedLocationTypeTV);
fullAddress = layout.findViewById(R.id.savedLocationAddressTV);
friendlyName = layout.findViewById(R.id.friendlyNameTV);
mapView = layout.findViewById(R.id.savedLocationMap);
if(mapView != null) {
mapView.onCreate(null);
mapView.onResume();
mapView.onPause();
mapView.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(mContext);
map = googleMap;
setMapLocation();
}
private void setMapLocation() {
if (map == null)
return;
SavedLocationDetails savedLocationDetails = (SavedLocationDetails) mapView.getTag();
if (savedLocationDetails == null)
return;
LatLng locationLatLng = new LatLng(savedLocationDetails.getLat(), savedLocationDetails.getLng());
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.location_marker_black_solid);
MarkerOptions markerOptions = (new MarkerOptions()).position(locationLatLng).icon(icon);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng, 18f));
map.addMarker(markerOptions);
}
public void bindView(SavedLocationDetails savedLocationDetails) {
layout.setTag(this);
mapView.setTag(savedLocationDetails);
setMapLocation();
if((savedLocationDetails.getLtp() != null) && (savedLocationDetails.getLtp() == 3)) {
locationType.setVisibility(GONE);
friendlyName.setText(savedLocationDetails.getFn());
friendlyName.setVisibility(View.VISIBLE);
} else {
friendlyName.setVisibility(GONE);
locationType.setVisibility(View.VISIBLE);
locationType.setText("Home");
}
fullAddress.setText(savedLocationDetails.getAdd());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/appBar"
app:cardElevation="3dp"
app:cardCornerRadius="30dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.gms.maps.MapView
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/savedLocationMap"
android:layout_width="match_parent"
android:layout_height="225dp"
app:mapType="normal"
map:liteMode="true"
map:mapType = "normal" />
<RelativeLayout
android:id="#+id/savedLocationDataRL"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignBottom="#+id/savedLocationMap"
android:background="#color/white"
android:visibility="gone">
<TextView
android:id="#+id/savedLocationTypeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Work"
android:textColor="#color/quantum_black_100"
android:textStyle="bold"
android:textSize="15sp"/>
<TextView
android:id="#+id/friendlyNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Friendly Name"
android:textColor="#color/quantum_black_100"
android:textSize="15sp"
android:textStyle="bold"
android:visibility="gone"/>
<TextView
android:id="#+id/savedLocationAddressTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/savedLocationTypeTV"
android:layout_marginTop="3dp"
android:layout_marginStart="10dp"
android:text="Full Address"
android:textColor="#color/quantum_black_100"
android:textSize="13sp"/>
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>

Trying to set a click listener to a recyclerView

I'm new to Android programming and trying to set a click listener to open another activity. The list loads fine, but when I click the items, nothing happens. This is my adapter:
public interface OnItemClickListener {
void onItemClick(Product product);
}
private List<Product> productList;
private double currentLatitude, currentLongitude;
private Context context;
private final OnItemClickListener listener;
public AdapterForProducts(List<Product> productList, OnItemClickListener listener, double currentLatitude, double longitude, Context context){
this.currentLatitude = currentLatitude;
this.currentLongitude = longitude;
this.productList = productList;
this.context = context;
this.listener = listener;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
ViewHolderForProducts holder = new ViewHolderForProducts(view);
return holder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
ViewHolderForProducts holder = (ViewHolderForProducts) viewHolder;
Product product = productList.get(position);
String distanceText = "Distancia: " + round(distance(product.getLatitude(), product.getLongitude())) + " meters";
holder.bind(productList.get(position), listener, distanceText);
}
#Override
public int getItemCount() {
return productList.size();
}
the viewHolder:
public ViewHolderForProducts(View view){
super(view);
cardView = (CardView) itemView.findViewById(R.id.cardView);
name = (TextView) view.findViewById(R.id.product_name);
value = (TextView) view.findViewById(R.id.product_value);
distance = (TextView) view.findViewById(R.id.product_distance);
}
public void bind(final Product product, final AdapterForProducts.OnItemClickListener listener,
String distanceText) {
name.setText(product.getName());
value.setText("Preço: " + product.getValue());
distance.setText(distanceText);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(product);
}
});
}
And I'm calling it in the activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_and_show_product_list);
Bundle extras = getIntent().getExtras();
if (extras != null) {
latitude = extras.getDouble("latitude");
longitude = extras.getDouble("longitude");
}
query = "";
query_field = (EditText) findViewById(R.id.query_field);
searchButton = (Button) findViewById(R.id.searchProductButton);
requestQueue = Volley.newRequestQueue(getApplicationContext());
recyclerView = (RecyclerView) findViewById(R.id.recycler);
productList = new ArrayList<>();
fillProductListByName();
adapterForProducts = new AdapterForProducts(productList, new AdapterForProducts.OnItemClickListener() {
#Override
public void onItemClick(Product product) {
showProductOnMap(product);
}
}, latitude, longitude, getApplication());
recyclerView.setAdapter(adapterForProducts);
adapterForProducts.notifyDataSetChanged();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
query = query_field.getText().toString();
productList.clear();
fillProductListByName();
adapterForProducts.notifyDataSetChanged();
}
});
}
This is the list item xml:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="#dimen/activity_vertical_margin"
app:cardElevation="#dimen/activity_vertical_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:clickable="true"
android:padding="16dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:text="Product Name"
android:textSize="14sp" />
<TextView
android:id="#+id/product_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_toRightOf="#+id/product_name"
android:text="Price: " />
<TextView
android:id="#+id/product_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="51dp"
android:layout_marginStart="51dp"
android:layout_toEndOf="#+id/product_value"
android:layout_toRightOf="#+id/product_value"
android:text="Distance:" />
</RelativeLayout>
I got it working. Apparently, this implementation doesn't work if there is some other clickable object inside the list item, as was the case with:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:clickable="true"
android:padding="16dp">
All I did was to erase the android:clickable="true" from the RelativeLayout
When you set the attr android:clickable="true" to the parent layout, its click method
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(product);
}
});
will only be called if its childs doesn't have the clickable = true or have a OnClickListener set.
Actually, you don't need all these android:clickable="true".
When you set android:clickable="true" you are setting a empty ClickListener to this view.
The click event is passed to the lowest view in the layout hierarchy with a ClickListener implemented, even with empty body. This is the cause of your bug, and its because of this that you have to remove the android:clickable="true".
Removing the android:clickable="true", removes the empty ClickListener, and now the lowest view in the layout hierarchy is the CardView with this ClickListener:
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(product);
}
});
So, this is the ClickListener that will be called.

CardView not displaying within RecyclerView

Before you ask, yes I know there are many questions that are very similar to this and I have tried most of them to no avail. My problem is that a CardView is not displaying within a RecyclerView. The items whithin it are displaying but not the card itself.
Without further or do, here's my code:
Adapter:
Integer count = 0;
Boolean isStart = true;
String datag = "";
String typeg = "";
Integer LastItemType=0; //0=None 1=Text 2=Image
ViewHolder a;
#Override
public EntryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
a= createholder(parent, viewType);
return(a);
}
public ViewHolder createholder(ViewGroup parent, int viewtype) {
if (typeg.equals("image")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
CardView card = (CardView) root.findViewById(R.id.card_view);
ImageView image = (ImageView) root.findViewById(R.id.Img);
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));
if (LastItemType == 2) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Img));
}
if (LastItemType == 1) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Txt));
}
ViewHolder vh = new ViewHolder(image, card);
LastItemType = 2;
return vh;
} else {
if (typeg.equals("text")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
CardView card = (CardView) root.findViewById(R.id.card_view);
TextView image = (TextView) root.findViewById(R.id.Txt);
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));
if (LastItemType == 1) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Txt));
}
if (LastItemType == 2) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Img));
}
ViewHolder vh = new ViewHolder(image, card);
LastItemType = 1;
return vh;
}
return null; //TODO: REMOVE!
}
}
#Override
public void onBindViewHolder(EntryAdapter.ViewHolder holder, int position) {
// Deal with data
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgg;
public TextView txtg;
public CardView cardg;
public ViewHolder(ImageView image, CardView card) {
super(image);
imgg = image;
cardg = card;
}
public ViewHolder(TextView text, CardView card) {
super(text);
txtg = text;
cardg = card;
}
}
#Override
public int getItemCount() {
return count;
}
public void refresh(String data, String type, ViewGroup parent) {
isStart = false;
datag = data;
typeg = type;
count++;
createholder(parent, -100);
}
listitems.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:id="#+id/Img"
android:scaleType="center"
android:maxHeight="100dp"
android:maxWidth="150dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.25"
android:id="#+id/Txt"
android:layout_gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Thanks in advance!
Check out my code, i use it for each of my app after modifying it slightly. You can also use adapter if you have more than one RecyclerViews by changing it's type field and layout and views accordingly. It contains a CoordinatorLayout that has CollapsingLayout with ImageView in it. It contains many layouts for Material Design, i hope it helps.
Activity contains RecyclerView and CardView, i removed things like database, Floating action buttons that you may not need and may make it more difficult to understand. If you need the whole class contact me.
public class MeasureListActivity extends AppCompatActivity
implements MeasureListAdapter.OnRecyclerViewMeasureClickListener {
// Views
private RecyclerView mRecyclerView;
private MeasureListAdapter mAdapter;
private Toolbar toolbar;
// List that keeps values displayed on the screen
private List<Measure> listMeasure;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prev_measures);
setViews();
}
private void setViews() {
/*
* Set toolbar and arrow icon to return back
*/
toolbar = (Toolbar) findViewById(R.id.toolbarPrevMeasure);
setSupportActionBar(toolbar);
// Enable home button for API < 14
getSupportActionBar().setHomeButtonEnabled(true);
// Enable home button for API >= 14
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*
* RecylerView to display items as a list
*/
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerViewPrevMeasure);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new MeasureListAdapter(this, listMeasure, 0);
// Attach an instance of OnRecyclerViewMeasureClickListener that
// implements itemClicked()
mAdapter.setClickListener(this);
mRecyclerView.setAdapter(mAdapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void itemMeasureClicked(View view, int position) {
}
}
public class MeasureListAdapter extends RecyclerView.Adapter<MeasureListAdapter.MyViewHolder> {
private LayoutInflater inflater;
private List<Measure> data = Collections.emptyList();
// This is for delegating event from adapter's onClick() method to
// NavigationDrawerFragment
private OnRecyclerViewMeasureClickListener recyclerClickListener;
private DecimalFormat decimalFormat;
private int type = 0;
private Context mContext;
public MeasureListAdapter(Context context, List<Measure> data, int type) {
mContext = context;
inflater = LayoutInflater.from(context);
this.data = data;
decimalFormat = new DecimalFormat("###.#");
this.type = type;
}
#Override
public int getItemCount() {
return data.size();
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
Measure measure = data.get(position);
String title = measure.getTitle();
String note = measure.getNote();
String date = measure.getFormattedDate();
double angle = measure.getAnglePhoto();
// Compass
double azimuth = measure.getAngleAzimuth();
double pitch = measure.getAnglePitch();
double roll = measure.getAngleRoll();
String bearing = measure.getBearing();
holder.tvTitle.setText(title);
holder.tvNote.setText(note);
holder.tvAngle.setText(
mContext.getString(R.string.angle) + ": " + decimalFormat.format(angle) + ConstantsApp.DEGREE_ICON);
// Compass
holder.tvAzimuth.setText(
mContext.getString(R.string.azimuth) + ": " + decimalFormat.format(azimuth) + ConstantsApp.DEGREE_ICON);
holder.tvPitch.setText(
mContext.getString(R.string.pitch) + ": " + decimalFormat.format(pitch) + ConstantsApp.DEGREE_ICON);
holder.tvRoll.setText(
mContext.getString(R.string.roll) + ": " + decimalFormat.format(roll) + ConstantsApp.DEGREE_ICON);
holder.tvBearing.setText(mContext.getString(R.string.bearing) + " " + bearing);
holder.tvDate.setText("Date" + ": " + measure.getFormattedDate());
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
View view = null;
view = inflater.inflate(R.layout.custom_row_angle_photo, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
/**
* get an instance of OnRecyclerViewClickListener interface
*
* #param OnRecyclerViewMeasureClickListener
* callback that is used by adapter to invoke the method of the
* class implements the OnRecyclerViewClickListener interface
*/
public void setClickListener(OnRecyclerViewMeasureClickListener recyclerClickListener) {
this.recyclerClickListener = recyclerClickListener;
}
public void delete(int position) {
data.remove(position);
notifyItemRemoved(position);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// Views
private TextView tvTitle, tvNote, tvAngle, tvAzimuth, tvPitch, tvRoll, tvBearing, tvDate;
public MyViewHolder(View itemView) {
super(itemView);
tvTitle = (TextView) itemView.findViewById(R.id.tvDisplayTitle);
tvAngle = (TextView) itemView.findViewById(R.id.tvDisplayAngle);
// Compass
tvAzimuth = (TextView) itemView.findViewById(R.id.tvDisplayAzimuth);
tvPitch = (TextView) itemView.findViewById(R.id.tvDisplayPitch);
tvRoll = (TextView) itemView.findViewById(R.id.tvDisplayRoll);
tvBearing = (TextView) itemView.findViewById(R.id.tvDisplayBearing);
tvNote = (TextView) itemView.findViewById(R.id.tvDisplayNote);
tvDate = (TextView) itemView.findViewById(R.id.tvDisplayDate);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (recyclerClickListener != null) {
recyclerClickListener.itemMeasureClicked(v, getLayoutPosition());
}
}
}
/**
* RecyclerViewClickListener interface helps user to set a clickListener to
* the RecyclerView. By setting this listener, any item of Recycler View can
* respond to any interaction.
*
* #author Fatih
*
*/
public interface OnRecyclerViewMeasureClickListener {
/**
* This is a callback method that be overriden by the class that
* implements this interface
*/
public void itemMeasureClicked(View view, int position);
}
}
Measure class only contains setters and getters for int and String values so i don't put it.
Layout for Activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutMainMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="#android:color/background_light"
android:paddingBottom="50dp" >
<android.support.design.widget.AppBarLayout
android:id="#+id/appbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp" >
<ImageView
android:id="#+id/backgroundPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="#drawable/bg_material" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#eeeeee"
android:paddingTop="12dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="80dp"
app:layout_anchor="#id/appbarPrevMeasure"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_save_white_36dp"
android:tint="#android:color/white"
app:backgroundTint="#FFA500" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabClearDB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_horizontal_margin"
app:layout_anchor="#id/appbarPrevMeasure"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_delete_white_36dp"
android:tint="#android:color/white"
app:backgroundTint="#D463C3" />
</android.support.design.widget.CoordinatorLayout>
Layout for adapter rows with CardView
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#eeeeee"
cardview:cardCornerRadius="5dp"
cardview:cardElevation="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="#+id/tvDisplayTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textColor="#FF0000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvDisplayAngle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="18sp" />
<TextView
android:id="#+id/tvDisplayAzimuth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayBearing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvDisplayPitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="12dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="12dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
</LinearLayout>
<TextView
android:id="#+id/tvDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="#string/date_"
android:textColor="#9EA9AD"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text=""
android:textColor="#828A8C"
android:textSize="14sp" />
</LinearLayout>
How it looks
Does your RecyclerView show ImageView or TextView only?
If yes, because your ViewHolder Constructor is wrong. You have to call super(itemView) in your ViewHolder's Constructor, itemView is the view which is displayed as a row in RecyclerView. To fix your issue, I think you should change your code as below:
public ViewHolder createholder(ViewGroup parent, int viewtype) {
if (typeg.equals("image") || typeg.equals("text")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
return new ViewHolder(root, typeg.equals("text"));
}
//FIXME: As my expericence you should NOT return null for ViewHolder. You have to sure the typeg is one of "image" or "text". I think you should change typeg to Boolean variable to not return null ViewHolder.
return null;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgg;
public TextView txtg;
public CardView cardg;
public ViewHolder(View itemView, boolean isTypeText) {
super(itemView);
imgg = (ImageView) itemView.findViewById(R.id.Img);
cardg = (CardView) itemView.findViewById(R.id.card_view);
txtg = (TextView) itemView.findViewById(R.id.Txt);
imgg.setVisibility(isTypeText ? View.GONE : View.VISIBLE);
txtg.setVisibility(isTypeText ? View.VISIBLE : View.GONE);
}
}
Here is the proper Documentation Read and And Follow it.
You must be Doing Something Wrong in Gradle.!
so, the issue was Your XML was not showing CardView in it here we go.!
Add following Dependencies.!
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
Here is Your XML is Working Perfectly Fine.!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:id="#+id/Img"
android:scaleType="center"
android:maxHeight="100dp"
android:maxWidth="150dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.25"
android:id="#+id/Txt"
android:layout_gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Now Clean and Rebuild Your Project.!
ok try this its Working Fine.now.!
Is it because you are removing card_view like so?
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));

Display image in infowindow from URL Google Maps Android API

I have a map in a fragment in a navigation drawer. Now the code for the map is found in the MainActivity.java of the navigation drawer. I have a for loop inside onMapReady which pin markers on my map. Now each iteration of the for loop takes retrieved data from Firebase, to be able to pin the markers. The retrieved data also contains URLs for images and I need to use those URLs to display an image in the infowindow of each marker. I've tried to understand the other solutions provided but I haven't got any idea how to implement this.
this is my code so far my code:
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
...
for (infoToStore details : info) {
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
.title(details.getName())
.snippet(details.getDesc()));
}
}
EDIT
I've tried to implement it as follows but the infowindow is blank; not showing the TextViews nor the ImageView.
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
for (final infoToStore details : info) {
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
.title(details.getName())
.snippet(details.getDesc()));
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.popup, null);
TextView name = (TextView) v.findViewById(R.id.name);
TextView desc = (TextView) v.findViewById(R.id.desc);
ImageView image = (ImageView) v.findViewById(R.id.image);
name.setText(marker.getTitle());
desc.setText(marker.getSnippet());
Picasso.with(getApplicationContext())
.load(URLString)
.into(image);
return v;
}
});
}
}
here's the popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/image"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/desc"/>
</LinearLayout>
Try this:
Create a Global Marker Variable
private Marker marker;
Now in your onMapReady() call
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
Create your CustomInfoWindowAdapter class and add the following code..
private class CustomInfoWindowAdapter implements InfoWindowAdapter {
private View view;
public CustomInfoWindowAdapter() {
view = getLayoutInflater().inflate(R.layout.popup,null);
}
#Override
public View getInfoContents(Marker marker) {
if (MainActivity.this.marker != null
&& MainActivity.this.marker.isInfoWindowShown()) {
MainActivity.this.marker.hideInfoWindow();
MainActivity.this.marker.showInfoWindow();
}
return null;
}
#Override
public View getInfoWindow(final Marker marker) {
MainActivity.this.marker = marker;
TextView name = (TextView) view.findViewById(R.id.name);
TextView desc = (TextView) view.findViewById(R.id.desc);
ImageView image = (ImageView) view.findViewById(R.id.image);
Picasso.with(getApplicationContext())
.load(URLString)
.error(R.mipmap.ic_launcher) // will be displayed if the image cannot be loaded
.into(image);
final String title = marker.getTitle();
if (title != null) {
name.setText(title);
} else {
name.setText("Default");
}
final String snippet = marker.getSnippet();
if (snippet != null) {
desc.setText(snippet);
} else {
desc.setText("Deafult");
}
//getInfoContents(marker);
return view;
}
}
your imageview is very large...which blocks the textView: try this layout in your popup
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Sample Text"
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="#+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Sample Text2"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
And for different image you need a arraylist of imageURL for different markers in the map.

Hide RelativeLayout when I touch inside a map

In my android app, I have a google maps v2 inside a fragment with marker of places. When I touch in a marker, it displays a RelativeLayout with the name of the marker. However, I would like that when I touch anywhere in the map, this RelativeLayout is hidden.
My code is this:
fragment_mapa.xml
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:gravity="center" />
<RelativeLayout
android:id="#+id/sliding_up"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#fff"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:clickable="true"
android:focusable="false"
android:animateLayoutChanges="true"
android:visibility="invisible" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp"
android:gravity="center_vertical"
android:paddingLeft="10dp"/>
</RelativeLayout>
Code where it creates the markers and onClick method to display the RelativeLayout
public void addItemsToMap() {
appState.mapa.clear();
if (appState.lista.isEmpty()) {
appState.readPlaces(5000, 0, appState.idCategoria);
}
appState.mapa.setOnMarkerClickListener(this);
appState.mapa.setOnInfoWindowClickListener(getInfoWindowClickListener());
LatLng miPosicion = new LatLng(obtLatitud, obtLongitud);
appState.mapa.addMarker(new MarkerOptions()
.position(miPosicion)
.title("Mi posición")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)));
for (int i = 0; i < appState.lista.size(); i++) {
LatLng posItem = new LatLng(appState.lista.get(i).latitud,appState.lista.get(i).longitud);
appState.mapa.addMarker(new MarkerOptions()
.position(posItem)
.title(appState.lista.get(i).nombre)
.snippet(appState.lista.get(i).descripcion)
/*.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))*/);
Log.v("MAPA", "Marker " + i + ": " + appState.lista.get(i).nombre);
}
}
#Override
public boolean onMarkerClick(final Marker marker) {
if(marker != null) {
//marker.showInfoWindow();
RelativeLayout slideLayout;
slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
slideLayout.setVisibility(View.VISIBLE);
Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
slideLayout.startAnimation(slide);
TextView t;
t = (TextView) findViewById(R.id.name);
t.setText(marker.getTitle());
return true;
} else {
RelativeLayout slideLayout;
slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
slideLayout.setVisibility(View.INVISIBLE);
return false;
}
}
// try this :
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng arg0) {
}
});

Categories

Resources