Display 3 buttons in a row IN a scroll view - android

at the moment I have a list of buttons showing up in a ScrollView however all the buttons are on the left hand side and go down in one column.
I would like these buttons to be shown in columns of 3, so 3 buttons in one row when I add row dynamically
Here is the current code I have to display these buttons
// Find the ScrollView
ScrollView scrollView = (ScrollView)
findViewById(R.id.scrollView);
// Create a LinearLayout element
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Add Buttons
for (int i = 0; i < 3; i++) {
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 20; j++) {
Button button = new Button(this);
button.setText("Some text");
linearLayout.addView(button);
}
}
// Add the LinearLayout element to the ScrollView
scrollView.addView(linearLayout);
}
I tried setting layout parameters for the buttons however it didn't change anything
Any ideas?
Thanks

Try this logic
// Find the ScrollView
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
// Create a LinearLayout element
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// Add Buttons
for (int i = 0; i < 20; i++) {
LinearLayout linearLayoutChild = new LinearLayout(this);
linearLayoutChild.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutChild.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++) {
Button button = new Button(this);
button.setText("Some text");
linearLayoutChild.addView(button);
}
linearLayout.addView(linearLayoutChild);
}
// Add the LinearLayout element to the ScrollView
scrollView.addView(linearLayout);
Unless and until it is required I will prefer GridView or RecyclerView over Dynamic view adding

Related

how to create Android Switch Programmatically Dynamically

I hope my layout will be this :
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for (int i = 0; i < 20; i++){
Switch sw = new Switch(this);
sw.setChecked(false);
ll.addView(sw);
}
this.setContentView(sv);
I want to create a textView is left side , and switch is right side,just like my post. I think layout is
-LinearLayout(horizontal)
- TextView -Switch
what do I do ? thanks.
for (int i = 0; i < 20; i++){
Switch sw = new Switch(this);
sw.setChecked(false);
sw.setText(String); //that' right
ll.addView(sw);
}

Set buttons ids with Custom Adapter

Which will be the better way to create a Vertical Lineal layout with four or more buttons in each row
The problems I have face are the following:
Setting the id of each button manually will result in a lot of repetitive code, more resources usage and you will have to change everyone to add a feature or change something (I think using an adapter will be the most efficient way, but...)
From what I know using a CustomAdapter don't help you set a unique ID to the buttons
Can you use an adapter to set a different id for each button dipending of the row?
Example:
second button of third row: r3b2
fifth button of first row: r1b5
Thanks.
You can create the buttons programmatically in our Activity class like this
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout); // the layout in which u want to display the buttons
layout.setOrientation(LinearLayout.VERTICAL);
int count = 1;
for (int i = 0; i < 5; i++) { // i = row count
LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 7; j++) { // j = column count (create 7 buttons in each row)
int id = count;
final Button btnTag = new Button(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
params.weight = 1;
params.gravity = Gravity.CENTER;
btnTag.setLayoutParams(params);
btnTag.setBackgroundColor(Color.parseColor("#ffffff"));
String dateSuffix = count + getDayNumberSuffix(count);
btnTag.setTag(dateSuffix);
btnTag.setId(id);
btnTag.setText(count + "");
btnTag.setTextSize(12.0f);
btnTag.setOnClickListener(getOnClickDoSomething(btnTag, count));
row.addView(btnTag);
count++;
}
layout.addView(row);
}
View.OnClickListener getOnClickDoSomething(final Button btnTag, final int count) {
return new View.OnClickListener() {
public void onClick(View v) {
// here you can do what u want to do on button click
}
}

Creating table layout and adding rows from the code behind in Android

I need to create a table layout and add rows dynamically from Java code behind. I have already read questions here, but they are mentioning to add table rows in an already created table layout (from xml).
I need to create the table layout as well as add data to it dynamically.
Can anyone please provide some inputs?
For now, I have linear layout code in place which adds button from code behind one below the other, I need to place it under a tabular format now.
To add three buttons to TableRow use the code below
TableLayout tableLayout = new TableLayout(this);
for (int i = 0; i < 10; i++)
{
TableRow tableRow = new TableRow(this);
Button button = new Button(this);
button.setText("1");
tableRow.addView(button);
button = new Button(this);
button.setText("2");
tableRow.addView(button);
button = new Button(this);
button.setText("3");
tableRow.addView(button);
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
Add the code below to your onCreate() method in you Activity class:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TableLayout tableLayout = new TableLayout(this);
for (int i = 0; i < 5; i++)
{
TableRow tableRow = new TableRow(this);
for (int j = 0; j < 3; j++)
{
Button button = new Button(this);
button.setText(""+j);
tableRow.addView(button);
}
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}
The code will add five rows with three buttons with the text 1 to 3 to the table.
Add the following code below your init() method:
for (int i = 0; i < GetGlobal.totalrow; i++) {
TableRow tbrow = new TableRow(this);
// tbrow.setLayoutParams(tableRowParams);
TextView t1v = new TextView(this);
t1v.setText(JSONParser.heading[i].replace('"', ' '));
t1v.setBackgroundResource(R.drawable.diamond_detail1);
t1v.setPadding(5, 3, 5, 3);
t1v.setMinHeight(50);
t1v.setTypeface(Typeface.SERIF);
t1v.setTextColor(Color.parseColor("#FFFFFF"));
t1v.setGravity(Gravity.FILL);
tbrow.addView(t1v);

android dynamically created layouts overlapping each other

I want to create a relative layout inside a linear layout dynamically.This relative layout contains a text view and button which are also created in dynamic method.The alignment of text view and button not work properly.The code which i have tried is given below.
final LinearLayout lab_linear = (LinearLayout) findViewById(R.id.contact_list_layout);
lab_linear.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value);
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton);
lab_linear.addView(layout);
}
Try this..
Give layout parems for RelativeLayout individually to TextView and Button and also add addRule(RelativeLayout.ALIGN_PARENT_RIGHT); or addRule(RelativeLayout.ALIGN_PARENT_LEFT); for that views
Or add the below in for Button
lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value,lp);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//Or below remove above code and uncomment the below code
//lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton,lp1);
lab_linear.addView(layout);
}
Might I suggest using LinearLayouts instead of RelativeLayouts in your loop? That way, you won't have the overlap problem.

Android: Dynamically add views to ScrollView

I have a ScrollView and I want to insert a user specified number of HorizontalScrollViews. So what user says he wants to have a matrix of 5x5 elements, I want to insert 5 HorizontalScrollViews with 5 EditText objects each. My program adds the first line just as it's supposed to, but the rest not.
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(par2);
HorizontalScrollView row = new HorizontalScrollView(this);
row.setLayoutParams(par1);
row.addView(ll);
for (int j = 0; j < number; j++) {
EditText txt = new EditText(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setHint(i+","+j);
ll.addView(txt);
}
latout_in_scrollview.addView(row);
}
Any ideas why? Thanks!
EDIT:
The 1:1 code im using
LinearLayout dijkstra_rows;
FrameLayout.LayoutParams par1 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams par2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_dijkstra);
dijkstra_rows = (LinearLayout) findViewById(R.id.dijkstra_rows);
Bundle extras = getIntent().getExtras();
number = extras.getInt("vertexes");
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(par2);
HorizontalScrollView row = new HorizontalScrollView(this);
row.setLayoutParams(par1);
row.addView(ll);
for (int j = 0; j < number; j++) {
EditText txt = new EditText(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setHint(i+","+j);
ll.addView(txt);
}
dijkstra_rows.addView(row);
}
}
ScrollView can contain only one childView. You can put any layout as per your requirement. I generally use Relative Layout...
Then add views dynamically to relative layout
viewLayout = (ViewGroup) mView.findViewById(R.id.YOUR_RELATIVE_LAYOUT_ID);
View lastCard = viewLayout.getChildAt(viewLayout.getChildCount() - 1);
// INFLATE YOUR NEW VIEW YOU WANT TO ADD
CardView cardView = (CardView)
LayoutInflater.from(getContext()).inflate(R.layout.card_nearest_stop, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Set id to view
int id = 125;
if (lastCard != null) {
params.addRule(RelativeLayout.BELOW, lastCard.getId());
id = lastCard.getId() + 125;
}
cardView.setLayoutParams(params);
cardView.setId(id);
viewLayout.addView(cardView);
ScrollView is a single element container.
A ScrollView is a FrameLayout, meaning you should place one child in
it containing the entire contents to scroll; this child may itself be
a layout manager with a complex hierarchy of objects. A child that is
often used is a LinearLayout in a vertical orientation, presenting a
vertical array of top-level items that the user can scroll through.
You are adding multiple LinearLayouts here
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
.
.
}
You should have only one out of this loop. Then add this one to your scrollView, in Loop you can add muliple HorizontolScrollViews to this LinearLayout

Categories

Resources