How to set layout parameters of a EditText programmatically?

Put your problem here if it does not fit any of the other categories.

How to set layout parameters of a EditText programmatically?

Postby raquibulbari » Wed Jan 23, 2008 1:19 pm

Hello all,
How are you? I am in a problematic situation, may be it's easy. I want to set layout parameters of an EditText programmatically. The edittext is in a table layout. i want to set the weight of the edittext. But i dont know how to set that programmatically. I know how to set that in xml, but dont know how to set that programmatically. Please some body help


Regards
Shimul
Shimugool
User avatar
raquibulbari
Developer
Developer
 
Posts: 25
Joined: Sun Dec 16, 2007 6:31 am
Location: dhaka,bangladesh

Top

Postby lordhong » Mon Jun 16, 2008 4:48 pm

you can do something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.5f)
  3.  
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


The last 1.5f is the weight value
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby Danuubz » Tue Jun 17, 2008 1:36 pm

Hello,

here's a small example:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.android.test;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.view.*;
  11.  
  12. import android.graphics.Color;
  13.  
  14. import android.widget.*;
  15.  
  16.  
  17.  
  18. public class Test extends Activity
  19.  
  20. {
  21.  
  22.         TableLayout view;
  23.  
  24.         ViewGroup.LayoutParams viewParams;
  25.  
  26.        
  27.  
  28.         TableRow tRowA;
  29.  
  30.         TableLayout.LayoutParams tRowAParams;
  31.  
  32.        
  33.  
  34.         TableRow tRowB;
  35.  
  36.         TableLayout.LayoutParams tRowBParams;
  37.  
  38.        
  39.  
  40.         TextView label;
  41.  
  42.         TableRow.LayoutParams labelParams;
  43.  
  44.        
  45.  
  46.         EditText edTextA;
  47.  
  48.         TableRow.LayoutParams edTextAParams;
  49.  
  50.        
  51.  
  52.         EditText edTextB;
  53.  
  54.         TableRow.LayoutParams edTextBParams;
  55.  
  56.    
  57.  
  58.     @Override
  59.  
  60.     public void onCreate(Bundle icicle)
  61.  
  62.     {
  63.  
  64.         super.onCreate(icicle);
  65.  
  66.         this.getWindow().setLayout(320,440);
  67.  
  68.        
  69.  
  70.         // TABLE
  71.  
  72.         view = new TableLayout(this);
  73.  
  74.         view.setBackgroundColor(Color.GRAY);
  75.  
  76.         viewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);        
  77.  
  78.         view.setLayoutParams(viewParams);
  79.  
  80.         //
  81.  
  82.        
  83.  
  84.         // FIRST ROW
  85.  
  86.         tRowA = new TableRow(this);
  87.  
  88.         tRowAParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
  89.  
  90.         tRowA.setLayoutParams(tRowAParams);
  91.  
  92.        
  93.  
  94.         label = new TextView(this);
  95.  
  96.         label.setBackgroundColor(Color.GRAY);
  97.  
  98.         labelParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT);
  99.  
  100.         tRowA.addView(label);
  101.  
  102.         //
  103.  
  104.        
  105.  
  106.         // SECOND ROW
  107.  
  108.         tRowB = new TableRow(this);
  109.  
  110.         tRowBParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
  111.  
  112.         tRowB.setLayoutParams(tRowBParams);
  113.  
  114.        
  115.  
  116.         edTextA = new EditText(this);
  117.  
  118.         edTextA.setHorizontalFadingEdgeEnabled(true);
  119.  
  120.         edTextAParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.FILL_PARENT,1.5f);
  121.  
  122.         edTextA.setLayoutParams(edTextAParams);
  123.  
  124.         tRowB.addView(edTextA);
  125.  
  126.        
  127.  
  128.         edTextB = new EditText(this);
  129.  
  130.         edTextB.setHorizontalFadingEdgeEnabled(true);
  131.  
  132.         edTextBParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.FILL_PARENT,1.0f);
  133.  
  134.         edTextB.setLayoutParams(edTextBParams);
  135.  
  136.         tRowB.addView(edTextB);
  137.  
  138.         //
  139.  
  140.        
  141.  
  142.         view.addView(tRowA);
  143.  
  144.         view.addView(tRowB);
  145.  
  146.         setContentView(view);        
  147.  
  148.     }
  149.  
  150. }
  151.  
  152.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], MSNbot Media and 25 guests