Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android: Create an input field with width and background at runtime

27.09.2018 (👁208)


 

How can you create an input field at runtime, in which the background is color-coded and the width is set.

 

Solution:

Over for the background creates a drawable style.

This drawable is then assigned with EditView.setBackground (drawablename).

 

For the width you can then use EditView.setWidth (123) to set the default width

 

 

 

Java code, runtime

Complete code for creating an edit field EditView at runtime

public void set_Input(View view) {
   
//------------< set_Input() >-----------
   
TableRow row =(TableRow) view;
   
row_Input=row;
    String sRow_Key =view.getTag().toString();
   
sDateShort_Input=  sRow_Key.substring(4);
   
   
//< get_Edit_Field >
   
EditText tbxDate_Input=row.findViewWithTag("edit_" + sDateShort_Input);
   
if(tbxDate_Input ==null)
    {
       
//-< add_new_Edit >-
       
tbxDate_Input=new EditText(this);
        tbxDate_Input.setTextColor(Color.
RED);
        Drawable edStyle=getDrawable(R.drawable.
input_date_style);
        tbxDate_Input.setBackground(edStyle);
        tbxDate_Input.setWidth(
180);
        row.addView(tbxDate_Input);
       
//-</ add_new_Edit >-
   
}
    tbxDate_Input.requestFocus();
   
//</ get_Edit_Field >

    //------------</ set_Input() >-----------
}

 

 

drawable

In the Drawable File, a presentation style is outsourced.

All you have to do is put the file in the path res / drawable, which then uses it

getDrawable (R.drawable.filename) can be found

 

Res / drawable /

input_date_style.xml

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <
item>
        <
shape android:shape="rectangle">
            <
solid android:color="#EEFFFF00"/>
            <
stroke
               
android:color="#a0a32f"
               
android:width="1dp"
               
/>
            <
corners android:radius="1dp"/>
        </
shape>
    </
item>
</
selector>