A look at your smartphone or a smart watch your colleague is wearing would
give you a fair idea of The Android Life.
In Q2 of 2015 Android had a market share of 82.8% which makes it a harbinger
against any other OS.
The reason for this majority market share is the continuous and frequent development
in this Google owned platform.
To cite an example the beginning of 2015 had Lollipop as Android’s latest version
and come October the upgrade was made to Marshmallow 6.0.
There is no doubt about the fact that these developments create new avenues for
IT professionals, programmers, developers and engineers.Also, The Android technology’s
advancement is going to touch almost every aspect of our lives in the future
& so my intertest in learning android and making people to get a hang of it..
Today I am going to cover on :- CREATING DYNAMIC COMPONENTS AT RUNTIME IN ANDROID
There is no static in real world scenario when it comes to Programming or Coding.
Dynamic is the way to go where one has no idea about the data,size of data,
nature of data,its content, etc.
Talking about dynamic component/content; I will be creating dynamic rows
and append it to already available table on android layout.
Below is the DemoActivity class which extends AppCompatActivity class
will be our main activity class which will get call when we will run this program.
public class DemoActivity extends AppCompatActivity {
TableLayout tableLayout = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
tableLayout = (TableLayout) findViewById(R.id.tableLayout);
Button addButton = (Button) findViewById(R.id.ADD);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addRow();
}
});
addRow();
}
}
In above code snippet; Below is the points of explanation :-
1) I am simply creating object of tableLayout which i have declared at
global/class level in onCreate method (overridden method).
R.id.tableLayout is available in layout file with ID as 'tableLayout'
2) same goes with addButton Object
3) addRow() is the function which will get call when this activity gets
loaded up as this method is getting called in onCreate method.
addRow() method is the hero of this blog. It is the actual method which will
create dynamic content of code.
private void addRow(){
TableRow tableRow = new TableRow(this);
TableLayout tableLayout1 = new TableLayout(this);
TableRow tableRow_toADD = new TableRow(this);
EditText editText1 = new EditText(this);
editText1.setText("one");
EditText editText2 = new EditText(this);
editText2.setText("two");
EditText editText3 = new EditText(this);
editText3.setText("three");
EditText editText4 = new EditText(this);
editText4.setText("four");
Spinner mySpinner = new Spinner(this);
List<String> list = new ArrayList<String>();
list.add("five");
list.add("six");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);
EditText editText21 = new EditText(this);
editText21.setText("seven");
tableRow_toADD.addView(editText1);
tableRow_toADD.addView(editText2);
tableRow_toADD.addView(editText3);
tableRow_toADD.addView(editText4);
tableRow2_TOADD.addView(mySpinner);
tableRow2_TOADD.addView(editText21);
tableLayout1.addView(tableRow_toADD);
tableLayout1.addView(tableRow2_TOADD);
tableRow.addView(tableLayout1);
tableLayout.addView(tableRow);
}
I am simply creating components above and adding it to its parent component
and finally to tablelayout already available.
Below is the Ouput :-
Complete tutorial can be view on below youtube link "-
https://youtu.be/QC8k3phGyog

No comments:
Post a Comment