setBackground to buttons in a table - android

I have a series of buttons, in tables, in a ViewFlipper, so I can paginate the tables. If I set them no background, they keep their margins between each other. But I need to "make them pretty", so I added a gradient to the buttons. But now they are not keeping their margins any longer.
I am adding them programatically, looping an ArrayList of the texts the button have to have, adding them in the proper row of the table, and so on. Like this:
final Button btn = new Button(this);
btn.setText(array.get(i));
btn.setTextSize(10);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.i("TAG", "Botón pulsado: " + texto);
goToModule(texto);
}
});
btn.setHeight(height);
btn.setWidth(width);
btn.setOnTouchListener(parentListener);
btn.post(new Runnable()
{
#Override
public void run()
{
btn.setCompoundDrawablesWithIntrinsicBounds(null,getDrawableForText(texto),null, null);
btn.setPadding(0,35,0,0);
int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TL_BR, colors);
gd.setCornerRadius(30f);
btn.setBackground(gd);
}
});
row1.addView(btn);
The result in the screen is like this:
Is there anyway to make the buttons look like the ones without the gradient, I mean, respecting the margins between each other, to make them look more like a proper table?
Thank you.

Try adding margin to button
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT
);
params.setMargins(10, 10, 10, 10);
btn.setLayoutParams(params);

try change margin to padding in xml.

Related

adding dynamically buttons in horizontal order

I'm trying to add some buttons dynamically in horizontal order. I have tried several options and none of them worked.
What am i doing wrong?
RelativeLayout layout = (RelativeLayout) findViewById(R.id.pickItem);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
String userName;
List<CheckBox> usersButtonList=new ArrayList<CheckBox>();
int i=0;
for(User user : users){
userName=user.getName();
CheckBox Userbutton = new CheckBox(this);
usersButtonList.add(Userbutton);
Userbutton.setText(userName);
Userbutton.setId(i);
Userbutton .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isChecked=((CheckBox)v).isChecked();
String s= (String)((CheckBox)v).getText();
updateActiveUsers(isChecked,s);
}
});
if(i!=0)
{
buttonParams.addRule(RelativeLayout.ALIGN_RIGHT,(i-1));
}
layout.addView(Userbutton,buttonParams);
i++;
}
There are a few things wrong with this. First, like #EasyJoin Dev said, substitute RelativeLayout with LinearLayout in your layout xml and set the orientation to horizontal. It should look something like this
<LinearLayout
android:id="#+id/pickItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
Then change the two top lines in your code to
LinearLayout layout = (LinearLayout) findViewById(R.id.pickItem);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Since you have ViewGroup.LayoutParams.FILL_PARENT it will take the whole space available. Let me know if you need any more help or this doesn't work

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

Button Fill Entire Layout Programmatically

I have a question for Android developers. I have a layout with a button (developed programmatically, not with xml) and I want the button to fill the entire layout right now but it currently doesn't and I'm not sure why, I thought I had everything set up correctly with the gravity of the button and the layout params but here's what I have. If you can point me in the right direction I would really appreciate it! Thanks.
LinearLayout bottom = new LinearLayout(this);
bottom.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.FILL;
bottom.setLayoutParams(params);
bottom.setBackgroundColor(Color.BLUE);
Button eqbttn = new Button(this);
eqbttn.setText("=");
eqbttn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
}
});
bottom.addView(eqbttn);
You've only applied the MATCH_PARENT size to the LinearLayout. You need to apply it to the button, too.
Button eqbttn = new Button(this);
LinearLayout.LayoutParams eqparams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
eqbttn.setLayoutParams(eqbttn);
eqbttn.setText("=");
eqbttn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
}
});
bottom.addView(eqbttn);
This will force the button to fill the LAYOUT both vertically and horizontally. If you need the layout itself to take up the whole screen, change its WRAP_CONTENT to MATCH_PARENT as well.
Also, in this case, you do not require the Gravity.FILL layout parameter on the LinearLayout.

button is squeezed when it exceeds layout

In my code, I create buttons dinamically. When I create multiple buttons is the following problem:
How do I get it when it happens the button is put down?
My code:
private void showGlossary(String ContentTab) {
LinearLayout layout;
LinearLayout.LayoutParams p;
layout = (LinearLayout) findViewById(R.id.GlossaryTab1);
p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT
);
Glossary = (TextView) findViewById(R.id.glossary);
Glossary.setText("Glossário:");
while (ContentTab.indexOf("<gloss") != -1) {
ContentTab = ContentTab.substring(ContentTab.indexOf("<gloss"));
uri = ContentTab.substring(ContentTab.indexOf("<gloss") + 1, ContentTab.indexOf(">"));
Button myButton = new Button(this);
myButton.setText(Html.fromHtml(ContentTab.substring(ContentTab.indexOf(">") + 1, ContentTab.indexOf("</gloss>"))));
myButton.setLayoutParams(p);
myButton.setContentDescription(uri);
layout.addView(myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Toast.makeText(ShowPhytoterapicActivity.this, Html.fromHtml(getGlossaryItem(view.getContentDescription().toString())), Toast.LENGTH_LONG).show();
}
});
if(ContentTab.indexOf("</gloss>") != -1)
ContentTab = ContentTab.substring(ContentTab.indexOf("</gloss>") + 9);
}
}
My XML:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/GlossaryTab1"
android:orientation="horizontal"
>
</LinearLayout>
Can anyone help me? Thanks!
You could set all of your buttons' weights to 1, but that would cause all of your buttons to become "Squished".
Do you think a HorizontalScrollField would work? I think that may be the best solution.
Just wrap your LinearLayout in a HorizontalScrollField and add your buttons to the LinearLayout as you are now.
Fitting 3 buttons in one row seems a bit messy. You could try a different layout style, perhaps a triangular setup, or change your UI design into something more compact and change the flow of how the buttons appear.
I made a LinearLayout Vertical same, thanks to everyone for the answers. :)

How to add space between buttons in same row

I found this example
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(20,20) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=3; f++) {
TableRow tr = new TableRow(this);
tr.setPadding(10,10,10,10);
for (int c=0; c<=3; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setPadding(10, 10, 10, 10);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setBackgroundColor(Color.BLUE);
b.setOnClickListener(this);
b.setWidth(24);
b.setHeight(24);
tr.addView(b);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
I need to have matrix of buttons ( something like GridLayout in Java ). The problem in this code is that I don't have any space between columns in same row. How to add space between buttons in same row ?
I think you need to set margins for your buttons, because padding may only size down the top layer of the button and not its background. Here is an example how to do something similar in code: Programmatically set margin for TableRow
However in this example you need to change the parent container to TableRow, because layout parameters always refer to its immediate parent, which for your buttons is TableRow.

Categories

Resources