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.
Related
`public class MainActivity extends Activity {
ImageButton button1;
ImageButton button2;
ImageButton button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button1 = (ImageButton)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
button2 = (ImageButton)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ThirdActivity.class);
startActivity(intent);
}
});
button3 = (ImageButton)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ForthActivity.class);
startActivity(intent);
}
});
}
}
`I'm stuck with building an apk, using Android Studio. Messages Gradle Build always shows ":app:mergeReleaseResource FAILD". I wonder if there are something wrong with .png file.The picture shows the details in Messages Gradle Build
It says Expression expected but no recommendations as to what symbol is missing. I'm using android studio. I suppose it's a simple fix I'm just not sure what symbol.
Heres my code:
public class CloudActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
},
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
}, // Where the problem is
));
}
#Override
public void onClick(View v) {
}
}
You are closing your anonymous inner classes (new View.OnClickListener() {) wrong. You shouldn't be separating them with a comma but closing them off with a );.
For example,
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
match up your {} and () for all of them. So you will want to do the same thing for button3.setOnClickListener(new View.OnClickListener() { and any others you might have.
You can read more about them in the Java Docs
Try This. You weren't closing your inner classes properly. I imagine you accidentally put commas in place of semi-colons?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
});
}
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(...);
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);
}
});
}
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);
}
});
}