0

I am trying to print dynamic horizontal lines in Android. I want to display the lines with some specific margin-left gap but i think all the lines are getting displayed in one place so , i am displaying all the lines in one place.

My Code :

MainActivity.java

package com.example.testlinear;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.app.Activity;
import android.graphics.Color;


public class MainActivity extends Activity 
{

LinearLayout lnr;

@Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    lnr = (LinearLayout)findViewById(R.id.lnr);

    int x = 5;

    lnr.setOrientation(LinearLayout.HORIZONTAL);

    View[] vv = new View[x];


    for(int i=0;i<x;i++)
    {

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1,50);

        vv[i] = new View(MainActivity.this);

        vv[i].setBackgroundColor(Color.BLACK);

        vv[i].setLayoutParams(params);

        vv[i].setPadding(50+i*10, 40, 0, 0);

        lnr.addView(vv[i]);

    }

}

 }

activity_main.xml

<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


 <LinearLayout
     android:id="@+id/lnr"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="horizontal">


  </LinearLayout>


   </RelativeLayout>

Output Image:

enter image description here

These are all codes and output image , please let me know , how can i print 5 lines with some specific gaps.

Please suggest me some good solution.

user3668350
  • 73
  • 1
  • 9
  • You want something like this ||||| ? I suppose you should use RelativeLayout.layoutParams. e.g. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); The second parameter of addRule() can be an id of another view. – Kumiho May 25 '14 at 10:27
  • @Kumiho Answers should be answers not comments. Especially ones with multi line code snippits. – indivisible May 25 '14 at 10:30
  • @indivisible Sorry, you are right. I'm not sure if it' correct, thats why I put it in a comment. – Kumiho May 25 '14 at 10:33
  • @Kumiho you didn't understand my question properly , there are 5 lines with width 1dp and height 50dp , how can i print them dynamically with some gaps to each other... – user3668350 May 25 '14 at 10:38
  • possible duplicate of [Android - Dynamically Add Views into View](http://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) – indivisible May 25 '14 at 10:42
  • @user3668350 Yes, now I see I misunderstood your Problem. Sorry. Have you tried to put some view into the xml file to see if thelayout works? – Kumiho May 25 '14 at 10:58
  • Yes i also thought so you have misunderstood my question in some way ... but nothing matters ... i am trying on my way but getting no new result ... can you please suggest me what could be the possible solution... – user3668350 May 25 '14 at 12:12

0 Answers0