I'm making a quiz app and I'm looking to have my "practice questions" activity refresh itself with new questions with a previous/next button is pressed. The questions are in an array placed inside a singleton "Global" class.
I want to update/refresh the activity so that a new question and set of answers pops up (ie. a counter is incremented/decremented when the previous/next button is pressed), but I don't want to reset the singleton class, which would re-randomize the questions in the array. Intentionally, they're randomized every time the activity is accessed from the main screen. I also have other variables (ie. counters, totals, flags) I would like to update with each passing to a new question.
Can someone point me in the right direction?
If it helps, the buttons are created and ID'd in XML And the Java file fills the buttons/textviews with strings.
If you just need to update the text inside the views, then you don't need to refresh the Activity. Instead, you can write an updateViews() method and call it when the previous/next button is pressed.
For example:
private void updateViews(String question, String answerA, String answerB,
String answerC, String answerD) {
TextView tvQuestion = (TextView) findViewById(R.id.question);
TextView tvAnswerA = (TextView) findViewById(R.id.answer_a);
TextView tvAnswerB = (TextView) findViewById(R.id.answer_b);
TextView tvAnswerC = (TextView) findViewById(R.id.answer_c);
TextView tvAnswerD = (TextView) findViewById(R.id.answer_d);
tvQuestion.setText(question);
tvAnswerA.setText(answerA);
tvAnswerB.setText(answerB);
tvAnswerC.setText(answerC);
tvAnswerD.setText(answerD);
}
Obviously, you would need to tailor this approach to your views and data, as well as keep track of state (current question number, etc.), but this should give you an idea of one way to move ahead.
Related
I'm trying to find the best approach to handle a user clicking a button, setting the text of that button to (in my attempt) a textview and then automatically move onto the the next textview to update on the next button press.
At the moment I can do this by manually specifying each element by ID to update but there must be a better way, or a different approach.
An example: imagine a table with 2 rows of each 4 columns (each contains a textview) and below that 6 buttons. When any button is pressed the first time the textview of the first column is set to the button text, the next press of any button sets the the textview of the second column is set to the button text, and so on. When a row is complete it will start back at column one of the next row.
at the moment I'm using a hard coded function triggered by the onclick event, I realized that I could implement some counter and user row[i]_col[j] (or such) but is there a better way to approach this (eg. some way similar to changing focus on edittext)?
public void setValue(View view) {
TextView textView = (TextView) findViewById(R.id.textView_row1_col1);
textView.setText(((Button) view).getText());
}
If i understood your problem correctly ,
I would suggest using an array of Textviews instead of using findviewbyid so much (very pricey memory wise )
TextView [][] txtArr = new TextView[XSize][Ysize];
Initial that in the start once and use it in the on click later.
I am new to android/java programming. I have two class, one is an activity and other normal class. In my activity class contains TextView. Can i update my TextView of one class from a editText(that the user enters) in a another class. I tried with random code, but it fails. Please help I've been looking forever
You might update the TextView from wherever in the java code by referring to the
findViewById(R.id.some_text_view_name).
Some what like this:
TextView textViewName = (TextView) findViewById(R.id.some_text_view_name);
textViewName.methodName();
Here methodName() refers to the Public methods listed here
Hope it helps. :)
You can start your second activity using startActivityForResult() instead of startActivity(). In the second activity you can set the result and its status using setResult() and get back to previous activity (via back press or something). In the first activity, this result will be received in onActvityResult(). From here, you can get the data set by second activity and update your textview.
This is the gist of what you are supposed to do. You can get code example here, here and here.
I am programming an app that the user can purchase buildings (drag and drop imagebuttons) that they are allowed to move where they would like on the background. Drag and drop works well and so does the dynamic creation of the imagebuttons works well. I even have sharedpreferences working so that the coordinates of the last spot the building was in is remembered, but the actual imagebutton shows up null when the app is closed and reopened. Thus, it gives me the coordinates, but does not have the button there. My question is, how do I have the app "remember" that the imagebutton has been created so that I can display the button at the coordinates that are saved?
Any help is appreciated. Thanks.
You should store the id of the image that the button had on it, and then when onRestoreInstanceState is called or if you are having it recreate your activity, onCreate() is called, re-initialize the ImageButton with the id. If you loaded it dynamically the first time, you have to load it dynamically when the activity is recreated!
Have the ImageButton as a global variable.
private ImageButton myImageButton;
//Code....
public void onSaveInstanceState(Bundle b){
super.onSaveInstanceState(b);//THIS IS VERY IMPORTANT
b.putInt("image_1", idofyourimage); //This is where you store the id of the image
//you can store all sorts of data in the bundle, so use whatever works
//for you.
}
public void onRestoreInstanceState(Bundle b){
super.onRestoreInstanceState(b);//IMPORTANT
int imageId = b.getInt("image_1");
myImageButton = new ImageButton();
myImageButton.setImageResource(imageId);
//Any other stuff you need to do
}
Let's say I have some strings in Java that I've retrieved from a web script. It doesn't matter really, they're just strings stored in variables.
Now my question is how to dynamically append them to the application (to show the user) and possibly style their position, from Java.
To draw an analogy, I want to do something similar to the following in JavaScript:
var text = document.createElement('div');
text.appendChild(document.createTextNode("some string"));
text.style.position = "whatever";
// etc, more styling
theDiv.appendChild(text); // add this new thing of text I created to the main application for the users to see
I've looked into the TextView, and I don't seem to be using it properly (I don't understand the constructor I guess?). What I want to try right now is to have the user press a button in my application and then have some text dynamically generated. Here's what I've been trying:
search_button = (Button) findViewById (R.id.backSearchButton);
search_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String test = new String("testing!");
TextView test2 = new TextView(this); //constructor is wrong, this line gives me an error
test2.setText(test);
setContentView(test2);
}
});
As you can probably tell, I don't come from much of a Java background. I'm just trying to draw parallels to stuff I would want to do for a web app.
Thanks for the help.
Try:
TextView test2 = new TextView(ParentActivity.this);
replace "ParentActivity" with the name of your activity.
your this pointer is a reference to the OnClickListener that you have anonymously created. You need a pointer to the containing Activity.
You would probably be better constructing your text view in the onCreate of your activity and just setting the text from your onClick method
I'm new to Java and Android, myself. But I'll give your question a try.
You have to decide if you want to create the TextView in XML or dynamically in Java. I think creating it in XML in the layout creator is better.
I don't think you should be using setContentView(test2).
If you create a TextView dynamically, I don't think you need to put anything in its constructor. But you do have to add the TextView to the parent view. In other words, let's say you have a LinearLayout somewhere in your layout. You have to do: linearLayout1.add(myTextView) or something.
The rest of your code seems fine. But then again, I'm still new to this, myself. Let me know how helpful this answer is, I'll try Googling for more help if it's not enough.
I have an simple android app that I want to write on the display the value of a single field, that belongs to a different class.
Using the simple text view I can write the initial value of the field but I don't know how to update the text on the display whenever the field has changed value.
Btw, it is my first android app so im still lost
Here is my activity code:
public class findIT extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PositionHolder ph = new PositionHolder();
TextView tv = new TextView(this);
setContentView(tv);
//this.updateText();
tv.setText("You are at " + ph.getPosition());
}
}
You need to create a TextView in an xml layout as following:
<TextView
android:id="#id/textView01"
android:text="Enter your initial text "
android:layout_width="wrap content"
android:layout_height="wrap content"
></TextView>
Then write the following in your class after setContentView() function:
TextView textView = (TextView) findViewById(R.id.textView01);
textView.setText("Enter whatever you Like!");
Chris.... Here is a most basic way to develop an app. Divide up the problem into three pieces. The presentation or view. The algorithm or model. The Controller that responds to user and system events. This creates a "separation of concerns" such that the Controller owns the view and the model. You create the view using xml as in main.xml. You create a separate class to do the work say MyModel.java and of course there is the Controller or the Activity class say MyActivity.java. So the data comes from the model goes to the Controller that updates the view.
So your question is how to get the data from the model and update the view. Naturally this will take place in the controller, your Activity. The simplest way to do this is to put a button in the activity and when the user hits the button, call model.getLatestData() and update the view. This is PULLing data. The next way is for the Controller to check for an update say every minute. This is POLLING for data. The next way is for the Controller to register an interest in changes to the model and sit around waiting for the model to signal a change and then update the view. This is asynchronous PUSHING of data from the model to the controller and could be done with the OBSERVER pattern.
I know this makes no sense to you when you are struggling with trying to just get the code to work, but hopefully I have planted the seed of an idea in your head that will bother you and make sense sometime in the future.
JAL
You're going to need to define a layout in xml, then when you create the TextView, you associate it with its layoutID. Something like:
TextView tv = (TextView) findViewById(R.id.something);
I can explain a little more, if you need, but this will give you a starting place to look for more answers.