Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set background color for Textview programmatically for another xml file?

hi am new to android how to set text background color for another xml file in programatically am already add xml file using set content view but it have only listview only and i have one another xml file using module for executing file,i want to text background in modelo xml file

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);
    EXECUTAR = (Button) findViewById(R.id.btn_buscar);
    ValorBusca = (EditText) findViewById(R.id.txt_buscar);
    Lista = (ListView) findViewById(R.id.listView1);
    ValorBusca.setText("");
    EXECUTAR.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            QuerySQL(null);
        }
    });
}
    public  void QuerySQL(String COMANDOSQL) {
    ResultSet rs;
    try {
        Statement statement = ma.connect.createStatement();
        rs = statement.executeQuery("SELECT * FROM "+ValorBusca.getText().toString()+"");
        List<Map<String, String>> data = null;
        data = new ArrayList<Map<String, String>>();
        while(rs.next()) {
            Map<String, String> datanum =new HashMap<String, String>();
            datanum.put("A",rs.getString(1));
            datanum.put("B",rs.getString(2));
            datanum.put("c",rs.getString(3));
            data.add(datanum);  
        }

        String[] from = {"A","B","c"};
        int[] views = {R.id.txttitulo,R.id.txtconteudo,R.id.textview3};
        AD = new SimpleAdapter(this, data, R.layout.modelo, from, views);
        Lista.setAdapter(AD);

    } catch (Exception e) {
        Log.e("ERRO",e.getMessage());
        Toast.makeText(getBaseContext(),"Enter Table Name",Toast.LENGTH_SHORT).show();
    }
}

and i want text background in this file modelo

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2"
 >

 <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="2dip"
    android:scrollbarAlwaysDrawHorizontalTrack="true"

    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/txttitulo" 
        android:text="Name"
        android:layout_height="wrap_content"
        android:layout_width="2dip"
        android:gravity="left"
        android:background="@drawable/cell_shape"
        android:padding="5dip"

        android:layout_marginLeft="3dp"
        android:textColor="#0174DF"/>
    <TextView
        android:id="@+id/txtconteudo" 
        android:text="Number"
        android:layout_height="wrap_content"
        android:layout_width="2dip"
        android:gravity="left"
        android:textColor="#0174DF"
        android:background="@drawable/cell_shape"
        android:padding="5dip" 
        />
    <TextView
        android:id="@+id/textview3" 
        android:text="Number"
        android:layout_height="wrap_content"
        android:layout_width="2dip"
        android:gravity="right"
        android:layout_weight="1" 
        android:textColor="#0174DF"
        android:background="@drawable/cell_shape"
        android:padding="5dip" 
        android:layout_marginRight="3dp"/>

</TableRow> 

like image 778
ibu Avatar asked May 30 '13 11:05

ibu


People also ask

How do I change the color of TextView?

There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File.

How do I add color to text in XML?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .

How do I change the background color of my text?

Android TextView Background Color To change the background color of TextView widget, set the background attribute with required Color value. You can specify color in rgb , argb , rrggbb , or aarrggbb formats. The background color is applied along the width and height of the TextView.


2 Answers

Check this one,

TextView textView = (TextView) findViewById(R.id.text1);
textView.setText("Welcome");
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.RED);
like image 72
Sathish Sathish Avatar answered Sep 20 '22 16:09

Sathish Sathish


This may be Help You.

textview.setBackgroundResource(R.color.white);

Note :instead of textview you need to write your TextView object name.

like image 41
Butani Vijay Avatar answered Sep 19 '22 16:09

Butani Vijay