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 drawable: How to find the drawable at runtime and adopt the style

19.09.2018 (👁203)


 

Task:

Under an Android app, an outsourced style is to be transferred to newly created presentation elements at runtime.

The style is stored in a file under res / drawable / xml.

 

Solution:

You can with getDrawable (..) determine and adopt the style of presentation

         

 

In Android Java at runtime:

Drawable background_XML= getDrawable(R.drawable.table_layout);
lbl1.setBackground(background_XML);

 

 

This will determine the style stored in an xml file and apply it to a display element such as a table, TableRow, or cell TextView

Here is the display style stored in the file table_layout.xml under

/res/drawable/table_layout.xml.

The display style is found using getDrawable

Drawable background_XML= getDrawable(R.drawable.table_layout);

 

 

Xml code of the table_layout file

Table_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:shape= "rectangle"    >
    <
stroke android:width="1px"  android:color="#555555"/>
</
shape>

 

 

Example how the TAbelle is dynamically filled at runtime with further rows of tables and cells and in which the display is taken over.

public void create_Table(){
   
//-------< create_Table() >--------
   
TableLayout tbl=findViewById(R.id.table_main);

   
//--< Row >--
   
TableRow row=new TableRow(this);
    row.setPadding(
2,2,2,2);
   
//< cell1 >
   
TextView lbl1=new TextView(this);
    lbl1.setText(
"Cell1");
   
//lbl1.setTextColor(Color.RED);
    //lbl1.setBackgroundColor(Color.YELLOW);
   
Drawable background_XML= getDrawable(R.drawable.table_layout);
    lbl1.setBackground(background_XML);

    row.addView(lbl1);
   
//</ cell1 >

    //< cell2 >
   
TextView lbl2=new TextView(this);
    lbl2.setText(
"Cell2");

    row.addView(lbl2);
   
//</ cell1 >
    //--</ Row >--

    //< add to table >
   
tbl.addView(row);
   
//</ add to table >
    //-------</ create_Table() >--------
}

 

 

 

After the start, the two lower cells are inserted and the display is taken over

 

Subject:

Android Drawable Style

Android Studio, Android Java at runtime