Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Add view to linearlayout in relativeLayout on button click
    primarykey
    data
    text
    <p>help me solve my problem. There is a main layout RelativeLayout it is Linearlayout where I want to programmatically add view. Here is my code.</p> <pre><code>public class GenBarCodeActivity extends Activity { BarCodeView Barview = null; LinearLayout.LayoutParams layoutParams; LinearLayout ll; RelativeLayout rl; Button getBarCode; private static final int ID = 34646456; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_barcode); getBarCode = (Button)findViewById(R.id.btnGetBarCode); getBarCode.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ll = (LinearLayout)findViewById(R.id.linearCodeView); EANTEXT = edBarText.getText().toString(); ImageView old = (ImageView) ll.findViewById(ID); if (old != null) { ((LinearLayout) old.getParent()).removeViewInLayout(old); } layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); ll.addView(Barview, layoutParams); rl.invalidate(); } }); fillLinear(); setBarCode(); } private void fillLinear(){ rl = (RelativeLayout)findViewById(R.id.layout_barcode); ll = (LinearLayout)findViewById(R.id.linearCodeView); layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); ImageView imBar = new ImageView(this); Bitmap imbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.barcode); imBar.setLayoutParams(layoutParams); imBar.setImageBitmap(imbitmap); imBar.setId(ID); ll.setBackgroundColor(Color.TRANSPARENT); ll.addView(imBar); } private void setBarCode(){ Barview = new BarCodeView(GenBarCodeActivity.this); Barview.setBarCodeNum(0); } } </code></pre> <p>This is my View </p> <pre><code>public class BarCodeView extends View { private static int numcode = 0; private static String codedata = null; public BarCodeView(Context context) { super(context); } public void setBarCodeText(String text){ this.codedata = text; } public String getBarCodeText(){ return this.codedata; } public void setBarCodeNum(int num){ this.numcode = num; } public int getBarCodeNum(){ return this.numcode; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); switch (numcode) { // ean 13 case 0: try{ showEAN13(canvas); } catch (Exception e) { e.printStackTrace(); } break; } } private static void showEAN13(Canvas canvas) throws Exception { EAN13 barcode = new EAN13(); /* EAN 13 Valid data char set: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits) EAN 13 Valid data length: 12 digits only, excluding the last checksum digit */ barcode.setData(codedata); // for EAN13 with supplement data (2 or 5 digits) /* barcode.setSupData("12"); // supplement bar height vs bar height ratio barcode.setSupHeight(0.8f); // space between barcode and supplement barcode (in pixel) barcode.setSupSpace(15); */ // Unit of Measure, pixel, cm, or inch barcode.setUom(IBarcode.UOM_PIXEL); // barcode bar module width (X) in pixel barcode.setX(2f); // barcode bar module height (Y) in pixel barcode.setY(90f); // barcode image margins barcode.setLeftMargin(10f); barcode.setRightMargin(10f); barcode.setTopMargin(10f); barcode.setBottomMargin(10f); // barcode image resolution in dpi barcode.setResolution(72); // disply barcode encoding data below the barcode barcode.setShowText(true); // barcode encoding data font style barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 10)); // space between barcode and barcode encoding data barcode.setTextMargin(6); barcode.setTextColor(AndroidColor.black); // barcode bar color and background color in Android device barcode.setForeColor(AndroidColor.black); barcode.setBackColor(AndroidColor.white); /* specify your barcode drawing area */ RectF bounds = new RectF(120, 120, 0, 0); barcode.drawBarcode(canvas, bounds); } } </code></pre> <p>here is my xml </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_barcode" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f0e0a2" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/closeBarCode" android:layout_width="30dip" android:layout_height="30dip" android:layout_alignParentRight="true" android:layout_alignTop="@+id/txtInfo" android:layout_marginRight="3dp" android:src="@drawable/delete" /&gt; &lt;TextView android:id="@+id/txtInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginLeft="3dp" android:layout_marginTop="3dp" android:text="@string/barcode_title" android:textColor="#000" android:textSize="20dip" /&gt; &lt;LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/closeBarCode" &gt; &lt;TextView android:id="@+id/txtEAN" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/barcode_ean" android:textColor="#000" android:textSize="20dip" /&gt; &lt;Spinner android:id="@+id/spEAN" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/txtInfo" android:layout_below="@+id/linearLayout1" &gt; &lt;TextView android:id="@+id/txtCodeColor" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/barcode_color" android:textColor="#000" android:textSize="20dip" /&gt; &lt;Spinner android:id="@+id/spCodeColor" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/linearLayout2" android:layout_below="@+id/linearLayout2" &gt; &lt;TextView android:id="@+id/txtCodeBackColor" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/barcode_back_color" android:textColor="#000" android:textSize="20dip" /&gt; &lt;Spinner android:id="@+id/spCodeBackColor" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/linearLayout3" &gt; &lt;TextView android:id="@+id/txtCodePosition" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/barcode_position" android:textColor="#000" android:textSize="20dip" /&gt; &lt;Spinner android:id="@+id/spCodePosition" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/linearCodeView" android:layout_width="300dip" android:layout_height="200dip" android:layout_below="@+id/edBarCode" android:layout_marginRight="45dp" android:layout_marginTop="25dip" android:layout_toLeftOf="@+id/closeBarCode" android:orientation="vertical" &gt; &lt;/LinearLayout&gt; &lt;Button android:id="@+id/btnSetBarCode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnGetBarCode" android:layout_alignBottom="@+id/btnGetBarCode" android:layout_toLeftOf="@+id/closeBarCode" android:text="@string/set_code" /&gt; &lt;Button android:id="@+id/btnGetBarCode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="31dp" android:text="@string/get_code" /&gt; &lt;EditText android:id="@+id/edBarCode" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignLeft="@+id/linearCodeView" android:layout_alignRight="@+id/linearCodeView" android:layout_below="@+id/txtCodeText" android:layout_marginTop="23dp" android:ems="10" /&gt; &lt;TextView android:id="@+id/txtCodeText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/edBarCode" android:layout_below="@+id/txtInfo" android:layout_marginRight="86dp" android:text="@string/code_text" android:textColor="#000" android:textSize="20dip" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>I need to Linearlayout "@+id/linearCodeView" insert my BarCodeView. View is not visible after adding LinearLayout.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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