activity_main.xml
<?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: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.example.divakar.dynamicbuttoningrid.MainActivity"> <Button android:id="@+id/additembutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add row to table" /> <TableLayout android:id="@+id/mTlayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/additembutton" /> </RelativeLayout>
Mainactivity.java
import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TableLayout table = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); table = (TableLayout)findViewById(R.id.mTlayout); Button additembutton = (Button) findViewById(R.id.additembutton); additembutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { update(); } }); } private void update() { TableRow tr = new TableRow(this); tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); // adding text TextView tv = new TextView(this); tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); tv.setText("New Entry"); tv.setBackgroundColor(Color.parseColor("#12e7f4")); tr.addView(tv); // adding button Button button = new Button(this); button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); button.setText("New Button"); tr.addView(button); table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); }}
No comments:
Post a Comment