Hope some Android experts could help on this
Is it possible to load android:text="Here"
from a XML file or TXT file on a server, so they are fetched over the internet. I need to change some b开发者_如何学运维utton text from a XML layout remotely.
Hope someone can help and show me how?
thanks inadvance Lucy
you can change, for example the text of the button in your Activity on fly. The layout xml:
<?xml version="1.0" encoding="utf-8"?>
<Button android:text="Push me" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>
In the Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btn = (Button)findViewById(R.id.Button01);
btn.setText("Do NOT Push me");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
btn.setText("I sad....");
}
});
}
As I know, we cannot load a string resource (or any other resource) from a remote server as we load a local resource (android:text="@string/stringId"
). However, it can be achieved through a web service. Let me explain, first you have to develop a web service which will provide the required resource(s) for your app. Deploy this web service on a web server. Then call that web service from your app, parse the XML response and then change the string (or any other) resource, you want as usual (usually using the method button.setText("New Text");
for string). And I think, this is costly to change a single string. If you want to change the string frequently, you can give multiple XML string entries, and/or develop a random string generator class which will pick a string randomly from a given set of strings resources. Go ahead and let me know about the results.
You are welcome for further queries.
精彩评论