Android: RelativeLayout.LEFT_OF does not work? - android

another question for which I could not find answer:
I've got a RelativeLayout, that should have a button at its right edge and an ImageButton close to the left. And I do not know how to arrange this.
What I try is:
RelativeLayout TopLayout = (RelativeLayout) findViewById(R.id.topLayout);
TopLayout.removeAllViews();
TopLayout.setPadding(m_TableRowPadding_px, 8, m_TableRowPadding_px, 4);
RelativeLayout.LayoutParams bParams = new RelativeLayout.LayoutParams(m_Resources
.getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
bParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
bParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
Button itemAddButton = new Button(this);
itemAddButton.setLayoutParams(bParams);
itemAddButton.setText(m_Resources.getString(R.string.add_item_button));
itemAddButton.setOnClickListener(new View.OnClickListener()
{...});
TopLayout.addView(itemAddButton);
RelativeLayout.LayoutParams ibParams = new RelativeLayout.LayoutParams(MIN_IMG_BUTTON_WIDTH,
m_DefaultButtonHeight_px);
ibParams.addRule(RelativeLayout.LEFT_OF, itemAddButton.getId());
ImageButton speechButton = new ImageButton(this);
speechButton.setLayoutParams(ibParams);
speechButton.setContentDescription(m_Resources.getString(R.string.AddSpeechItemString));
speechButton.setOnClickListener(new View.OnClickListener()
{... });
speechButton.setImageDrawable(m_Resources.getDrawable(R.drawable.micro2));
TopLayout.addView(speechButton);
}
But the result is a button to the right (as desired) and a ImageButton to the left. :(
Could anyone help me? O.o
Cheers
Chris

You need to set an id to the itemAddButton button before u use the line. Right now itemAddButton.getId() will just return -1.
ibParams.addRule(RelativeLayout.LEFT_OF, itemAddButton.getId());
You can set something like
itemAddButton.setId(5005000);

Try adding additional rule to your speechButton:
ibParams.addRule(RelativeLayout.ALIGN_TOP, itemAddButton.getId());
You can also use RelativeLayout.ALIGN_BOTTOM if that works better for you.

Related

findViewById for dynamically added View

Is it possible to find the added TextView to update it with other options.
I am adding by this code new TextView with a Button. Now, I want also set a onClickListerner for the dynamically added TextViews. For this I have to find them with findViewById method. But they aren't createt yet.
Can I make this in a other way and if yes, How?
Button hinzufügenButton = (Button) findViewById(R.id.hinzufügen_Button);
hinzufügenButton.setOnClickListener(new Button.OnClickListener() {
public void onClick (View view){
EditText tischName = (EditText) findViewById(R.id.tisch_name_EditText);
TextView tisch = new TextView(getApplicationContext());
tisch.setText(tischName.getText());
tisch.setAllCaps(true);
tisch.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tisch.setBackgroundColor(Color.parseColor("#9FA8DA"));
tisch.setTextSize(25);
tisch.setId(id);
id++;
LinearLayout tischeAnzeigen = (LinearLayout) findViewById(R.id.tische_LinearLayout);
tischeAnzeigen.addView(tisch);
}
});
Just add the onClickListener as you create your dynamic views
Example:
...
tisch.setId(id);
tisch.setOnClickListener(yourListener);
...
You can use setId to add an id. But you probably don't need to. If you're creating a view dynamically, you have a reference to it when you create it. Just set it on that reference. No need to find it.

Add many linear layout in to scroll view

I have some code like this:
l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(l);
TextView ch = new TextView(this);
Button ndda = new Button(this);
ndda.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button nddb = new Button(this);
Button nddc = new Button(this);
Button nddd = new Button(this);
ch.setText("ndch");
ndda.setText("da");
nddb.setText("db");
nddc.setText("dc");
nddd.setText("dd");
l.addView(ch);
l.addView(ndda);
l.addView(nddb);
l.addView(nddc);
l.addView(nddd);
now i want to add lot of linear layout( may be 15) in to scroll view. How can I do that with shortly code? and the Button in each linear layout i want to setOnClickListener on them to do some thing. I try to do this with listview but it's alway refresh when scroll, i can't disable refresh. I'm a newbie so pls show me detail. Thank for all
Some guys already mentioned it before in the comments... use a ListView or even better ... a RecyclerView.
Here's a good tutorial for the RecylcerView.
Nevertheless a ScrollViewcan have only 1 child (in your case a LinearLayout), which again can contain multiple layouts within.

Android: Dynamically create image button with click event

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);

How can I remove / delete a dynamically generated button in Android?

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);

Creating button dynamically

hey guys need a little help........my intention is to develop an app which contains just a button... and on clicking it it must create another button on the screen dynamically.......
here's my code...
public void onClick(View v) {
// TODO Auto-generated method stub
Button bee=new Button(getBaseContext());
bee.setText("hello:");
RelativeLayout a;
a=(RelativeLayout)findViewById(R.layout.activity_main);
a.addView(bee,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
});
this code is error free but when executed says "unfortunately 'myapplication' has stopped"....
pls do help guys..( my sole aim is to create widgets dynamically ,hence tried dis program). if u have any other suggestions pls post nd comment
a=(RelativeLayout)findViewById(R.layout.activity_main);
activity_main is a layout, not a RelativeLayout widget!
you should use
a=(RelativeLayout)findViewById(R.id.my_relative_layout);
Please post the stack trace (logcat) so you will get better and faster answer.
Try this.
RelativeLayout rLayout = (RelativeLayout )findViewById(R.id.relativeLayout); // Here this should be id of your relative layout.
Button btn = new Button(this);
rLayout.addView(btn);
try with this code i think you are getting problem just because of not setting the parameters properly so check with this code-
LinearLayout container = (LinearLayout)findViewById(R.id.container);
Button btn = new Button(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(lp);
container.addView(btn);
You are finding id rather then layout
so change this line:
a=(RelativeLayout)findViewById(R.id.activity_main);
You are trying to "find" a layout which can't exist! Please, make sure you have defined a RealtiveLayout with a valid id and then do
a=(RelativeLayout)findViewById(R.id.valid_layout_id);
instead of
a=(RelativeLayout)findViewById(R.layout.activity_main);

Categories

Resources