This is my code for a n app in which i want to add textviews in a row
dynamically on click of a button and not working can you tell me whats wrong with the code,i am just a beginner.
thanking you in anticipation.
public class MainActivity extends AppCompatActivity {
Button save;
TableLayout tableLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button save = (Button) findViewById(R.id.save);
TableLayout tableLayout = (TableLayout) findViewById(R.id.table1);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
TableRow tableRow;
TextView textView;
TableLayout tableLayout = (TableLayout) findViewById(R.id.table1);
for (int i = 0; i < 4; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < 3; j++) {
textView = new TextView(getApplicationContext());
textView.setText("####");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}}});
setContentView(tableLayout);
}
}
and my xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.kalla.monu.saplist.MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="28dp"
android:layout_marginTop="160dp"
android:id="#+id/table1"
android:background="#color/colorPrimaryDark">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#android:color/holo_green_dark" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:id="#+id/save" />
</RelativeLayout>
Try this,
I assume you are trying to add tablerows and textviews both dynamically. So I have removed existing tablerows from layout xml.
TableLayout tableLayout = (TableLayout) findViewById(R.id.table1);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
TableRow tableRow;
TextView textView;
for (int i = 0; i < 4; i++) {
tableRow = new TableRow(this);
for (int j = 0; j < 3; j++) {
textView = new TextView(this);
textView.setText("####");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
table1.addView(tableRow);
}}});
In XML:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table1"/>
Related
I'm doing an android application where I have to dynamically put a button inside the table row. The problem is that the button that I create is stretched as in the image below:
I also put some of the application code here below so you can understand better.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_punteggio);
showAlertDialog_modality(R.layout.dialog_change_modality);
final TableLayout tableLayout = findViewById(R.id.tableLayout);
final RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
final Button btn_settings = findViewById(R.id.btn_settings);
Bundle datipassati = getIntent().getExtras();
String player = datipassati.getString("players");
giocatore = player.split("%");
Log.d("TAG", "array: " + giocatore[0]);
for (int i = 0; i < giocatore.length; i++) {
punti[i] = 0;
TableRow tbrow = new TableRow(this);
final TextView t3v = new TextView(this);
txPunti[i] = t3v;
//------------------------- Textview Player
final TextView t1v = new TextView(this);
t1v.setText(giocatore[i].toUpperCase());
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
t1v.setTextSize(20);
t1v.setWidth(400);
tbrow.addView(t1v);
//-------------------- BTN MENO
Button btnMeno = new Button(this);
btnMeno.setText("-");
btnMeno.setTextColor(Color.BLACK);
btnMeno.setGravity(Gravity.CENTER);
tbrow.addView(btnMeno);
btnMeno.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
removepoint(t3v);
}
});
// --------------------- TEXT VIEW PUNTI
t3v.setText(punti[i]+"");
t3v.setTextSize(25);
t3v.setMaxWidth(150);
t3v.setPadding(20,0,20,0);
t3v.setTypeface(t3v.getTypeface(), Typeface.BOLD);
t3v.setTextColor(Color.RED);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
//----------------------------- BTN PIU
Button btnPiu = new Button(this);
btnPiu.setBackground(ContextCompat.getDrawable(Activity_punteggio.this, R.drawable.ic_add_circle_black_24dp));
btnPiu.setTextColor(Color.BLACK);
btnPiu.setGravity(Gravity.CENTER);
tbrow.addView(btnPiu);
btnPiu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addPoint(t3v, t1v);
}
});
tableLayout.addView(tbrow);
}
btn_settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAlertDialog_modality(R.layout.dialog_change_modality);
}
});
}
Here's also the xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity_punteggio">
<Button
android:id="#+id/btn_settings"
android:layout_width="55dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:layout_marginRight="1dp"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_settings_black_24dp" />
<TextView
android:id="#+id/title_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Chip-Chop"
android:textSize="25dp"
android:textStyle="bold|italic" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp">
</TableLayout>
</RelativeLayout>
I hope you could help me.
Please check this solution. setImageResource is the method assign
image resource. Set background method is recommended for the
background of the button.
ImageButton btnPiu = new ImageButton(this);
btnPiu.setImageDrawable(ContextCompat.getDrawable(Activity_punteggio.this, R.drawable.ic_add_circle_black_24dp));
btnPiu.setTextColor(Color.BLACK);
btnPiu.setGravity(Gravity.CENTER);
tbrow.addView(btnPiu);
btnPiu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addPoint(t3v, t1v);
}
});
tableLayout.addView(tbrow);
I used ImageButton other than Button.
setImageDrawable
try to edit android:layout_height="32dp" to android:layout_height="55dp"
android:id="#+id/btn_settings"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:layout_marginRight="1dp"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_settings_black_24dp" />
I hope it will work with you .
THIS is make very complex coding, Make it simpler.Don't create whole row dynamically in FOR loop. Instead of that
First Create Separate XML for that row.
Write Below code and your row append into tablelayout.
for (int i = 0; i < giocatore.length; i++) {
View trView = LayoutInflater.from(getActivity()).inflate(R.layout.xmlID,null);
TextView lblGuestName;
lblGuestName = trView.findViewById(R.id.lbl);
lblGuestName.setText("Guest 1");
tableLayout.addView(trView);
}
This Way you easily configure any complex row in tablelayout. No need to make it dynamically.
Feel free to reply.
I'm new to Android developing.
At the moment I'm trying to fill in a TableLayout with my data.
The Data part works just well but TextView parameters from TableRow don't affect my design.
Here's my Activity whith the table:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<include layout = "#layout/table_row"/>
</TableLayout>
</ScrollView>
and here's TableRow XML I use to inflate
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBFBFB">
<TextView
android:id="#+id/number"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="2dp"
android:layout_weight="0"
android:textSize="16sp"/>
<TextView
android:id="#+id/school_name"
android:textSize="16sp"
android:background="#color/colorAccent"
android:layout_weight="1"
android:text="School Name"/>
<TextView
android:id="#+id/rating"
android:textSize="16sp"
android:padding="2dp"
android:layout_weight="2"
android:text="Rating"/>
</TableRow>
and Java code:
public void fillTable () {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
LayoutInflater inflater = LayoutInflater.from(this);
for(int i = 0; i<schools.size(); i++){
addRow(i, inflater, tableLayout);
}
}
public void addRow(int num, LayoutInflater inflater, TableLayout parent) {
TableRow tr = (TableRow) inflater.inflate(R.layout.table_row,parent,false);
tr.setId(num + 1);
TextView tv = tr.findViewById(R.id.number);
tv.setText(String.valueOf(num+1));
tv = tr.findViewById(R.id.school_name);
tv.setText(schools.get(num).schoolName);
tv = tr.findViewById(R.id.rating);
tv.setText(String.valueOf(schools.get(num).score));;
final int index = num ;
tr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SchoolCard.class);
intent.putExtra(SCHOOL_LINK,schools.get(index).schoolCardLink);
startActivity(intent);
}
});
parent.addView(tr);
}
Can't find where problem is.
Place the TableRow inside the Table tab and run.
I have the following code:
public class MainActivity extends Activity implements OnClickListener {
public ArrayList<RelativeLayout> buttons = new ArrayList<RelativeLayout>();
public ArrayList<Integer> drawableId = new ArrayList<Integer>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
linear = (LinearLayout) findViewById(R.id.Rlayout);
scrool = (ScrollView) findViewById(R.id.scrool);
for (int i = 0; i < buttons.size(); i++) {
{
String buttonID = "Llayout" + i;
int resID = getResources().getIdentifier(buttonID, "id",
getPackageName());
//The problem is here
buttons.get(i).add((RelativeLayout) findViewById(resID));
buttons.get(i).setOnClickListener(this);
drawableId.add(getResources().getIdentifier("supa" + i, "drawable", getPackageName()));
}
}
}
#Override
public void onClick(View v) {
for (int i = 0; i < buttons.size(); i++) {
if (buttons.get(i).getId() == v.getId()) {
index = i;
Intent trimite = new Intent(MainActivity.this, RecipeView.class);
Bundle colet = new Bundle();
colet.putString("key", Content.RETETE[i]);
colet.putInt("keyimg", drawableId.get(i));
trimite.putExtras(colet);
startActivity(trimite);
break;
}
}
}
}
I cannot get this to work:
buttons.get(i).add((RelativeLayout) findViewById(resID));
Java error says that: add((RelativeLayout) is undefined for the type RelativeLayout.
The add method works for ArrayList drawableId, one line below but not for buttons. Can someone, please, help me?
Here is my xml layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrool"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="#+id/Rlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:keepScreenOn="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/Llayout0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv0"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="left"
android:contentDescription="#string/desc"
android:src="#drawable/supa0" />
<TextView
android:id="#+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/supa"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Llayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="left"
android:contentDescription="#string/desc"
android:src="#drawable/supa0" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/supa1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
I made buttons from RelativeLayouts. Each RelativeLayout button has text and an icon next to the text. All of the RelativeLayouts buttons are in a LinearLayout so that they align under each other.
When the RelativeLayouts buttons and the ArrayList drawableId were simple arrays it worked great but I don't want to modify the array every time I add a new RelativeLayout button.
Any suggestions?
The RelativeLayout class has no add() method.
buttons is an ArrayList of RelativeLayouts. When you call buttons.get(i), you get a single RelativeLayout object. Since RelativeLayout has no add() method, you get this error.
It isn't entirely clear what your intent is with this code, but I am assuming that you are trying to add each of your RelativeLayouts to the buttons list.
If this is correct, you just need to remove the get(i) call, making that line into
buttons.add((RelativeLayout) findViewById(resID));
Try this way,hope this will help you to solve your problem.
public class MainActivity extends Activity implements View.OnClickListener {
private ArrayList<RelativeLayout> buttons = new ArrayList<RelativeLayout>();
private ArrayList<Integer> drawableId = new ArrayList<Integer>();
private LinearLayout linear;
private ScrollView scrool;
private int index;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
linear = (LinearLayout) findViewById(R.id.Rlayout);
scrool = (ScrollView) findViewById(R.id.scrool);
for (int i = 0; i < linear.getChildCount(); i++) {
{
String buttonID = "Llayout" + i;
int resID = getResources().getIdentifier(buttonID, "id",getPackageName());
buttons.add((RelativeLayout)findViewById(resID));
buttons.get(i).setOnClickListener(this);
drawableId.add(getResources().getIdentifier("supa" + i, "drawable", getPackageName()));
}
}
}
#Override
public void onClick(View v) {
for (int i = 0; i < buttons.size(); i++) {
if (buttons.get(i).getId() == v.getId()) {
index = i;
Toast.makeText(MyActivity.this,String.valueOf(index),Toast.LENGTH_SHORT).show();
Intent trimite = new Intent(MainActivity.this, RecipeView.class);
Bundle colet = new Bundle();
colet.putString("key", Content.RETETE[i]);
colet.putInt("keyimg", drawableId.get(i));
trimite.putExtras(colet);
startActivity(trimite);
break;
}
}
}
}
I have following data in this format:
(0,0) -10
(1,0) - 20
(1,1) - 30
Medical- (0,0) -Jack
One medical Student
Engineer - (1,0) - Jones
(1,1) - Danny
two Engineer Student
I try this:
for (int j = 0; j < Test.subNameArrayList.size(); j++) {
for (int i = 0; i < 2; i++) {
Subject[j] = new String[1];
Subject[j][i] = Test.subNameArrayList
.get(j);
Name[j] = new String[2];
Name[j][i] = Test.NameArrayList
.get(j);
}
}
how to display in matrix form in java and android. and how to find row size and column size.Thanks in advance.
Xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
Activity:
public class TestActivity extends AppCompatActivity {
TableLayout tableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tableLayout = findViewById(R.id.tableLayout1);
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
fillTable(matrix,tableLayout);
}
private void fillTable(final int[][] matrix, TableLayout table) {
table.removeAllViews();
for (int i = 0; i < matrix.length; i++) {
TableRow row = new TableRow(TestActivity.this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < matrix[i].length; j++) {
TextView textView = new TextView(TestActivity.this);
textView.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);
textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
textView.setText((matrix[i][j]+""));
textView.setPadding(10,10,10,10);
row.addView(textView);
}
table.addView(row);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/myImView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="#drawable/image1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/myTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</LinearLayout>
I want to add n-rows in table "myTable" at run time what should i do ?
currently i am using this code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTable = (TableLayout) findViewById(R.id.myTable);
TextView tv = new TextView(this);
for (int x = 0; x < 5; x++) {
tv.setText("This is row number=" + (x + 1));
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tv);
myTable.addView(tr, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
}
but it is giving me this error
03-26 00:29:34.070:
E/AndroidRuntime(2230): java.lang.RuntimeException: Unable to start activity ComponentInfo{assignment.1/assignment.1.myView}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTable = (TableLayout) findViewById(R.id.myTable);
for (int x = 0; x < 5; x++) {
TextView tv = new TextView(this);
tv.setText("This is row number=" + (x + 1));
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tv);
myTable.addView(tr, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
}
Try now a small and minor change
I guess this is because you are creating the row object new every time in your loop, but you dont do this with the TextView Object.
Like this it should work:
TableLayout myTable = (TableLayout) findViewById(R.id.myTable);
TextView tv;
TableRow tr;
for (int x = 0; x < 5; x++) {
tv = new TextView(this);
tv.setText("This is row number=" + (x + 1));
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tv);
myTable.addView(tr, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}