I have an activity class with many buttons. If i click one button then it will go to next page then go back to main class.If I click another button in main class, it will go to next page together with data. Do anyone know how to write the function in activity class?
Can I write like this in a class? But when i run it only one button is working , when i clicked other i get error. I am new to android ,so please give me suggestion.
public class MyClass extends Activity {
private Button button,button1,button2;
public void onCreate(){.... initControl();}
public void initControl() { button=(Button)findViewById(R.id.button); .....
button.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
button1.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
button2.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
}
thanks for help.
First initialize button1, button2 before overriding onCreate(). Then assign the values in oncreateMethod call initializemthod
Have a look on the following code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class WebViewTest extends Activity {
Button button1 = null;
Button button2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.firstbutton);
button2 = (Button) findViewById(R.id.secondbutton);
initControl();
}
public void initControl() {
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
}
Thanks
Deepak
You have to add on click method
Related
Hi I am new in android learning and I'm practice it. I Want to implement click listener on my button but it is giving me error. even i imported android.view.View.OnClickListener; and other all libraries.
please have look at my code check where is my mistake
Cannot Resolve symbol onClickelistner.
package com.example.nexus.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
}
button.OnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
Try this and put it in onCreate method
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
Combine R2R and Toan Tran answers will give you the best solution:
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
This should work.
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
}
});
Try button click inside Oncreate()
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
}
I am learning Android and trying one simple Android app development, I got one demo code from my lecture and the teacher simply do the following:
There are 2 buttons, 1 textview. When touching button A, it will show "text A" in the textview, while touching button B, it will present "text B" in textview.
I followed the code and rewrote it, but i can't get the correct result when I ran with emulator.
When I touch either button, there's no content in the TextView. But my teacher's reference code works:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
How is report(View v) called? I can't understand how this class is called. Could someone please help me out?
You need to let your button know that, when pressed, report() should be called. This may be done through the android:onClick attribute of your button on your layout's XML:
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_text"
android:onClick="report" />
Or by code, attaching an OnClickListener to the button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
report(v);
}
});
}
Hope it helps.
You should implements the View.OnClickListener, and override that OnClick method. You also need to setOnClickListener on your buttons. So your code should be like:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity implements View.OnClickListener{
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 =(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
button2 =(Button)this.findViewById(R.id.button2):
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
I want to either be able to have a single button that you click to start and click again to stop or a start button that does not start multiple sounds each time it clicks. I have an audio file that if you click start 3 times, it has 3 different tracks going all at once. And the stop button stops only one, the other tracks keep playing until I kill the app.
Here is my code. I am a noob
package com.example.mitch_000.button;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player=MediaPlayer.create(MyActivity.this,R.raw.lw);
player.start();}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
}
When you call player=MediaPlayer.create(MyActivity.this,R.raw.lw); in your OnClickListener, you are creating a new instance of the audio file and then playing it every time the button is clicked.
You need to create a single instance of the player object outside of the OnClickListener (in OnCreate) and then you can use this reference to play, pause and stop the one audio file.
Here is the code you can use:
package com.example.mitch_000.button;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
player=MediaPlayer.create(MyActivity.this,R.raw.lw);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
put
MediaPlayer mp = MediaPlayer.create(this, R.raw.mamacita_zero);
outside of setOnClickListener
like this
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
MediaPlayer mp = MediaPlayer.create(MyActivity.this,R.raw.lw);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
}
You seem to be doing per click than you need to. You're creating and adding a new onClickListener for every click in the Activity's View, not the Button. You only need to set the listener once, and for the Button rather than the overarching View; I tend to do that in the constructor of the Activity.
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 ;
}
I am trying to move from one activity to another activity by clicking to the ImageButton. But when I click to button, it doesn't move to the activity which I specify in the code, and even it does not throw an error. I'm not getting where is problem
Here is my code which calls next activity :
package com.birthdayreminder;
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;
import android.widget.ImageButton;
import android.widget.TextView;
public class Reminder extends Activity {
ImageButton view, add, edit;
TextView tvadd, tvedit, tvview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
}
public void innicialize() {
// assigning buttons
view = (ImageButton) findViewById(R.id.bView);
add = (ImageButton) findViewById(R.id.bAdd);
edit = (ImageButton) findViewById(R.id.bEdit);
// assign textview
tvadd = (TextView) findViewById(R.id.tvAdd);
tvedit = (TextView) findViewById(R.id.tvEdit);
tvview = (TextView) findViewById(R.id.tvView);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
}
}
I have also declared the class name in the manifest.xml but it does not working
Logcat Log file : 5 lines of last logcat file after clicking the button :
05-15 18:19:25.495: W/AudioFlinger(33): write blocked for 69 msecs, 1245 delayed
writes, thread 0xc658
05-15 18:19:28.964: I/ActivityManager(60): Starting: Intent {
act=com.birthdayreminder.REMINDER cmp=com.birthdayreminder/.Reminder } from pid 548
05-15 18:19:29.409: I/ActivityManager(60): Displayed
com.birthdayreminder/.Reminder: +426ms
try using getApplicationContext() in place of Remainder.this
check if you have a click listener on you imagebutton, you have declared what to do on click, but if you don't have any listener on the imagebutton, it won't work anyway.
You can try something like this :
yourImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Reminder.this, Addreminder.class);
startActivity(intent);
}
});
After doing this, you must have declared in your XML file the activity
<activity android:name="packageName.className" />
Finally, check if the new class is working well too but it must be ok!
==================================================================================
You mustn't declare a function or procedure in a function (like you did in onCreate), you can simply do what you do in initialize in your onCreate function like this :
package com.birthdayreminder;
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;
import android.widget.ImageButton;
import android.widget.TextView;
public class Reminder extends Activity {
private ImageButton view;
private ImageButton add;
private ImageButton edit;
private TextView tvadd;
private TextView tvedit;
private TextView tvview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
// Instantiating Buttons
view = (ImageButton) findViewById(R.id.bView);
add = (ImageButton) findViewById(R.id.bAdd);
edit = (ImageButton) findViewById(R.id.bEdit);
// Instantiating Views
tvadd = (TextView) findViewById(R.id.tvAdd);
tvedit = (TextView) findViewById(R.id.tvEdit);
tvview = (TextView) findViewById(R.id.tvView);
// Adding a clickListener on add button
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
// Adding a clickListener on edit button
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
// Adding a clickListener on view button
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
}
}
If you still have a problem, check your XML files, and look if you have well declared your buttons.
Problem Fixed: Never define a function in a function, because everytime you call the first function you'll define again the second function.
What you did which is wrong :
onCreate { //WRONG VERSION
bla bla bla bla
initialisation {
bla bla bla
}
}
What you have to do :
onCreate { //GOOD VERSION
initialisation();
bla bla bla
}
initialisation {
bla bla bla
}
you can call a function in a function, but you can't define a function in a function.
Moreover, you never call your initialisation function, you must call it now in your onCreate function or nothing will happen.
have you set onClickListener on add button.
btnAdd.setOnClickListener(this);
also check the
is your v.getId() and case R.id.bAdd: same?
debug and check that your case R.id.bAdd: is getting executed..
Seperately initialize the buttons in the first activity as
Button bAddbutton= (Button) findViewById(R.id.bAdd);
Button bEditBtn= (Button) findViewById(R.id.bEdit);
Button bViewBtn= (Button) findViewById(R.id.bView);
Then add seperate onClick listeners for each button as given below,
bAddbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
bEditBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
bViewBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
initialize the button and add listener first in oncreate()
Button btnAdd = (Button) findViewById(R.id.bAdd);
btnAdd.setOnClickListener(this);