Lets say my main screen has a lot of Buttons.
I want to make a class in which all the Button listeners are inside and call the onClick from my main method each time I press a button.
public class Buttons extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onClick(View v) {
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button3);
int viewId = v.getId() ;
if(viewId == R.id.button1){
Intent ScrollViewTest = new Intent(this, ScrollViewTest.class);
startActivity(ScrollViewTest);}
if(viewId == R.id.button2){
Intent ScrollViewTest = new Intent(this, CameraTest.class);
startActivity(CameraTest);}
}
}
}
Is there a way to call the onClick method like:
Buttons allButtons = new Buttons();
allButtons.onClick(btn1);
or is my logic wrong ?
........................................
Already checked
calling a method from an onClick listener
How to call onClick(View v) method explicitly in an Android? Is it possible?
Android onClick method
Is there a method to set methods for multiple objects in one go?
public class Buttons extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button3); // did you mean R.id.button2?
// Create the onClickListener for btn1
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent ScrollViewTest = new Intent(this, ScrollViewTest.class);
startActivity(ScrollViewTest);
}
});
// Create the onClickListener for btn2
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent ScrollViewTest = new Intent(this, CameraTest.class);
startActivity(CameraTest);
}
});
}
Related
i have a one layout and 3 buttons and textView , when i cick a button it change the textView , button3 i have a song to play and textView show lyrics, and my problem here is how to make the MediaPlayer stopping when click any other button
here is my code i used :
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles= MediaPlayer.create(Langue.this, R.raw.iles);
iles.start();
}
});
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final MediaPlayer iles= MediaPlayer.create(Langue.this, R.raw.iles);//creating iles here
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles.start();
}
});
I believe this will do., in the above code i created a iles as MediaPlayer object and used it to stop the song...
So far I have a button from my second activity that opens a new activity. As shown below..
public class FifthActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
Now the thing is, I have multiple buttons in my fifthActivity.java that I need to make start a new Activity. Going from this code, From what part of the code do I need to put in my FifthActivity.java to make it so the other buttons open?
I don't really know, if I get your question. But I guess your FifthActivity contains some buttons and you want to start different activities with these buttons. (Am I right?)
Heres an short example:
public class FifthActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity1.class);
FifthActivity.this.startActivity(intent);
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity2.class);
FifthActivity.this.startActivity(intent);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity3.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
This is really basic android programming. If you struggle with this you should probably do some android tutorials, before starting programming your own app.
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),Travelling.class);
startActivityForResult(intent, 0);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),Rectangles.class);
startActivityForResult(intent, 0);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),Squares.class);
startActivityForResult(intent, 0);
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),Triangles.class);
startActivityForResult(intent, 0);
Button button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),Circles.class);
startActivityForResult(intent, 0);
Button button6 = (Button) findViewById(R.id.button6);
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),Cube.class);
startActivityForResult(intent, 0);
Button button7 = (Button) findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),Cuboid.class);
startActivityForResult(intent, 0);
Button button8 = (Button) findViewById(R.id.button8);
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),SimpleInterest.class);
startActivityForResult(intent, 0);
Button button9 = (Button) findViewById(R.id.button9);
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),UsefulFacts.class);
startActivityForResult(intent, 0);
}
});
}{
}
});}});}});}});}});}});}});}});}}
Ok to begin with...... my app works..........However anytime i press a random button lets say button3, nothing happens. I've noticed that to get the buttons to work i have to start pressing the button at the beginning then go down (sequentially that is).............When this is done the buttons start working. My question is: How can I make each button work with a direct touch(That is without having to press the list of buttons from top to bottom to get there)
If you have an algorithm like:
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),Rectangles.class);
startActivityForResult(intent, 0);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
then you are creating a click handler for button3 when you click on button2. This is not correct, you should define the click listener of button3 outside the scope of the click listener of button2. The same applies for your other buttons, as you have consistently repeated the same mistake in your code.
My suggestion would be to let your Activity handle the onClick event, and add all the listeners at the same level, as opposed to what you are doing now which requires you to press the first to button to reach the line of code that sets the OnClickListener for the second button and so on.
findViewById(R.id.button1).setOnClickListener(...);
findViewById(R.id.button2).setOnClickListener(...);
findViewById(R.id.button3).setOnClickListener(...);
In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}
Is there a way to open the system dialog settings->location & security->Install from SD card programmatically from my application?
You launch at least the security setting by the following code.
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName(
"com.android.settings",
"com.android.settings.SecuritySettings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}