How do I prevent programatically generated buttons from center aligning in Android? - android

I have a set of buttons that generate based on the number of items pulled from the SQLite database. The buttons generate and work fine but they center align and evenly space themselves out. Meaning if there is one: the button is centered; If there is two: they space evenly from the center; etc.
What I need is for them to stay left align and allow me to use margins to space them out. I know how to set the margins using the parameters but can't figure out why they are defaulting centered in the TableLayout row.
Again the code works and responds perfectly aside from the alignment. I've having similar issues on other activities and I'm sure it's related.
I've included some code that may show what I'm doing:
imgTitleTable = (TableLayout) findViewById(R.id.imagesTableLayout);
int i = 0;
while (i < imgTitle.length) {
if (i % 6 == 0) {
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
imgTitleTable.addView(tr);
imgtr = new TableRow(this);
imgtr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
imgTitleTable.addView(imgtr);
}
// add images
ImageButton imgButton = new ImageButton(this);
imgButton.setImageResource(R.drawable.images_sample);
imgButton.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
imgButton.setBackground(null);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent theIntent = new Intent(getApplicationContext(),
Viewer.class);
startActivity(theIntent);
}
});
// add img title
TextView title = new TextView(this);
title.setText(imgTitle[i]);
title.setId(i);
title.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
title.setGravity(Gravity.CENTER);
title.setWidth(imgButton.getDrawable().getIntrinsicWidth());
title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent theIntent = new Intent(getApplicationContext(),
Viewer.class);
startActivity(theIntent);
}
});
tr.addView(imgButton);
imgtr.addView(title);
i++;
}

Try
TableRow.LayoutParams params = new TableRow.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.LEFT;
imgButton.setLayoutParams(params);

Related

Create buttons in sequential order programmatically

I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other
String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
Button myButton = new Button(this);
myButton.setText(q[i]);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
layout.addView(myButton, params);
}
See this custom library: FlowLayout
While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.
There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.
Use FlowLayout instead and you'll be fine.
You need to define RelativeLayout parameters as in example below
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are
here
From: How to add a view programmattically to RelativeLayout?
Check the link below to get more useful informations.
Hope it will help
In the following code, you should change the upper limits of the for, to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class

Android Dyanmic buttons layout

When creating dynamic buttons I would like them to stack one under the other vertically. I am not sure how to create this effect.
for(int i = 0; i <notificationArrayList.size(); i++)
{
if(i == 0)
{lp.addRule(RelativeLayout.BELOW, R.id.searchButton);}
else
{} //maybe tell the code here to stack under the lastID?
Notification oNote = notificationArrayList.get(i);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText(oNote.NotificationText);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
rl.setLayoutParams(lp);
rl.addView(btn, lp);
}
Maybe in the else statement have it get the last id and add RelativeLayout that way?
The easiest way would be to put all the buttons in a LinearLayout and just add the LinearLayout beneath the search button. This produces easier code, but slightly worse drawing performance. Pseudocode would be like:
LinearLayout ll = new LinearLayout(context);
for(i=0; i<numButtons; i++) {
ll.addView(new Button(context));
}
RelativeLayout.LayoutParam lp = new RelativeLayout.LayoutParam();
lp.addRule(RelativeLayout.BELOW, R.id.searchButton);
relativeLayout.addView(ll,lp);
This example should give you an idea:
public class MyActivity extends Activity {
private RelativeLayout rel;
private EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
rel = (RelativeLayout) findViewById(R.id.main_rel);
editText = (EditText) findViewById(R.id.pref_edit_text);
Button button = new Button(this);
button.setText("Delete");
// create the layout params that will be used to define how your
// button will be displayed
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your button below your object (here a editText)
params.addRule(RelativeLayout.BELOW, editText.getId());
// set the layoutParams on the button
button.setLayoutParams(params);
// add button to your RelativeLayout
rel.addView(button);
}
}

Adding Buttons dynamically in RelativeLayout to LinearLayout

When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs "aaaa" he will create 4 Buttons, side by side, in the first row. Then if the user enters "bb" he will create 2 Buttons, side by side, in the second row. And "ccc" he creates 3 Buttons...
Image to demonstrate:
I dynamically create a RelativeLayout, then dynamically add Buttons to that layout. And finally I add the RelativeLayout to my existing LinearLayout. But the problem is, only one Button is added per row. And my program currently looks like this:
Can someone please me fix this problem?
CODE:
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
RelativeLayout relativeLayout = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length(); //the user input number of buttons
int id = 1;
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
myButton.setId(id);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
relativeLayout.addView(myButton, rlp);
id++;
}
linearLayout.addView(relativeLayout, llp);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
The structure should be simple
Just need to add your buttons in 3 different linear layout with orientation horizontal.
Like
<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}
You guys are right. It is much easier using a LinearLayout. For those interested
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
linearLayout.addView(linearLayout2, llp);

How to perfectly horizontally center a RadioButton with no text in a layout?

I am trying to place RadioButtons underneath some TextViews and center them horizontally, essentially moving the label above the button.
Here is my code:
XML:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:id="#+id/someRadioGroup"/>
Java:
LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(VERTICAL);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);
TextView choiceTextView = new TextView(context);
choiceTextView.setText("1");
choiceTextView.setLayoutParams(layoutParams);
choiceTextView.setGravity(Gravity.CENTER);
choiceLinearLayout.addView(choiceTextView);
RadioButton choiceRadioButton = new RadioButton(context);
choiceRadioButton.setText("");
choiceRadioButton.setGravity(Gravity.CENTER);
choiceRadioButton.setLayoutParams(layoutParams);
choiceLinearLayout.addView(choiceRadioButton);
someRadioGroup.addView(choiceLinearLayout);
Please note that the above code is in a loop to add each of the seven options.
Here is what it looks like on MOST devices (tested on Android 2.3, 4.3, and 4.4):
Here's what it looks like on Android 4.1:
Please note that the TextViews are not actually off-center - they are perfectly centered. It is the RadioButtons that are too far left.
What can I do to fix this issue?
EDIT:
I have added choiceTextView.setGravity(Gravity.CENTER); to the code above. It did not do anything as the text was already centered. The text is just fine. The RadioButtons are too far to the left. Here's a screenshot with the layout bounds option enabled on my device:
//This layout is to group the options
LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);
//You can use a cicle
for (int i = 0; array.size(); i++){
//This layout is to group the label and radiobutton.
LinearLayout radioLayout = new LinearLayout(context);
radioLayout.setGravity(Gravity.CENTER);
radioLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams radioParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
radioLayout.setLayoutParams(radioParams);
TextView choiceTextView = new TextView(context);
choiceTextView.setText(i);
radioLayout.addView(choiceTextView);
RadioButton choiceRadio = new RadioButton(context);
radioLayout.addView(choiceRadio);
choiceLinearLayout.addView(radioLayout);
}
RadioButton choiceRadioButton = new RadioButton(context);
choiceRadioButton.setText("");
choiceRadioButton.setGravity(Gravity.CENTER);
choiceRadioButton.setLayoutParams(layoutParams);
choiceLinearLayout.addView(choiceRadioButton);
someRadioGroup.addView(choiceLinearLayout);
I ended up with a different solution - using ToggleButtons instead of RadioButtons. I set a StateListDrawable as the background of the toggle buttons and made sure that text was always an empty string, whether the button was toggled on or off. Here's the code I ended up with:
LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(VERTICAL);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);
TextView choiceTextView = new TextView(context);
choiceTextView.setText("1");
choiceTextView.setLayoutParams(layoutParams);
choiceTextView.setGravity(Gravity.CENTER);
choiceLinearLayout.addView(choiceTextView);
ToggleButton choiceToggleButton = new ToggleButton(context);
choiceToggleButton.setText("");
choiceToggleButton.setTextOn("");
choiceToggleButton.setTextOff("");
choiceToggleButton.setGravity(Gravity.CENTER);
StateListDrawable radioDrawable = getRadioDrawable(context); // this function creates the state list our of pngs that I've added to the project
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
choiceToggleButton.setBackgroundDrawable(radioDrawable);
} else {
choiceToggleButton.setBackground(radioDrawable);
}
LinearLayout.LayoutParams choiceToggleButtonLayoutParams = new LayoutParams(radioDrawable.getIntrinsicWidth(), radioDrawable.getIntrinsicHeight());
choiceToggleButton.setLayoutParams(choiceToggleButtonLayoutParams);
choiceLinearLayout.addView(choiceToggleButton);
choiceToggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ToggleButton toggleButton = (ToggleButton) view;
// do not allow toggling a button off
if (!toggleButton.isChecked()) {
toggleButton.setChecked(true);
}
// uncheck all other buttons, leaving the current one checked
for (int i = 0; i < someRadioGroup.getChildCount(); i++) {
LinearLayout linearLayout = (LinearLayout) someRadioGroup.getChildAt(i);
if (linearLayout != null) {
ToggleButton tb = (ToggleButton) linearLayout.getChildAt(1);
if (tb != null && tb != toggleButton) {
tb.setChecked(false);
}
}
}
}
});
someRadioGroup.addView(choiceLinearLayout);
Note that an OnClickListener is required for each ToggleButton to mimic proper RadioButton behavior.
Here's the result on Android 4.1 (with some left and right margin applied to each ToggleButton):

Insert linearlayout in tablerow

I want to add linearlayout in table row and that linear layout will hold some buttons and textview.How I can achieve that.I am using this but it does not display anything
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final CheckBox checkbox = new CheckBox(this);
checkbox.setPadding(10, 5, 0, 0);
checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
checkbox.setOnClickListener(new View.OnClickListener() {
TextView tv = new TextView(this);
tv.setText("Hello");
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 14);
tv.setPadding(0, 0, 0, 0);
tv.setOnClickListener(new View.OnClickListener() {
checkbox.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout = new LinearLayout(tr.getContext());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(checkbox, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tr.addView(linearLayout);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Please Help me.
Try adding row params like this
TableRow row = new TableRow(this);
row.addView(linearLayout, new TableRow.LayoutParams(1));
Add relativeLayout instead of linearlayout and set params like this for textview
RelativeLayout.LayoutParams rel_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_lp.addRule(RelativeLayout.BELOW);
tv.setLayoutParams(rel_lp);
For a start you need to close your curly braces at right place. While Setting onClickListener, you are not closing the curly brace and not even putting a semicolon there. So it seems you are making a textView in the listener. The following line
checkbox.setOnClickListener(new View.OnClickListener() {
should be like this
checkbox.setOnClickListener(new View.OnClickListener() {
protected void onClick(View v){
//your listener code here.
}
});
And when the layout is so complex, you should use XML files instead of Dynamically coding, unless you have to.

Categories

Resources