1

In my app. there is activity contain multiple linear layout and divider which created programmatically , its run fine ,

but i have to duplicate the linear layout and divider 5 times ,and all are the same except two things :

1- each linear layout has different string .

2- first divider margin differ than others divider margin .

is there's better approach to do that with more clean and shorter code .

any help will be much appreciated .

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);
        }
    }
androidqq6
  • 1,464
  • 2
  • 22
  • 47

2 Answers2

5

Since all that is changing is the TextView setText() you can make this a for loop with a list of String inputs. For example:

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);       

String[] textEntries = {  getString(R.string.dream),
                          getString(R.string.dream_1),
                          getString(R.string.dream_2),
                          getString(R.string.dream_3),
                          getString(R.string.dream_4),
                          getString(R.string.dream_5)
                        };

for ( int i = 0; i < textEntries.length; i++)
{
    TextView tv = new TextView(this);
    tv.setGravity(Gravity.RIGHT);
    tv.setTextSize(30);
    tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
    ll.addView(tv);
    tv.setText(Html.fromHtml(textEntries[i]));

    ImageView divider = new ImageView(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
    lp.setMargins(10, 10, 10, 10);
    divider.setLayoutParams(lp);
    divider.setBackgroundColor(Color.RED);
    ll.addView(divider);
}
sbyrnes
  • 201
  • 2
  • 4
0

first of all it would be easier if you define your layouts in XML instead of adding them programmatically. You will profit from the benefits of the UI editor as well. :)

Second, you may want to use ListView and an Adapter to fill the list, since you do not want do duplicate the same tasks for each layout.

Maybe these two links are helpful: 1. http://developer.android.com/guide/topics/ui/declaring-layout.html 2. http://developer.android.com/guide/topics/ui/layout/listview.html

So, to finally answer your question, I would do the following:

  • Create a file, e.g. list_item.xml, with something like:

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"><TextView your attributes.../></LinearLayout>
    
  • Create another layout, for instance main.xml, which contains a ListView. You can change the color of the divider like described here How to change the divider color in the listview?.

  • In your code (activity or fragment) add the main.xml as content view via setContentView().

  • Also in your code you should then add an adapter to the ListView which then populates the list for you. Here is an example How to customize listview using baseadapter

Finally, and since you separate the concerns (design and code), you could achieve what you want with just a few lines in your activity (the layout stuff would be in the xml and the population could be moved to a separated class like MyAdapter.java...)

Hope that helps...

Community
  • 1
  • 1
André Diermann
  • 2,515
  • 18
  • 27
  • thanks for your answer , first off all , id like to have it in jave as : one linear layout and one divider and one class , something like creationlayoutwithdivider class , in the other hand for listview each item text will be different and must get it from string .xml as its long text and customized each string with html tags so how to get listview items strings from string.xml , thanks again – androidqq6 Jul 01 '13 at 18:49
  • It's possible to define a string array http://developer.android.com/guide/topics/resources/string-resource.html#StringArray. If you want to define HTML in your ressources you should consider http://stackoverflow.com/questions/4490073/xml-within-an-android-string-resource. Finally this example might help you http://stackoverflow.com/a/2395270/2538428. – André Diermann Jul 01 '13 at 19:01