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);
}
Related
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
}
}
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
I have problem in creating programmatically layout in Android. Where I want to display in list with name with check box but check box little down and not in same horizontal range. Here is the my code:
mDialog.setContentView(R.layout.exmple);
mDialog.setCancelable(true);
mDialog.setTitle("Hobbies");
mDialog.show();
final LinearLayout mLinearLayout = (LinearLayout) mDialog
.findViewById(R.id.linear);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
final Button okButton = new Button(this);
final int size = hobbyList.size();
LinearLayout[] mLinearLayout2 = new LinearLayout[size];
LinearLayout[] mLinearLayout3 = new LinearLayout[size];
Check = new CheckBox[size];
Text = new TextView[size];
for (int i = 0; i < size; i++) {
Check[i] = new CheckBox(this);
Text[i] = new TextView(this);
mLinearLayout2[i] = new LinearLayout(this);
mLinearLayout3[i] = new LinearLayout(this);
Check[i].setId(i);
Text[i].setText(hobbyList.get(i));
Text[i].setTypeface(font_regular);
Text[i].setTextColor(color.black);
mLinearLayout2[i].addView(Text[i]);
mLinearLayout3[i].addView(Check[i]);
mLinearLayout2[i].setGravity(Gravity.LEFT);
mLinearLayout3[i].setGravity(Gravity.RIGHT);
mLinearLayout.addView(mLinearLayout2[i]);
mLinearLayout.addView(mLinearLayout3[i]);
}
okButton.setText("OK");
mLinearLayout.addView(okButton);
Here is the output pictures:
The problem is with the layout
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
so the layouts are coming one after another so the checkbox is not in line
you can try having a one relative layout and placing the text and checkbox right and left it's far more easier than your current approach
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
I'm trying to add LinearLayouts dynamically but it's just not working. I think I just need another set of eyes to look it over. Can anyone help me?
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
lLayout = new LinearLayout[8];
for(int i = 0; i < lLayout.length; i++) {
lLayout[i] = new LinearLayout(this);
lLayout[i].setId(i);
lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
if(i%2 == 0) {
lLayout[i].setBackgroundColor(Color.GREEN);
} else {
lLayout[i].setBackgroundColor(Color.MAGENTA);
}
parentLayout.addView(lLayout[i]);
}
You need to set LayoutParams, try adding this:
lLayout[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
It looks like you are trying to make a listview yourself using linearlayouts rather than actually using the ListView already supported.
If you are actually trying to do that, you should first give the layouts inside the view a width and height. I'd also put the list in a scrollview in case it overflows the outer layout.
Maybe try this
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
lLayout = new LinearLayout[8];
for(int i = 0; i < lLayout.length; i++) {
lLayout[i] = new LinearLayout(this);
lLayout[i].setId(i);
lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
if(i%2 == 0) {
lLayout[i].setBackgroundColor(Color.GREEN);
} else {
lLayout[i].setBackgroundColor(Color.MAGENTA);
}
LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
myLayoutParams.leftMargin = 0;
myLayoutParams.topMargin = 50 * i;
myLayoutParams.width = myScreenSize; //e.g. 480
myLayoutParams.height = 50;
lLayout[i].setLayoutParams(myLayoutParams);
parentLayout.addView(lLayout[i]);
}