How to get only a specific button's click - android

I have 2 buttons on the MainActivity. I want the first button to send me to TestPage1.class(Which it already does). And I want the second button to send me to TestPage2.class. Whenever I click on the second button, it sends me straight to TestPage1 and not TestPage2. How can I make this correct?
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = ((Button) findViewById(R.id.button1));
button1.setOnClickListener(this);
button2 = ((Button) findViewById(R.id.button2));
button2.setOnClickListener(this);
}
#Override public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TestPage1.class));
}
public void onClick2(View v){
startActivity(new Intent(MainActivity.this, TestPage2.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.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Use this inside onClick(View v)
switch(v.getId()) {
case R.id.button1:
// do stuff;
break;
case R.id.button2:
// do stuff;
break;
...

use v.getId() to check which Button in clicked inside onClick method:
#Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
startActivity(new Intent(MainActivity.this, TestPage1.class));
}
or use switch-case for handing multiple Button clicks with same listener:
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// ... your code here...
break;
case R.id.button2:
// ... your code here...
break;
default:
//...
}
}

// declare buttons b1,b2
// remove "implements onclick listener" and use this
enter code here
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent inten = new Intent(this,
TestPage1.class);
startActivity(inten);
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent inten = new Intent(this,
TestPage2.class);
startActivity(inten);
finish();
}
});

Related

Android, menu option with items

I have create a menu with couple options, but I want to get a separate page when I click on them in MainActivity. As in: if I click on Movies, I would have other page showing lists of movies and so on.
Which functions should I call for that?
package com.example.popupmenu.popupmenu;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements PopupMenu.OnMenuItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
public void showPopUp(View v){
PopupMenu popup=new PopupMenu(this,v);
popup.setOnMenuItemClickListener(MainActivity.this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu. main_menu, popup.getMenu() );
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.Movies:
Toast.makeText(getApplicationContext(), "movies Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.MeloDramas:
Toast.makeText(getApplicationContext(), "Melodramas Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.Songs:
Toast.makeText(getApplicationContext(), "Songs Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
//return false;
}
}
On Your menu Click do this thing to StartActivity.
case R.id.Movies:
Intent launchNewIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(launchNewIntent);
return true;
this will open new Activity.
Create a movie list activity. Then in your menuitemclick function in MainActivity, call intent to MovieActivity.
For eg;
Intent intent = new Intent(MainActivity.this, MovieActivity.class);
startActivity(intent);
You can start a new activity by using startActivity. To learn more about starting activities please visit this link.
Intent intent = new Intent(MainActivity.this, MovieActivity.class);
startActivity(intent);
first of all you didn't called showPopUp(View v) you need to call that for inflating menu. and if you want regular menu then why you are using popup menu.. just use this for inflating menu...
#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, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Movies) {
Intent i = new Intent(MainActivity.this,SplashScreen.class);
startActivity(i);
return true;
}
if (id == R.id.MeloDramas) {
Intent i = new Intent(MainActivity.this,melodramas.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
In this case you don't need to call any method just put these two method..
i would advise you to use fragments based on MenuItem selected
public boolean onMenuItemClick(MenuItem item)

Why this program is not working in Android Virtual Device?

WelcomeActivity:
package com.emdad.androidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
public class WelcomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(WelcomeActivity.this, GameActivity.class);
startActivity(intent);
}
});
setContentView(R.layout.activity_welcome);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GameActivity:
package com.emdad.androidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class GameActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
When it was run in Android Virtual Device, it replied "Unfortunately, it has stopped." What is the problem?
You should move
setContentView(R.layout.activity_welcome);
before
findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() {
Corrected:
setContentView(R.layout.activity_welcome);
findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(WelcomeActivity.this, GameActivity.class);
startActivity(intent);
}
});

Create a button than send the user to the main page

I am new with Android Studio world. I made a two activity pages. The first button in the first page send the user to the second page which it works fine
public void change(View v){
setContentView(R.layout.activity_main_activity2);
but I made the same button in the second page to send the user the main page but no luck. Please see the code and let me know why I got this error.
public void HomePage(View v){
setContentView(R.layout.activity_main_activity);
This is the first activity page
package com.example.lenovo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void change(View v){
setContentView(R.layout.activity_main_activity2);
}
}
and this is the second activity
package com.example.lenovo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import static com.chapter2.example.lenovo.chapter2.R.layout;
public class MainActivity2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main_activity2);
Button mButton = (Button) findViewById(R.id.button2);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent mIntent = new Intent(MainActivity2.this,
MainActivity.class);
startActivity(mIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void HomePage (View v){
setContentView(R.layout.activity_main);
}
}
Currently you are switching layouts of Activity instead of changing Activity. this not right way to switch between Activities. use startActivity method with intent to start next Activity on Button click:
Start MainActivity2 from MainActivity:
public void change(View v){
Intent intent=new Intent(v.getContext(),MainActivity2.class);
startActivity(intent);
}
Start MainActivity2 from MainActivity:
public void HomePage(View v){
Intent intent=new Intent(v.getContext(),MainActivity.class);
startActivity(intent);
}
and make sure to have added both Activities in AndroidManifest.xml
If you want use inbuilt device back button then simply put
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
and if you want to go back by button click put this line
bbsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
}
In the first activity you are not launching the mainactivity2.class when you click the button, instead you are just changing the content view.
and the R.id.button2 is not registered in the first activity.
So it is not working
You have to send user first page to second page using Intent
You need to add onClick tag in that button in activity_main layout like this
android:onClick="change"
in activity_main_activity2 add onClick tag in that button like this
android:onClick="HomePage"
In MainActivity2 in setContentView change like this
setContentView(R.layout.activity_main_activity2);
In first activity do like this
public void change(View v){
Intent mIntent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(mIntent);
}
In second activity do like this
public void HomePage(View v){
Intent mIntent = new Intent(MainActivity2.this,MainActivity.class);
startActivity(mIntent);
}
Don't forget to declare those Activities in manifest

Why my aplication call the OnCreate's event every i call an activity?

i having a problem.
I try add a value in a listview since other activity, so i have 2 activities
the number 1 have a button (to call the activity 2) and have a listview
the other activity hace a 2 edittext and a button where call to activity 1 (here pass the 2 values ina putextras)
SO when i call the activity 2 and i back the event Oncreate is called againg, someone know why?
my code activity 1
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private ArrayList<User> arrayOfUsers;
private UsersAdapter adapter;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lvItems);
arrayOfUsers = new ArrayList<User>();
adapter = new UsersAdapter(this, arrayOfUsers);
listView.setAdapter(adapter);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if(extras != null)
{
if( extras.getString("ename") != null && extras.getString("ehome") != null) {
User newUser = new User(extras.getString("ename"), extras.getString("ehome"));
adapter.add(newUser);
}
}
listView.setAdapter(adapter);
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void clicbtn(View v){
Intent i = new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
}
my code activity 2
public class MainActivity2 extends ActionBarActivity {
private EditText ename;
private EditText ehome;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ename = (EditText) findViewById(R.id.editText);
ehome = (EditText) findViewById(R.id.editText2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void accion(View v){
Intent i = new Intent(MainActivity2.this, MainActivity.class);
//i.putExtra("ename",ename.getText().toString());
//i.putExtra("ehome",ehome.getText().toString());
startActivity(i);
}
}
onCreate() is getting called because you are again starting it as a new activity from this line
Intent i = new Intent(MainActivity2.this, MainActivity.class);
//i.putExtra("ename",ename.getText().toString());
//i.putExtra("ehome",ehome.getText().toString());
startActivity(i);
If you want to send values to previous activity use startActivityForResult
This SO question can help you implementing this How to manage `startActivityForResult` on Android?

How can i switch activity using Button?

How can i switch activity using Button? this is my problem:
package net.example.finals;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.AdapterView;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnClickListener {
Button bq;
Button bw;
Button be;
Button br;
Button bt;
Button by;
Button bu;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second,
container, false);
bq=(Button)rootView.findViewById(R.id.btn1);
bq.setOnClickListener(this);
bw=(Button)rootView.findViewById(R.id.btn2);
bw.setOnClickListener(this);
be=(Button)rootView.findViewById(R.id.btn3);
be.setOnClickListener(this);
br=(Button)rootView.findViewById(R.id.btn4);
br.setOnClickListener(this);
bt=(Button)rootView.findViewById(R.id.btn5);
bt.setOnClickListener(this);
by=(Button)rootView.findViewById(R.id.btn6);
by.setOnClickListener(this);
bu=(Button)rootView.findViewById(R.id.btn7);
bu.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//i dont know what to put here
case R.id.btn1:
if(bq.callOnClick()==true){
Intent myOwnIntent = new Intent(getActivity(), Second.class);
startActivity(myOwnIntent);
}
break;
}
}
}
Why did you use "if(bq.callOnClick()==true)" this?
Android use a lots of java code, first you need to learn java properly.
AND Google too
#Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btn1:
Intent myOwnIntent = new Intent(YourActivityName.this, Second.class);
startActivity(myOwnIntent);
break;
}
}
Try this one.
How to start new activity on button click
Start another activity by clicking a button
Android eclipse how to start a new activity on button click

Categories

Resources