I am basically only trying to get R.id.button1 to open up Google in a web browser, not sure whats wrong!
No errors, it just does nothing when pressing the button, I am using the emulator.
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
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.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btn;
#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();
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener((OnClickListener) this);
}
}
public void onClick(View v)
{
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
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 {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
It seems to me your button is in your fragment layout (right?)
then you should handle your button's events in your fragment, not in the activity!
Your onCreate() method should be like this :
#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();
}
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener( new OnClickListener{
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
});
}
The problem is here :
btn.setOnClickListener((OnClickListener) this);
replace it with :
btn.setOnClickListener(this);
And implement View.OnClickListner in order to get the onClick method
public class MainActivity extends ActionBarActivity implements View.OnClickListner {
/*
some code here
*/
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
}
You can also do it like this :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
});
Implement OnClickListner
public class MainActivity extends ActionBarActivity implements OnClickListner {
Button btn;
Define the listener to your button1.
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
use a switch in onClick() method to handle several views:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
break;
default:
//code..
break;
};
}
other way to set the listener to your button:
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener( new OnClickListener{
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
Related
I did not expect an error to come up but the 3-dot ActionBar menu is not displaying and I am getting an unexpected error. I am not sure where I went wrong in my code.
Please help,
thanks in advance!
MainActivity.java
package com.example.it5.foothillers;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button;
Button button2;
Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Display app icon in the ActionBar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.ic_launcher);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
}
private void buttonClick() {
startActivity(new Intent("it5.foothillers.news"));
}
private void button2Click() {
startActivity(new Intent("it5.foothillers.sports"));
}
private void button3Click() {
startActivity(new Intent("it5.foothillers.events"));
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
buttonClick();
break;
case R.id.button2:
button2Click();
break;
case R.id.button3:
button3Click();
break;
}
}
#Override
public void onPause() {
super.onPause();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(MenuItem item) {
int id = item.getItemId();
}
}
You have 3 onCreateOptionsMenu methods. Remove the last 2, you only need the first one with inflater. Also, Override the onOptionsItemSelected method to control the actions.
Hey guys so i'm trying to get my imagebutton to work within my fragment. The code works fine with a Activity but I cannot get it to work within the fragment. What do I need to change? Errors keep occurring within the configureImage method, Thanks guys.
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*
*/
public class FragmentA extends Fragment {
public FragmentA() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_a);
configureImageButton();
}
private void configureImageButton() {
// TODO Auto-generated method stub
ImageButton btn = (ImageButton) findViewById(R.id.imageButton1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(FragmentA.this, "You Clicked the button!", Toast.LENGTH_LONG).show();
}
});
}
Your fragment implementations is wrong. Do this way.
public class FragmentA extends Fragment {
private View v;
public FragmentA() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_a,container, false);
configureImageButton();
return v;
}
private void configureImageButton() {
// TODO Auto-generated method stub
ImageButton btn = (ImageButton) v.findViewById(R.id.imageButton1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();
}
});
}
}
public class FragmentA extends Fragment {
public FragmentA() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.fragment_a, container, false);
configureImageButton(view);
return view;
}
private void configureImageButton(View view) {
// TODO Auto-generated method stub
ImageButton btn = (ImageButton) view.findViewById(R.id.imageButton1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();
}
});
}
}
use this this will work for you, and follow this for fragment.
public class FragmentA extends Fragment {
ViewGroup rootViewA;
ImageButton btn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootViewA = (ViewGroup) inflater.inflate(
R.layout.fragment_a, container, false);
btn = (ImageButton ) rootViewA
.findViewById(R.id.imageButton1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();
}
});
return rootViewA;
}
}
I'm trying to declare buttons and EditTexts in the OnCreate() function, calling the function findViewById(). But when I try to use the button in the emulator the application fails. When I use the findViewById() in the function of the treatment of the event (I used android:onClick because the treatment in java wasn't working either) the aplication worked normally. Why I can't only use the findViewById() one time, inside the onCreate?
import android.app.Activity;
import android.app.Fragment;
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.widget.Spinner;
public class MainActivity extends Activity implements OnClickListener{
String[] names;
EditText status;
EditText par1;
EditText par2;
EditText par3;
Button b1;
Button b2;
Button b3;
Button b4;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (EditText) findViewById(R.id.text);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
status.setText("HA");
}
});
Button b2 = (Button) findViewById(R.id.b2);
b2.setActivated(false);
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Display any data about the persons
}
});
Button b3 = (Button) findViewById(R.id.b3);
b3.setActivated(false);
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Start the sensors lecture
}
});
Button b4 = (Button) findViewById(R.id.b4);
b4.setActivated(false);
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Execute the music
}
});
}
public void treatment_button(View v){
status = (EditText) findViewById(R.id.text);
status.setText("Connect");
}
#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 {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Inside oncreate() you have again define Button b1 like:
Button b1 = (Button) findViewById(R.id.b1);
Change it to:
b1 = (Button) findViewById(R.id.b1);
As you are creating the Button variable inside onCreate(),it will not work outside that method.So use the global reference variable of the Button that you have created.
try this hope it's worked:
import android.app.Activity;
import android.app.Fragment;
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.widget.Spinner;
public class MainActivity extends Activity {
String[] names;
EditText status;
EditText par1;
EditText par2;
EditText par3;
Button b1;
Button b2;
Button b3;
Button b4;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (EditText) findViewById(R.id.text);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status.setText("HA");
}
});
b2 = (Button) findViewById(R.id.b2);
b2.setActivated(false);
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Display any data about the persons
}
});
b3 = (Button) findViewById(R.id.b3);
b3.setActivated(false);
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Start the sensors lecture
}
});
b4 = (Button) findViewById(R.id.b4);
b4.setActivated(false);
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Execute the music
}
});
}
public void treatment_button(View v){
status = (EditText) findViewById(R.id.text);
status.setText("Connect");
}
#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 {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
I have made an app recently and I added a count down timer one day and made a few changes here and there and now when I run the app on my (actual) Galaxy Note 3 it crashes as soon as I hit the Play button below are my LevelOne.java file and my MainActivity.Java file. By the way the logcat shows nothing as if it doesn't realize it crashed.
LevelOne.java
package com.parker.orangedot;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class LevelOne extends ActionBarActivity {
Button myButton;
LevelOne context;
CountDownTimer timer = new CountDownTimer(1500, 1500)
{
#Override
public void onFinish() {
Intent intent = new Intent(context, Scores.class);
startActivity(intent);
}
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_level_one);
//ass soon as this activity is created, I start my timer.
timer.start();
myButton = (Button) findViewById(R.id.button2);
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
timer.cancel();
}});
}
#Override
public void onBackPressed() {
}
public void next(View view) {
// Do something in response to button
Intent intent = new Intent(this, LevelTwo.class);
startActivity(intent);
}
public void openScores(View view) {
// Do something in response to button
Intent intent = new Intent(this, Scores.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.level_one, 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 {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_level_one,
container, false);
return rootView;
}
}
}
MainActivity.java
package com.parker.orangedot;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void openScores(View view) {
// Do something in response to button
Intent intent = new Intent(this, Scores.class);
startActivity(intent);
}
public void play(View view) {
// Do something in response to button
Intent intent = new Intent(this, LevelOne.class);
startActivity(intent);
}
#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 {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
UPDATE:
When using the emulater for the Galaxy Nexus, pressing "play" does absolutely nothing.
I'm having two button in one activity. One button having onclick event & another one is onclicklistener. If i'm going to run my application, it shows force close error. Is there possible to use these two event at the same activity?
the OnClickListener for all the buttons as follows:
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
then add the onClick() event as shown below:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == btn1){
//Things to do
}
if(v == btn2){
//Things to do
}
}
You can use...
public class MyClass extends Activity implements OnClickListener
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
btn1. setOnClickListener( new OnClickListener()
{
public void onClick( View v )
{
//add action for btn1.
}
});
btn2.setOnClickListener( this );
}
public void onClick( View v )
{
if( v== (Button)findViewById( R.id.btn2_id )
{
//perform action for btn2.
}
}
}
Can you please sent actual error from logcat.
OR Following code may be help full.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Test2Activity extends Activity implements OnClickListener{
private Button btn1, btn2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(this);
btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
System.out.println("Button 1 Press");
break;
case R.id.button2:
System.out.println("Button 2 Press");
break;
default:
break;
}
}
}
You have two options:
(as specified by Uttam) is to implement one OnClickListener and add a switch case to distinguish between different buttons.
Create a separate anonymous OnClickListeners for each button:
btn1.setOnClickListener(new OnclickListener() {
#Override
public void onClick(View v) {
// first button behavior
}
}
btn2.setOnClickListener(new OnclickListener() {
#Override
public void onClick(View v) {
// second button behavior
}
}