Multiple buttons in MainActivity Android studio - java

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

Related

I want to do INVISIBLE on SubActivity with the button on MainActivity

SubActivity cannot INVISIBLE the button in MainActivity I tried to use intent, but I'm not sure.
it SubActivity java code
try to INSIBLE the resetB button in Sub Activity
public class SubActivity extends Activity {
Button resetH, resetH2;
Button resetB, miC;
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
ImageButton back = (ImageButton) findViewById(R.id.back);
resetH = (Button) findViewById(R.id.resetH);
resetH2 = (Button) findViewById(R.id.resetH2);
resetB = findViewById(R.id.resetB);
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.VISIBLE);
}
});
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.INVISIBLE);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
I used intent, but I can't move the button

Passing EditText integer to TextView via Intent app crashes after clicking (button) three

I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.
Home.java
start and exit button
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.b1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToSecondActivity();
}
});
Button two = (Button) findViewById(R.id.b2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void goToSecondActivity() {
Intent i = new Intent(this, SelectNumberOfPlayers.class);
startActivity(i);
}
}
SelectNumberOfPlayers.java
Taking only the numbers from the input and passing it to StartGame.class
public class SelectNumberOfPlayers extends AppCompatActivity {
EditText numberOfPlayers;
Button three;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_number_of_players);
three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String txt = numberOfPlayers.getText().toString();
Intent i = new Intent(getApplicationContext(), StartGame.class);
i.putExtra("players", txt);
startActivity(i);
}
});
}
}
StartGame.java
Receiving Int and printing to TextView
public class StartGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("Player number"));
}
}
Where is the error happening? I've set the input keyboard to take only numbers
1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity
numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);
2.
the key must be same to pass and receive data with intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("players"));
}

Getting a crash every time i press a button"Android Studio"

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

Adding second onClick method in Android Studio?

I've just started to learn Java and I've been stumped on adding the code for a second button in an activity. I apologize for my (possible dumb question) and any wrong terminology.
Here is the MainActivity Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, GoActivity.class));
}
});
}
}
How can I add to this code for btnEscape to go to EscapeActivity?
You just have to do exacly the same thing you do with btnGo - this is find your button by id, and then set clickListener to it. It could looks like that:
Button btnEscape = (Button) findViewById(R.id.btnEscape);
btnEscape.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, EscapeActivity.class));
}
});
The optimised way to do that is implement your class with View.OnClickListener and override the method onClick and inside it use switch cases to switch between views and apply on clicks like this:
public class SampleActivity extends AppCompatActivity implements View.OnClickListener{
Button btnGo,btnEscape;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btnGo);
btnEscape= (Button) findViewById(R.id.btnEscape);
btnGo.setOnClickListener(this);
btnEscape.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnGo:
startActivity(new Intent(MainActivity.this, GoActivity.class));
break;
case R.id.btnEscape:
startActivity(new Intent(MainActivity.this, EscapeActivity.class));
break;
default:
break;
}
}
}
In code, what the two comments by Shiram and Nik above are saying is to add the block below that begins Button btnEscape... after Button btnGo.setOnclick's block.
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Button btnGo = (Button) findViewById(R.id.btnGo);
...
Button btnEscape = (Button) findViewById(R.id.<<name of button in xml>>);
btnEscape.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... whatever ... }
});
}
In short, the following is a really good pattern to have at your fingertips:
Button btn___ = (Button) findViewById(R.id.<<name of button in xml>>);
btn___.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... whatever ... }
});

How do I call the FileExplorer.java with a button from 2 available buttons?

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.

Categories