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. :)
Related
I am trying to add buttons to a GridView for each string from textArray.
void addButtons() {
GridView gridView = (GridView) findViewById(R.id.gridView);
List<Button> buttons = new ArrayList<Button>();
for (int i = 0; i < textArray.length; i++) {
Button newButton = new Button(this);
newButton.setText(textArray[i]);
newButton.setId(i);
newButton.setOnClickListener(onClickListener);
buttons.add(newButton);
}
ArrayAdapter<Button> arrayAdapter = new ArrayAdapter<Button>
(this, android.R.layout.simple_list_item_1, buttons);
gridView.setAdapter(arrayAdapter);
}
But in result, I get this: Virtual Device screen
What can be wrong? Or maybe there is a better way to do the same thing? I've tried LinearLayout and everything was ok, but I was not able to scroll down.
how to add button in gridview dynamically might have a solution, but, to be honest, it is too hard for me at the moment.
Try this out, it shows how to create buttons during runtime https://forums.xamarin.com/discussion/49225/dynamic-buttons-in-gridview
So the initial problem was to get a view with programmatically generated buttons. The first solution was a LinearLayout, but I found out that I was not able to scroll it down, so the number of buttons was limited. On the internet, I found that GridView also can do such a task and it would be scrollable. Well, it is true, but I have stuck upon the problem mentioned in this thread.
After a few days of googling I found that LinearLayout inside ScrollView is what I need. So here is both my xml and java code for someone who stumbles up on the same problem:
<ScrollView
<LinearLayout
android:id="#+id/cityLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
void addButtons() {
LinearLayout linearLayout = findViewById(R.id.cityLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < cityArray.length; i++) {
Button newButton = new Button(this);
newButton.setText(cityArray[i]);
newButton.setId(i);
newButton.setOnClickListener(onClickListener);
linearLayout.addView(newButton, layoutParams);
}
}
Anyway, thanks to everyone for help!
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
I'm having an issue working with layouts, I've a linear layout (could be a relative layout or a table layout) which will contain an undefined number of buttons when the activity is loaded. This means, the quantity of buttons will be determined when the activity is being created. The thing is, I'm trying to fit them all in one line (with a center gravity) without changing each buttons' width UNTIL one of them reaches the margin of the screen. In other words, I want the buttons JUST to resize when at least one of them reaches the margin of the screen. That is because, I can't determine the space they're going to use because they are not created.
My actual linear layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="#+id/linearLayout_1"
android:layout_above="#+id/linearLayout_2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="30dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</LinearLayout>
Piece of code that creates the buttons:
protected void hacerVisiblesRespuesta(){
ViewGroup linearLayout = (ViewGroup) findViewById(R.id.linearLayout);
assert linearLayout != null;
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,40, getResources().getDisplayMetrics());
for(int i = 0; i < longuitudPalabra; i++){
String boton = "btn_rsp" + Integer.toString(i+1);
Button bt = new Button(this);
bt.setText("");
bt.setId(getResourceId(boton,"id",getPackageName()));
bt.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT
, height
, 1.0f));
bt.setTextColor(Color.BLACK);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickBotonRespuesta(v);
}
});
bt.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
bt.setBackground(getDrawable(R.drawable.bgbtnrsp));
}else{
//bt.setBackgroundDrawable(getDrawable(R.drawable.bgbtnrsp));
}
bt.setTextSize(20);
Typeface typeFace= Typeface.createFromAsset(getAssets(),"fonts/Montserrat-Regular.ttf");
bt.setTypeface(typeFace);
linearLayout.addView(bt);
}
}
I've tried many things, one of them was to make the buttons' width variable with weight property. The thing is if there are a small quantity of buttons, lets say 4, their width ended up enormous. Is there any way to achieve this through code? Thanks.
have you tried this?
button.setLayoutParams (new LayoutParams(50, LayoutParams.WRAP_CONTENT)
I have been attempting to programmatically add buttons based on a list of values.
PROBLEM: Only one button is produced, rather than a series. This button contains the information of the last value in the array.
I gather an array of values aptly named 'values', I then use a for loop to add the buttons.
Here is the code of my loop to add buttons:
public void updateButtons(List<String> values, View rootView) {
//Find relative layout
RelativeLayout rl = (RelativeLayout) rootView.findViewById(R.id.RelativeLayoutManage);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(50, 10, 50, 10);
for (String mTrip : values) {
//New button
Button Postbtn = new Button(mContext);
//Style
Postbtn.setBackgroundResource(R.drawable.buttonshape);
Postbtn.setTextColor(getResources().getColor(R.color.DarkGreen));
Postbtn.setTextSize(25);
//set text
Postbtn.setText(mTrip.toString());
//set id
Postbtn.setId(i);
int id_ = Postbtn.getId();
//Add to view
rl.addView(Postbtn, params);
Postbtn = ((Button) rootView.findViewById(id_));
//Add listener
Postbtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Log.v("TripNumber", Integer.toString(i));
//TODO: Change Fragment
}
});
i++;
}
}
And my corresponding layout file if needed:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayoutManage"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</RelativeLayout>
Seems that they might be overlapping each other. You need to use a LinearLayout
<LinearLayout android:id="#+id/RelativeLayoutManage"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
/>
You are adding Button to RelativeLayout. In your current code all your buttons are present but one above other. You should make one below/above other to make all buttons visible. Else use a LinearLayout
I solved the problem by positioning each button as it is added to the layout. Simply using:
params.addRule(RelativeLayout.BELOW, Postbtn.getId() - 1);
Postbtn.setLayoutParams(params);
I'm having a difficulty adding buttons dynamically to a ScrollView. The code below is adding the buttons BUT there is no scroller.
If I'm putting the buttons directly in the XML (not dynamically) it's working and I can scroll down/up.
My view:
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="264dp"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="264dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
>
** HERE THE BUTTONS SHOULD BE ADDED DYNAMICALLY **
</LinearLayout>
</ScrollView>
The code which adding buttons:
// create new button
final Button newbutton = new Button(this);
// set background color
newbutton.setBackgroundColor(Color.GRAY);
// set width and height
newbutton.setWidth(50);
newbutton.setHeight(20);
// set position
newbutton.setY(((float)numOfButton*20)+20);
newbutton.setX(100);
// set text
newbutton.setText(Integer.toString(numOfButton));
// create patameter
final LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
//set listener
android.view.View.OnClickListener buttonListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// make all the DrawView invisible
for(View view : comments){
view.setVisibility(View.INVISIBLE);
}
// set the chosen comment visible
comments.get(numOfButton).setVisibility(View.VISIBLE);
boardsHandler.setCurrenBoard(numOfButton);
}};
newbutton.setOnClickListener(buttonListener);
// creating a thread to add button
buttons.post(new Runnable() {
#Override
public void run() {
buttons.addView(newbutton, p);
}
});
Is it something with the LinearLayout.LayoutParams p ?
Thanks!
Try following code
first do
LinearLayout myContainer = findViewById(R.id.layoutId);
When you set parameters for a view, they need to correspond to the parent view for your widget.
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT);
finally add button as you are doing.
try and tell if it works
Setting X and Y position will not work. The LinearLayout layouts it's children vertically or horizontally, only taking their width/height into account.
Besides this -- have you tried calling buttons.invalidate() after buttons.addView(...). This should refresh the layout and should show your newbutton.
This is a rather old post but I found it quickly when doing research on that kind of problem. So I'll post am answer anyway, maybe it'll be of help to anyone..
I had a similar problem with a relative layout to which buttons were added dynamically. I found a workaround in defining the layout's size manually when adding the buttons. For your case, adding the line
buttons.getLayoutParams().height = numOfButton*20+40;
after
buttons.addView(newbutton, p);
might help, though it's probably not the best solution.
I thought my mistake was using the RelativeLayout at all, but since you appear to have the same problem...
Ever thought of using a table layout?