Android custom InfoWindow Google Map v2 onclick button? - android

Hi i want to add marker info-window to my Google map in android application.So far i have add custom info window and it has Image and button.My target after click the marker then show the info-window and then after user click the button in window dismiss the info-window and do some activity after click the button.But i cant identify when user click button in marker info-window.This is my code. Plz tell me where is the wrong in my code.
googleMap.setInfoWindowAdapter(new 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) {
final View infoview = getLayoutInflater().inflate(R.layout.info_window,
null);
LatLng latLng = arg0.getPosition();
//tvLat.setText("Latitude:" + latLng.latitude);
//tvLng.setText("Longitude:" + latLng.longitude);
Button pickMe = (Button)infoview.findViewById(R.id.pickme);
pickMe.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Requested Send", Toast.LENGTH_SHORT).show();
}});
//String reverceGeoCode=new GetLocationAddress().getAddress(String.valueOf(latLng.latitude), String.valueOf(latLng.longitude));
//Toast.makeText(getApplicationContext(), "Rev :"+reverceGeoCode, Toast.LENGTH_LONG).show();
return infoview;
}
});
And This is my customInfo-window xml file
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sajith Vijesekara"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:src="#drawable/driver" />
<Button
android:id="#+id/pickme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:text="Pick Me" />
</LinearLayout>
</LinearLayout>
Thanks
Sajith

Quoting the documentation:
Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.
Hence, you cannot find out when somebody taps on a Button in your info window. You are welcome to respond to clicks anywhere on the info window, though.

Someone wrote a library to address this problem
https://github.com/Appolica/InteractiveInfoWindowAndroid
InteractiveInfoWindowAndroid is an Android library which gives you the opportunity to show interactive info windows on your google map. The UI of your window is encapsulated in your own fragment with its own lifecycle. You just pass it to the InfoWindowManager and display it above whichever marker you want.

Related

Getting all checkboxes, radio buttons, ratings from an expandable list view: android

I have an expandable list view that houses a short survey (3-4 fields for a user to insert data about) per record that we have. Then we have a button that they can hit to submit the data to our server. When they hit the button I need it to grab all the data in the surveys and send it to our site. I have the sending portion of it figured out; what I'm struggling with is grabbing all the data from the survey.
I've looked through many of the related posts/google results and attempted to implement and adapt their solution for listviews to expandedlistviews but I have not been able to get any of them working. I've spent quite a few hours trying to get some workable solution but alas, I cannot figure it out.
I think part of my problem is I'm not exactly certain how children views come into play for expandable list views. In any event below is my code in hopes that someone smarter than me can help me solve this problem.
So I have my ExpandableListView fragment_survey.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="edu.ucr.aum.fragments.SurveyFragment$PlaceholderFragment">
<ExpandableListView
android:id="#+id/list_view_survey_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/rowPaddingMedium"
android:layout_marginRight="#dimen/rowPaddingMedium"
android:divider="#null"
android:dividerHeight="0dp"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay" />
<TextView
android:id="#+id/tvResponseCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/list_view_survey_list"
android:text="Response Code: "/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit Survey"
android:layout_below="#+id/tvResponseCode"
android:id="#+id/btnSubmitSurvey" />
</RelativeLayout>
And here is my row xml list_view_survey_row.xml:
<TextView
android:id="#+id/tvSurveyTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/surveyTitle" />
<TextView
android:id="#+id/tvSurveyShared"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/surveyShared"
android:layout_below="#+id/tvSurveyTitle"
/>
<RadioGroup
android:id="#+id/radioShared"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvSurveyShared">
<RadioButton
android:id="#+id/radioButtonYes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/yes"
android:focusable="true"/>
<RadioButton
android:id="#+id/radioButtonNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/no"
android:focusable="true"/>
</RadioGroup>
<TextView
android:id="#+id/tvSurveyReason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/surveyReason"
android:layout_below="#+id/radioShared" />
<TextView
android:id="#+id/tvSurveyRating"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/surveyRating"
android:layout_below="#+id/tvSurveyReason" />
<CheckBox
android:id="#+id/checkBoxInterest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/surveyInterestPositive"
android:layout_below="#+id/tvSurveyRating"
android:focusable="true" />
<CheckBox
android:id="#+id/checkBoxPrivacy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/surveyShareLevelPositive"
android:layout_below="#+id/checkBoxInterest"
android:focusable="true" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxPrivacy"
android:focusable="true" />
<View android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF00FF00"
android:layout_below="#+id/ratingBar" />
Then I have a fragment (fragment_survey.java) which adds an onclicklistener to the button (btnSubmitSurvey) in the fragment_survey.xml file. That onClickListener() function calls two methods (one to get the data and one to send that data).
this.buttonSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getData(v);submitSurvey();
}
});
public void getData(View view) {
//get data here
}
public void submitSurvey() {
// Create a new HttpClient and Post Header
String url;
if(Singletons.Debug.debug) {
url = "url for postback";
} else {
url = "url for postback";
}
NetworkAsyncTask nat = new NetworkAsyncTask();
try {
String response = nat.execute(url).get();
//Log.d(TAG, "Response: " + response);
} catch(ExecutionException e) {
} catch(InterruptedException e) {
}
}
You should create a data structure like an array to store the survey data, I would index it by group and/or child position, whatever makes sense for the data. Then you should add listeners for each question input, when a question is answered yes, add into that data structure the answer at the appropriate location. Then when you press the submit button it can iterate over that data structure, prompt for unanswered questions or fill out default values, gather it all together and then submit.
Basically there's no easy/built in way to access the individual rows of your dynamic/reusable rows that I have been able to find. When the context of which row is important I've found the best way is to have my own data structure that uses the group/child position to find which button is being pressed since every row has a button with the same id. I also store the group position/index in the button using setHint on creation so that in the context of the listener i can determine the position.
I had to do this via a horrific hack-job, but it works. I added a button for each row that will allow the user to "save" the row. When they save the row then the data is saved to my data structure. Unfortunately to get this working properly I had to create a custom OnClickListener which takes the view as an argument and from there I can scrape the elements from the view.
It's not an elegant or good solution; but it works.

osmdroid workaround for the classic markers overlapping

I am developing an Android offline mapping application using osmdroid and osm bonus pack and loading the tiles and data from external storage. Right now, as the data grows, markers are starting to get cramped together, I even have the situation of two places on the same building. I know this kind of issue has been asked a lot before, mine is about a simple temporal workaround I'm thinking of implementing. How about if two places are near enough(right in top of each other!) the standard info window pops up as a ListView with each row designed like the standard bubble(image, title, moreInfoButton).
My question is: some thoughts or advices on how to create the new bonuspack_bubble.xml layout file.
I don't know if this will help you.
I needed to create a CustomInfoBubble for my project. What I did was, to extend the InfoWindow default class, and pass to it my custom bubble layout. Something like this:
http://mobiledevstories.wordpress.com/2014/03/01/osmdroid-bonus-pack-markers-with-clickable-infowindows/
My Java class MapCustomInfoBubble looks like this:
public class MapCustomInfoBubble extends InfoWindow {
public MapCustomInfoBubble(MapView mapView) {
super(R.layout.map_infobubble_black, mapView);//my custom layout and my mapView
}
#Override
public void onClose() {
//by default, do nothing
}
#Override
public void onOpen(Object item) {
Marker marker = (Marker)item; //the marker on which you click to open the bubble
String title = marker.getTitle();
if (title == null)
title = "";
Button moreInfo = (Button)mView.findViewById(R.id.bubble_moreinfo);//the button that I have in my XML;
moreInfo.setText(title);
moreInfo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
gotoMoreInfoWindow();//custom method; starts another activity
}
});
}
}
In my XML file, I have:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/map_infobubble_black" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/bubble_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:maxEms="17"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Title" />
<Button android:id="#+id/bubble_moreinfo"
android:background="#drawable/map_btn_moreinfo"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0" />
</LinearLayout>
</LinearLayout>
Somewhere else in my code, I then use:
Marker wp = new Marker(mapView);
wp.setPosition(new GeoPoint(mylocation.getMyLocation()));
wp.setTitle(editTextName.getText().toString());
wp.setInfoWindow(new MapCustomInfoBubble(mapView));
mapView.getOverlays().add(wp);
mapView.invalidate();
In my code, I set the text on the button with the Marker's title.
The Marker is the item on which I click. If you want to put info about more markers in the same InfoWindow (inside a ListView), I think you would need to know in advance what the info will be.
I believe that, You can put whatever code you want inside onOpen(), however, I am not so sure if it's a good practice. You could try creating a custom Constructor and put your logic there. It should work.
You need to pass the Resource Id (layout) and mapView to the super constructor, so it returns a valid mView object.

button press in Android app creates casting error

I am new to android development, and have been trying to use the beginner's tutorial as my starting point for developing a simple app. There is one screen, with an image, a row of four buttons, a textbox for the user to enter a pin, and a textview to display results.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/skytrek"
/>
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_today"
android:onClick="today_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_tomorrow"
android:onClick="tomorrow_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_this_week"
android:onClick="this_week_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_next_week"
android:onClick="next_week_click" />
</LinearLayout>
<EditText android:id="#+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/edit_message" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/init_message"/>
</LinearLayout>
I have code to deliver the results I want based on the button pressed - all fine. But then I wanted the user to enter a PIN as well as pressing a button, so I added the EditText control. This threw the following error:
E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
My Java class:
private String content = null;
private TextView textView1;
private EditText editText1;
public void today_click(View view) {
getPage("today");
}
public void tomorrow_click(View view) {
getPage("tomorrow");
}
public void this_week_click(View view) {
getPage("thisweek");
}
public void next_week_click(View view) {
getPage("nextweek");
}
public void getPage(String strParam) {
editText1 = (EditText) findViewById(R.id.editText1);
String message = editText1.getText().toString();
if (message.equals("4567")) {
content = "PIN recognised";
} else {
content = "PIN not recognised";
}
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(content);
}
I thought I had done something silly, using the name of a TextView instead of an EditText control, but I can't find it if I have.
The error is being thrown at the line
getPage("thisweek");
I didn't understand how this line involved views of any sort, but of course the function heading
this_week_click(View view)
does, and when I changed the order of the TextView and the EditText in the XML file (so that the TextView comes first), the error disappeared. It is as if the "view" being passed is not the button, but the nearest widget to the button. I have read
existence of parameter (View view)
but it only seems to confirm my (mis)understanding that a button should be passed as the view parameter. I have also tried cleaning the project, and building a completely new project. What on earth is causing the casting error?
If you're using Eclipse, go to the menu voice "Project" and select "Clean"
Sometimes Eclipse has some problem with ids, by cleaning the project you regenerate them..
Everytime if you make any changes in xml or any interchange of position of views in xml or change of id's,you need to clean build your project.If not you will get this exception.
Hello I have tested your code, your code is fine. Please clean your project from
select your project then click on Project and then Clean and also check the Build Automatically . Your auto generated class R is not generated properly.
Try cleaning your project and run it again..
Eclipse -> Project menu ->Clean
It is because eclipse gets confused when we play around in the xml file ;)

How to show selected View in an Activity first

I have a activity in Android TextView followed ListView again TextView. The content of first TextView fills the whole screen and in order to see the ListView and second TextView the user has to scrolldown. Now I wish to show the second TextView when activity start instead of first TextView.
Instead I want TextView2 to be visible when activity starts.
EDITED
Sample code as per advice of #Urban
public class FocusTestActivity extends Activity {
private static final String TAG = "FocusTestActivity";
ScrollView scroll;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text1 = (TextView) findViewById(R.id.text1);
TextView text2 = (TextView) findViewById(R.id.text2);
TextView text3 = (TextView) findViewById(R.id.text3);
String hello = "We provide services to K-12 education sector."
+ "Our services include Feasibility report, Marketing, Curriculum design"
+ "Teachers planning, Design and Technology, Parents expectation"
+ "management, Transport planning, Day-to-day operations and Statutory"
+ "compliances. Please find a brief introduction attached with mail. Also visit"
+ "www.wissenways.com We can help you with overall process of setting-up school. We have"
+ "experience include establishing International, pre-school and CBSE"
+ "schools from scratch. Please feel free to contact if you need some help in your school venture."
+ "Best of Luck!<br/><br/>";
text1.setText(Html.fromHtml(hello));
text2.setText(Html.fromHtml(hello));
text3.setText(Html.fromHtml(hello));
ScrollView scroll = (ScrollView) findViewById(R.id.scrollcontainer);
}
public void onStart() {
super.onStart();
Log.d(TAG,"Inside onStart");
scroll.post(new Runnable() {
#Override
public void run() {
scroll.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}
XML Code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollcontainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textSize="17sp"/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textSize="17sp"/>
<TextView
android:id="#+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textSize="17sp"/>
</LinearLayout>
</ScrollView>
LOGCAT output:
getScrollView().post(new Runnable() {
#Override
public void run() {
getScrollView().fullScroll(ScrollView.FOCUS_DOWN);
}
});
Using this code in the onCreate should start the scrollview (in which your list and two text views are) at the bottom position. I dont know why you would want to do that, and im guessing there could be a better way to do what you want rather than forcing the scroll position to the bottom, but anyways...
This is the question from where the above code is taken from, you couldve searched for it.
Hope this helped.
Here are 3 simple options i can give you:
Change there places
Make another activity with the second textview and launch that activity first then show you current one
Show a Toast with the text in the second textview at the start of your activity (problem with this one is the user closes it and you want to show it again or user wants to see it again you need to launch a new toast)
I don't fully understand your difficulty with this problem
EDIT:
when must the first textview display at the top? if never just do the first option :\ if under some event, have a empty textview at the top and at the bottom and by code change it's text to what you want at the moment you want
You could also use in your xml file a RealtiveLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:id="#+id/txtview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="#id/android:list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#id/textview2/>
<TextView
android:id="#+id/txtview1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#id/android:list"/>
</RelativeLayout>

Clickable image - android

How do i make a image clickable? I have tried some ways, but without success.
Here's the last code i tried (it's clickable but gets error):
ImageView btnNew = (ImageView) findViewById(R.id.newbutton);
btnNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do stuff
}
});
and here's the part from xml:
<ImageView
android:src="#drawable/tbnewbutton"
android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/newbutton"
android:clickable="true"
android:onClick="clickImage"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
When running this code, and clicking the image i get this error:
01-24 19:14:09.534: ERROR/AndroidRuntime(1461): java.lang.IllegalStateException: Could not find a method clickImage(View) in the activity
HERE'S THE SOLUTION:
The XML:
<ImageButton
android:src="#drawable/tbnewbutton"
android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/newbutton"
android:clickable="true"
android:onClick="clickNew"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#null" />
The code :
public void clickNew(View v)
{
Toast.makeText(this, "Show some text on the screen.", Toast.LENGTH_LONG).show();
}
As other said: make this an ImageButton and define its onClick attribute
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left"
android:onClick="scrollToTop"
android:src="#drawable/to_top_button"
/>
The image is here encoded in a file res/drawable/to_top_button.png. If the user clicks on the button, the method scrollToTop() is called. This method needs to be declared in the class that sets the Layout with the ImageButton as its content layout.
public void scrollToTop(View v) {
...
}
Defining the OnClick handler this way saves you a lot of typing and also prevents the need to anonymous inner classes, which is beneficial for the memory footprint.
Does an ImageButton do what you want?
The error message you get implies that you do not have a method in your activity that matches your onClick handler.
You should have something like clickImage(View view) in your activity with the click handling implementation.
You could just use the ImageButton class...
http://developer.android.com/reference/android/widget/ImageButton.html
Use a ImageButton ;)
You've set the onclick method to call "clickImage" when the image is clicked in your XML, but you haven't created a clickImage method in your code. You shouldn't need to set the onclick listener at all. Just implement the method from your XML and you should be set.

Categories

Resources