ScrollView FOCUS_DOWN, not quite there yet - android

I apologize for this, and I am sure it's a very rookie question... but I am still trying to grasp java and how everything works (I'm surprised I made it this far.)
I have a TextView inside of a ScrollView and I am trying to get it to focus on the bottom entry each time a new entry is added.
I have this in the code:
getScrollView().post (new Runnable() {
#Override
public void run() {
getScrollView().fullScroll(ScrollView.FOCUS_DOWN);
}
});
that is inside the
public void onClick(View src) {
switch(src.getId()){
case R.id.buttonOk:
(So that when I click ok, it will focus to the bottom.)
now.. I am getting this error: The method getScrollView() is undefined for the type
do i need to call out the name of the scroll view within that first set of code, android:id="#+id/scrollView1"
Again, I am sorry I am so confused on this. I am obviously not getting something right here. Any help is greatly appreciated.
Thanks

you can used this.
scrollview=((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
#Override
public void run() {
scrollview.fullScroll(ScrollView.FOCUS_DOWN);
}
});

A couple things:
To get access to the view from code, you use (ScrollView)findViewById(R.id.scrollView1) -- getScrollView() doesn't exist, though you can write it using the command I described.
Based on what it sounds like you're trying to do, you might want to look into ListView instead (rather than appending lines to a TextView).

Related

Problem Onclick + "v" does not recognize the methods in android studio code

I am fairly new to programming, I was writing an application code in Android Studio but had a problem
It turns out that when I put this line in my code (In image)
it does not detect it and an error occurs both with "onClick" and with "(v)" I reached this point and I don't know how to solve it
Attached images:
[1]: https://i.stack.imgur.com/huZLu.png Error Code
[2]: https://i.stack.imgur.com/hCJYJ.png All Code
I would really appreciate the answer
This problem is eating my head!
Thank you
it's not a big problem, you can write like this, it will solve your issue
mOpicon2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});

Adjusting the scrollview at the bottom of the page

I have lots of contents on one page so i am using scrollview. I have one scan button at the bottom of the page, which i need to click many times to scan lots of barcodes. when i click on scan button, i am opening "BarcodeScanner" application that will sacn the barcode and returns the result to my application. so when i come back to my application, again i need to scroll the entire page to click the scan button which is at the bottom side. So is there any way to initialize the scroll at the bottom of the page? Please help
Thanks in advance
How do you think that you can initialise the scroll view.!! If you have lots of contents in a page you have to use scroll view for seeing all the contents. ! Try this, put your scroll view xml code in to botom of the xml code. and check.!!
Say your ScrollView id is scrollId, the code will lokk like this
scrollView = (ScrollView) findViewById (R.id.scrollId);
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
Try this
s.post(new Runnable() {
public void run() {
s.scrollTo(0, mScrollView.getBottom());
// or this
//s.fullScroll(s.FOCUS_DOWN);
}
});

Android - Trigger a loop that repeatedly redraws in a listener (force invalidate even though UI thread is busy)

I have a view that contains a user's drawing. When clicking a 'play' button I want to have a loop that draws the image from start to finish.
In attempting to do this I basically have a loop inside of an onClickListener for the play button that essentially invalidates the view, waits, sets the next set of points and invalidates again.
The problem is that the image doesn't actually redraw until after the entire image is draw. So essentially nothing happens and then it's all of a sudden done. After doing some research, my assumption is that invalidate is telling the main UI thread to redraw when it gets time, but it's already hung up on dealing with the listener, so it doesn't actually draw until the loop is finished. I've tried calling onDraw directly, using postInvalidate, and just about any other ridiculous thing I can think of, but I'm not sure how to get this to work.
Here are some code snippets:
In my PaintActivity(Which has a PaintAreaView)
The scrubber indicates the progress of the drawing. The first to lines in onProgressChanged works when I manually use the seek bar, but not when pressing play, which is why I tried adding all the extras.
_scrubber.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
_paintAreaView.setPointsToDraw(_scrubber.getProgress());
_paintAreaView.invalidate();
_mainViewGroup.invalidate();
_paintAreaView.postInvalidate();
_mainViewGroup.postInvalidate();
_paintAreaView.drawPoints(_paintAreaView.getCanvas());
_paintAreaView.invalidate();
}...
My Play Button stuff:
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
...
playDrawing();
...
}
});
private void playDrawing()
{
int endPoint = getPaintPointCount();
while(_scrubber.getProgress() < endPoint)
{
_scrubber.incrementProgressBy(1);
try{
Thread.sleep(10);
}catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
//_paintAreaView.setPointsToDraw(_scrubber.getProgress());
//_paintAreaView.invalidate();
//_mainViewGroup.invalidate();
if(_isPaused)
break;
}
}
I'm not a huge fan of using that sleep, but I've struggled with other solutions and that's my next thing to research. But for this question, incrementing the progress bar does successfully call onProgressChanged, but again it just isn't drawing the view until it's out of that play button click listener.
If I have to wait until I'm out of the listener to get this to work, I'm new enough that I'm not sure how I would approach that.
Sorry for my bad code, any help is much appreciated to help me understand what's happening here and what I need to understand to get this to work.
Thank you!
I got it to work, I'm not sure if all I did was necessary, but here are the changes I made based on my Main UI thread theory being the problem.
I created a class Thread member variable:
private Thread _drawThread;
In onCreate():
_drawThread = new Thread(){
public void run(){
playDrawing();
}
};
play Button onClick:
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
...
_drawThread.start();
}
});
Seems to be doing just what I want! Except the app crashes the second time I hit play :) From here I don't think it'll be to bad to figure out that issue though.
EDIT-----
Creating the Thread in my play button onClickListener instead of having it be a member variable fixed the crashing. I incorrectly assumed the thread would be destroyed as soon as the listener went out of scope on the main thread, but I guess that's not the case.

WebView must be loaded twice to load correctly

When the page with the WebView first loads, sometimes images are missing or displayed incorrectly. If I reload the page the WebView always displays perfectly. I know everyone will first think I set javascript after loadUrl, but that isn't true.
In onCreate I have:
learnWebView = (WebView)findViewById(R.id.learnWebView);
learnWebView.setWebViewClient(new WebViewClient());
learnWebView.getSettings().setJavaScriptEnabled(true);
Then later in the function called after onCreate I have:
learnWebView.loadUrl("myurl");
And yes, I know that the function with loadUrl is called after onCreate every time.
Please try this instead of your way, that is a bad practice:
learnWebView.post(new Runnable() {
#Override
public void run() {
learnWebView.loadUrl("myurl");
}
});
Or this, in case the first one wont work:
learnWebView.postDelayed(new Runnable() {
#Override
public void run() {
learnWebView.loadUrl("myurl");
}
}, 500);
Hope this helps.
Look at onViewAttachedToWindow.
You should process your logic in javascript only after onViewAttachedToWindow fired.
I met the same issue. not sure if it is the same case as yours. I spent more than 3 days and actually found the cause it that another WebView object calls pauseTimers() for saving some CPU performance which actually " Pauses all layout, parsing, and JavaScript timers for all WebViews."

How link 2 layout with a Button

I don't speak english very well, so please try to understand me.
iìm trying to develop a little app for android in eclipse. I've create 2 layout where, in first there are a button and in a second there is only the text "helloword". I want that the button to send me at the "helloworld" layout
I followed a tutorial but it's not correct.
Someone can help me posting th code to implement??
Excuse my English still
and thanks for any help
You need to create two different Activities, with separate layouts. You set their layouts using setContentView inside onCreate. Don't forget to include your activities in the manifest (eclipse generated one for you. follow how it was done in the first activity and you should be fine. no need to add the filters and such, just the .Activity)
then in your first activity's button, do this:
my_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(this, MySecondActivity.class);
startActivty(i);
}
});
If you want to use just 1 activity then use the following code,
my_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
FirstActivity.this.setContentView(R.layout.second_layout);
}
});
Its not so hard just do this:
#Override
public void onClick(View args0) {
setContentView(R.layout.aardelayout);
}
just a simple Search on google and you find your answer .
see this tutorials ( two are in frensh if you are prefering frensh tutorials ) :
tuto1
tuto2
tuto3

Categories

Resources