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);
Related
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.
As the title states, I am looking to find out how to create a button dynamically when another button in another activity is pressed. This is being done with the Android SDK.
Basically, I have two activities, MainActivity and SecondaryActivity, within the SecondaryActivity you enter some information, title, text, id, so on and so forth. When you click the "Save" button it sends some, information to MainActivity(not the issue). As well as sending the information, I need to create an entirely new button within the MainActivity.
Any suggestions on how this should be accomplished?
Thanks.
Edit 1
public void CreateNewButton(View view)
{
LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
TextView newTextView = new TextView(this);
int id = rand.nextInt(100);
int newId;
newTextView.setVisibility(View.VISIBLE);
newTextView.setId( R.id.new_button + id );
newTextView.setText("New Item");
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(getBaseContext(), SecondActivity.class);
startActivity(intent);
}
});
lineLayout.addView(newTextView);
}
This code generates the new TextView( Decided to change it up ) but now the issue I have, is newTextView.setText(); needs to get the text from the other activity
newTextView.setText(i.getData().toString());
putting this in the CreateNewButton(View view) methods causes an error since technically there is no data in the field that it is trying to grab from.
The problem at hand is I need to create the new TextView field WITH the name of the new account that has yet to be created. If that makes any sense.
I'm going to assume you want to add this button to a LinearLayout:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
Button button = new Button(this);
button.setText("I'm a button!");
// add whatever other attributes you want to the button
linearLayout.addView(button);
So I generated some button. The numbers it depends on the user (when clicked a button, create a new one).
This is how I made it:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutcprompt);
RelativeLayout.LayoutParams OBJ = new RelativeLayout.LayoutParams (140,80);
if ((commandsi%6)==0) {adjust=adjust+86; commandsi=1;}
OBJ.leftMargin =(140*(commandsi-1))+10;
OBJ.topMargin =250+adjust;
Button command = new Button(this);
command.setLayoutParams(OBJ);
command.setId(ID);
command.setText(edittxt.getText().toString());
edittxt.setText("");
command.setBackgroundResource(R.drawable.costum_button);
command.setTextColor(Color.WHITE);
command.setTextSize(14);
layout.addView(command);
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
}
});
command.setVisibility(View.VISIBLE);
I want to remove/delete them, but I don't know how.... I gave them a unique id, but I still dunno how can I remove them :/
I cannot comment another post, but using
command = new Button(this)
might involve an implicit memory Leak on this! (which can be the Activity). Rather use Context object. Or remove the button at least.
Then because you have the parent of your Button. Just remove it:
ViewGroup layout = (ViewGroup) findViewById(R.id.layoutcprompt);
View command = layout.findViewById(ID);
layout.removeView(command);
Make command a global variable. Then you can access it later, and call command.setVisibility(View.GONE);
So at the top, of your class file, you would declare the global variable:
Button command;
Then remove the redefinition later on and instead assign to the global variable:
command = new Button(this);
Then when you want to hide it, call:
command.setVisibility(View.GONE);
Try using the documentation, 5 seconds of research can lead you to the RemoveView method.
layout.removeView(command);
Update
If you have a null pointer exception on this line, means your layout is null, not your command. Make your layout variable global for that class.
Also be sure to keep different variables for each of your created buttons. If you have a global variable, and create 10 buttons using the same variable you will only have a reference to the last one created. If you explain exactly when you want to remove the button we might be able to help you further.
As an example, if you want to remove the button when the user clicks on it you can change your clickListener:
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
layout.removeView(view);
}
});
One other implementation that may help others who view this thread is that you can consider removing all the child elements in the parent layout. Once you get the view's parent (which I assume is a layout container),you can remove all the child elements.
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ViewGroup vg = (ViewGroup)view.getParent();
vg.removeAllViews();
}
});
You can also do it like this for security purpose:
ViewGroup layout = (ViewGroup) command.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(command);
You can also use this snipper
For Adding the Button
LinearLayout dynamicview = (LinearLayout)findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(count);
final int id_ = btn.getId();
btn.setText("Capture Image" + id_);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
dynamicview.addView(btn, lprams);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(this);
For removing the button
ViewGroup layout = (ViewGroup) findViewById(R.id.buttonlayout);
View command = layout.findViewById(count);
layout.removeView(command);
In my app, i am loading a layout dynamically with text views ,on a button's onclick event. when i click the button for the first time , i got my layout with text views. when i click it again ,it should display the layout again. but its showing error. my code is
private OnClickListener some_name = new OnClickListener(){
public void onClick(View v) {
LinearLayout my_list_layout = (LinearLayout)findViewById(R.id.my_list_layout);
my_list_layout.setOrientation(1);
my_list_layout.setId(50);
my_textview = new TextView[length];
for(int i=0; i<length ; i++)
{
my_textview[i]= new TextView(getApplicationContext());
my_textview[i].setText("sample text");
my_textview[i].setId(i);
if(i==0)
{
RelativeLayout.LayoutParams my_textviewparams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
my_textviewparams.addRule(RelativeLayout.ALIGN_PARENT_TOP,my_list_layout.getId());
my_list_layout.addView(my_textview[i],my_textviewparams);
}
else
{
RelativeLayout.LayoutParams my_textviewparams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
my_textviewparams.addRule(RelativeLayout.BELOW,my_textview[i-1].getId());
my_list_layout.addView(my_textview[i],my_textviewparams);
}
}
}
and my error log is
quick response is needed. anyone tell me, what is my wrong in coding, or what changes i have to do for my requirement?
You should remove this line:
my_list_layout.setId(50);
because once you set the id to 50, and again when you click the Button, the LinearLayout is null because you call findViewById(R.id. my_list_layout); its id has been changed to 50.
Cheers!
I got the answer.Here while i was doing first time the btn click, the child view will added to my parent view dynamically.
During the second click the i removed previously added child from my parent view and added the new child. So this will updates my content and displays clearly every time when I click the btn.
I got error in removing the views from my child, now I clear the error by assigning some variables for identifying btn click, whether user clicks it for first time or not. thank you all for your time here and for your answers.
I create buttons dynamically based on the size of an array list that i get from another object. For each entry of the arraylist i need to create three buttons each one with different action.
Like this i need to create 3 times the sizes of arraylist number of buttons. If i had only one set of buttons I can write onClick()-method which takes the id of the button but here i have 3 buttons for each entry and need to write 3 different actions for those three buttons.
How could this be done?
Similar thing I have done when i needed a textview for each of my array item.it was like-
String[] arrayName={"abc","def","ghi"};
for(int i=0;i<arrayName.length;i++)
{
TextView tv=new TextView(context);
tv.setPadding(20, 5, 40, 5);
tv.setText(arrayName[i]);
tv.setTextSize(1, 12);
tv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.parseColor("#2554C7"));
tv.setClickable(true);
tv.setId(i);
layout.addView(tv);
}
In same way,you can add button in similar way. and in click event of each of them,you can code for their actions separately.(I have not tried this).So in each iteration,you will have 3 buttons for each of the array item.
Edit - 1:
You can differentiate ids like:
mButton1.setId(Integer.parseInt(i+"1"));
mButton2.setId(Integer.parseInt(i+"2"));
mButton3.setId(Integer.parseInt(i+"3"));
Then you can set click listener on each of the button like mButton1.setOnClickListener... and so on.
Declare a list of buttons:
private List<Button> buttons = new ArrayList<Button>();
Then add each button to this list:
buttons.add(0, (Button) findViewById(id in ur layout));
Inside a for loop give click listener:
Button element = buttons.get(i);
element.setOnClickListener(dc);
where dc is ur object name for your inner class that implements OnClickListener.
To access each button you can give:
Button myBtn;
#Override
public void onClick(View v) {
myBtn = (Button) v;
// do operations related to the button
}
Create the Common Listener class for your button action event and set the used into the setOnClickListener() method
Now set the unique id for the buttons.
Now suppose your class look like this :
class MyAction implements onClickListener{
public void onClick(View view){
// get the id from this view and set into the if...else or in switch
int id = view.getId();
switch(id){
case 1:
case 2:
/// and so on...
}
//// do operation here ...
}
}
set this listener in button like this way.
Button b1 = new Button(context);
b1.setId(1);
b1.setOnClickListenr(new MyAction());
Button b2 = new Button(context);
b2.setId(2);
b2.setOnClickListener(new MyAction());