Activity not updating properly - android

I'm new to android, so maybe I'm doing something horribly wrong. I want to have a particular Activity that shows details about an instance of a "Creature" class for a game. Name, damage taken, that sort of thing.
I'm having a problem getting the creature data to be properly shown in the GUI objects. Both at initial creation (where it should copy the creature's name into the name field) and when a damage mark is added (where it doesn't update to show the proper image).
Here's my mini-example of what I have:
public class CreatureDetailActivity2 extends Activity
{
Creature creature;
public void addMark(View v)
{
// connected to the button via android:onClick="addMark" in the XML
creature.getTrack().addDamage(DamageType.Normal, 1);
refreshDisplay();
new AlertDialog.Builder(this).setTitle(creature.getName())
.setMessage(creature.getTrack().toString()).show();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creature_detail);
creature = new Creature("Example");
refreshDisplay();
}
public void refreshDisplay()
{
final View creatureDetailView = this.getLayoutInflater().inflate(
R.layout.activity_creature_detail, null);
final EditText nameField = (EditText) (creatureDetailView
.findViewById(R.id.textbox_creature_name));
nameField.setText(creature.getName());
final ImageView damageBox0 = (ImageView) (creatureDetailView.findViewById(R.id.damageBox0));
damageBox0.setImageResource(R.drawable.__n);
// in the full program this does the same for 0 through 9, but this is a sample
// also, in the full program, this is a dynamic lookup for the correct pic
// but again, this is just a sample version.
}
}
Now the problem is that the app will load up and start, but then none of the widgets will update properly. You can click the button, and it'll show the AlertDialog, and the text of the AlertDialog will change, but the textfield in the activity won't be changed, and the ImageView doesn't change at any point from what it starts as to the one it's supposed to change to.
So I'm very stumped. I can post more about the project's setup if I'm leaving out something important, but I'm not even sure what the problem going on is so I'm not sure what else to include in my question.

final View creatureDetailView = this.getLayoutInflater().inflate(
R.layout.activity_creature_detail, null);
Inflates your Activity's layout into basically nothing, just returning the View it inflated. setContentView is what actually inflates your layout into the Activity's View hierarchy.
Once you inflate your layout you don't need to do it again. Just use findViewById without the reference to a dangling unattached View.
Change your refreshDisplay method to this:
public void refreshDisplay()
{
final EditText nameField = (EditText) findViewById(R.id.textbox_creature_name);
nameField.setText(creature.getName());
final ImageView damageBox0 = (ImageView) findViewById(R.id.damageBox0);
damageBox0.setImageResource(R.drawable.__n);
// in the full program this does the same for 0 through 9, but this is a sample
// also, in the full program, this is a dynamic lookup for the correct pic
// but again, this is just a sample version.
}

Nothing changes because You do it completely wrong.
If You wish to update any view element of current activity You do it like this
View v = findViewById(R.id.element);
v.setText("text");
this is just simple example.
You would need to cast a returned element to correct type like to be able to access all available methods.
What You do wrong is trying to inflate a layout again.

Related

android - replace only content in the same activity

I have a MainActivity, which contains ImageView, TextView and 3 clickable Buttons.
After clicking the button, I want to change something in SQlite dtb and according that load different data, but show it again in the same activity.
public void ClickBtn(View v)
{
//insertData(String...
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
So generally - in Main Activity.js I am getting the data from ID, which was clicked before and show that data. The MainActivity should be used infinity times to show different data.
The layout will be always the same - ImageView, TextView and 3 clickable Buttons, just the text will be different.
The question is, how can I only change content inside the same Activity?
I don't think Intent intent = new Intent(MainActivity.this, MainActivity.class); from the current activity can open the same activity...
You really need to study the basics.
When you are working in android, XML layout files are merely blueprints which ultimately are parsed into a reflection-created anonymous view instance, which contains as children each of the members of the XML layout, with the valid XML tag parameters applied to them. Therefore, you aren't dealing with 'Layouts', but with java/kotlin objects, which can be:
Referenced
Mutated
Replaced
So, if you want to change the contents, the first steps is to keep a reference to each object: ImageView, TextView and Buttons, and move the code in charge of filling them to a new method, so you can call it either when loading the activity (onCreate), or when clicking the button. That way the same activity can perform the same action over and over.
Finally, constant recreation of an activity is a TERRIBLE idea. For every object you generate (and an activity IS an object, like everything else), you need X+Y memory, where X is the sum of all the members of the object's class, and Y is the sum of all the operations necessary for instantiation, so by recreating the activity constantly, you waste the device resources, with the added problem of generating a huge backstack of identical activities.
Take a look at a java book, then a kotlin one. It will make your life easier.
This is how I solved it. Just replacing text without refreshing activity. Tested it hundred times also via Memory monitoring and absolutely no impact on device memory.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get from dtb
int room = 1; int a1 = 2; int a2 = 3; int a3 =4;
TextView views = findViewById(R.id.text1);
views.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//get from dtb - img, text where room = a1;
TextView vv = findViewById(R.id.textof);
vv.setText("text from dtb");
}
});
TextView view2 = findViewById(R.id.text2);
view2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//get from dtb - img, text where room = a2;
TextView vv = findViewById(R.id.textof);
vv.setText("another text from dtb");
}
});
}

Weird Issue with Android Activity

I am dynamically adding views to Android and am seeing a really weird result which makes little to no sense. Here is the code that shows the bug:
private static int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
count++;
// Allow 'up' action for actionBar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("TEST");
setContentView(R.layout.test);
LinearLayout testLinear = (LinearLayout)findViewById(R.id.testLinear);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.job_step_text, null, false);
TextView title = (TextView) view.findViewById(R.id.jobStepTextTextView);
final EditText editText = (EditText)view.findViewById(R.id.jobStepEditText);
title.setText("Field 1");
editText.setText("Value 1");
testLinear.addView(view);
if(count == 1) {
View view2 = inflater.inflate(R.layout.job_step_text, null, false);
TextView title2 = (TextView) view2.findViewById(R.id.jobStepTextTextView);
final EditText editText2 = (EditText)view2.findViewById(R.id.jobStepEditText);
title2.setText("Field 2");
editText2.setText("Value 2");
testLinear.addView(view2);
}
}
First time round the Activity is as follows:
Field 1: [ Value 1 ]
Field 2: [ Value 2 ]
Then when the device rotates the following is shown:
Field 1: [ Value 2 ]
Can anyone help? It seems that after the first time round the line:
final EditText editText = (EditText)view.findViewById(R.id.jobStepEditText);
is fetching an old reference and not calling setText()? When the screen rotates the line of code which sets the text field to 'Value 2' isn't even called and yet that's the result in the Field 1 text box.
Can anyone help?
By the way - a way to fix this is to give each EditText a unique ID before adding it to the view... but that makes no sense... Surely the inflated View has no knowledge of any other views (past or present) until testLinear.addView(view); is added?
Thanks in advance.
The onSaveInstanceState in the Activity (your super class) captures information about the attached content view and reloads it for you during onCreate() So when the Activity gets recreated after the device rotates, values are put into views found by ID as soon as you call setContentView(). This can produce surprising results if you are changing your layout on-the-fly.
One approach I have used is to call findViewById to see if the child view is already present, before adding it "by hand".
Another approach would be to inflate the layout first, make your adjustments to it, then call setContentView.
[I don't quite understand Android's reason for doing this -- maybe to allow super-simple applications to skip the onSaveInstanceState/reload from bundle process??)

shifting the onCreate() content into the application class

My app contains 25 edittexts. I am getting this 25 edittexts with the help of adapter class by giving count=25 and fitting in gridView by gridView.setAdapter(new TextAdapter(this)); in the activity class. So, the edittexts are dynamically generated. But the thing is I am unable to set the initial values in the edittexts. This is because the edittext objects are unavailable to set the values.
Suppose if I don't set any initial values in the edittexts and continue with my app. The same problem repeats while setting the values back in the edittexts which are entered in previous mode after changing the orientation. Because change in orientation creates new activity. Even I tried android:configChanges="orientation|keyboardHidden", but no use while I am setting the values back in the **onConfigurationChanged()**. Because I am setting the setContentView(); in the onConfigurationChanged() as I need the respective view, but still the edittext objects are unavailable to set their values.
Is there any solution to set back the values? If not, I am thinking(Might be completely wrong way, but as a newbie please go easy) to move the onCreate() method content to Application class. So the initial part goes to Application class including the creation of edittexts. and getting that edittext objects in the onCreate() method to set the values. Is it possible? Please suggest. Code snippet would be appreciated.
You will need to modify TextAdapter. Store the initial values in a String array, with the position of the String array element aligned to the position of the EditText in your GridView.
Pseudo-code (untested):
public class TextAdapter extends BaseAdapter {
String [] initial_value = {"Initial Value 1", "Initial Value 2", "Initial Value 3", ..., };
public View getView(int pos, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.edit_text_container, null);
}
(EditText) edtTemp = (EditText) view.findViewById(R.id.edit_text_id);
edtTemp.setText(initial_value[pos]);
}
}

Change background image on user's choice

Guys I want the user to change the background image of all the activities in my app on user selection.
I am able to change the background image of the Activity from where am changing the image
but if I try to change the image of other Activity, I get a NullPointerException!
Yes, I have checked that the id of other activity's Layout !
this is the code.
public class setting extends Activity {
TextView tv;
CheckBox cbS, theme1, theme2;
RelativeLayout rel;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
cbS = (CheckBox) findViewById(R.id.cb);
theme1 = (CheckBox) findViewById(R.id.theme1);
theme2 = (CheckBox) findViewById(R.id.theme2);
// cbW=(CheckBox)findViewById(R.id.cbWordPress);
checkBoxListener = new OnClickListener() {
public void onClick(View v) {
if (cbS.isChecked()) {
// anything
}
if (theme2.isChecked()) {
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rel);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.back_image1);
rel.setBackgroundDrawable(drawable);
// findViewById(R.id.rel).setBackgroundResource(R.drawable.back_image1);
}
}
};
cbS.setOnClickListener(checkBoxListener);
theme2.setOnClickListener(checkBoxListener);
// cbW.setOnClickListener(checkBoxListener);
}
}
you cannot access the UI components which are not yet instantiated. Use Intents to pass information across activities (user's choice, or some custom flags or Strings) and use this "extra" information in the launched activity to change the background accordingly.
Read more about intents in the documentation for better understanding and examples.
You cannot do this.. When you refer a layout file using findViewById(), the android system looks for this in your current ContentView only. (i.e the view which you have set using setContentView() for the current activity). So obvioulsy the sytem will not be able find the resource you are specifying and hence you will get the NullPointerExeption only.
You have to maintain the reference to the backgrounds separately and use it in your other Activity when you actually pass on there.

How to get a layout from activity that is linked with onClick attribute in XML?

I'm a beginner in Android and I'm testing my code using Android JUnit test.
So, I have a test activity that extends ActivityInstrumentationTestCase2<Activity>.
Activity has it's own layout in onCreate() method. (Main layout)
In my XML, I have a onClick attribute for a button that calls foo() method.
Back to Activity, in the foo(View v), I set my content view to a different layout.
I want to test that layout.
How do I get the layout though??
I know for the main layout, I can do this.
Activity act = getActivity();
View mainLayout = (View) act.findViewById(bla.bla.bla.R.id.main_layout);
How do I get the layout that I set in foo(View v)??
I've already tried doing,
fooLayout = (View) act.findViewById(bla.bla.bla.R.id.foo_layout);
and
act.setContentView(bla.bla.bla.R.layout.foo_layout);
fooLayout = (View) act.findViewById(bla.bla.bla.R.id.foo_layout);
I think I got NullPointerException for the first one and android.view.ViewRootImpl$CalledFromWrongThreadException
for the second one.
In your first try you get a NullPointerException because you are searching for your foo_layout wihtin your main_layout. findViewById is used to search for views wihtin a layout and not to find/inflate layouts. In your second try you get a CalledFromWrongThread Exception because you access the UI (setContentView()) from outside the UI thread. This is how you change the layout wihtin your test class:
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
getActivity().setContentView(R.layout.foo_layout);
}
});
getInstrumentation().waitForIdleSync();
// now you can access your views from your foo_layout via getActivity().findViewById(...)
I don't know what you mean by "I want to test my layout". Either you want to check if your layout got successfully loaded through a button click or you want to access the views of the new (loaded) layout. In both cases you can do something like the following:
public void testLayout() {
// get your button that changes the layout of your activity.
// that button is in your main_layout
final Button btChangeLayout = (Button) getActivity().findViewById(R.id.yourButtonThatChangesTheLayout);
// perform a click in order to change the layout
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
btChangeLayout.performClick();
}
});
getInstrumentation().waitForIdleSync();
// get a reference of a view thats in your foo_layout e.g. a Button
Button aButtonInYourFooLayout = (Button) getActivity().findViewById(R.id.aButtonInYourFooLayout);
// now you can do what your want with your button/view.
//if you just want to know wheter your layout has successfully been loaded
//or not you can test your view if it's null
assertNotNull(aButtonInYourFooLayout);
}
i'm not sure ... but i think the first problem is:
act.setContentView(bla.bla.bla.R.id.foo_layout);
change to:
act.setContentView(bla.bla.bla.R.layout.foo_layout);
because in res/layout/ you have your UI, isn't it?

Categories

Resources