embed MapView into Activity - java

I am trying to embed MapView into FrameLayout of an activity for example ,
UserActivity layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="#+id/map_frame" android:layout_gravity="center_horizontal" android:padding="0.0dip" android:clipChildren="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4.0dip">
<ImageView android:id="#+id/map_img" android:layout_gravity="center" android:layout_width="294.0dip" android:layout_height="136.0dip" android:src="#drawable/place_map_frame" />
</FrameLayout>
</LinearLayout>
Here , inside FrameLayout i want to display MapView when ever the list item gets clicked
for some reason its not possible to directly put inside framelayout so just because of that i created another activity which extends MapActivity
<com.google.android.maps.MapView
android:layout_gravity="center"
android:id="#+id/mapview"
android:clickable="true"
android:layout_width="294.0dip"
android:layout_height="136.0dip"
android:layout_centerInParent="true"
android:apiKey="#string/google_maps_key" />
UserMap layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView android:layout_gravity="center" android:id="#+id/mapview" android:clickable="true" android:layout_width="294.0dip" android:layout_height="136.0dip" android:layout_centerInParent="true" android:apiKey="#string/google_maps_key" />
</LinearLayout>
Here is the code for both the activities
import com.google.android.maps.MapActivity;
import android.app.Activity;
import android.os.Bundle;
public class UserMap extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_map);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class UserActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_activity);
}
}
Now , i want to embed a UserMap into UserActivity's FrameLayout and UserActivity will be called when a list item get clicked.

Remember to include the Google API instead of the single Android API.
You also need to include the following line in your Manifest, under the <application> tag:
<uses-library android:name="com.google.android.maps" />

Related

Android Floating Action Button not being recognized

When I look at my code in the editor, nothing seems to be a problem. However, when I run the code on my phone and try to go from one activity to another which features a floating action button, the floating action button cannot find the view by Id despite the ID being exactly the same in both java and XML.
My Java:
package com.test.app.app2_test1;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class TermsOfUse extends AppCompatActivity {
FloatingActionButton mFloatingActionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.terms_of_use);
mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab);
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.app.app2_test1.TermsOfUse"
android:background="#color/backdrop">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="44dp"
android:orientation="vertical">
</ScrollView>
<View
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#color/colorPrimary"
android:elevation="4dp"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/terms_back_fab"
android:layout_gravity="bottom"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="16dp"
android:src="#drawable/ic_arrow_back"
app:pressedTranslationZ="12dp"/>
</android.support.design.widget.CoordinatorLayout>
The message that I get in my console is the following:
Attempt to invoke virtual method 'android.view.View android.support.design.widget.FloatingActionButton.findViewById(int)' on a null object reference
Any help would be appreciated.
You are treating a view as a child of its own!
Replace:
mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab);
With:
mFloatingActionButton = (FloatingActionButton)findViewById(R.id.terms_back_fab);

Android Studio add EditText input to a ListView with button onClick

I am trying to make a simple Planner app so I have an XML file with a ListView on it, and another XML file with an EditText and a sumbit button on it, and of course my Java files. I have done a ton of research but I can't find a working method to make it when I press the button, it adds the EditText stuff to the ListView as an item. Here is my code:
activity_main.xml
<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=".MainActivity" >
<ListView
android:layout_width="300dp"
android:layout_height="fill_parent"
android:id="#+id/event_list"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/datePicker" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calendarView"
android:layout_alignBottom="#+id/datePicker"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Event"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignEnd="#+id/datePicker"
android:layout_toEndOf="#+id/event_list"
android:background="#android:color/holo_blue_light"
android:onClick="newEvent"
android:textSize="20dp"
android:textColor="#android:color/white" />
</RelativeLayout>
new_plan.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_name"
android:hint="Event Name"
android:inputType="textCapWords"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_date"
android:hint="Event Date" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:textSize="20dp"
android:textColor="#android:color/white" />
</LinearLayout>
MakePlan.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MakePlan extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
}
}
MainActivity.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEvent(View v){
Intent i = new Intent(this, MakePlan.class);
startActivity(i);}}
Please help, thank you.
Take a look on this google developers project. It's for animations but i think it also have the solution to your question.
https://developer.android.com/training/animation/layout.html
You should append data to the collection of items linked to your ListView via your adapter and notify your adapter that your data has changed. This illustration assumes that you have a List of String objects.
EditText editText = ...
List<String> list = ...
YourAdapter adapter = ...
ListView listView = ...
You must have done something like this to setup your listView.
listView.setAdapter(new YourAdapter(list, this));
Now on button click you should get the String in EditText, add it to your List and finally notify the adapter that your data has changed in order to update your ListView for e.g.
void OnButtonClick(View view){
String strToAdd = editText.getText().toString();
list.add(strToAdd);
adapter.notifyDataSetChanged();
}

ImageViewTouch doesn't display image on DialogFragment

I've found out the ImageViewTouch since I wanted to create a zooming image preview on my app though I need to display that image on one of my DialogFragment. But it works just fine on a normal FragmentActivity which is the MainActivity but not in my DialogImage and I'm not sure why.
I imported the library using the gradle and here's my code so far:
The MainActivity
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
ImageViewTouch mImage;
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView( R.layout.fragment_main );
mImage = (ImageViewTouch) findViewById( R.id.image );
ImageDownloader2 imgDownload = new ImageDownloader2();
imgDownload.download("MY_URL", mImage);
Button click = (Button)findViewById(R.id.button);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
showPromoImage();
}
});
}
private void showPromoImage(){
DialogImage promo_details = new DialogImage();
promo_details.show(getFragmentManager(), "promo_details");
}
}
The DialogImage
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
/**
* Created by Bahay on 2/19/14.
*/
public class DialogImage extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_image, container);
getDialog().setTitle("Test");
getDialog().setCanceledOnTouchOutside(true);
ImageViewTouch promo = (ImageViewTouch) view.findViewById(R.id.image);
ImageDownloader2 imgDownload = new ImageDownloader2();
imgDownload.download("MY_URL", promo);
return view;
}
}
fragment_main.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" >
<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="158dp"
android:layout_height="322dp"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:gravity="center"
android:weightSum="2" >
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Load Image" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Change Display" />
</LinearLayout>
</LinearLayout>
dialog_image.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">
<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
</LinearLayout>
The ImageDownloader2 is just a library which downloads the image from the URL then convert it into bitmap. I just made some customizations for this to work with ImageViewTouch.
As for the ImageViewTouch you can see it here:
https://github.com/sephiroth74/ImageViewZoom
For the ImageDownloader2 class you can see it here:
http://theandroidcoder.com/utilities/android-image-download-and-caching/
I just replaced the ImageView with ImageViewTouch for the download method.
UPDATE:
Tried using a normal old-school dialog wherein I use Activity and Intent where I just themed the DialogImage as android:theme="#android:style/Theme.DeviceDefault.Dialog". Output is same where image is not loaded.
Hope someone can help. Thanks!

Items in another layout do not respond to user events

Please have a look at the following code
game_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Game" >
//Please note the GUI Has been removed
<include layout="#layout/common_status_bar"/>
</RelativeLayout>
Game.java
package game.games;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class Game extends Activity {
private ImageView goToLanguageSelection;
private ImageView goToInternet;
private Button giveUp;
private LayoutInflater layoutInflater;
private View commonView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
//Intializing instance variables
goToLanguageSelection = (ImageView)commonView.findViewById(R.id.backToLanguageSelectionButton);
goToInternet = (ImageView)commonView.findViewById(R.id.internetButton);
giveUp = (Button)commonView.findViewById(R.id.giveUpButton);
flag = getIntent().getBooleanExtra("LANGUAGE", true);
//Add listeners
goToLanguageSelection.setOnClickListener(goToLanguageSelectionClicked);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
//Will get activated when the ThunderBolt image is clicked
private OnClickListener goToLanguageSelectionClicked = new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(),LanguageSelector.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
};
}
common_status_bar.xml
<?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="wrap_content"
android:layout_alignParentBottom="true"
android:background="#373734"
android:orientation="horizontal" >
<ImageView
android:id="#+id/backToLanguageSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:paddingTop="5dp"
android:src="#drawable/thunderbolt" />
<ImageView
android:id="#+id/internetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:src="#drawable/globe_small_2" />
<ImageView
android:id="#+id/justForFun"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp" />
<Button
android:id="#+id/giveUpButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Button" />
</LinearLayout>
Here, the common_status_bar.xml is a common layout. I have added this layout to game_activity.xml by <include layout="#layout/common_status_bar"/>.
It displays everything fine, no issue. But the case is I can't make any of it's elements to work. I have attached a OnClickListener to the first element of it, the goToLanguageSelection but it is not responding to the Click. I tested this by adding the click event to all other buttons, images as well (one at a time) and I got the same result. It is not responding to user click, or whatever.
Why this button and images in separate layout do not respond to the user events?
The commonView should not be created again.
If you want keep a reference of the common_status_bar.xml, you can give name it by an id, like the code below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<include
android:id="#+id/common_status_bar"
layout="#layout/common_status_bar" />
</RelativeLayout>
Then, change this code:
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
to:
commonView = findViewById(R.id.common_status_bar);

Opening one screen from another

I am trying to open one screen from another in an Android App. I copied the outline of the following code from a tutorial online and then tried to substitute my own screens. When I try to run the following code, I get a "Force Close" when I click the button on the first screen (the second screen never displays). Can someone tell me what I am doing wrong? There are four files: ScreenTestActivity.java, main.xml, screen2.java and screen2.xml.
ScreenTestActivity.java
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ScreenTestActivity extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.btnClick);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(ScreenTestActivity.this, screen2.class);
startActivity(i);
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open New Screen"
/>
</LinearLayout>
screen2.java:
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.os.Bundle;
public class screen2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
}
}
screen2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the New Screen"
/>
</LinearLayout>
<activity android:name="screen2"></activity>
<activity android:name="ScreenTestActivity"></activity>
put this 2 line of code in your androidmanifest.xml file

Categories

Resources