How to Set Text for Textview for Particular Id which are generated dynamically in Linear Layout???
else if(j==4)
{
tvprodpcs_tot = new TextView(this);
tvprodpcs_tot.setInputType(InputType.TYPE_CLASS_NUMBER);
tvprodpcs_tot.setId(+i );
int totid=tvprodpcs_tot.getId();
tvprodpcs_tot.setBackgroundResource(R.drawable.edittext);
tvprodpcs_tot.setHeight(60);
tvprodpcs_tot.setPadding(0, 15,0, 15);
tvprodpcs_tot.setTextColor(Color.parseColor("#0000E6"));
tvprodpcs_tot.setBackgroundColor(Color.parseColor("#A2FF74"));
// tvprodpcs_tot.setVisibility(View.INVISIBLE);
// tvprodpcs_tot.setText("a");
tvprodpcs_tot.setLayoutParams(new LinearLayout.LayoutParams(0,90, 20));
tvprodpcs_tot.setGravity(Gravity.RIGHT);
linearLayout.addView(tvprodpcs_tot);
}
** I need to set text at child layout**
LinearLayout pLayout= (LinearLayout) findViewById(R.id.LinLayStkSub);
if (pLayout == null)
{
return;
}
TotClo=0; TotCloKg=0;TotPcs=0;
int rows=pLayout.getChildCount();
for(int i = id; i < pcount ; i++)
{
if ((pLayout.getChildAt(i) instanceof LinearLayout) )
{
LinearLayout SubLayout = (LinearLayout) pLayout.getChildAt(i);
for(int j = 0; j < SubLayout.getChildCount(); j++)
{ }
Try this,
TextView txtResMemberAge = new TextView(RegistrationPatientActivity.this);
txtResMemberAge.setLayoutParams(lparams);
txtResMemberAge.setText("Member Age: " +familyMemberModel.getStrMemberAge());
txtResMemberAge.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
txtResMemberAge.setTextSize(14);
txtResMemberAge.setTextColor(Color.parseColor("#9C9C9C"));
linearLayout.addView(txtResMemberAge);
lparams.setMargins(50, 10, 0, 0);
Related
I have 1 top layout and 3 bottom layouts, a user drags items from top layout to one of the 3 below.
The code below:
puts sequentially every image in every bottom layout into a list
removes items from each bottom layout
puts them back into top layout
public void onClick(View view) {
LinearLayout bottomLinearLayout1 = (LinearLayout) findViewById(R.id.bottom1);
LinearLayout bottomLinearLayout2 = (LinearLayout) findViewById(R.id.bottom2);
LinearLayout bottomLinearLayout3 = (LinearLayout) findViewById(R.id.bottom3);
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
int total_num1 = bottomLinearLayout1.getChildCount();
int total_num2 = bottomLinearLayout2.getChildCount();
int total_num3 = bottomLinearLayout3.getChildCount();
View current_image = null;
List<View> listOfkids = new ArrayList<>() ;
//************ REPEATS **************
for(int i = 0 ; i < total_num1 ; i++){
current_image = bottomLinearLayout1.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout1.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num2 ; i++){
current_image = bottomLinearLayout2.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout2.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num3 ; i++){
current_image = bottomLinearLayout3.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout3.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
Basically, the only difference between these loops is the last digit in "bottomLinearLayout"; the other code just copies itself!
Can i do this:
if(id == R.id.bottom1){
String current_layout = "bottomLinearLayout" + 1 ;
}
else if( id == R.id.bottom2){
current_layout = "bottomLinearLayout" + 2 ;
}
else if( id == R.id.bottom3){
current_layout = "bottomLinearLayout" + 3 ;
}
and then add this string as a command right into the java source code?
is this possible?
Assuming that you want to refactor the onClick method, you could create an array of the layout ids and iterate through them as follows:
int bottomLayoutIds[] = {R.id.bottom1, R.id.bottom2, R.id.bottom3};
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
for (int layoutId : bottomLayoutIds){
LinearLayout bottomLinearLayout = (LinearLayout) findViewById(layoutId);
int childCount = bottomLinearLayout.getChildCount();
List<View> listOfKids = new ArrayList<>() ;
for(int i = 0 ; i < childCount ; i++){
View currentImage = bottomLinearLayout.getChildAt(i);
listOfKids.add(currentImage);
}
bottomLinearLayout.removeAllViews();
for(int i = 0 ; i < listOfKids.size();i++){
topLinearLayout.addView( listOfKids.get(i));
}
}
I have problem with creating header of column and row with respective cell values. Only show last position value in both column and row.
ScrollView sv = new ScrollView(this);
TableLayout ll = new TableLayout(this);
HorizontalScrollView hsv = new HorizontalScrollView(this);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6", "Row 7" };
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6" };
int nor = row.length;
int noc = column.length;
TextView tv = new TextView(this);
tv.setText("Matrix Implemention Test");
for (int i = 0; i < nor; i++)
{ // for rows
TableRow tbrow = new TableRow(this);
for (int j = 0; j <= noc; j++)
{ // for columns
TextView tv1 = new TextView(this);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
// tv1[i][j].setId(id);
if (i == 0 & j==0) {
tv1.setText("0=0");
Log.d("TAH", "Display00!!!");
} else if (i == 0) {
for (int r = 0; r < nor; r++) {
tv1.setText(row[r]);
Log.d("TAG", "i==0->"+row[r]);
}
} else if (j == 0) {
for (int c = 0; c < noc; c++) {
tv1.setText(column[c]);
Log.d("TAG", "j==0->"+row[c]);
}
} else {
tv1.setText("Table Cell No=>> " + id);
}
tbrow.addView(tv1);
}
ll.addView(tbrow);
}
hsv.addView(ll);
sv.addView(hsv);
setContentView(sv);
Output like this: I want of respective header of both column and row. first row 1, row 2.... and column 1, column 2....and so on. but only last position value display above code.
change your code to:
for (int i = 0; i < nor; i++) { // for row
TableRow tbrow = new TableRow(this);
for (int j = 0; j <= noc; j++) { // for column
TextView tv1 = new TextView(this);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
// tv1[i][j].setId(id);
if (i == 0 & j==0) {
tv1.setText("0=0");
Log.d("TAH", "Display00!!!");
} else if (i == 0) {
tv1.setText(column[j]);
} else if (j == 0) {
tv1.setText(row[i]);
} else {
tv1.setText("Table Cell No=>> " + id);
}
tbrow.addView(tv1);
}
ll.addView(tbrow);
}
First thing you seems to have confused columns with rows so i corrected that.
And second notice this for loop:
for (int r = 0; r < nor; r++) {
tv1.setText(row[r]);
Log.d("TAG", "i==0->"+row[r]);
}
is missing.
Here you are first setting row[0] value to tv1 and then row[1] ... so on.
And in the end tv1 has row[6] as the last value.
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]);
}
}
I want this table to extend as possible to fill the remaining (empty) area of the linear layout that contains it
I generate the table using the following method
public void drawTable(LinearLayout tableLayout,String tableTitle, String[][] data,String[] headerTitles, int type){
//clearing previous views
tableLayout.setVisibility(View.VISIBLE);
tableLayout.setGravity(Gravity.CENTER);
int chids = tableLayout.getChildCount();
if (chids > 0){
tableLayout.removeAllViews();
}
if((headerTitles!= null) && (headerTitles.length != data[0].length) ){
return;
}
TableLayout table = new TableLayout(context);
// table.setStretchAllColumns(true);
// table.setShrinkAllColumns(true);
//Rendering the table title
TextView tableTitleView = new TextView(context);
tableTitleView.setText(tableTitle);
tableTitleView.setTextColor(Color.WHITE);
tableTitleView.setGravity(Gravity.CENTER);
tableTitleView.setTextSize(18);
tableTitleView.setTextAppearance(context, R.style.boldText);
tableTitleView.setBackgroundColor(Color.parseColor("#008888"));
tableTitleView.setPadding(2, 2, 2, 10);
tableLayout.addView(tableTitleView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
ScrollView scroll1 = new ScrollView(context);
HorizontalScrollView scroll2 = new HorizontalScrollView(context);
int rows = data.length;
int columns = data[0].length;
if(headerTitles!=null){
//Rendering the header
TableRow headerRow = new TableRow(context);
for (int i = 0; i < columns; i++) {
TextView t = new TextView(context);
t.setTextSize(17);
t.setTextAppearance(context, R.style.boldText);
t.setPadding(5, 5, 5, 5);
t.setText(headerTitles[i]);
t.setGravity(Gravity.CENTER);
t.setTextColor(Color.WHITE);
t.setBackgroundResource(R.drawable.text_border_header_1);
headerRow.addView(t, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(headerRow, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
//Rendering the Table Data
TableRow row;
for (int current = 0; current < rows ; current++) {
row = new TableRow(context);
TextView t;
for(int i =0 ; i < columns ; i++){
t = new TextView(context);
t.setTextSize(15);
t.setPadding(5,5,5,5);
t.setText(data[current][i]);
t.setGravity(Gravity.CENTER);
if (type == 1) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd);
} else {
t.setBackgroundResource(R.drawable.text_border_even);
}
}else if (type == 2) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_2);
} else {
t.setBackgroundResource(R.drawable.text_border_even_2);
}
}else if (type == 3) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_3);
} else {
t.setBackgroundResource(R.drawable.text_border_even_3);
}
}
t.setTextColor(Color.BLACK);
row.addView(t,TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
scroll2.addView(table,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.addView(scroll2,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.setPadding(2, 20, 2, 20);
tableLayout.addView(scroll1,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
}
I couldn't figure out exactly the problem with my code.
EDIT :
when I added the table directly to the linear layout instead of the scroll views the table fits with the linear layout width. It seems that my problem is with the scroll view
I found the answer Here (thanks to Felix)
Just a single line of code to be added to the scroll views
scrollView.setFillViewPort(true);
try this on your TableLayout object.
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.width = LayoutParams.FILL_PARENT;
table.setLAyoutParams(params);
Edit:
TextView tView = new TextView(this);
LinearLayout.LayoutParams params = tView.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.weight = 1;
tView.setLayoutParams(params);
regards,
Aqif
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);