I need to exchange data between activity and it's layout xml in Android. But I do not find a way to do this in Android. For example, views and controller in mvc pattern always has a way to exchange data between. So I am wondering is there any way to exchange data between them to should I refresh my mind and realize there is no such way?
-
Its unclear what you're asking - what are you trying to exchange exactly? – Katana24 May 09 '14 at 13:07
-
like a id or an image path – ssj May 09 '14 at 13:07
-
Try to google something like a listview or gridview if that is what you mean. – Lunchbox May 09 '14 at 13:08
-
you mean the id of an image that is selected? – Katana24 May 09 '14 at 13:10
-
@Katana24 like what we did in html and php, the brower can get or post data to the server, and if use smarty, smarty can get data from server. – ssj May 09 '14 at 13:13
-
your talking about webservices then - like REST and SOAP - so you want to know how to do that in android java? – Katana24 May 09 '14 at 13:20
4 Answers
use below code to get value from layout in your activity
String value;
EditText editText= (EditText) findViewById(R.id.editText);
value=editText.getText();

- 273
- 2
- 5
- 23
code in xml example:
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
in java class (like onCreate()):
ImageView image = (ImageView) findViewById(R.id.image_view);
Then you can do what you want with image

- 1,538
- 2
- 16
- 42
-
-
do you want to change a TextView value from your activity? or something like it? – Lunchbox May 09 '14 at 13:20
-
-
@Katana24 Haha, yes, yes it is, epic character, better than Sasuke for me (not related to the question though) – Lunchbox May 09 '14 at 13:22
I believe you're unsure exactly what you're asking. If you want to exchange information, such as ID or text entered into a textfield then any good android tutorial should be-able to demonstrate this. Considering your last comment I think you're talking about GET
and POST
based technologies which can be done usingREST
andSOAP
, or both if you want.
This questions answer has a good implementation and definition of what both of these webservices are.
P.S. If this is what you're looking for then upvote that answer.
As some additional info, the "view" (XML Layout file) gets set by your activity initially on your activity's onCreate method. Right after you call it's parent method (super.onCreate()).
To maintain scope throughout the activity I tend to declare all the layout widgets that I need to the activity to interact with outside of any methods and within the class.
TextView textWelcomeMessage;
public void MyActivity(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// before calling setContentView() we have the option to change
// window features ex requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_my_activity);
// Now to set the textview
textWelcomeMessage = (TextView) findViewById(R.id.textWelcomeMessage);
// Set some data
textWelcomeMessage.setText("Hello, welcome to my activity");
}
It's not exactly like traditional php style mvc since static typing changes thing up a bit and we have to worry about scope. But the core principles can still apply as far as data abstraction and separation go. Hope this helps =)

- 351
- 2
- 12