Forum Xamarin Xamarin.Android

how to a width and height in pragrammatically way..

JunCainJunCain Member ✭✭✭

Take a look of this picture, all I wanted is to have an equal height and width to each of the elements inside my tableRow..

productTextView.xml

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_weight="1"
  android:layout_marginLeft="2dp"
  android:textSize="16sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

ExistingCustomer.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:background="@android:color/black"
    android:textColor="#b4ebf7"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="none"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <Spinner
                android:id="@+id/spinner3"
                android:background="@drawable/EditTxtStyle"
                android:singleLine="true"
                style="@style/edit_margin"
                android:padding="8dp"
                android:popupBackground="#00826b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <Spinner
                android:id="@+id/spinner"
                android:background="@drawable/EditTxtStyle"
                android:singleLine="true"
                style="@style/edit_margin"
                android:popupBackground="#00826b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <Spinner
                android:id="@+id/spinner1"
                android:background="@drawable/EditTxtStyle"
                android:singleLine="true"
                style="@style/edit_margin"
                android:popupBackground="#00826b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <TableLayout
                android:id="@+id/tbleLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*"
                android:isScrollContainer="true">
                <TableRow
                    android:id="@+id/tr"
                    style="@style/tableRow">
                    <TextView
                        android:text="Product"
                        style="@style/edit_margin"
                        android:background="@drawable/borderColor"/>
                    <TextView
                        android:text="Product Quantity"
                        style="@style/edit_margin"
                        android:background="@drawable/borderColor"/>
                    <TextView
                        android:text="Product Unit"
                        style="@style/edit_margin"
                        android:background="@drawable/borderColor"/>
                </TableRow>
                <TableRow
                    android:id="@+id/tr"
                    style="@style/tableRow">
                    <Spinner
                        android:id="@+id/spinner2"/>
                    <EditText
                        android:id="@+id/qty"
                        style="@style/edit_margin"
                        android:background="@drawable/borderColor"
                        android:hint="Product Quantity"/>
                    <EditText
                        android:id="@+id/unit"
                        style="@style/edit_margin"
                        android:background="@drawable/borderColor"
                        android:hint="Product Unit"/>
                </TableRow>
            </TableLayout>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Generate"
            android:id="@+id/btn"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

tableRowBackground.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="tableRow">
    <item name="android:layout_width">0dp</item>
    <item name="android:background">#000000</item>
    <item name="android:layout_height">20dp</item>
  </style>
</resources>

EditDesign.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item>
    <layer-list>
      <item>
        <shape android:shape="rectangle">

          <gradient android:startColor="#8dcfe8" android:endColor="#90edbb" />
          <stroke android:width="1px" android:color="#27e1e8" />
          <corners android:radius="2dp" />
        </shape>
      </item>

    </layer-list>
  </item>
</selector>

EditTxtStyle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item>
    <layer-list>
      <item>
        <shape android:shape="rectangle">

          <gradient android:startColor="#a2e8e8" android:endColor="#cce8e8" android:angle="270" />
          <stroke android:width="1px" android:color="#a8f7be" />
          <corners android:radius="2dp" />
        </shape>
      </item>

      <item>

        <bitmap android:gravity="center|right" android:src="@drawable/dropdown_green1"/>

      </item>

    </layer-list>
  </item>
</selector>

design.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="edit_margin">
    <item name="android:layout_margin">0dp</item>
    <item name="android:padding">5dp</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">#0b7eea</item>
    <item name="android:layout_weight">1</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:textColorHint">#0b7eea</item>
  </style>
</resources>

spinnerPadding.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="edit_spinner">
    <item name="android:layout_marginTop">-2dp</item>
    <item name="android:layout_marginBottom">0dp</item>
    <item name="android:layout_marginLeft">0dp</item>
    <item name="android:layout_marginRight">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_weight">1</item>
    <item name="android:popupBackground">#00826b</item>
    <item name="android:padding">0dp</item>
  </style>
</resources>

ToExistingCustomer.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Java.Lang;
using Java.Net;
using System.IO;
using Console = System.Console;
using Exception = System.Exception;
using Object = Java.Lang.Object;
using MhylesApp.Synchronizer;
using Java.IO;
using System.Collections.Generic;
using Android.Content.PM;
using System;
using Android.Views;

namespace MhylesApp
{
    [Activity(Label = "ToExistingCustomer", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Landscape)]
    public class ToExistingCustomer : Android.Support.V7.App.AppCompatActivity
    {
        //IList<string> agentData = new List<string>();
        IList<string> agentList = new List<string>();
        IList<string> custList = new List<string>();
        IList<string> prodList = new List<string>();
        TableLayout tbleLayout;
        TableRow tr, _tr;
        private static Spinner spinner, spinner1, spinner2, spinner3, _spinner;
        private EditText qty, unit, _td1, _td2;
        //private TextView _spinner2;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.ExistingCustomer);

            qty = FindViewById<EditText>(Resource.Id.qty);
            unit = FindViewById<EditText>(Resource.Id.unit);
            tbleLayout = FindViewById<TableLayout>(Resource.Id.tbleLayout);
            tr = FindViewById<TableRow>(Resource.Id.tr);
            //tbleLayout.SetColumnStretchable(0, true);
            //tbleLayout.SetColumnStretchable(1, true);

            //qty.SetBackgroundResource(Resource.Drawable.EditDesign);
            //unit.SetBackgroundResource(Resource.Drawable.EditDesign);




            agentList = Intent.GetStringArrayListExtra("agentList");
            agentList.Add("Select Agent Name");
            spinner = FindViewById<Spinner>(Resource.Id.spinner);

            custList = Intent.GetStringArrayListExtra("custList");
            custList.Add("Select Customer Name");
            spinner1 = FindViewById<Spinner>(Resource.Id.spinner1);

            prodList = Intent.GetStringArrayListExtra("prodList");
            //prodList.Add("Select Product Name");
            spinner2 = FindViewById<Spinner>(Resource.Id.spinner2);


            //spinner2.SetPromptId(Resource.String.product);
            //spinner2.SetBackgroundResource(Resource.Drawable.EditDesign);
            spinner3 = FindViewById<Spinner>(Resource.Id.spinner3);
            var btn = FindViewById<Button>(Resource.Id.btn);
            var adapter3 = ArrayAdapter.CreateFromResource(this, Resource.Array.Recipient, Android.Resource.Layout.SimpleSpinnerItem);
            adapter3.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinner3.Adapter = adapter3;
            spinner3.SetBackgroundResource(Resource.Drawable.EditTxtStyle);

            ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, agentList);
            spinner.Adapter = adapter;
            ArrayAdapter<string> adapter1 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, custList);
            spinner1.Adapter = adapter1;

            ArrayAdapter<string> adapter2 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line, prodList);


            adapter2.SetDropDownViewResource(Resource.Layout.productTextView);
            spinner2.SetBackgroundResource(Resource.Drawable.borderColor);
            prodList.Add("Select Product Name");
            spinner2.Adapter = adapter2;
            spinner2 = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));


            btn.Click += (sender, e) =>
            {
                _tr = new TableRow(this);
                _spinner = new Spinner(this);

                _td1 = new EditText(this);
                _td2 = new EditText(this);


                //_td1.LayoutParameters = new LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);

                _tr = new TableRow(new ContextThemeWrapper(this, Resource.Style.tableRow));




                //_tr.SetPadding(2, 2, 2, 2);
                //_tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams 0dp, ))


                //_td1.SetBackgroundResource(Resource.Drawable.EditDesign);
                //_td2.SetBackgroundResource(Resource.Drawable.EditDesign);
                //TextInputLayout textInputLayout = new TextInputLayout(this);
                //TextInputLayout textInputLayout1 = new TextInputLayout(this);


                _td2 = new EditText(new ContextThemeWrapper(this, Resource.Style.edit_margin));
                _td2.SetBackgroundResource(Resource.Drawable.borderColor);
                _td2.SetHint(Resource.String.unit);

                //textInputLayout.AddView(_td1);
                //textInputLayout11.AddView(_td2);
                //_td1.SetBackgroundResource(Resource.Drawable.EditDesign);
                //_td2.SetBackgroundResource(Resource.Drawable.EditDesign);
                //_spinner.SetBackgroundResource(Resource.Drawable.EditTxtStyle);


                //_tr.AddView(textInputLayout);
                _spinner = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
                _spinner.SetBackgroundResource(Resource.Drawable.borderColor);
                ArrayAdapter<string> _adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line, prodList);
                _adapter.SetDropDownViewResource(Resource.Layout.productTextView);
                _spinner.Adapter = _adapter;
                _tr.AddView(_spinner);




                _tr.AddView(_td1);
                _td1 = new EditText(new ContextThemeWrapper(this, Resource.Style.edit_margin));
                _td1.SetBackgroundResource(Resource.Drawable.borderColor);
                _td1.SetHint(Resource.String.qty);



                _tr.AddView(_td2);



                tbleLayout.AddView(_tr);


            };


        }
    }

}

Answers

  • JunCainJunCain Member ✭✭✭
    edited March 2019

    case solved

    Solution

    btn.Click += (sender, e) =>
                {
                    _tr = new TableRow(this);
                    _spinner = new Spinner(this);
    
                    _td1 = new EditText(this);
                    _td2 = new EditText(this);
    
                    //_td1.LayoutParameters = new LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
    
                    _tr = new TableRow(new ContextThemeWrapper(this, Resource.Style.tableRow));
    
    
    
    
                    //_tr.SetPadding(2, 2, 2, 2);
                    //_tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams 0dp, ))
    
    
                    //_td1.SetBackgroundResource(Resource.Drawable.EditDesign);
                    //_td2.SetBackgroundResource(Resource.Drawable.EditDesign);
                    //TextInputLayout textInputLayout = new TextInputLayout(this);
                    //TextInputLayout textInputLayout1 = new TextInputLayout(this);                
                   _td1.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                    _td1.Visibility = ViewStates.Visible;
                    _td1.SetBackgroundResource(Resource.Drawable.borderColor);
                    _td1.SetHint(Resource.String.qty);
    
                  _td2.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                    _td2.Visibility = ViewStates.Visible;
                    //_td2 = new EditText(new ContextThemeWrapper(this, Resource.Style.edit_margin));
                    _td2.SetBackgroundResource(Resource.Drawable.borderColor);
                    _td2.SetHint(Resource.String.unit);
    
                    //textInputLayout.AddView(_td1);
                    //textInputLayout11.AddView(_td2);
                    //_td1.SetBackgroundResource(Resource.Drawable.EditDesign);
                    //_td2.SetBackgroundResource(Resource.Drawable.EditDesign);
                    //_spinner.SetBackgroundResource(Resource.Drawable.EditTxtStyle);
    
    
                    //_tr.AddView(textInputLayout);
                    _spinner = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
                    _spinner.SetBackgroundResource(Resource.Drawable.borderColor);
                    ArrayAdapter<string> _adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line, prodList);
                    _adapter.SetDropDownViewResource(Resource.Layout.productTextView);
                    _spinner.Adapter = _adapter;
                    _tr.AddView(_spinner);
    
    
    
    
    
    
                    //_td1.LayoutParameters = _params;
                    //ViewGroup.LayoutParams _params = _td1.getLayoutParams();
    
    
                    //_td1 = new EditText(new ContextThemeWrapper(this, Resource.Style.edit_margin));
    
                    _tr.AddView(_td1);
    
    
    
    
                    _tr.AddView(_td2);
    
    
    
                    tbleLayout.AddView(_tr);
    
    
                };
    
  • jezhjezh Member, Xamarin Team Xamurai

    Congrats, you solved this problem so efficient.

Sign In or Register to comment.