-1

In my application i want to show VIEW ALL to left align like below image:- enter image description here

But I found this type of layout and more thing i also do all these layout programmatically. enter image description here

My SourceCode:-

private void printFrontCategory(){

    for(int i=0;i<main.size();i++) {

        View v = new View(MainActivity.this);
        v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, 5));
        v.setBackgroundColor(Color.rgb(51, 51, 51));

    /*    View v1 = new View(MainActivity.this);
        v1.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT,5));
        v1.setBackgroundColor(Color.rgb(255, 255, 255));*/

        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(MainActivity.this);
        horizontalScrollView.setHorizontalScrollBarEnabled(false);

        TextView textView = new TextView(MainActivity.this);
        String content = front_category_catetory_name.get(i);
        content = content.replace("&#039;", "");
        content = content.replace("&amp;","");
        textView.setText(content);
        textView.setGravity(Gravity.RIGHT);
      //  textView.setGravity(Gravity.CENTER);
        textView.setTextSize(20);

        TextView textView2 = new TextView(MainActivity.this);
        textView2.setText("VIEW ALL");
        textView2.setTextColor(Color.BLUE);
        textView2.setGravity(Gravity.LEFT);

        LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
        linearLayout2.setOrientation(LinearLayout.HORIZONTAL);

        linearLayout2.addView(textView,0);
        linearLayout2.addView(textView2,1);


        LinearLayout linearLayout = new LinearLayout(MainActivity.this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        for (int j = 0; j < main.get(i).size(); j++) {
            LinearLayout linearLayout1 = new LinearLayout(MainActivity.this);
            linearLayout1.setOrientation(LinearLayout.VERTICAL);

            ImageView image = new ImageView(MainActivity.this);
            TextView nameProduct = new TextView(MainActivity.this);
            TextView priceProduct = new TextView(MainActivity.this);
            TextView special_discount = new TextView(MainActivity.this);
                /*    Log.d("counter val",cnt+"");
                    Log.d("thumb ",front_category_thumb.size()+"");
                    Log.d("image", front_category_thumb.get(52));*/
            new SetImageView(image).execute(main.get(i).get(j).get(1));

            nameProduct.setText(main.get(i).get(j).get(2));

            if (!String.valueOf(main.get(i).get(j).get(5)).equals(null)) {
                priceProduct.setText(main.get(i).get(j).get(3));
                special_discount.setText(main.get(i).get(j).get(5));
                priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                priceProduct.setGravity(Gravity.CENTER);
                special_discount.setGravity(Gravity.CENTER);
                nameProduct.setGravity(Gravity.CENTER);
                linearLayout1.addView(image);
                linearLayout1.addView(nameProduct);
                linearLayout1.addView(priceProduct);
                linearLayout1.addView(special_discount);
            } else if (!String.valueOf(main.get(i).get(j).get(4)).equals(null)) {
                priceProduct.setText(main.get(i).get(j).get(3));
                special_discount.setText(main.get(i).get(j).get(4));
                priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                priceProduct.setGravity(Gravity.CENTER);
                special_discount.setGravity(Gravity.CENTER);
                nameProduct.setGravity(Gravity.CENTER);
                linearLayout1.addView(image);
                linearLayout1.addView(nameProduct);
                linearLayout1.addView(priceProduct);
                linearLayout1.addView(special_discount);
            } else {
                priceProduct.setText(main.get(i).get(j).get(3));
                priceProduct.setGravity(Gravity.CENTER);
                nameProduct.setGravity(Gravity.CENTER);
                linearLayout1.addView(image);
                linearLayout1.addView(nameProduct);
                linearLayout1.addView(priceProduct);
            }
            linearLayout.addView(linearLayout1, j);
        }
        horizontalScrollView.addView(linearLayout);
   //    linearLayoutmens.addView(textView);
    //    linearLayoutmens.addView(v1);
        linearLayoutmens.addView(linearLayout2);
        linearLayoutmens.addView(horizontalScrollView);
        linearLayoutmens.addView(v);
    }
}

I am new in android programming. Please help me!

Anand Jain
  • 2,365
  • 7
  • 40
  • 66

1 Answers1

1

If you haven't found solution yet, then you can try the following
textView.setGravity(Gravity.RIGHT); This basically act as Gravity for text within TextView for more info refer So basically it is counter part of android:Gravity. Hence we need LayoutParam
Your problem it can be solved in two ways using LinearLayout and RelativeLayout
change the below code from

TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("&#039;", "");
content = content.replace("&amp;","");
textView.setText(content);
textView.setGravity(Gravity.RIGHT);
//  textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);

TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.LEFT);

LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);

linearLayout2.addView(textView,0);
linearLayout2.addView(textView2,1);

Solution1 Using LinearLayout

TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("&#039;", "");
content = content.replace("&amp;","");
textView.setText(content);
textView.setGravity(Gravity.START);  //Gravity.Left also works
textView.setTextSize(20);

TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.END);   //Gravity.Right also works

LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);

LinearLayout.LayoutParams linearLayoutParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,2);
linearLayout2.setLayoutParams(linearLayoutParam);

LinearLayout.LayoutParams viewParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,1);

linearLayout2.addView(textView, 0, viewParam);
linearLayout2.addView(textView2, 1, viewParam);

In above solution I have assigned weightSum = 2 to LinearLayout and weight=1 to each TextView.

Solution2 Using RelativeLayout

TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("&#039;", "");
content = content.replace("&amp;","");
textView.setText(content);
//textView.setGravity(Gravity.START);  //Gravity.Left also works
textView.setTextSize(20);

TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
//textView2.setGravity(Gravity.END);   //Gravity.Right also works

RelativeLayout relativeLayout2 = new RelativeLayout(MainActivity.this);

RelativeLayout.LayoutParams relativeLayoutParam1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParam1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
relativeLayout2.addView(textView, 0, relativeLayoutParam1);

RelativeLayout.LayoutParams relativeLayoutParam2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParam2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeLayout2.addView(textView2, 1, relativeLayoutParam2);

In this case textView.setGravity(Gravity.RIGHT); is optional.

Update Based on comment
If you want underlined text as in SAMPLE below then check one of the answer here

<u>Sample</u>

But if you want a black line then it is view and you need to add it after adding linearlayout2 or relativelayout2 depending upon the solution in the main view.

Community
  • 1
  • 1
Shadow Droid
  • 1,696
  • 1
  • 12
  • 26