Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate multiple linear layout and divider programmatically
    text
    copied!<p>In my app. there is activity contain multiple <strong>linear layout and divider</strong> which created programmatically , <strong>its run fine</strong> ,</p> <p>but i have to duplicate the linear layout and divider 5 times ,and all are the same except two things :</p> <p>1- each linear layout has different string .</p> <p>2- first divider margin differ than others divider margin .</p> <p>is there's better approach to do that with more clean and shorter code .</p> <p>any help will be much appreciated .</p> <pre><code>public class Dreams extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.trip); if (customTitleSupported) { getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); } TextView tv = (TextView) findViewById(R.id.title); tv.setTypeface(FontFactory.getOldEnglish(getBaseContext())); tv.setText("Dreams"); LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout); // add text view TextView tv1 = new TextView(this); tv1.setGravity(Gravity.RIGHT); tv1.setTextSize(30); tv1.setTypeface(FontFactory.getOldEnglish(getBaseContext())); ll.addView(tv1); tv1.setText(Html.fromHtml(getString(R.string.dreams))); ImageView divider1 = new ImageView(this); LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); lp1.setMargins(40, 0, 40, 0); divider1.setLayoutParams(lp1); divider1.setBackgroundColor(Color.RED); ll.addView(divider1); TextView tv2 = new TextView(this); tv2.setGravity(Gravity.RIGHT); tv2.setTextSize(30); tv2.setTypeface(FontFactory.getOldEnglish(getBaseContext())); ll.addView(tv2); tv2.setText(Html.fromHtml(getString(R.string.dream_1))); ImageView divider2 = new ImageView(this); LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); lp2.setMargins(10, 10, 10, 10); divider2.setLayoutParams(lp2); divider2.setBackgroundColor(Color.RED); ll.addView(divider2); TextView tv3 = new TextView(this); tv3.setGravity(Gravity.RIGHT); tv3.setTextSize(30); tv3.setTypeface(FontFactory.getOldEnglish(getBaseContext())); ll.addView(tv3); tv3.setText(Html.fromHtml(getString(R.string.dream_2))); ImageView divider3 = new ImageView(this); LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); lp3.setMargins(10, 10, 10, 10); divider3.setLayoutParams(lp3); divider3.setBackgroundColor(Color.RED); ll.addView(divider3); TextView tv4 = new TextView(this); tv4.setGravity(Gravity.RIGHT); tv4.setTextSize(30); tv4.setTypeface(FontFactory.getOldEnglish(getBaseContext())); ll.addView(tv4); tv4.setText(Html.fromHtml(getString(R.string.dream_3))); ImageView divider4 = new ImageView(this); LinearLayout.LayoutParams lp4 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); lp4.setMargins(10, 10, 10, 10); divider4.setLayoutParams(lp4); divider4.setBackgroundColor(Color.RED); ll.addView(divider4); TextView tv5 = new TextView(this); tv5.setGravity(Gravity.RIGHT); tv5.setTextSize(30); tv5.setTypeface(FontFactory.getOldEnglish(getBaseContext())); ll.addView(tv5); tv5.setText(Html.fromHtml(getString(R.string.dream_4))); ImageView divider5 = new ImageView(this); LinearLayout.LayoutParams lp5 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); lp5.setMargins(10, 10, 10, 10); divider5.setLayoutParams(lp5); divider5.setBackgroundColor(Color.RED); ll.addView(divider5); TextView tv6 = new TextView(this); tv6.setGravity(Gravity.RIGHT); tv6.setTextSize(30); tv6.setTypeface(FontFactory.getOldEnglish(getBaseContext())); ll.addView(tv6); tv6.setText(Html.fromHtml(getString(R.string.dream_5))); ImageView divider6 = new ImageView(this); LinearLayout.LayoutParams lp6 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); lp6.setMargins(10, 10, 10, 10); divider6.setLayoutParams(lp6); divider6.setBackgroundColor(Color.RED); ll.addView(divider6); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload