I am very new to android programming. I want to use a code that takes me to MainActivity from my current activity on a click of a Button.
Here is my current code:
package com.example.flashlightapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Whitelight extends Activity implements OnClickListener {
Button b1 = (Button) findViewById(R.id.b3);
Intent i = new Intent(this, MainActivity.class);
{
this.startActivity(i);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
What should I put in
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
At first you must check if you declared all your activities in the manifest.xml file.
and in your Java code try this:
package com.example.flashlightapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Whitelight extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
Button b1 = (Button) findViewById(R.id.b3);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Whitelight.this, MainActivity.class);
this.startActivity(i);
}
});
}
}
This tutorial explains how to use intents and listeners : http://goo.gl/phLWkx
Try this..
public class Whitelight extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
Button b1 = (Button) findViewById(R.id.b3); // Initialization of Button
b1.setOnClickListener(this); // Initialization of ClickListener to the Button
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
Follow the links
http://developer.android.com/index.html
http://developer.android.com/training/index.html
http://www.mkyong.com/tutorials/android-tutorial/
Use following :
Button b1;
public class Whitelight extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
b1 = (Button) findViewById(R.id.b3); // Initialization of Button
b1.setOnClickListener(this); // Initialization of ClickListener to the Button
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==b1){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
}
in onClick(...) :
you can choose if your button is press then perform specific activity, in your case if you press b1 button then perform button specific activity :
so we can check view v==b1 button. if you want to more button then
if(v==b1){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
else if(v==b2)
{
// perform another action ;
}
Related
When I press the 'back' button of a mobile-phone it shows me a blank page. When I again press the 'back' button it then shows me the main page of the application. I want to get to the mainActivity page on first back press.
mainActivity code:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.style.SuperscriptSpan;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button b1,b2,b3,b4,b5,b6,b7;
EditText e1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.call1);
e1=(EditText)findViewById(R.id.call);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String no=e1.getText().toString();
Intent call=new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:"+no));
startActivity(call);
}
});
b2=(Button)findViewById(R.id.sens_sms);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sendsms=new
Intent(getApplicationContext(),waytosms.class);
startActivity(sendsms);
}
});
b3=(Button)findViewById(R.id.facebook);
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,facebook.class);
startActivity(facebook);
finish();
}
});
b4=(Button)findViewById(R.id.gmail);
b4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,gmail.class);
startActivity(facebook);
}
});
b5=(Button)findViewById(R.id.utube);
b5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,utube.class);
startActivity(facebook);
}
});
b6=(Button)findViewById(R.id.google);
b6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,google.class);
startActivity(facebook);
}
});
b7=(Button)findViewById(R.id.twitter);
b7.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,twitter.class);
startActivity(facebook);
}
});
}
}
This is the Facebook class I have used here with webview to open the Facebook page:
public class facebook extends Activity{
WebView facebook;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
facebook=(WebView)findViewById(R.id.webfacebook);
facebook.getSettings().setJavaScriptEnabled(true);
facebook.loadUrl("https://facebook.com");
}
Here I have implemented onbackPressed() and called finished() The onbackpressed method isn't affecting this at all.
#SuppressLint("NewApi")
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
Intent i=new Intent(facebook.this,MainActivity.class);
startActivity(i);
}
}
1> first is main activity page where there are icon of social sites when
i click on facebook icon it will open facebook as 2nd facebookActivity page,3> when i clicked back it will open a blank page and4>. nd when i again press back button it will opened the main activity page...
Change your onBackPressed() method like this:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
I'm trying to make a simple math game with two modes, addition and subtraction. I figured out how to create a button that will link the "Addition button" to the addition activity but I can't seem to figure out how to create a second "Subtraction Button" that will link to the subtraction activity. Here's my broken code:
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Your code just have some syntax error which is solved as per given code...
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
}
Your second instruction to add listener is inside the first OnClickListener.
So the listener is never added to the second button. You code should be like this :
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
});
}
You need to have different click listeners for different buttons. Right now, you're putting the click listener for the second button inside the click listener for the first button. Separate them like this..
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
)};
Some points before the solution (those will help you for further coding).
Declare context after buttons declaration : So after Button
button, button2, write :
final Context context;
Always keep in mind : Always initialize objects in onCreate()
method. So in OnCreate() after
setContentView(R.layout.activity_main_menu);, write :
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
//then define context
context = MainMenu.this;
//or context = getApplicationContext();
Now addListenerOnButton() function will be like this (just replace
addListenerOnButton() with following code):
public void addListenerOnButton(){
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Now what is the wong in your code ?
Your onClick listener code is wrong...
enjoy the coding :) (and android :))...
you can even do like this
public void mainClickHandler(View v)
{
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
case R.id.button2:
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
}
I have 3 classes. In every class I have 2 buttons like yes and no.
I want to pass 1 for yes and 0 for no.Finally I want to count the total number,
I mean if someone presses ' yes ' every time then it should be 3.
How to do it?`
passing values by buttons and count the whole thing
//this is 1st class
package com.atlantis.learnactivities;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener (new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Main.this,Second.class));
}
});
Button b2 = (Button) findViewById(R.id.b2);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Main.this,Second.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//this is 2nd class
package com.atlantis.learnactivities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second extends Activity {
String gender =null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button b3 = (Button) findViewById(R.id.b3);
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Second.this,Three.class));
}
});
Button b4 = (Button) findViewById(R.id.b4);
b4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Second.this,Four.class));
}
});
}
}
Use sharedPreference.
When click button add an integer value.
To add
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt("btn_value", "value");
editor.commit();
To retrieve data from shared preference
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int value = prefs.getInt("btn_value", -1);
In your current Activity, create a new Intent:
Intent i = new Intent(Main.this, Second.class);
i.putExtra("new_variable_name","value");
startActivity(i);
Then in the Second Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
You don't need to use SharedPreferences to pass data in the same application.
If you don't need to Store the values and just pass it from one class to the next, use putExtra with intent.
Eg. Inside your click listener:
intent.putExtra("value", 1);
//or intent.putExtra("value", "1") , depending on whether you want to use it as a String or int
And in the class you are passing the value to, retrieve it
this.getIntent().getExtras().getString("value");
//this.getIntent().getExtras().getInt("value");
check:
http://pkhandroid.wordpress.com/2012/10/02/post9-intent-message-passing-within-activities/
http://mobileorchard.com/android-app-development-using-intents-to-pass-data-and-return-results-between-activities/
package com.example.example;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.activity_main);
this.btn = (Button)this.findViewById(R.id.button);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
}
}
I don't get any errors however my program also dont run as I want. I am new in android and I want to change the screen after the button is clicked for that I am using two classes so in one class my program should invoke another one onclick. How can I do this ? My code is as above.
First remove this.setContentView(R.layout.activity_main); because you declared it twice . Then declare
btn = (Button)this.findViewById(R.id.button);
after
setContentView(R.layout.activity_main);
Declare your profile activity in manifest file.Check my code below.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
1) your oncreate method should look like below one.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
2) Add profile Activity into Manifest.
3) add logs in onClick method so you should know if it is getting called on not.
Happy coding!!
I want the values of edit text restored when user comes back to my first activity?
Please help me out.
Thanks in advance
this is my first activity code for getting user values in edit text
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
/** Called when the activity is first created. */
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
ed1_val = Float.parseFloat(ed1.getText().toString());
ed2_val = Float.parseFloat(ed2.getText().toString());
Log.v("TAG", "inside saved instance");
savedInstanceState.putFloat("ed1", +ed1_val);
savedInstanceState.putFloat("ed2", +ed2_val);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v("TAG", "inside on restore");
float ed_val = savedInstanceState.getFloat("ed1");
float ed2_val = savedInstanceState.getFloat("ed2");
ed1.setText("" + ed_val);
ed2.setText("" + ed2_val);
}
}
this is my second activity code
public class Second_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
IntentActivity.class);
startActivity(intent);
}
});
}
}
It is not a good idea to start the first activity again on back pressed. Call finish() in the second activity. This will lead to the resume of the first activity which is what you need.
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish(); }
});
}
You don't need neither onSaveInstanceState nor onRestoreInstanceState.
Just call finish in the onClick listener for the button in the second Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
}
This is the second one:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
That way you are resuming the previous Activity instead of starting new one.
If you need to pass data between them you could use startActivityForResult / onActivityResult and setResult methods:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
private static final int GET_VALUES_REQUEST_ID = 1;
public static final String FIRST_VALUE_ID = "first_value_id";
public static final String SECOND_VALUE_ID = "second_value_id";
private static final float DEFAULT_VALUE = 0;
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivityForResult(intent, GET_VALUES_REQUEST_ID);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case GET_VALUES_REQUEST_ID: {
if (Activity.RESULT_OK == resultCode) {
ed1_val = data.getFloatExtra(FIRST_VALUE_ID, DEFAULT_VALUE);
ed2_val = data.getFloatExtra(SECOND_VALUE_ID, DEFAULT_VALUE);
setValues();
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
protected void setValues() {
ed1.setText(Float.toString(ed1_val));
ed2.setText(Float.toString(ed2_val));
}
}
The second activity could be something like that:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent data = new Intent();
data.putExtra(IntentActivity.FIRST_VALUE_ID, 324f);
data.putExtra(IntentActivity.SECOND_VALUE_ID, 32234f);
setResult(Activity.RESULT_OK, data);
finish();
}
});
}
}
This is a very basic example so I just hardcoded some return values - please implement something more meaningful.
Beside that you could avoid using underscores as word separator in class names - camel case is much more accepted as name convention.
just finish the second activity don't start it via intent.
When you finish the second activity first activity will be automatically resumed,
you can go for overriding protected void onSaveInstanceState(Bundle outState) and protected void onRestoreInstanceState(Bundle savedInstanceState)
Here is an example
Remember it will work when you will not finish your previous activity.Do this in your first activity.
You are starting a new activity by pressing back button on Second Activity. The new activity instance is not the previous one that has started the Second Activity hence onRestoreInstanceState callback is not getting called.
Its easy if you do not need to send data back to the first activity. How do send back data without using an intent.