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 ... }
});
Related
I'm super new in Java! Pardon for this question although there are similar ones but I'm completely clueless on fixing my problem.
I'm trying to set an OnClickListener for each ImageButton to open a new activity.
The first ImageButton works but not for the subsequent ones, it is unclickable in AVD.
Would greatly appreciate some help on it!
public class CharacterSelect extends AppCompatActivity {
ImageButton arrowbtnright;
ImageButton contchibtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_select);
arrowbtnright = (ImageButton) findViewById(R.id.arrowbtnright);
contchibtn = (ImageButton) findViewById(R.id.contchibtn);
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
}
});
}
You are currently adding the listener of the second button, from inside the listener of the first button, this is probably not what you want to do .
Try putting both at the same level, i.e :
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
}
});
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
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 activities. First activity has different layouts which opens another activity. In another activity I have a segmented group of radio button which I want to set it checked on the basis of the layout is clicked in first activity. To check the radio button I have used this.
segmented5.check(R.id.pickupbakButton);
This sets the button checked but I want it to be checked from another activity on basis of that I have another changes in the layout..
I have first activity like this :
and lets say onClick of PICKUPBOX/BLIND VAN
It should show radio button of pickupbox/blind van checked.
How can I achieve this??
GoBoxActivity
public class GoBoxActivity extends AppCompatActivity {
private Intent i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_go_box);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("GO-BOX");
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
final GoBoxPickupActivity gobox = new GoBoxPickupActivity();
LinearLayout pickupBak = (LinearLayout)findViewById(R.id.PickupBak);
LinearLayout picupBox = (LinearLayout)findViewById(R.id.pickupbox);
LinearLayout engkelBak = (LinearLayout)findViewById(R.id.engkelBak);
LinearLayout engkelBox = (LinearLayout)findViewById(R.id.engkelBox);
pickupBak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
startActivity(i);
}
});
picupBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
startActivity(i);
}
});
engkelBak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
startActivity(i);
}
});
engkelBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
startActivity(i);
}
});
}
}
PickupBoxActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_go_box_pickup);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
segmented5 = (SegmentedGroup) findViewById(R.id.segmented3);
segmented5.setOnCheckedChangeListener(this);
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.pickupbakButton:
engkelTextview.setVisibility(View.GONE);
extraEngkelTextView.setVisibility(View.GONE);
airConditioning.setVisibility(View.GONE);
picBoxTextview.setVisibility(View.VISIBLE);
extraPickBoxTextview.setVisibility(View.VISIBLE);
return;
case R.id.pickupboxButton:
engkelTextview.setVisibility(View.GONE);
extraEngkelTextView.setVisibility(View.GONE);
airConditioning.setVisibility(View.VISIBLE);
picBoxTextview.setVisibility(View.VISIBLE);
extraPickBoxTextview.setVisibility(View.VISIBLE);
return;
case R.id.engkelbakButton:
picBoxTextview.setVisibility(View.GONE);
extraPickBoxTextview.setVisibility(View.GONE);
engkelTextview.setVisibility(View.VISIBLE);
extraEngkelTextView.setVisibility(View.VISIBLE);
airConditioning.setVisibility(View.INVISIBLE);
return;
case R.id.engkelboxButton:
picBoxTextview.setVisibility(View.GONE);
extraPickBoxTextview.setVisibility(View.GONE);
engkelTextview.setVisibility(View.VISIBLE);
extraEngkelTextView.setVisibility(View.VISIBLE);
airConditioning.setVisibility(View.VISIBLE);
return;
default:
engkelTextview.setVisibility(View.GONE);
extraEngkelTextView.setVisibility(View.GONE);
airConditioning.setVisibility(View.GONE);
picBoxTextview.setVisibility(View.VISIBLE);
extraPickBoxTextview.setVisibility(View.VISIBLE);
}
}
}
Modify your code inside onClick in GoBoxActivity, as follows
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
i.putExtra("selected", 1);
startActivity(i);
And in PickupBoxActivity , do something like this
int selected = getIntent().getIntExtra("selected",1);
after that
switch(selected)
{
case 1:
segmented5.check(R.id.pickupbakButton);
break;
case 2:
.... So on
}
Let me know further for any issues..
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.