I am trying to pass an imagebutton as a reference through an indexed array.
I thought I could set the ID and then pass that ID as such:
MyButton=(ImageButton) findViewById(R.id.imageButton1);
MyButton.setId(0);
Then in my onClick I want to pass index "0":
MyButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (MyButton.isSelected()){
Switch_Ctrl(0, OFF );
}
});
index is passed to method:
boolean Switch_Ctrl(char button_num, byte state){
button_num.setImageResource(R.drawable.switch_off);
button_num.setSelected(false);
}
I get error can't resolve method setImageResource.
So I can't use the id "button_num". Not sure how I can reference the ImageButton?
You can just store a reference to the button and change it onClick. Just make it an instance variable in your Activity or Fragment. You can declare a button seperately and then initialize it onCreate or onActivityCreated (once findViewById has become available). For example
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState){
myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
changeButtonBackground(v.getId());
});
}
}
//if doing this for multiple butons, check which one
//was pressed and change the background
private void changeButtonBackground(int id){
if(id == R.id.myButton){
myButton.setBackground(R.drawable.whatever);
}
}
Also - I wouldn't set the id of my button to anything else like 0. Your button already has an id, it's the one you defined in the XML as android:id="#+id/whateveritis". Unless you are creating view's programatically that do not exist in the XML (e.g. if you were adding TextView's to some parent layout on click of this button) you don't need to define a new id.
Related
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_started1);
mNextBtn1 = findViewById(R.id.getStartedBtn);
mNextBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started2);
mNextBtn2 = findViewById(R.id.button2);
}
});
mNextBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started3);
}
});
}
I am getting this error:
Process: com.example.storefrontapp, PID: 32160
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.storefrontapp/com.example.storefrontapp.Get_Started}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
What I am trying to achieve is to change layouts when button click. I want to show 1 field at a time as the user clicks next? For example, first user sees only Name TextField and click next to see Address TextField etc..Kinda like a registration page but with 1 field at a time.
I am creating 1 activity that points to multiple layouts. I can have Layout1, Layout2 Layout3 etc. Next button on Layout1 takes user to Layout2 and so on.?
Move your mNextBtn2.setOnClickListener() call into the click listener for mNextBtn1:
mNextBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started2);
mNextBtn2 = findViewById(R.id.button2);
mNextBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started3);
}
});
}
});
The way you have it now, your app will still try to set the click listener for mNextBtn2 immediately. Since the button is null until the user taps mNextBtn1, this will throw an exception.
If you're concerned about this nesting poorly, consider something like this instead. In your layout files, add the following attribute to each button you'd normally call setOnClickListener() for:
android:onClick="switchLayout"
Then, in your activity, create the switchLayout method:
public void switchLayout(View view) {
switch (view.getId()) {
case R.id.getStartedBtn:
setContentView(R.layout.get_started2)
break;
case R.id.button2:
setContentView(R.layout.get_started3)
break;
...
}
}
Now you don't need to call setOnClickListener() at all; your buttons will just automatically call through to this switchLayout() method when you click them. And you can use the button's id to figure out which layout to swap in.
You are getting that error because you did not initialize your second button. You can't attach a listener before initializing.
You have two options. Either initialize your second button outside of the listener like this:
mNextBtn1 = findViewById(R.id.getStartedBtn);
mNextBtn2 = findViewById(R.id.button2);
or put your second button inside of the listener of the first button:
mNextBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started2);
mNextBtn2 = findViewById(R.id.button2);
mNextBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started3);
}
});
}
});
From this code it creates dynamic buttons accodring to a given value from another layout. I need to get the id of that and add another button (if dynamic button clicks then I need to add another button dynamically).
for (int i = 0; i < value1; i++) {
LinearLayout.LayoutParams paramsIButton = new LinearLayout.LayoutParams
((int) ViewGroup.LayoutParams.WRAP_CONTENT, (int) ViewGroup.LayoutParams.WRAP_CONTENT);
ibutton = new ImageButton(HomePage.this);
ibutton.setImageResource(R.drawable.add);
ibutton.setLayoutParams(paramsIButton);
paramsIButton.topMargin = -70;
paramsIButton.leftMargin = 370;
paramsIButton.bottomMargin = 30;
ibutton.setId(i);
ibutton.getPaddingBottom();
ibutton.setBackgroundColor(Color.WHITE);
ibutton.setAdjustViewBounds(true);
rR.addView(ibutton);
}
If I understood correctly from the additional information you provided on your comment, you need to know when a user clicked on a button. You could set an OnClickListener to your button.
// Somewhere in your activity . . .
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// The button is clicked! Do whatever you want.
}
});
}
// ...
// Rest of the code
// ...
Of course, you should replace R.id.button1 with your button's id.
Seems to me like you need to add an onClickListener for the dynamically added button.
Make your class implement OnClickListener and then add a listener for the dynamic button:
ibutton.setOnClickListener(this);
and add an onClick Listener within your class:
#Override
public void onClick(View v)
{
// do something with this ID
v.getId()
}
I don't know how you keep track of the bulbs and fans, I'd hope you don't do it via the UI elements alone. I'd probably do it a bit differently, creating a data structure to track the bulbs and fans and attach the specific bulb or fan object to the UI element as a tag.
I am trying to put validations in my ImageButton's onClick(). It is like the tile in Boggle game. If I clicked image 1 then the nearest ImageButtons must be the only ImageButton that is clickable and the remaining buttons will be set as unclickable. How can I achieve it? Here's my code declared at onCreate().
public void tileClick() {
if (image1.isPressed()) {
image1.setClickable(false);
image1.setImageResource(R.drawable.changes);
//clickable when image1 is pressed/clicked
image2.setClickable(true);
image5.setClickable(true);
image6.setClickable(true);
//unclickable
image3.setClickable(false);
image4.setClickable(false);
image7.setClickable(false);
image8.setClickable(false);
image9.setClickable(false);
image10.setClickable(false);
image11.setClickable(false);
image12.setClickable(false);
image13.setClickable(false);
image14.setClickable(false);
image15.setClickable(false);
image16.setClickable(false);
}
}
CustomClickListener
//get ImageButton letter
View.OnClickListener myCommoClickListner = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Log.i(TAG, "arg0.getId()=" + arg0.getId());
if (arg0.getId()==R.drawable.a){
Log.i(TAG,"arg0.getId()="+arg0.getId());
generatedString=generatedString+("a");
text.setText(generatedString);
//change ImageButton's background when clicked
((ImageButton) arg0).setImageResource(R.drawable.changea);
//Set ImageButton clickable = false when already clicked
arg0.setClickable(false);
}
}
};
//all 16 ImageButtons declared like this :
image1.setOnClickListener(myCommoClickListner);
Change your method to accept an ImageButton
public void tileClick(ImageButton clickedBtn) {
// validation logic
}
pass the clicked ImageButton to the function from onClick()
View.OnClickListener myCommoClickListner = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
...
tileClick((ImageButton) arg0) // should change arg0 to something meaningful (v, view, etc...)
...
}
}
};
Then set the buttons clickable true/false according to the button passed.
You could put the ImageButtons in an Array and iterate over them and set the clickable according to which button was pressed.
In my onCreate, I insert every buttons of my Activity in a ArrayList and I loop on them to bind a clickListener. Only the last element gets the bind. Why is that?
for(Button bouton: tousLesBoutons) {
bouton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
resultat.setText(((Button) v).getText());
}
});
}
i do something similar but in the layout i set the onClick value to the same function for each button and then have a function like below. All 10 buttons hit this function,maybe you can try this approach
public void onButtonClick( View v )
{
Button but = (Button) findViewById( v.getId() );
String input = but.getText().toString();
Is there a way to have 1 onClick Lister for many buttons where I can toss a case statement to do things based on what buttons were clicked.
I know I can make 100 different listeners for 100 buttons but I have to think I can create some nifty variables to do it in less lines of code.
Button btn1, btn2;
public void onCreate(Bundle b)
{
// here you do normal things like assigning a
// content view to the activity, initiate buttons, etc.
// then you assign the same listener to both buttons
btn1.setOnClickListener(yourListener);
btn2.setOnClickListener(yourListener);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener yourListener = new View.OnClickListener(){
public void onClick (View v){
if( v == btn1 ){
// do something
}
elseif( v == btn1 ){
// do another thing
}
}
};
If you are using 1.6+ version of the SDK you can use android:onClick to set the onClick handler of a view. In your activity you must have a method with the following signature. The view is the view that was clicked.
void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
//do something fantastic;
break;
}
}
public class MainActivity extends Activity implements View.OnClickListener{
btnXXX.setOnClickListener(this);
public void onClick(View v) {
if (v.getId()==R.id.btnXXX){
dialog.show();
} else {
handleOtherViews(v);
}
}
Alternatively, you can specify the method to call in xml:
<Button android:id="#id/button" android:text="#string/button" android:onClick="someMethod" />