Change color of response text on my snippet - on s

2019-08-07 06:48发布

I am working on android with google map marker, look at the images below

Green marker shows user has responded yes i.e. Y as showen

I want to change the color of Y & N according to the response, like if it is Y I want the color should be GREEN & if it is N then the color should be RED.

Right now both Y & N are having RED color because I have written following code:

<TextView
            android:id="@+id/response"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/response_lbl"
            android:textAllCaps="true"
            android:textColor="@color/event_response_s"
            android:textSize="18sp" />

Where event_response_s = RED.

I tried this by removing the android:textColor="@color/event_response_s" xml code.

    if (marker.getSnippet().equals(currentEvent.getButton_one())) { 

       response.setTextColor(context.getResources().getColor(R.color.green)); 
    } 
    else response.setTextColor(context.getResources().getColor(R.color.red));

Value of marker.getSnippet() is either Y or N, depends on the button user will press. I want to check this with currentEvent.getButton_one();, which gives value of 1st button. Problem here is I am getting correct value of currentEvent.getButton_one() in other class where it is declared but not in other class i.e. CustomInfoWindow. Final condition should check like this: if Y == Y ---> GREEN else RED.

Please let me know if you need more information or to know what else I have tried.

In Event.java:

Intent i = new Intent(getApplicationContext(), MyActivity.class);  
i.putExtra("response1", currentEvent.getButton_one());

In MainActivity.java:

String response_1 = getIntent().getStringExtra("response1");

Now from MyActivity.java I have to use response_1 in CustomInfoWinidow.java for comparison:

if (marker.getSnippet().equals(response_1)) { 
    response.setTextColor(context.getResources().getColor(R.color.green)); 
} else  
   response.setTextColor(context.getResources().getColor(R.color.red));
}

2条回答
贪生不怕死
2楼-- · 2019-08-07 07:23

Edit: Try passing in response_1 into the constructor of CustomInfoWindow

public class CustomInfoWindow implements InfoWindowAdapter { 

public static final String DEVICE_ACTIVE = "Device Active"; 

private String response_1; //Add this as a member variable

//.......


public CustomInfoWindow(Context context, String response_1_custom) {
  this.response_1 = response_1_custom; //set the value here 
  this.context = context; 
  inflater = (LayoutInflater) LayoutInflater.from(context); 
} 

public View getInfoContents(Marker marker) { 

//........


//Now this should work:
try { 
if (marker.getSnippet().equals(response_1)) { 
response.setTextColor(context.getResources().getColor(R.color.green)); 
} else response.setTextColor(context.getResources().getColor(R.color.red)); 
}catch(Exception e){ 

} 
if (marker.getSnippet().equals(DEVICE_ACTIVE)) { 
response.setTextColor(context.getResources().getColor(R.color.red)); 
} 
response.setText(marker.getSnippet()); 
return view; 
} 
//.....................

Then in MainActivity.java, pass in response_1 when you create the CustomInfoWindow:

if (user_id.equalsIgnoreCase(userid)) { 
  map.setInfoWindowAdapter(new CustomInfoWindow(getBaseContext(), response_1)); //added response_1 to constructor parameter
  map.moveCamera(CameraUpdateFactory.newLatLngZoom(PERTH, 12)); 
} 

Original answer:

I got it to work using a combination of a InfoWindowAdapter and a MarkerClickListener.

Basically, you need to set the text color of the snippet in the InfoWindowAdapter, and set the color of the Marker in the MarkerClickListener

      mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {

                Log.d("marker", "marker click");

                String s =  marker.getSnippet();
                Log.d("marker", "snippet: " + s);

                if (s.equals("Y")){
                    marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));

                }
                else if (s.equals("N")){
                   marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
                }

                return false;

            }

        });



        mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker arg0) {

                View v = getLayoutInflater().inflate(R.layout.customlayout, null);

                //set other fields.....

                //Snippet:
                TextView tSnippet = (TextView) v.findViewById(R.id.snippet);

                if (arg0.getSnippet().equals("N")){
                    tSnippet.setTextColor(Color.RED);

                }
                else if (arg0.getSnippet().equals("Y")){
                    tSnippet.setTextColor(Color.GREEN);
                }

                return v;

            }
        });

Result:

Y

N

查看更多
该账号已被封号
3楼-- · 2019-08-07 07:25

the condition:

    if (marker.getSnippet().equals("Y")){
            response.setTextColor(context.getResources().getColor(R.color.green));
        }
if (marker.getSnippet().equals("N")) {
            response.setTextColor(context.getResources().getColor(R.color.red));
        }

ain't right it must be:

    if (marker.getSnippet().equals("Responded Y")){
            response.setTextColor(R.color.green);
        }
if (marker.getSnippet().equals("Responded N")) {
            response.setTextColor(R.color.red));
        }

and make sure that the event will only occurs when the marker is clicked. hope this will help you

查看更多
登录 后发表回答