Step to create custom layout dynamically
You need to either create android.widget.*; from java or inflate view from
already defined xml layout.
Code to create view dynamically by creating View from android.widget:
private void createTextView(){
TextView textView = new TextView(getApplicationContext());
textView.setText("Dynamic Text");
linearLayoutParent.addView(textView);
//You can define setOnClickListener textView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
}
});
}
Code to create view dynamically by inflating custom view from xml:
private void addView(String data){
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//You can use this view to findViewById, setOnClickListener, setText etc;
View view = layoutInflater.inflate(R.layout.rowlayout,null,false);
//FindView using inflated view
TextView textView = view.findViewById(R.id.name);
linearLayout.addView(view);
}
Comments
Post a Comment