I have this TableRow of EditText inserted in a tablelayout, this tablelayout is inserted in one Scrollview, this Scrollview is inserted in a HorizontalScrollView. HorizontalScrollView contains, Also, an image of the background. The problem is that I can't scroll properly and display all the EditText. Can you help me?
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
HorizontalScrollView HSC = new HorizontalScrollView(this);
HSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
HSC.setBackgroundResource(R.drawable.lavagna);
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
values = new EditText[r][c];
for (int i = 0; i < r; i++){
tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
for (int j = 0; j < c; j++){
values[i][j] = new EditText(this);
values[i][j].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
values[i][j].setHint("r: " + (i) + " " + "c: " + (j));
values[i][j].setPadding(10, 10, 10, 10);
tableRow.addView(values[i][j]);
}
Remove Gravity.CENTER parameters, or the scroller will scroll from the center of its content that is big enough. It's there, but you can't get it.
Correct various values
Code:
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
HorizontalScrollView HSC = new HorizontalScrollView(this);
HSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
HSC.setBackgroundResource(R.drawable.lavagna);
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// you may or may not change here and below
// values = new EditText[15][15];
for (int i = 0; i < 15; i++){
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
for (int j = 0; j < 15; j++){
EditText value = new EditText(this);
value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
value.setHint("r: " + (i) + " " + "c: " + (j));
value.setPadding(10, 10, 10, 10);
tableRow.addView(value);
}
tableLayout.addView(tableRow);
}
VSC.addView(tableLayout);
HSC.addView(VSC);
frameLayout.addView(HSC);
// ... add frameLayout
// ...
}
Related
I want to add button to grid-layout dynamically one by one as they are created but in my case the buttons are created one by one through each iteration but they are added to layout at once when the loop iteration complete.
// here is the code...
public void Add_Button(View view){
final MediaPlayer mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.button_sound);
gridlayout= (GridLayout) findViewById(R.id.layout);
animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.scale_button);
for(int i=0;i<10;i++) {
Button button = new Button(MainActivity.this);
button.setText(i+1 + "");
button.setPadding(10,10,10,10);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.height=70;
params.width=70;
params.topMargin = 10;
params.leftMargin = 10;
*//after one 1 second i want to add button*
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
button.setLayoutParams(params);
button.setAnimation(animation);
mediaPlayer.start();
gridlayout.addView(button);
}
}
You can add like this
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++ {
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
I want to make that TextView fill parent but my code doesn't work.
Here is my code:
TableLayout tableLayout = new TableLayout(this);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
TableRow.LayoutParams param = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
param.span = 2;
param.setMargins(1, 1, 1, 1);
int trainIndex = 0;
int stationIndex = 1;
for(int i = 0; i < 6; i++){
TableRow row = new TableRow(this);
if(i == 0){
TextView text = new TextView(this);
text.setText("Way");
text.setBackgroundColor(Color.GRAY);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(text, tableRowParams);
for(int j = 0; j < 4; j++) {
TextView text1 = new TextView(this);
text1.setText(getInfo(0, stationIndex));
text1.setBackgroundColor(Color.GRAY);
row.addView(text1, param);
stationIndex++;
}
}else {
for (int j = 0; j <= 8; j++) {
TextView text = new TextView(this);
if(j == 0){
text.setText(getInfo(1,trainIndex));
text.setBackgroundColor(Color.GRAY);
row.addView(text, tableRowParams);
trainIndex++;
}else {
text.setText(" train " + j);
text.setBackgroundColor(Color.GRAY);
row.addView(text, tableRowParams);
}
}
}
tableLayout.addView(row);
}
setContentView(tableLayout);
When I apply layout params to textview it doesn't work and when text isn't very long it causes white space in cells, I want that all cells have grey background.
Here is the sample how it looks:
What am I doing wrong ?
Be sure that your container has the FILL_PARENT property
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
You forgot to set LayoutParams for the TextView. By default is uses WRAP_CONTENT for both width and height.
Also, use MATCH_PARENT instead of FILL_PARENT
EDIT
Try this:
TableRow row = new TableRow(this);
TextView text = new TextView(this);
text.setText("Way");
text.setBackgroundColor(Color.GRAY);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(text);
I am creating a number of buttons in a table row programmatically, but they always end up left-aligned. How can I center the entire row? I am trying this to create each row:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);
You can simply center the row using a Gravity. Just set it to your row with
row.setGravity(Gravity.CENTER_HORIZONTAL);
Try this:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setLayoutParams(buttonParams);
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);
I'm trying to make a dynamic radio button to linear layout. I have two radio buttons which are called from List and added to Linear Layout. Everything works fine until I get bad looks at my radio button and get stuck with this layout. My question is why does my radio button layout not work at all?
Here is my code :
TextView label;
RadioButton[] rb;
RadioGroup radiogroup;
LinearLayout ll;
String title;
public MyBtn(Context context,String labelText,String options) {
super(context);
ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
label = new TextView(context);
label.setText(labelText);
label.setTextColor(getResources().getColor(R.color.label_color));
radiogroup = new RadioGroup(context);
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
radiogroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
LinearLayout.LayoutParams rbParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rbParam.weight = 1f;
rbParam.gravity = Gravity.CENTER | Gravity.LEFT;
LinearLayout.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rbParam2.gravity = Gravity.CENTER | Gravity.RIGHT;
rbParam2.weight = 1f;
rbLayout[0] = new RadioGroup.LayoutParams(rbParam);
rbLayout[1] = new RadioGroup.LayoutParams(rbParam2);
List<String> list = Arrays.asList(options.split("\\|"));
String[] opts = new String[list.size()];
list.toArray(opts);
rb = new RadioButton[list.size()];
for (int i = 0; i < list.size(); i++) {
rb[i] = new RadioButton(context);
rb[i].setText(opts[i]);
rb[i].setId(i);
radiogroup.addView(rb[i],rbLayout[i]);
}
title = label.getText().toString();
radiogroup.check(0);
ll.addView(label);
ll.addView(radiogroup);
//int resID = getResources().getIdentifier(buttonID, "id", context.getPackageName());
this.addView(ll);
}
Thank you.
My radio button :
image link
It should :
image link
change this:
LinearLayout.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
LinearLayout.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
to this:
RadioGroup.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
RadioGroup.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
there are many same mistake in your code. find out them and correct
let me know about result
LinearLayout.LayoutParams rbParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT,1f);
that must work.
let me know about it
something like this i want
for(i=0; i<10; i++) {
Textview tx[i] = new TextView(this);
tx[i].setText("Data")
TableRow tr[i] = new TableRow(this);
tr[i].addView(tx);
// and then adding table row in tablelayout
}
can somebody give me small example
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
for(i=0; i<10; i++) {
tx[i] = new TextView(this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx[i].setText("Data")
tr[i] = new TableRow(this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
// and then adding table row in tablelayout
}
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
for(i=0; i<10; i++) {
tx[i] = new TextView(this);
tx[i].setText("Data")
tr[i] = new TableRow(this);
tr[i].addView(tx[i]);
// and then adding table row in tablelayout
}