i'm trying to create buttons programmatically on my android application depending on how many items I have on my sqlite database. The buttons are there, but my problem is to set onClick on every button because I want to show different content when user's click the buttons. I'm using this code for now :
for(cursorCol.move(0); cursorCol.moveToNext(); cursorCol.isAfterLast()){
Id = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("id")));
Log.i("Id","Id : "+Id);
titleButton = cursorCol.getString(cursorCol.getColumnIndex("title"));
Log.i("titleButton","titleButton : " + titleButton);
elemOrder1 = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("elemOrder")));
Log.i("elemOrder1 ","elemOrder1 : " + elemOrder1 );
btn = new Button(this);
btn.setText(" " + titleButton + " ");
btn.setId(Id);
btn.setTextColor(Color.parseColor("#000000"));
btn.setTextSize(12);
btn.setPadding(10, 10, 10, 10);
btn.setBackgroundResource(R.drawable.gray_button);
btnlayout.addView(btn,params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
infoCard.removeAllViews();
for(int i=0;i<=cursorCol.getCount();i++){
Log.i("","titleButton : "+titleButton);
}
}
}
But the problem is that when I click the button it's showing only the last titleButton. Actually I don't need to show titleButton, I just did it for testing purposes. Any ideas how can I create different onClick methods for every single button?
I think the problem lies with this line of code:
btn = new Button(this);
You are editing the same button over and over again in your loop and not acutally creating a new one. To create a new one you will need to do this:
Button new_btn = new Button(this);
This will create a brand new one every time you iterate through your for loop.
Related
I'm new to android and I require to have a list of image buttons in an activity which are created based on the data in a database. I haven't created anything like this in android before and so far I've been using HTML tables to show my data in a grid view. I'm not asking for any special code, I'm just clueless on how to implement this. I wanna know what the best approach is.
One problem I've faced is with the click events(in the way I've been doing them so far) which do not take in any EventArg, so I can't get the name of the button out of them.
If you're sure that the OnClickListener instance is applied to a Button, then you could just cast the received view to a Button and get the text:
public void onClick(View v) {
// 1) Possibly check for instance of first
Button b = (Button)v;
String buttonText = b.getText().toString();
}
// create the layout params that will be used to define how your button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// Create Button
final Button btn = new Button(this);
// Give button an ID
btn.setId(someinteger);
btn.setText("Add Text");
// set the layoutParams on the button
btn.setLayoutParams(params);
I want to ask how to make a list of text that we can tap in each of the text and then get the selected text to editText.
I just added the screenshot
http://i.stack.imgur.com/ddZSg.png
I have been searching it since yesterday, but I can not find exact solution. I also tried with listview, but I don't know how it's possible with horizontal, and flow list item.
I am also new in android. But I can think of the logic what you want. You can try this out if you want.
First of all, you can make your list of texts EditText by list of buttons Buttons
You can dynamically add as many Buttons with their respective text as you want to show.
set their corresponding onClickListener
In their onClickListener , create an object of the EditText you are using to add texts.
Store the value of EditText into a String Variable first.
Add the text of the clicked Button to the variable.
and now again set the text in EditText with the variable you created to store the values.
Your task will be done.
Try referring this code.
and change the code accordingly as your needs.
// Adding EditText and a button in a new linear layout and then adding
// the new linearLayout to the main layout
String[] valuesToBeAdded={"A","B","C","D"};
String selectedValues=null;
LinearLayout mainLayout=(LinearLayout) findViewById(R.id.mainLayout);
LinearLayout localLayout = new LinearLayout(context);
localLayout.setOrientation(LinearLayout.VERTICAL);
localLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
EditText editText=new EditText(context);
editText.setText(selectedValues);
editText.setId(5000);
localLayout.addView(editText);
for(int i=0;i<valuesToBeAdded.length();i++){
Button button = new Button(context);
button.setText(R.string.scanDocument);
button.setId(i);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed=(EditText) findViewById(5000);
selectedValues=ed.getText();
selectedValues=selectedValues +" " + this.getText();
ed.setText(selectedValues);
}
});
localLayout.addView(button);
}
mainLayout.addView(localLayout);
Thank You
Could you not make as many buttons as you need and then in the button_Click method for all buttons add:
Buttonwithtext_Click(object sender, EventArgs e)
{
editTextBox.Text = editTextBox.Text + Buttonwithtext.text + ", ":
}
I've developed one Android application which uses a HorizontalScrollView, and the HorizontalScrollView has one child as a LinearLayout.
Now I want to add buttons on LinearLayout at Runtime means dynamically.
I added the button successfully, But the problem is that my button click event does not work in Android.
ArrayList listClassItems = objCompany.getListClassItems();
Button[] btnCategory = new Button[listClassItems.size()];
for(int i=0;i<listClassItems.size();i++)
{
System.out.println("OTHER_CLASS LENGTH : " + listClassItems.size());
System.out.println("CLASS ID : " + listClassItems.get(i).getClassId());
System.out.println("CLASS NAME : " + listClassItems.get(i).getClassName());
btnCategory[i] = new Button(myContext);
btnCategory[i].setId(i);
btnCategory[i].setTag(listClassItems.get(i).getClassId());
btnCategory[i].setText(listClassItems.get(i).getClassName());
btnCategory[i].setClickable(true);
btnCategory[i].setPadding(10,10,10,10);
LayoutParams layParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
if(i!=0 || i!=listClassItems.size()-1)
{
layParams.leftMargin = 10;
layParams.rightMargin = 10;
}
tabRowBottom.addView(btnCategory,layParams);
btnCategory[i].setOnClickListener(null);
tabRowBottom.addView(btnCategory[i]);
btnCategory[i].setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myContext, "=== Button CLICKED ===",Toast.LENGTH_SHORT).show();
btnCategory.setBackgroundColor(Color.BLACK);
}
});
}
Instead of an array, create a list of buttons, you can create buttons and set their id, tags and onclicklistenners like this and add them to the button list:
buttonList = new ArrayList<Button>();
for (int i=0;i<5;i++){
Button button = new Button(getApplicationContext());
button.setOnClickListener(customListenner);
button.setId(i);
button.setTag(i);
myLayout.addView(button);
buttonList.add(button);
}
and when you need to use the button again, just call with their id or tags from the list.
If you need different listenners, you can control them by using the unique tag check in if function and declare another action.
This is the method that I always use when I create dynamic views programmatically.
Im going to write some android app, which will basically consists of two activities. So first should have a lot of buttons (100+) and on click on any of them I will just get some special id and move to second activity. But is there any alternative to declare that hundreds of buttons and copy/paste one piece of code to every of them setting almost same onClickLister? Is there any special construction? Thanks
Edit: every of buttons are actually indexed from 1 to n. And on the click second activity will be launched and get that index to show it. I cant basically use any spinner or smth else, because there will be 3 rows of clickable things and each of them carring different images
Edit 2: so, to give you an idea, im going to do some table of buttons like in Angry Birds menu when you actually choosing the level you want to play. So, on click you will get id of button and start second activity
Call the method to add buttons
private void addButton(){
LinearLayout view = (LinearLayout) findViewById(R.id.linear_layout_id_here);
Button btn = null;
int w = 50;
int h = 25;
for(int i=1; i<100; i++) {
btn = new Button(this);
btn.setLayoutParams(new LayoutParams(w,h));
btn.setText("button " +i);
btn.setTag(""+i);
btn.setOnClickListener(onClickBtn);
view.addView(btn);
btn = null;
}
}
Call this method for handling onclick on button
private View.OnClickListener onClickBtn = new View.OnClickListener() {
public void onClick(View view) {
final int tag = Integer.parseInt(view.getTag().toString());
switch (tag) {
case 1:
// Do stuff
break;
case 2:
// Do stuff
break;
default:
break;
}
}
};
You should use a ListView.
ListViews are great for handling a lot of items at the same time. They are also natural for the user. Additionally, you use only one click listener - OnItemClickListener.
There's a useful example on how to work with ListViews in the Android Referenence.
You may add buttons in code, something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
/*your code here*/
GroupView gw =findViewById(R.id.pnlButtonscontainer); //find the panel to add the buttons
for(int i=0; i<100; i++) {
Button b = new Button(this);
b.setLayoutParameters(new LayoutParameters(w,h));
b.settext = i+"";
b.setOnClickListener(new OnClickListener(){
});
}
}
I coded directly into browser, so some syntax error may appear in my code, but this is the point, a way, not the only one, to add 100 buttons.
I am creating a dynamic list in which has 2 buttons (Accept and Reject) and one TextView (Email ID which is unique).
I have to accept/reject the email Id on button click event. My problem is I am not able to get the Id or any other reference to point which accept button i had clicked.
Assign an ID to each button in your view.
Then create a separate OnClickListener for each button, each calling their own method?
If each item in your list has 2 buttons, search for the ID withing the parent container, then add the OnClickListener to the found button.
Create dynamic controll.You have to set some id for your controll
` ////////////Create weekdays button//////////////
Button week_btn = new Button(this);
week_btn.setWidth(55);
week_btn.setHeight(45);
week_btn.setText("days");
week_btn.setGravity(Gravity.TOP);
week_btn.setId(NEW_BTN_SELECT_DAYWEEK_id + i);
cur_lin_layout.addView(week_btn, p);
week_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
current_period_id = v.getId() + 500;
lDayFlags = 0;
showDialog(0);
}
});`
just create your own constant id and increase it after creating new controll(button). You set your controll and you can give it from click listner or finviewbyid