I'm obviously new to app development but basically what I want to do is make a simple app that has a button called 'View Map' that, when pressed, will open up Google maps.
So far, I've coded an app with the 'View Map' button that simply returns some text below the button when pressed, which looks like this.
I have also made another app which uses the Google Developers' tutorial to display a Google map when the app is opened.
My problem arises when I try to combine the two apps together. Instead of having it display just the button alone when opened, it displays both the button and the map in the home screen which looks like so:
and does nothing when the button is pressed.
What I've tried to do is use an OnClickListener that loads the map when the button is pressed, but that clearly ain't working. I assume it has something really basic to do with my activity_main class but I need help sorting it out so that the map isn't created immediately when the app is loaded.
My activity_main class looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="**My Key**"
android:clickable="true"
android:enabled="true" />
<Button
android:id="#+id/buttonMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View Map" />
</RelativeLayout>
And my MainActivity class looks like this:
public class MainActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonMap = (Button) findViewById(R.id.buttonMap);
buttonMap.setOnClickListener(mapListener);
}
private OnClickListener mapListener = new OnClickListener() {
public void onClick(View v) {
MapView mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
}
};
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Any help is appreciated, cheers.
Ok, I think you need to use your MainActivity for the button, then set the button's OnClickListener to trigger another Activity which will extend MapActivity. Your MainActivity will then extend Activity only. Here's what your "mapListener" should consist of:
Intent i = new Intent(ctx, MyMapView.class);
// i.putExtra("key", "value"); // use this to give extra data to the new Activity
startActivity(i);
Define ctx as an instance variable (i.e. outside of any functions) in MainActivity and set it to "this":
Context ctx = this;
From within mapListener, "this" is the OnClickListener object itself, not the Activity, so using "this" in "new Intent()" will cause an error. Since mapListener exists inside the Activity though, "ctx" can be seen just fine and its meaning won't change.
Everything inside "mapListener.onClick" should go into your new Map Activity's onCreate() method.
Related
I have a main activity where the user has 3 list views and 3 buttons displayed. When the user clicks on 2 of the three buttons I want the user to be sent to another screen (another layout). I already have the two other layouts created and the two other classes created. I am not sure why this is not working. My application reports no errors, no warnings, and I have found that I cannot go through my code step by step because my debugger is not working.
I have an OnClickListner(I am currently just testing one button at a time to get the issue fixed) that is linked to the button in the Main Activity layout.xml android:OnClick. I have an Intent put in place within the Main Activity that (according to multiple sources and YouTube tutorials) is set up properly and the new activity that the user should be redirected to is linked to the appropriate layout. When I run the application the button click registers (I know this because I have a sound effect in place when the button is pressed) but nothing else happens. Also all of my activities are present in the AndroidManifest.xml file. And all of the activity files are at the same folder level, all layouts are under res/layout
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// instantiate button
Button btnClicked = (Button)findViewById(R.id.goal);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
btnClicked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
ChangeGoal.class));
mp.start();
}
});
// method called when set goal button is clicked
public void setGoal(View view)
{
// intent to send user from main activity to the change goal
activity
Intent setGoal = new Intent(this, ChangeGoal.class);
startActivity(setGoal);
}
}
public class ChangeGoal extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_goal); // connect
class to the other layout
}
}
//button from activity_main.xml
<Button
android:id="#+id/goal"
android:layout_width="200dp"
android:layout_height="200dp"
android:text="#string/Set"
android:onClick="setGoal"
android:textSize="20sp"
android:textColor="#000000"/>`
Again, I am expecting the button click from the main activity to send the user to a new activity where they need to make a selection and then return to the main activity. Thus far the button click registers (I can hear it since I included some audio already upon the button click) but the application does nothing, I am left on the same screen and the button can be pressed again and again.
In your Activity layout, you have declared this on your Button:
android:onClick="setGoal"
Then, in code, you have attached an onClickListener to the same Button. This is wrong. Do either the one, or the other, but not both.
I'm trying to make a button for my app, which will bring the screen to another page. However, I'm not successful in doing so.
I've tried many things, without a reliefing answer.
My project doesn't accept "Intent" in my program.
My button I need to open a new layout is called "OptionButton"
Here's what I've got:
in MainActivity.java
In the beginning I got this
public class MainActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;
public Button OptionButton;
/** further I got this**/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/** (I'm just mentioning this because I use savedInstanceState here too)**/
/**MainActivity.java And my code for my button is this **/
OptionButton = (Button) findViewById(R.id.Button1);
OptionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
public void myClick(View v) {
Intent intent = new Intent(this, Background2.class);
startActivity(intent);// for calling the activity
}
});
}
}
}
I added this in AndroidManifest:
<activity android:name=".Background2"></activity>
and this in the 2nd class (java file in src map)
(package & imports, then this:
public class Background2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
_
I got 2 classes in src map:
-Background2.java
-MainActivity.java
Also 2 layout xml's:
activity_main.xml
activity main2.xml
In Activity_main, I got this for the button:
<Button
android:id="#+id/Button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/OptionButtonLabel"/>
Still it's not working. What am I missing?
Ty all so much!
I've tried changing this:
button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this);
But it didn't work.
Inside of the listener you are calling this (which refers to the listener itself), while what you want to refer to is the MainActivity.
Simply change to
Intent intent = new Intent(MainActivity.this, Background2.class);
You Main Activity has to implement OnClickListner
public class MainActivity extends Activity implements OnClickListener{
From Eclipse IDE press Ctrl+Shift+O it will automatically implement and import necessary functions
Thank you for the answers! :)
I've tried installing it on my real device.
Now, it opens, but it's the 2nd xml that opens, and when I click the button, it re-opens the same xml.
:-/
I don't get any error messages though when I changed your suggested solutions (from both of you)
I am new to android. Can I get to know Onclick text how can I redirect to another XML page in Android ?
You can redirect to another xml through a button click, But here I think you have asked how to do it clicking on some text, for a example if you want to click on this text "click to sign up" and redirect you to the signup page.
So here is how I did it.
1)for text click
Inside Main Activity class
you write the following code.
//Text redirect. signup textview should be created within mainActivity.xml
final TextView signup=(TextView)findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1) {
Intent launchActivity1= new Intent(MainActivity.this,SignUp.class);
startActivity(launchActivity1);
}
});
In the AndroidManifest file, You create a New Activity
<!-- Sign Up activity -->
<activity
android:name=".SignUp"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.example.xxxx.SignUp"/>
<categeory android:name="android.intent.categeory.LAUNCHER"/>
</intent-filter>
</activity>
Sign Up Java class. You make signup.xml appear inside this class by accessing it through the R.id.layout
package com.example.xxxx;
import android.app.Activity;
import android.os.Bundle;
public class SignUp extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
}
}
.
2) for button click
You will do the same as for Text click.
In MainActivity.java class
final Button button=(Button)findViewById(R.id.loginbtn);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent launchactivity= new Intent(MainActivity.this,Details.class);
startActivity(launchactivity);
Then you will create A new Activity in AndroidManifest.
You Have to have a Details.java class. Since you have mentioned to redirect to Details.class, according to this example.
Inside Details class like in previous access the relevant xml file.
"click here to signup" in the following image will redirect you to the signup.xml .
I'm guessing here that with 'new XML page' you refer to another view?
Most basic way to accomplish this is to make 2 activities with each a different layout (that XML file). On activity 1 add a button. In onclicklistener for that button start the other activity.
Use the visual tools in your IDE to create the activities. Use below code to switch to other activity:
public void startActivity2(View view) {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
More info at http://developer.android.com/training/basics/firstapp/starting-activity.html
There so many ways to do it i can't even describe them all :)
Possibly you thing about starting new activity. So you need to create new activity class with proper layout (in xml) and then start it with intent.
Heres an example
I'm using an android panel widget in my application to create a sliding panel effect from the top of my app (similar to the notification panel). When clicked the panel opens to fill about 30% of the screen. By default the panel is closed and has a handle to "show". I'm trying to modify it to be in the shown state by default but am unsure how to do it. I assume that I'll have to modify the Panel.java file but I suppose it might also be done in my layout file.. Any tips?
Assuming you're defining your own class which extends Panel...in your onCreate() override, you could try calling setOpen(true, false); ?
EDIT As you're not extending it yourself, you could simply call the setOpen() method as soon as your activity is created instead. Example...
public class MyActivity extends Activity {
protected Panel topPanel = null;
#Override
public void onCreate(Bundle savedInstance) {
super.onCreate();
setContentView(R.layout.main);
topPanel = (Panel) findViewById(R.id.top_panel);
topPanel.setOpen(true, false);
}
}
I have one application. I need to display map view when we click on map button. Actually this map button is in one class and google map is in another class. Now How can we call one map activity class from another activity class.
Please help me!
For the button, in the xml layout, set android:clickable="true" and android:onClick="showMap", then in your first activity/class, it's like this:
public void showMap(View v) {
Intent intent = new Intent(this, Map.class);
startActivity(intent);
}
(Map.class being the class/name of your activity with the map. You need to adjust the name to your case.)