Reputation: 303
So i have an activity that imports a list of names from a .txt that is on a webserver. But how do i set the background color? On the page that shows the names in the app? Because it doesnt use the layout i set? Which is roster.xml Do i have to do something to the .txt file?
Roster.class code:
package com.frede.iii;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.widget.ScrollView;
import android.widget.TextView;
public class IIIRoster extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.roster);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Scrollview
ScrollView sv = new ScrollView(this);
/* We will show the data we read in a TextView. */
TextView tv = new TextView(this);
/* Will be filled and displayed later. */
String myString = null;
try {
/* Define the URL we want to load data from. */
//http://androidtest.host.org/roster.txt
URL myURL = new URL(
"http://androidtest.host.org/roster.txt");
/* Open a connection to that URL. */
URLConnection ucon = myURL.openConnection();
/* Define InputStreams to read
* from the URLConnection. */
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/* Read bytes to the Buffer until
* there is nothing more to read(-1). */
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
myString = new String(baf.toByteArray());
} catch (Exception e) {
/* On any Error we want to display it. */
myString = e.getMessage();
}
/* Show the String on the GUI. */
tv.setText(myString);
// Adds textview to scrollview
sv.addView(tv);
// Sets contentview to scrollview
this.setContentView(sv);
}
}
Upvotes: 0
Views: 408
Reputation: 8546
Hello you can simple use tv.setBackgroundColor(COLOR.XYX)
or sv.setBackgroundColor(COLOR.XYX)
Also at the same time a textColor can be applied to text as tv.setTextColor(COLOR.XYX)
For defining you own colors to be used at run time, use the following:
1.In the string.xml file use the following tag
<color name="mycolor">#F5DC49</color>
Now in your code.
tv.setTextColor(getResources().getColor(R.color.mycolor))
2.Also we can set RGB values as:
tv.setTextColor(Color.rgb(170, 00, 00));
Upvotes: 2