Show progress dialog while adding views dynamically to scrollview in android - android

In the start of an activity in OnCreate method, I'm trying to add some text views dynamically to a scrollview of the layout file. But during the time of this operation the screen remains blank. Is it possible to show a progress dialog when adding views dynamically to a scrollview?

You should use a AsyncTask for that. Please post some code you're having in your onCreate method at the moment.

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lottery);
for(i = 0; i < 20; i++) {
TextView txt = new TextView(this);
scrolview.addView(txt);
}
}
The code is similar to this...can u explain how to put a progress dialog...in this sample using AsyncTask...where should i perform this operation doInBackground method or on postExecute?

Related

Android Add Items One By One Into a LinearLayout

I'm new to Android and I'm trying to find out how to do that:
- I have an activity with a ScrollView and inside it I have a LinearLayout (R.id.my_layout)
- I nedd to add TextView programmatically so I'm doing this:
I load the main XML layout via seContentView, I refer to my LinearLayout inside the ScrollView as "mLayout" and so on.
I load a list of names from a file and with a function called populateList()I do:
private void populateList() {
try {
for (final String team : mTeams) {
rCount++;
addRow(team);
}
}
The addRow() method just create a new LinearLayout (mRow), a TextView, 2 Button, add the TextView and the 2 Buttons to the LinearLayout, and then I use addView to add the new mRow to the mLayout.
Everything is working fine, but the ScrollView is shown only when i finished creating the list (so when the populateList() ends). What I would like to do is to show the rows one by one in sequence to give the activty a better look and a bit of animation.
Is there a way to do this?
I hope i was able to explain it :-)
Thank you
new Thread(new Runnable() { // i am creating the new thread
#Override
public void run() {
// so call populateList() function here
}
}).start();
and for your addRow(String string) method the place you call View.addView(); edit it this way and place the following code in your addRow(String string) method
View.post(new new Runnable() {// view here is mlayout the scrollView.
#Override
public void run() {
mlayout.addView(yourview); // note yourview should be final,
//eclipse might help you with that
}
});
remember to declare mlayout globally, so you do not have to attach final

Force Main-Layout to Inflate

I'll try another shot. Hopefully now i get the answere i totally Need.
Just imagine:
I have a widget which calls(after onClick) a blank activity with no hardcoded code, just a Relative Layout with some views.(Layout provided by XML-layout-file).
My Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
When i add a for-loop to the onCreate-Method which adds 50 Buttons(take no care about layoutparams, orientation and so on). Just 50 simply Buttons
Like:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
for(int i = 0; i<50; i++) {
Button btn = new Button(this);
myLayout.add(btn);
}
I recognize that he only Shows the activity on the Screen until he finished to add all the Buttons.
My Question is: How can i prevent this!? How can i Show up the Activity with the Content from the XML-layout file and then(ONLY then) add one Button after another to the Layout.
Is this possible? If so, do i neew to redraw the whole activity and so on. Please give me advise to my issue.

Auto-scrolling textview in Android

I've tried a lot of different ways, most of the suggestions found here, but none of them seems to work. What I'm trying to achieve is at chat area below my game area, a SurfaceView. It is supposed to scroll upwards as new lines are added to the textview.
At first, it looks like a really simple task, but having tried all kinds of suggestions, like a TextView in a ScrollView, like a TextView in a TableRow in a TableLayout in a ScrollView, and so on...I've still not made it happen. Of course this must be something easily achieved in Android, right??
The task is to display like 6 lines of text in the bottom of the screen, and as a new message is added last it should scroll the rest upwards, like a terminal window. The important thing is that it should add the latest message after the other and, when reached the bottom line, scroll the text upwards and add the new line(s) at the end.
Any kind of help or suggestions would be highly appreciated!!
I needed the same behavior in one of my apps and I achieved in just with one command:
view.setGravity(Gravity.BOTTOM);
Or, analogously, setting this attribute in your layout:
android:gravity="bottom"
Then simply add your lines using:
your_text_view.append(newLine);
Suppose, you declared your ScrollView as follows...
private ScrollView mScrollView;
you initialized it as...
mScrollView = (ScrollView) findViewById(R.id.scroll_view_chat_window);
Now, create a method to perform scroll down when you call the method. Inside the method implement a thread which will do the scroll down independently. And call the method after every chat message update thats will do the auto-srcoll functionality.
private void scrollDown() {
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.smoothScrollTo(mScrollView.getScrollY(), mScrollView.getScrollY()
+ mScrollView.getHeight());
}
});
}
I've achieved this (crudely!) by maintaining my own list, deleting the lowest element then adding at the end each time. Here i've just got a 3 line window:
public class MessageWindow {
private ArrayList <String> msgs;
private Activity parentActivity;
public MessageWindow(Activity act, int allMsgsMax) {
this.parentActivity = act;
msgs = new ArrayList <String> ();
// create empty list elements for initial display
for (int i = 0; i < allMsgsMax; i++){
msgs.add("");
}
}
//
public void put (String msg){
msgs.remove(0);
msgs.add(msg);
// get a handle to the textview 'messages', a 3-line box
TextView t2v = (TextView) parentActivity.findViewById(R.id.messages);
// crappy but you get the idea:
t2v.setText(msgs.get(0) + "\n" + msgs.get(1) + "\n" + msgs.get(2) );
}
then in the activity:
protected MessageWindow messageWindow;
// setup splash screen
messageWindow = new MessageWindow(this, 3);
// write some stuff - row1 will disappear off the top of the box
messageWindow.put ("row1")
messageWindow.put ("row2")
messageWindow.put ("row3")
messageWindow.put ("row4")

Show white screen until layout is completely constructed

My problem is that I have complicated layout, and when my application starts, I see how every view is adding to layout. I see my custom window title bar construction too. I see empty custom window title, with gradient background on start, and then 1 - 2 sec later I see completed window title with my views.
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
Is there simple way to show white screen, until my activity and custom title bar will be completely constructed?
The main trouble is that I cant start application with no title, and then add custom window title.
Only solution I know is splash screen, but it is too complicated for just this small task.
Use an asynctask like this:
private class LoadingMyView extends AsyncTask<void, void, int> {
protected void onPreExecute ()
{
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
//show whiteView
}
protected int doInBackground() {
return 0;
}
protected void onPostExecute(int result) {
//Hide white View
}
}
Note: I didnt test the code but i think more or less is ok
AsyncTask

I need to add onClickListener to a bunch of ImageButtons

I have created a bunch of ImageButtons programmatically while in a for loop. They have worked fine as the data displayed in a HorizontalScrollView. Now I need each one to go dim or bright when clicked. First click will setAlpha(45); second click will setAlpha(255);.
I don't think I fully understand how the Views and onClickListener works yet. It seems the onClick function examples I find take a View. How would that function know which button is clicked? Perhaps there is an easier way to do what I want?
Here are the ImageButtons.
TableRow tr0 = new TableRow(this);
tr0.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setBackgroundResource(0);
clueBut.setImageBitmap(ClueList.get(but).btmp);
//clueBut.setOnClickListener(this);
tr0.addView(clueBut);
}
Is there something I need to do to make the buttons identifiable? And how would that pass in through into the onClick function to be used?
-: Added Information :-
I am starting to wonder if the problem isn't with the buttons, but with the way I built the screen. More information added.
The Game activity is the main game, which uses the PuzzleView for the upper part of the screen holding the game grid. The lower part is where the ImageButtons are and I built them in place in the Game class.
public class Game extends Activity{
//various variables and stuff
private PuzzleView puzzleView; // The PuzzleView is from another .java file
// public class PuzzleView extends View
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout mainPanel = new LinearLayout(this);
mainPanel.setOrientation(LinearLayout.VERTICLE);
puzzleView = new PuzzleView(this);
mainPanel.addView(puzzleView);
HorizontalScrollView bottom = new HorizontalScrollView(this);
mainPanel.addView(bottom);
TableLayout clues = new TableLayout(this);
bottom.addView(clues);
TableRow tr0 = new TableRow(this);
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setImageBitmap(ClueList.get(but).btmp);
tr0.addView(clueBut);
}
When I try to add the ClickListener(this) I get errors about this not being able to be a Game. I have similar problems in the onClick(View v) function referencing the View. Are these problems because I am building the buttons in the Game Activity instead of a View class?
Thanks
When you set up an OnClickListener and implement the onClick(View v) callback, it's the Dalvik VM the one that will call that method each time the View is clicked, and it will pass the View instance as a parameter. Thus, the code you write inside that method will be applied only to the View that received the click and not to any other View. Add something like this to your loop:
clueBut.setOnClickListener(new OnClickListener() {
public void onClick (View v) {
if (v.getAlpha() == 1f)
v.setAlpha(0.2f);
else
v.setAlpha(1f);
}
});
In the onClick event:
public void onClick(View currentView)
{
Button currentButton = (Button)CurrentView;
//Do whatever you need with that button here.
}
To identify each view uniquely use the property
View. setId(int)
In your case the code would look something like this
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setBackgroundResource(0);
clueBut.setImageBitmap(ClueList.get(but).btmp);
clueBut.setId(but);
//clueBut.setOnClickListener(this);
tr0.addView(clueBut);
}
Inside the onclick listener match the id of the view using findViewByID()

Categories

Resources