I have a scroolView containing a table with buttons, I'm trying to request focus to one of the buttons but with no success
here is the code (some lines were removed to avoid floating)
private void reDraw()
{
ll = new LinearLayout(this);
ll.setOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ll.setGravity(Gravity.FILL);
ll.setBackgroundResource(R.drawable.modern_background);
ll.setPadding(20, 20, 20, 20);
sv = new ScrollView(this);
sv.setPadding(0, 20, 0, 0);
tl = new TableLayout(this);
tl.setGravity(Gravity.FILL);
tl.setStretchAllColumns(true);
get_start_and_end_of_month(Start.globalDate);
day = 1;
int dayInWeek = startOfTheMonth.get(java.util.Calendar.DAY_OF_WEEK);
String temp;
tr = new TableRow(this);
tr.setGravity(Gravity.FILL_HORIZONTAL);
for(int k = 0; k < 7; k++)
{
b = new Button(this);
b.setGravity(Gravity.CENTER);
b.setBackgroundResource(R.drawable.widget_cell_day);
b.setText(Start.hebLayout ? daysHebrew[k] : daysEnglish[k]);
tr.addView(b);
}
tl.addView(tr);
for(int i = 0; i < 5; i++)
{
tr = new TableRow(this);
tr.setGravity(i == 0 ? Gravity.RIGHT : Gravity.LEFT);
for(int j = 1; j < 8; j++)
{
if(!(i == 0 && j < dayInWeek))
{
db = new DateButton(this, startOfTheMonth.getTime());
db.setGravity(Gravity.CENTER);
if(new HdateNew(Start.globalDate).get_hd_day() == day)
db.setFocusableInTouchMode(true);
db.setId(day);
tr.addView(db);
if(day == new HdateNew(endOfTheMonth.getTime()).get_hd_day())
break;
day++;
startOfTheMonth.add(java.util.Calendar.DATE, 1);
}
}
tl.addView(tr);
}
sv.addView(tl);
ll.addView(sv);
setContentView(ll);
db = (DateButton)findViewById(new HdateNew(Start.globalDate).get_hd_day());
db.setFocusable(true);
db.requestFocus();
}
Now as you can see I did set the button to be focusable but it ain't working for me
//try set content view # last
db = (DateButton)findViewById(new HdateNew(Start.globalDate).get_hd_day());
db.setFocusable(true);
db.requestFocus();
setContentView(ll);
Related
I have a string, comma separated, from mysql database witch I split into an array.
My problem is that when I try to put them in a tablerow programatically it display each split value in a new row. I want to display it in same row with 4 columns.
This is my code:
String currentString = Order_list;
String[] separated = currentString.split(",");
TableLayout secondTbl = findViewById(R.id.secondTable);
for(int i=0;i<separated.length;i++){
TableRow row= new TableRow(getApplicationContext());
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setBackgroundResource(R.drawable.row_drawable);
txtOrdprod = new TextView(getApplicationContext());
row.addView(txtOrdprod);
TableRow.LayoutParams params = (TableRow.LayoutParams) txtOrdprod.getLayoutParams();
params.leftMargin = 18;
params.rightMargin = 10;
params.topMargin = 10;
txtOrdprod.setLayoutParams(params);
txtOrdprod.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
txtOrdprod.setTextColor(Color.BLACK);
txtOrdprod.setTypeface(null, Typeface.BOLD);
txtOrdprod.setText(separated[i]);
secondTbl.addView(row,i);
}
You have to create a row, and put your columns (seperated strings) into that row. For example:
String orders = "2, Product, 100 Tablets, 50 Eur, 3, Product, 100 Tablets, 75 Eur";
int k = 0;
int startPoint = 0;
List<String> orderList = new ArrayList<>();
for(int i = 0; i < orders.length(); i++)
{
if(orders.charAt(i) == ',')
{
k++;
if(k == 4)
{
String ab = orders.substring(startPoint, i);
orderList.add(ab);
startPoint = i + 1;
k = 0;
}
}
}
TableLayout secondTbl = findViewById(R.id.secondTable);
int rowIndex = 0;
for(String order : orderList)
{
String[] separated = order.split(",");
TableRow row = new TableRow(getApplicationContext());
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setBackgroundResource(R.drawable.row_drawable);
secondTbl.addView(row, rowIndex);
for(int i = 0; i < separated.length; i++)
{
txtOrdprod = new TextView(getApplicationContext());
row.addView(txtOrdprod);
TableRow.LayoutParams params = (TableRow.LayoutParams)
txtOrdprod.getLayoutParams();
params.leftMargin = 18;
params.rightMargin = 10;
params.topMargin = 10;
txtOrdprod.setLayoutParams(params);
txtOrdprod.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
txtOrdprod.setTextColor(Color.BLACK);
txtOrdprod.setTypeface(null, Typeface.BOLD);
txtOrdprod.setText(separated[i]);
}
rowIndex++;
}
But keep in mind that the orders string needs to be formatted like the on in the example, otherwise it will not work. It would be better if you have a POJO for an order and work with this one.
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 want to set user input to the textviews in a tablelayout for a word game application.i am new to android please help me through this.
tl = (TableLayout)findViewById(R.id.table);
TableLayout.LayoutParams tablelparam = new TableLayout.LayoutParams(30,30);
for (int r = 1; r <=9; r++) {
r1= new TableRow(this);
r1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int c = 1; c<=5; c++) {
TextView txtview=new TextView(MainActivity.this);
txtview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
txtview.setId(counter);
counter++;
txtview.setText( +counter + inputWord);
r1.addView(txtview);
}
tl.addView(r1,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
}
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<String>words = new ArrayList<String>();
for (int j = 0; j < words.size(); j++) {
TextView txtview = new TextView(MainActivity.this);
txtview.setId(j);
txtview.setText("" + words.get(j));
txtview.getLayoutParams();
t3.setText(t3.getText() + " " + txtview.getText());
}
};
Set the onClickListener() for each TextView you instantiate inside your for loop.
How to get an array items like this
["String1", "string2", "string3",.....,"Stringn"]
but if we use Arrays.toString(array) it will display like [string1, string2, string3] but i want like above.
Thnaks in advance
This is how I did it:
private void btn3() {
LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
layout.removeAllViewsInLayout();
layout.setPadding(15, 15, 15, 15);
tv2 = new TextView[list.size()];
for (int i = 0; i < list.size(); i++) {
tv2[i] = (TextView) new TextView(Random.this);
}
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
for (int i = 0; i < list.size(); i++) {
tv2[i].setLayoutParams(lparams);
tv2[i].setText((i + 1) + " " + list.get(i));
layout.addView(tv2[i]);
}
}