I am developing a new Android app, i have listed out the records. Now need to show the particular record while clicking on the corresponding button.
Can somebody help me to share how to create buttons dynamically and assign record id into it?
Button btn=new Button(this);
btn.setId(you record id here);
then add this button to your view
RelativeLayout relativeLayout =(RelativeLayout)findViewById(R.id.relative);
Button button2 = new Button(this);
button2.setId(recordId);
relativeLayout.addView(button2);
you can do as below
r
LinearLayout btnLayout = new LinearLayout(this);
Button send = new Button(this);
send.setText("Send");
send.setTextColor(Color.WHITE);
btnLayout.addView(send);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Write yout code here
}
}
Why can't you list it out in a ListView?? If you can do so, you can dynamically add items easily, and get the click function from listItem's onclick listner
ListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// Write your click action here
}
});
Related
My program asks a user to enter their name and click on a button called btn. Once btn is clicked, their name is dynamically added to a TableRow along with another dynamically created Button. It's these Buttons that I'm having an issue with. I need to somehow access them later on in the program. I created a number of IDs in my res/value folder to keep track of each one(changebtn1, changebtn2, etc..). They're all stored in an array called buttonIDs.
Let's say that the user enters the first name, a new row is created with a dynamically created button:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tableRow = new TableRow(getApplicationContext());
Button changeButton = new Button(getApplicationContext());
changeButton.setText("Change");
changeButton.setId(buttonIDs[i]);//From res/values
tableRow.addView(changeButton);
tableLayout.addView(tableRow);
i++;
});
Now let's say they enter a second name, another Button is created and so on and so forth. How can I now set an OnClickListener to my first Button that I created, which has the ID of R.id.changeBtn1? In other words, I have all of these dynamically created buttons and am not sure how to add OnClickLsteners() to earlier ones or access them in anyway. Thank you
Or you attach the OnClickListener directly in the creation of the button or you can store the references to the buttons like this:
ArrayList<Button> buttons = new ArrayList<Button>();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tableRow = new TableRow(getContext());
Button changeButton = new Button(getContext());
buttons.add(changeButton);
changeButton.setText("Change");
changeButton.setId(buttonIDs[i]);//From res/values
tableRow.addView(changeButton);
tableLayout.addView(tableRow);
i++;
});
for(Button button: buttons){
button.setOnClickListener(new OnClickListener()
...etc...
);
}
You won't waste a lot of memory since the buttons.add() line won't copy the button in the array but just the reference to the button. If you need a in id access to the buttons, use an HashMap, like this:
HashMap<String, Button> map = new HashMap<String, Button>();
map.put("id", new Button(getContext()));
And then access it like this:
Button button = map.get("id");
How about something like this
for(int i=0;i<buttonIDs.size();i++) {
Button currentButton = (Button) findViewById(buttonIDs[i]);
currentButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Stuff
}
});
}
I did not check the code
#filnik The first part of your answer also gave me an idea. I created an OnClickListener method outside of my OnCreate() method.
View.OnClickListener changeTeamName(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
//Do Stuff
}
};
}
I then set an OnClickListener to EACH dynamically created Button and use the method that I created.
changeButton.setText("Change");
changeButton.setTag("ButtonOne");
changeButton.setOnClickListener(changeTeamName(changeButton));
The fact that each Button now has an OnClickListener associated with it, they can now perform whatever function I add to my method.
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.
We make listener for Items(Rows) click listener in ListView. How to get know, in a particular row a specific View get clicked?
R1 --> TextView1 | TextView2
R2 --> TextView1 | TextView2
I can get which row is clicked. but i also want to know which View of the row get clicked.
You need to set position as tag to textView, and then onClick you can receive its tag.
So basicly you set textView's staff on adapter's getView method
textView.setTag(position);
textView.setOnClickListener(listener);
And then you'll have click listener like this
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
// Do whatever you like...
}
};
You can add specific click handlers to the textViews:
<TextView
android:onClick="clickHandler">
Then in your activity:
public void clickHandler(View v) {
// obtain parent row
LinearLayout parentView = (LinearLayout) v.getParent();
// with the parentView you can get all other views as well:
TextView textView1 = (TextView) parentRow.getChildAt(0);
}
I do it like this:
assign onClickListener to whatever can be clicked
I assign tag to it to identify v.setTag(R.id.itemId, item.id);
In onclick I do:
#Override
public void onClick(View v) {
final int id = (Integer) v.getTag(R.id.itemId);
}
I am adding buttons to a linear layout at runtime and need to add the functionality of removing them if the user desires. At the moment I have a button which opens a popup with a list consisting of the text for each button added. If possible, would I be able to have each onItemClick delete the corresponding button? If not, what would be the best way to remove a specific button?
Here is the code for adding buttons:
private void addButton(){
LinearLayout lL = (LinearLayout) findViewById(R.id.requirement_linear);
lL.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(0,2,0,0);
Button b = new Button(this);
b.setBackgroundResource(R.drawable.blue_button);
b.setOnClickListener(openRequirement);
b.setTextColor(Color.parseColor("#FFFFFF"));
String button_text = (index + 2) + ". " + requirement_list.get(index + 1).getName();
b.setText(button_text);
requirements_text.add(button_text);// requirements_text is an arraylist<string> which stores the text so I can display them in my popup to delete them.
index ++;
lL.addView(b,p);
}
You can use removeView() on your LinearLayout and pass your button. You can identify the button on the OnClick(View view) callback, the view there is the button.
as requested, here is an example.
final LinearLayout lL = (LinearLayout) findViewById(R.id.requirement_linear);
Button b = new Button(this);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View button) {
lL.removeView(button);
}
});
lL.addView(b);
Alternatively you can use removeViewAt() to remove a child view by index. You can use the index of your 'list of text of buttons'. Assuming its a listview, you can try this.
lview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
lL.removeViewAt(position);
}
});
here is how i hide and show the buttons
Button b = new Button(this);
b.setVisibility(View.GONE); // this will hide the button and it will now show
b.setVisibility(View.VISIBLE); // this will show the button if it was hidden before
Enjoy!
I'm creating buttons dynamically ...
for(int i=0; i<colSize;i++){
final Button btn = new Button(this);
btn.setText(SectionName[i]);
btn.setTextSize(10);
btn.setPadding(8, 3,8, 3);
btn.setTextColor(Color.WHITE);
btn.setTypeface(Typeface.SERIF, Typeface.BOLD);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//***Every time that I click my button is selected !:)
btn.setSelected(true);
}
});
}
But how could I deselect the other buttons that were selected, I just want one Button selected! :)
The brutal way (works if you have few buttons) - save your button references and create private method which loops through your buttons and deselects once you don't need
Extend your button class and make it listen for custom event which is generated when one of the buttons is clicked
Look at the RadioGroup implementation
Variation of #1. Instead of creating separate listeners for your buttons create just one and reuse it for all buttons. Extend that listener from OnClickListener and add List field. Each time you assign listener to the button add button reference to that list. Now, when onClick is triggered simply loop through the list and disable "other" buttons
Declare a variable to store the Id of the Clicked Button ::
private int EnabledButton;
set an ID on every button when are created ::
btn.setId(i);
or a tag ::
btn.setTag(i);
then in that Listener get the "EnabledButton", and call a function to deselect the other buttons::
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EnabledButton=btn.getId();
DeselectButtons();
btn.setSelected(true);
}
});
The Function to deselect the other Buttons ::
public void DeselectButtons() {
for(int i=0; i<NumberofButtons;i++){
if (EnabledButton!= i)
this.findViewById(i).setSelected(false);
}
}