I'm having issues with my view controller in my android application. I'll admit I'm a noob at android programing please forgive me here is my issue and photos of the application. Basically i just what to press the button and go to the related page and i can't quite figure it out.
http://imgur.com/aVpqUCZ
package com.Apps.jarredapp;
public class ViewController extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button btnSearchStudent = (Button) findViewById(R.id.button1);
Button btnNewStudent = (Button) findViewById(R.id.button2);
Button btnLegalInfo = (Button) findViewById(R.id.button3);
btnSearchStudent.setOnClickListener(this);
btnNewStudent.setOnClickListener(this);
btnLegalInfo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Intent intent = new Intent(this, SearchStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button2: {
Intent intent = new Intent(this, NewStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button3: {
Intent intent = new Intent(this, LegalInfoActivity.class);
startActivity(intent);
break;
}
}
}
{
Button searchStudentButton;
Button newStudentButton;
Button legalButton;
searchStudentButton = (Button) findViewById(R.id.button1);
searchStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, searchStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
newStudentButton = (Button) findViewById(R.id.button2);
newStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, newStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
legalButton = (Button) findViewById(R.id.button3);
legalButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, legalActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
You have written two onclicklisteners for one button, use only one then it will work
Related
I am trying to make a mobile application,but when ever i try to press the button i get a crash. The button should take me to a new activity page, i have already connect the other pages with the button in a right way. i am just wondering if this code is correct or not:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v)
{
Button a1= (Button) findViewById(R.id.button);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
}
public void onButtonClick1(View c)
{
Button a1= (Button) findViewById(R.id.button2);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, ThirdPage.class);
startActivity(intent);
}
});
}
public void onButtonClick2(View d)
{
Button a1= (Button) findViewById(R.id.button3);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FourthPage.class);
startActivity(intent);
}
});
}
public void onButtonClick3(View f)
{
Button a1= (Button) findViewById(R.id.button4);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FifthPage.class);
startActivity(intent);
}
});
}
}
i just learned java from some videos in the youtube,so i am not sure if i did the activity function well or not. Thank you
There can be few reasons after it
1) Make sure you defined activity in Manifest file
2) check for android:onClick="" in xml file..
OR
Try Binding Buttons in onCreate() method.
and You can simply use this for onClick
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
Remove onClick attribute in your activity_main.xml file and copy and past this code in your MainActivity.java file
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button) findViewById(R.id.button);
b2= (Button) findViewById(R.id.button2);
b3= (Button) findViewById(R.id.button3);
b4= (Button) findViewById(R.id.button4);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, ThirdPage.class);
startActivity(intent);
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FourthPage.class);
startActivity(intent);
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FifthPage.class);
startActivity(intent);
}
});
}
}
If you are using the onclick attribute in layout XML then you doesn't need to set the onclick listener. If you are not using onclick then you need to set click listener. In your case your case, I think use are using onclick attribute in layout XML and also trying to set the click listener in using java code. so you need to either use onclick attribute in layout XML or use setOnCliickListener() in java code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText textmsg;
static final int READ_BLOCK_SIZE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textmsg = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
noteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Notes.class);
}
});
resuBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Results.class);
}
});
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
}
});
When I run the application the buttons don't work. If I set the buttons as so..
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
Button resuBtn= (Button) findViewById(R.id.resuBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
If I use this code above, the code works fine and the buttons work correctly. But other functionality with different classes/activities won't run. Could someone please show me a solution or explain how to solve this issue.
You should place:
this.noteBtn = (Button) findViewById(R.id.noteBtn);
this.notBtn.setOnClickListener(this);
this.resuBtn = (Button) findViewById(R.id.resuBtn);
this.resuBtn.setOnClickListener(this);
this.timeBtn = (Button) findViewById(R.id.timeBtn);
this.timeBtn.setOnClickListener(this);
in onCreate() instead of onClick(). Make the buttons belong to the class, so you can reference them in onClick(). You should not be setting new onClickListeners in onClick(), but should rather have a switch statement based on the clicked view's id to determine which button was pressed.
this code
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button timeBtn = (Button) findViewById(R.id.timeBtn);
...
}
is not working because it's never going to get call, since none of your buttons is implementing it, worse yet they have not even been initialized because the onClick has not and will never be called.
the correct way to do it is like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but1 = (Button) findViewById(R.id.but1);
Button resBtn = (Button) findViewById(R.id.resBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agendaBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewTimeTable) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
because you are first getting a reference to your buttons and assigning them the onClickListener to each one of it.
I will suggest reading more about the Android Activity life cycle you can find it
here
I have two buttons in my layout namely,
buttonAddImage
buttonLoadImage
Now my need is, I want to enable "buttonLoadImage" If user onclick "buttonAddImage" else "buttonLoadImage" always disabled.How to achieve that help me.
final Button buttonAddImage = (Button) findViewById(R.id.buttonAddPicture);
final Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonAddImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonLoadImage.setEnabled(true);
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
}
});
Add the code for disabling the button outside onClick method
final Button buttonAddImage = (Button) findViewById(R.id.buttonAddPicture);
final Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setEnabled(false);
buttonAddImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonLoadImage.setEnabled(true);
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
}
});
try setting the listener of buttonLoadImage in onclick of buttonAddImage
#Override
public void onClick(View view) {
buttonLoadImage.setOnClickListener(this);
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
}
use this
final Button buttonAddImage = (Button) findViewById(R.id.buttonAddPicture);
final Button buttonLoadImage = (Button)findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setEnabled(false);
buttonAddImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonLoadImage.setEnabled(true);
}
Define varible in your class like this
private boolen ISCLICKED=false;D
then
buttonAddImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
ISClLICKED=true
}
});
now check button is clicked or not
if(ISCLICKED==true){
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do somththing
}
});
}else
{
Toast.maketext(contex,"Add image 1st",Toast.LENGHT_SHORT).show;
}
My problem is in set OnClickListener I don't know why but always at the start my game crushes:
ImageButton button1 = (ImageButton) findViewById(R.id.imagePlay);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
callListOfLevels();
}
});
ImageButton button2 = (ImageButton) findViewById(R.id.imageQuit);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
public void callListOfLevels(){
Intent intent = new Intent(this, ListOfLevels.class);
startActivity(intent);
}
I am still new to Java
I am trying to implement the FileExplorer! <<= link
I have 2 buttons, one of them is to call the ExplorerFile class. But it does not seem to work.
My first button seems to work though.
The followings are my code (buttons):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Status = (TextView)findViewById(R.id.app_status);
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.setPriority(500);
this.registerReceiver(mUsbReceiver, filter);
DeviceInformationContext = this;
//Button1
Button connectBtn = (Button)this.findViewById(R.id.connectBtn);
connectBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myMucNil.Muc_Config(19256, (byte) 0x8A);
}
});
myMucNil = new MUC_NIL(DeviceInformationContext);
//Button2
Button getPy = (Button) findViewById(R.id.getFilePy);
OnClickListener pyList = new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, FileExplorer.class));
}
};
getPy.setOnClickListener(pyList);
}
I thank you in advance for your kind help, if you would like to guide me.
Try this code...
Button getPy = (Button) findViewById(R.id.getFilePy);
getPy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), FileExplorer.class));
}
});
And remove following code.
Button getPy = (Button) findViewById(R.id.getFilePy);
OnClickListener pyList = new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, FileExplorer.class));
}
};
getPy.setOnClickListener(pyList);
Also you need to register FileExplorer.java file in AndroidMenifest.xml file.
using following code you register.
<activity android:name="FileExplorer"></activity>
before application tag is over.