EditText behaving strange on orientation change - android

See the following Activity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.root);
for (int i = 0; i < 8; i++) {
EditText editText = (EditText) LayoutInflater.from(this).inflate(R.layout.edittextlayout, null);
editText.setText("#" + i);
linearLayout.addView(editText);
}
}
}
The layout R.layout.activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
and the layout R.layout.edittext_layout:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
After starting the app it looks like I would expect: every EditText being filled with it's index.
After rotating the device though, the Activity looks like this:
All the EditTexts are there, but they all contain the same text.
What baffles me even more is that this doesn't happen when creating the EditTexts programmatically with
EditText editText = new EditText(this)
instead of inflating it from a layout.
What's happening there?
You can check out my example and try for yourself here.
EDIT: This is not a duplicate of this question as in my case the text in the EditText does not double but get mixed up between different EditTexts.

Try to set some ID for each View.
For example:
view.setId(id);
Or use
onSaveInstanceState() - onRestoreInstanceState()
for saving info.

The problem is your activity recreates on every orientation and saved instance data handled wrong by Android. You can avoid it by setting an id to your dynamically created views or you can
Override onSaveInstanceState method to save your data like text in your edittexts and recover it on onCreate method like so:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check if we're recreating
if (savedInstanceState != null) {
// Restore value of edittext from saved state
editText.setText(savedInstanceState.getString("edittext"));
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("edittext", editText.getText().toString());
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
If you encounter any problem similar after doing this try to Override onRestoreInstanceState method to prevent android from doing your work.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
//do nothing
}

Related

How to setText to a TextView in a different Activity [duplicate]

This question already has answers here:
What's the best way to share data between activities?
(14 answers)
Closed 4 years ago.
So what I am trying to make happen is when you check this checkBoxA, some text will appear in a different TextView in a different Activity that the user will reach later on. The app is kind of like a quiz app so this off the text being displaid like the final score or something.
At first I tried this:
if (checkBoxA.isChecked()){
systemView.setText("Business");
}
But then I got a nullPointerException cause the "systemView" is not in the same activity. The activity is extended to the other activity that the "systemView" is located. So I am not really sure whats wrong anyone know what I should do?
Your issue is that even though you can get the ID of the systemView TextView by using R.id.systemView when you try to find that view using findViewById(R.id.systemView) the view cannot be found as it is not in the current activity's list of ViewGroup. As such null is returned.
Note systemView as the id given to the TextView has been assumed.
That is, You can only successfully use findViewById to find views within the current ViewGroup (e.g. for this.FindyViewById the layout as set by setContentView).
Instead you need to make the value available to the other activity and then retrieve the value in the other activity.
There are various ways that you can make the value available, some options are :-
To pass it to the activity via the Intent that starts the other activity as an intent extra, you could store the value in shared preferences and then retrieve it in the other activity or you could store the value in a database, e.g. SQLite and retrieve it.
Using an IntentExtra is ideal if you are directly starting the other activity with a limited number of values.
using chained Intent Extras is also feasible (that is passing to one activity, then to another and so on).
Shared preferences could suit a situation where there are a limited number of values to be passed and the other activity isn't directly started from the activity.
A database would suit a situation where there is a fair amount of structured data and/or related data (or if you are using a database for other aspects).
An example of using an Intent could be :-
In the Activity that is passing the value
Intent i = new Intent(this, yourOtherActivity.class);
i.putExtra("YOURINTENTEXTRAKEY","Business"); //<<<< 1st parameter is a Key for identification, the 2nd parameter is the value to be passed
startActivity(i);
In the other Activity's onCreate (after you've set the contentView)
TextView mSystemView = this.find(R.id.systemView);
if (this.getIntent().getStringExtra("YOURINTENTEXTRAKEY") != null) {
mSystemView.setText(this.getItent().getStringExtra("YOURINTENTEXTRAKEY"));
} else {
mSystemView.setText("NO VALUE PASSED");
}
You set pass and return multiple IntentExtras see Intent for various options and types of values that can be passed/retrieved.
Simple Working Example
The following is code for a working example. The first activity (MainActivity) has a CheckBox and a Button.
The Button can be clicked or longClicked. If the latter then nothing is passed to the second activity. If the former then depedning upon whether or not the CheckBox is ticked will either pass "Not Checked" or "Business".
The second activity, if passed a value (either "Not Checked" or "Business") will display the passed value, if nothing is passed then it will display "NOTHING PASSED". The button on the second activity will return to the first activity (alternately using the back button will return to the first activity).
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final String INTENTKEY_CHECKBOXA = "checkboxa";
CheckBox checkBoxA;
Button nextActivity;
String valueToPass = "Not Checked";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBoxA = this.findViewById(R.id.checkBoxA);
checkBoxA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBoxA.isChecked()) {
valueToPass = "Business";
} else {
valueToPass = "Not Checked";
}
}
});
nextActivity = this.findViewById(R.id.nextActivity);
//Set onlick listener (pass value via intent)
nextActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
callNextActivity(true);
}
});
// Set onlongclick listener (doesn't pass value via intent)
nextActivity.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
callNextActivity(false);
return true;
}
});
}
private void callNextActivity(boolean passvalue) {
Intent i = new Intent(this,NextActivity.class);
if (passvalue) {
i.putExtra(INTENTKEY_CHECKBOXA, valueToPass);
}
startActivity(i);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="#+id/nextActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT ACTIVITY"/>
<CheckBox
android:id="#+id/checkBoxA"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
NextActivity.java
public class NextActivity extends AppCompatActivity {
Button doneButton;
TextView systemView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
systemView = this.findViewById(R.id.sysstemView);
doneButton = this.findViewById(R.id.done);
if (this.getIntent().getStringExtra(MainActivity.INTENTKEY_CHECKBOXA) != null) {
systemView.setText(this.getIntent().getStringExtra(MainActivity.INTENTKEY_CHECKBOXA));
} else {
systemView.setText("NOTHING PASSED");
}
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
doneWithActivity();
}
});
}
// Return from this activity
private void doneWithActivity() {
this.finish();
}
}
activity_next.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NextActivity">
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"/>
<TextView
android:id="#+id/sysstemView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
you can pass your text in the intents of navigations between the activities .. otherwise if you want to save your text to recover it later, even if you close and restart your application, you can save it in a shared preferences file, to retrieve it from this file when you want to display it later.. but you can not set a text to a textview of an activity that is not in foreground.
There are many ways to do this depending on your exact use case one may be better suited than the others.
You can wrap the data in a bundle and pass it to your other activity through an intent if you are opening a second activity
Or you could store the values (in shared prefs or sqlite) and then retrieve them in the next activity.
You could use RxJava to create a stream via a subject (bevahior subject most likely in this case) and write to the stream in the first activity, then subscribe on the stream in the next to get the values.
If you are using an intent to go to the next activity you could put the value in a string and pass the string as an extra to the intent. Take the value in the next activity and set the text view.
//First Activty
String valueToPass = "";
if (checkBoxA.isChecked()){
valueToPass = "Business";
}
startActivity(new Intent(CurrentActivity.this, NextActivity.class).putExtra("value", valueToPass));
//Second Activity
if(getIntent().getString("value") != null)){
systemView.setText(getIntent().getString("value"));
}
Just use SharedPreferences to save the TextView value and then when go to the Activity that contain the TextView get the saved value and set it to the TextView in the onCreate method

ImageView not changing with setImageResource()?

Here's the code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ImageView button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageView)findViewById(R.id.startButton);
button.setOnClickListener(this);
button.setImageResource(R.drawable.play);
}
public void onClick(View v) {
button.setImageResource(R.drawable.pause);
}
}
Apparently if I put the button.setImageResource(R.drawable.pause);in the onCreate it works and changes images but not in the onClick? I tried debugging it and it ran the onClick code but didn't change the button image. I'm probably making a very stupid mistake but I can't figure it out.
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.larry.app.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
Make sure you use
android:src
to set the initial image in the xml.
you had better set ImageView self OnClickListener
Could you try this code and tell me the result then :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ImageView button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageView)findViewById(R.id.startButton);
button.setOnClickListener(this);
button.setImageResource(R.drawable.play);
}
public void onClick(View v) {
button.setImageResource(R.drawable.pause);
}
}
and in the xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.larry.app.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
In the OnClick there is a switch statement, and when I put the button.setImageResource(R.drawable.pause); before the switch it doesn't always activate even when OnClick activates. When I click one button android.os.SystemClock.sleep is executed which prevents the image from swapping. When the other button is pressed the clock doesn't sleep, so the image can switch successfully.
I stumbled into the same problem Larry faced when using the setImageResource method when changing the source of an imageView. For me this happened when trying to use a switch but for a different scenario. Incase someone would find it helpful, i'll explain how I used a work around for this from the code.
SCENARIO
For me, I had a recyclerView in one activity and based on the selection, it would start another activity using an intent and the widgets in the new activity will be manipulated by the click of the recycler view including an imageView
SOLUTION
Using an If or a Switch inside the new class did not work (Which was the problem). What I did was inside the Custom Adapter for my recycler view, I used an If condition (Again a Switch will not work) as follows,
THE CLASS OF THE ACTIVITY WITH THE RECYCLER VIEW
`#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position)
{
holder.indexTextView.setText(String.valueOf(position + 1));
holder.nameTextView.setText(String.valueOf(name.get(position)));
holder.gradeTextView.setText(String.valueOf(grade.get(position)));
holder.itemView.setOnClickListener(v ->
{
Intent i = new Intent(context, DisplayingProductDetails.class);
if(novaGroup.get(position).equals(1))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_1);
else if(novaGroup.get(position).equals(2))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_2);
else if(novaGroup.get(position).equals(3))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_3);
else if(novaGroup.get(position).equals(4))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_4);
activity.startActivityForResult(i, 1);
});
}`
The Important Part here is passing the Resource ID as a String from the putExtra method.
if(novaGroup.get(position).equals(1))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_1);
else if(novaGroup.get(position).equals(2))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_2);
else if(novaGroup.get(position).equals(3))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_3);
else
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_4);
FROM THE CLASS OF THE NEW ACTIVITY
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaying_product_details);
Bundle extras = getIntent().getExtras();
novaGroupImage = findViewById(R.id.novaGroupImage);
novaGroup = extras.getInt("PRODUCT_NOVA_GROUP");
novaGroupImage.setImageResource(novaGroup);
}
The important part here being using the resource ID inside the onCreate Method,
Bundle extras = getIntent().getExtras();
novaGroupImage = findViewById(R.id.novaGroupImage);

Layout not refreshing after orientation change

I have the following Activity definition:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/inspectionMainLayout"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:singleLine="false"
android:id="#+id/breadCrumb"
android:visibility="gone">
</LinearLayout>
<ExpandableListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/expandableListView" />
</LinearLayout>
Now in my code I do add buttons dynamically in breadCrumb LinearLayout:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inspection);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.inspectionMainLayout);
if (mainLayout != null) {
ExpandableListView list = (ExpandableListView) findViewById(R.id.expandableListView);
list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
LinearLayout breadCrumb = (LinearLayout) findViewById(R.id.breadCrumb);
Button filterButton = new Button(InspectionActivity.this);
filterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onFilterButtonClick((Button) view);
}
});
filterButton.setText(item.getFormattedFilter());
breadCrumb.addView(filterButton);
}
}
}
...
}
This code works well, until I do not change the device orientation and my Activity is recreated. Although all the code is executing correctly, screen seems not being updated. Once I restore the previous orientation, all the items suddenly appear. Any idea why and how to fix it?
Thanks
EDIT:
I do think that I'm running into the same problem as describe in this post:
Android: findViewById gives me wrong pointer?
Any idea on how to solve this?
As requested my onRestoreInstanceState:
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
baseCategories = savedInstanceState.getParcelable(BASE_CATEGORIES_STATE);
currentFilter = savedInstanceState.getParcelable(FILTERS_STATE);
}
and on onSaveInstanceState:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(BASE_CATEGORIES_STATE, baseCategories);
outState.putParcelable(FILTERS_STATE, currentFilter);
}
now both of my classes do implement Parcelable interface.
They are persisted and restored correctly.
Still for some resaon the call to the findViewById get's me pointed to the wrong object (not the one that is recreated).
You add views dynamically (on user click event).
By default, android does not "remember" to keep these dynamic views when re-creating the activity on configuration changes, you have to handle this process yourself.
Some possibilities :
Avoid recreating activity on screen rotation by declaring android:configChanges="keyboardHidden|orientation|screenSize" for your activity in AndroidManifest.xml - This is highly not recommended
"Remember" what views were dynamically added when re-creating activity after rotation (for example using extra flags to detect that new filter button was added and pass it via bundle in onSaveInstanceState, and check in onCreate whether you need to re-create the button), or retain the whole view object as explained here
One extra note : you perhaps want to specify "vertical" orientation for your breadCrumb layout, it is horizontal by default.
I found out why this is happening.
onSave/onRestoreInstanceState I was persisting the currentFilter class which has some custom listeners on it.
As onResume method I was doing the following:
#Override
protected void onResume() {
if (currentFilter == null) {
currentFilter = new FilterItemList();
currentFilter.addListener(new FilterItemListListener() {
#Override
public void filterChanged(FilterChangedEvent e) {
filterCategories(categoryRepository);
}
#Override
public void filterAdded(FilterAddedEvent e) {
FilterItem addedItem = e.getAddedItem();
baseCategories.remove(new BaseCategory("", addedItem.getSectionName()));
}
#Override
public void filterRemoved(FilterRemovedEvent e) {
FilterItem removedItem = e.getRemovedItem();
baseCategories.add(new BaseCategory("", removedItem.getSectionName()));
}
});
}
}
The pointer to the previous instance was persisted. That's why my interface was not behaving correctly.
Now I do re-register listeners even when currentFilter is not null (it is restored) so they can point to the right instance.
Is there any pattern in handling this situations?
Thanks

Is it possible to get a Button view from ListView Fragment instead of ListView Adapter?

I have this:
Where Red square is the activity containing several fragments, almost all of them are lists, all of them with different items but built by my custom ListAdapter class.
Orange square is one of those fragments, actually it's a right drawer. This fragment extends from ListFragment, and inflates a simple xml without Items.
Inside of the fragment's onActivityCreated I instantiate a custom ListAdapter which I fill with the items you see in the screenshot.
I have several xml depending on the type of Item I want, here you can see 3 of them:
Button Title (The green square)
Checkable item (The ones which can have the checked icon at right)
Regular Title (The one that says "Día y hora del viaje")
Button title (Green square) xml is a simple linear layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_row_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/row_button"
android:layout_width="45dp"
android:layout_height="45dp"
android:padding="10dp" />
<TextView
android:id="#+id/row_title"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:minLines="1"
android:padding="10dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textStyle="bold" />
</LinearLayout>
What I want to do is to access the button from the orange square (my custom ListFragment class) to set a click listener. I have made my research and I know I can do it (I have succesfully tried) from the adapter, but that's what I don't want, because in other menus I'm going to have other buttons that behave different but they're going to call same listener because they are built by same adapter.
I have tried after inflating the Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.menu_list, null);
//Button of location settings
((ImageButton)fragmentView.findViewById(R.id.row_button)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Do here stuff I want to
}
});
super.onStart();
return fragmentView;
}
Also from onStart:
#Override
public void onStart() {
//Button of location settings
((ImageButton)getView().findViewById(R.id.row_button)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here my stuff
}
});
super.onStart();
}
and also after the setListAdapter()
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//A new instance of my own ListAdapter
MyListAdapter adapter = new MyListAdapter(getActivity());
//Prepare my items and its propierties within adapter
//...
//Set the Adapter
setListAdapter(adapter);
//And then try to get the Button view
//Button of location settings
((ImageButton)getView().findViewById(R.id.row_button)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//My stuff here
}
});
}
But guess what... I always get same error in xml inflating the fragment. If I remove the lines of setting the click listener, it works, but obviously the button does nothing.
Any help is greatly appreciated.
Here is the answer from the comments :
Your button is located in a row of your ListView, so you can only find it from your list adapter (in getView()). Calling findViewById() from your fragment can't return views contained in your ListView.
One solution is to set a tag to your button when you inflate it, and then do something depending on the tag when the button is clicked.

android image display as per user choice on activity

I am new to android and would like to know as to why thiswont work.If i have a set of images in my res folder and i want to display them based on users choice such that the Mainactivity is like below:-
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.drawable.img1);//here i put the image name
}
}
Suppose i use this code snippet
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(R.id.text1=='1')
setContentView(R.drawable.img1);
else
setContentView(R.drawable.img2);
}
}
This doesnt work.But i would like to know why,how is the android stuff really working.This does seem to me logically right.
You can't set drawable to setContentView, but you can do this instead:
setContentView(R.layout.MyLayout);
ImageView view = (ImageView)findViewById(R.id.myviewid);
view.setImageResource(R.drawable.img1);
In each click add view.setImageResource(R.drawable.img1); to change the imageView resources.
if you see more carefully the documentation, can see the definition for setContentView method:
public void setContentView (int layoutResID)
if you note, the method receive a layoutResId, the key word is layout, remember that android used the logic (view - controller), an activity is the controller and a layout is the view. You can have visual elements in your view, like an image (in android can be contained in an ImageView).
For example, you can define an xml layout to the next way:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/selected_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</ImageView>
this xml only contain an ImageView, in this ImageView with the id selected_image you will show the choice image.
In your activity you need put the next code (imagine the xml layout call image_layout.xml):
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_layout);
//here you need find the ImageView in this layout
((ImageView) findViewById(R.id.selected_image).setImageResource(R.drawable.img1);
}
}

Categories

Resources