[Q] ScrollView, SetText and scrollTo - Android Software Development

Hi,
Im just getting into Android development and picking up pace slowly. Getting confused how people are getting settings style menus with the appropriate colour Summary Line while adding icons for Stars and Checkboxes etc to highlight and tick.
Anyhoo, I have a TextViews inside a LinearLayout inside a ScrollView, seems to work OK. I use SetText to set the Text on the TextViews and I create everything at run time instead of through the XML files.
But the application I have is a polling type application and I need to repopulate the TextViews and redraw the ScrollView in the same position that the user scrolled to previously.
Im accutely aware of problems using ScrollTo after you've used SetText, so I found the following code:
Code:
sView.post(new Runnable() {
public void run() {
sView.scrollTo(0, iTop);
}
});
It works, but there's a screen flicker where it first draws the screen in its normal position then sets the scrollTo.
What is the best way of achieving this, or am I doing everything completely backwards and most normal people wouldn't have this kind of problem?
Thanks
Simon

It sounds more like you might want to consider using a listview instead. It was designed to contain several child views and scrolls and updates seamlessly.

Hi,
Thanks for the reply. Perhaps I am a bit too much of a beginner.
I seem to be favouring TableLayout so that I can display tabular data and have columns resize correctly and have the data presented OK.
I think that's why I went with updating everything at design time because I have a variable number of rows that can be added. IIUC, variable rows (in an XML file) I think can only be created using ListView, but of course a TableRow needs a TableLayout as a parent.
Here is the idea...
Code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="@+id/TextView01" android:paddingRight="5px" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="@+id/TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</TableRow>
</TableLayout>
The code I have for TableRows is not dissimilar to, http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout
But of course using setText means I cannot then scrollTo

Duh.
First learning curve mastered.
You only need to add views to containers etc the first time you display the screen.
You can update the text in the views later on without affecting the screen position.
I like this.

Related

[Q] layout help

I just started to build my app's layout and I'm confused, so I could use a little help.
I want it to look like this:
- there's a bar with buttons which always stays on the bottom of the screen (like position:fixed in CSS)
- there are two (or more) lists with a dynamic number of records which will mos definitely overgrow the screen size, so I need a scrollbar on them. These will not push down the button bar.
This is easy as hell, I know, but I'm not sure how to do it properly so I won't need to change it later.
It's all about learning to love (and hate) RelativeLayout
http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/
Yeah, should just be a ScrollView with a nested RelativeLayout putting your buttons on the bottom...not too painful
I have a problem with my ListView. I want it to be full size without a scrollbar (the parent of ListView is scrollable), but it shows up really short (only 1.5 rows fit in). Here's my code :
Code:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/top_controlbar"
android:layout_above="@id/bottom_controlbar">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- GEOTASKS -->
<LinearLayout
android:id="@+id/geotasks_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="@string/main_header_geotask"
android:textSize="18sp"
android:background="@color/header_bg"
android:textColor="@color/header_text"
android:gravity="center"
android:layout_marginBottom="5dp"
/>
<ListView
android:id="@+id/list_geotasks"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/empty_geo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_empty_geo"
android:textSize="15sp"
android:paddingLeft="5sp"
/>
</LinearLayout>
etc, same for timed and simple tasks
I tried changing the height of the parent layout and the listLayout (all 4 combinations), but it didn't help.
Well **** my rusty sheriff's badge. I can't believe nobody knows the answer.
ScrollViews are weird. The way they stretch to fill content is not intuitive.
I can't remember the solution off the top of my head but do some googling for "ScrollView" and I think there are some answers on stackoverflow.
hth
My problem is not the scroll view, but ListView. Scrollview stretches without problems.
I want to achieve a simple thing - make the ListView display all of it's content without scrolling. How is that not possible ? OMG
At first I used LinearLayouts and added new views to the root layout. That was very ugly and I couldn't register those views for context menus. Then I found out about list views, and I couldn't be happier if I could just do this one thing.
Sorry I misunderstood what you were asking.
What's the point of having the parent of a ListView scrollable anyways? I think you're just thinking about things in a strange way and that what has got you stuck.
ListView has an addHeaderView() addFooterView() if you need static items at the top and bottom, and you know how to put bottom and top control bars using relative layout, why not eliminate the ScrollView?
I think you're making things difficult on yourself by putting something scrollable inside something scrollable.
Your other option is to use views like you were using them, they do support context menus: http://developer.android.com/reference/android/view/View.OnCreateContextMenuListener.html
Hope that helps.
Edit: btw, the reason I am recommending it this way is that even if you override ListView's onTouchEvent, I dont think it will expand to wrap all the content and be scrollable by the ScrollView. Instead it will just sit there as an unscrollable listview that leaves the user unable to reach the content beyond what they can see originally.

[Q] how to show focus on clickable image?

I have created a custon UI menu, and for backwards compatabilty to non-touchscreen devices (such as google TV) I want the user to be able to visually see that the image is selected (I have tried with my desires trackball and it works but just looks the same)
How would i get my app to show the different image on focus and switch back when off focus?
So far i have tried:
Code:
@Override
public void onFocusChange(View arg0, boolean arg1) {
ImageView imageView = (ImageView) findViewById(R.id.tl);
Drawable newFocusImage;
newFocusImage = getResources().getDrawable(R.drawable.menu_top_left_trans_ic_fcs);
imageView.setImageDrawable(newFocusImage);
}
However this somehow changes it to a completely different image than the new drawable specified, and does not revert on un-focus.
All help would be greatly appreciated
Thanks
Rather than draw a new image, why not say change the alpha channel values to make it appear lighter or darker? Or overlay it with a solid color image with a low alpha value?
The alpha change is a good idea. As a photographer i would say adding a transparent vignette would look great. If you don't know what a vignette is, its basically darkening the edges of an image / a circular gradient (dark edges, light middle)
From something awesome
ye that sounds cool
however my main problem still lies in the coding of the problem, do you know how to code a vignette? is it possible to change a image like that in java?
Instead of using getDrawable and setImageDrawable use setImageResource
Create two images:
menu_top_left_trans_ic_fcs_normal - to be shown when not focused
menu_top_left_trans_ic_fcs_focused - to be shown when focused
Then add to res/drawable a new file named menu_top_left_trans_ic_fcs.xml with the following content:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_top_left_trans_ic_fcs_focused" android:state_focused="true" />
<item android:drawable="@drawable/menu_top_left_trans_ic_fcs_normal" />
</selector>
Note: You can also set android:state_pressed
Use in your code
Code:
imageView.setImageResource(R.drawable.menu_top_left_trans_ic_fcs);
I never tested this exact code but it should work.
thanks it worked !!

Noob layout issue - probably VERY EASY!!

I'm essentially trying to create a MadLibs app for my kids. I'll type in the text of the books they already have and they can play with it in the car. But I'm a noob and I haven't done much else other than the first few simple apps in tutorials - my brain got tired trying to figure out sqlite stuff, so this all went on the backburner and now here I am again giving it another effort.
So I was thinking something ridiculously simple, like just having a textview string of the first part of a sentence, then an edittext with a hint set for the type of word to fill in, followed by the rest of the sentence in another textview string. Lather, rinse, repeat. Easy, right?
I can't seem to figure out how to get the objects to flow properly in the layout... First problem is that the edittext is gigantic compared to the textview. I could probably figure that one out easy enough, but my second problem prevents me from actually seeing the effects of my changes, and that problem is that the text doesn't flow onto the next line.
I've tried linearlayout and relativelayout, but I'm turning here for some guidance before I waste countless hours. Should I just build each paragraph (textview) as a single string comprised of other strings and edittext variables?
Any help would be great appreciated. And remember: go easy on me.
Here's my current activity_main.xml:
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textBody01"
android:weight="1" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint_adjective"
android:weight="1" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textBody02"
android:weight="1"/>
</LinearLayout>
etnpnys said:
I'm essentially trying to create a MadLibs app for my kids. I'll type in the text of the books they already have and they can play with it in the car. But I'm a noob and I haven't done much else other than the first few simple apps in tutorials - my brain got tired trying to figure out sqlite stuff, so this all went on the backburner and now here I am again giving it another effort.
Click to expand...
Click to collapse
Could you post a quick picture (just a rough one in paint or something) showing the desired layout ?
Absolutely. I'll mock one up and get it on here shortly.
Sent from my SPH-L710 using Tapatalk
PicomatStudios said:
Could you post a quick picture (just a rough one in paint or something) showing the desired layout ?
Click to expand...
Click to collapse
Here's a little bit of how I'm thinking this should work out pretty easily. In the end, I would like to have the filled-in words in bold red text and the rest of the body paragraphs as regular black text, and I would like to make the background black to hide the body of text. I would then have a button that calls a function to change the background to white to make the body text visible "onclick" -- or maybe toggle between black/white/whatever.
The first hurdle is right here with the layout. I mean, this is REALLY simple, right?
View attachment 2380717
etnpnys said:
Here's a little bit of how I'm thinking this should work out pretty easily. In the end, I would like to have the filled-in words in bold red text and the rest of the body paragraphs as regular black text, and I would like to make the background black to hide the body of text. I would then have a button that calls a function to change the background to white to make the body text visible "onclick" -- or maybe toggle between black/white/whatever.
The first hurdle is right here with the layout. I mean, this is REALLY simple, right?
View attachment 2380717
Click to expand...
Click to collapse
Maybe not that simple
As I understand it you want the text to flow continuously left-to-right, top-to-bottom, a bit like floating CSS layouts ?
That is you might want it to do...
Mary had a [edit text] lamb. Its
fleece was white as snow.
Where that's 2 TextViews with an EditText between them ?
You will struggle to do that on Android, since there is not built in concept of 'flow' as you may be imagining.
The closest you will get is a RelativeLayout but you'll have to work hard to line the views up correctly.
You could try something like this, or maybe this. The second one links here where there is somebody's FlowLayout code towards the end of the page. I haven't tried it but it looks close to what you need at first glance. The concept appears to be a ViewGroup which calculates where its children should be and positions them accordingly.
You get my ideas exactly. I'll look into those! Much appreciated!
Sent from my SPH-L710 using Tapatalk
Just to button this up, I wanted to chime in to let everybody know which direction I went.
Basically, I have a vertical linear layout comprised of:
several editText fields for user input. @hint points to strings that indicate what kind of word (noun, adjective, etc) needs to be filled in.
a "Create" button
a "Clear" button
a textView
Behind the scenes, I set a series of "sentenceFragment" variables that contain the body text. Those all end with a "[" and start with a "]" so that the filled-in words can be easily seen once the story is built. Users fill in the editText fields and click the "Create" button. When clicked, I used getText on each of the editTexts to store each as a string, then a "paragraph" variable is built from concatenating all of the sentenceFragments + editTexts, then I used setText to the textView to display the entire story below.
The "Clear" button then uses setText on the textView and the editTexts to blank them out.
So, in the end, the user is presented with stuff like:
Sometimes you just want to get a [green] car. In your search, you find several [bugs] on the seat.
Thanx for you help, everybody! Now I'm looking into putting a spinner at the top to let the user select different stories... Anybody wanna point me in the right direction there?

Drawable Help..

Hey all -
I'm getting into an area I hadn't gone before - drawable XML files, and am stumped, since what I think should work is giving me an exception.
Some background -
I have multiple images, used as a background. Then there are a couple other images used for foreground symbols. (These are for markers on a map.) So, for example, I have 5 different colors, and 5 symbols. Instead of creating 30 different markers (5 symbols + blank) I figured I could include the resources, and then use a list-layer to show the symbol on top of the marker. Would save some room when the APK's created.
So, I made a simple test, like this. XML file is simply named "marker":
Code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="34dp"
android:drawable="@drawable/marker_green_blank"
android:left="0dp"
android:right="20dp"
android:top="0dp"/>
<item
android:bottom="20dp"
android:drawable="@drawable/marker_symbol_int"
android:left="3dp"
android:right="19dp"
android:top="4dp"/>
</layer-list>
Oddly, when I go to use this, I get an exception when it's tried to be used:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
Any thoughts? I obviously missed something - probably something idiotic, since I do that a lot.
Or, is there another way to combine two images/res's together?
--Mike
Edit:
I forgot to add - this is how I'm creating the 'icon' to add it to the map. The second line is map-specific code and may not really apply here. I'm starting to think that the first line - BitmapDescriptor - is causing the issue:
Code:
BitmapDescriptor marker_red_blank = BitmapDescriptorFactory
.fromResource(R.drawable.marker);
<!-- Code that sets other options using a MarkerOptions object ... -->
opt.icon(marker_red_blank);
mark.associatedMapMarker = MapViewFragment.this.mMap.addMarker(opt);
coyttl said:
Hey all -
I'm getting into an area I hadn't gone before - drawable XML files, and am stumped, since what I think should work is giving me an exception.
Some background -
I have multiple images, used as a background. Then there are a couple other images used for foreground symbols. (These are for markers on a map.) So, for example, I have 5 different colors, and 5 symbols. Instead of creating 30 different markers (5 symbols + blank) I figured I could include the resources, and then use a list-layer to show the symbol on top of the marker. Would save some room when the APK's created.
So, I made a simple test, like this. XML file is simply named "marker":
Code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="34dp"
android:drawable="@drawable/marker_green_blank"
android:left="0dp"
android:right="20dp"
android:top="0dp"/>
<item
android:bottom="20dp"
android:drawable="@drawable/marker_symbol_int"
android:left="3dp"
android:right="19dp"
android:top="4dp"/>
</layer-list>
Oddly, when I go to use this, I get an exception when it's tried to be used:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
Any thoughts? I obviously missed something - probably something idiotic, since I do that a lot.
Or, is there another way to combine two images/res's together?
--Mike
Edit:
I forgot to add - this is how I'm creating the 'icon' to add it to the map. The second line is map-specific code and may not really apply here. I'm starting to think that the first line - BitmapDescriptor - is causing the issue:
Code:
BitmapDescriptor marker_red_blank = BitmapDescriptorFactory
.fromResource(R.drawable.marker);
<!-- Code that sets other options using a MarkerOptions object ... -->
opt.icon(marker_red_blank);
mark.associatedMapMarker = MapViewFragment.this.mMap.addMarker(opt);
Click to expand...
Click to collapse
Although I haven't used BitmapDescriptors, I do use layer-list a fair bit to overlay shapes, backgrounds etc.
Looking at the documentation for fromResource...
'Creates a BitmapDescriptor using the resource id of an image.'
It seems that you're not passing it an image, you're passing it a drawable (layer-list in this case).
PicomatStudios said:
Although I haven't used BitmapDescriptors, I do use layer-list a fair bit to overlay shapes, backgrounds etc.
Looking at the documentation for fromResource...
'Creates a BitmapDescriptor using the resource id of an image.'
It seems that you're not passing it an image, you're passing it a drawable (layer-list in this case).
Click to expand...
Click to collapse
Thanks, I'll look into that. I had thought I tried to create a bitmap out of it, but it still gave the error - but since my memory is flaky, I'll do it and see what I get.
Cheers-
Mike
Drawable Button XML resource selector
This technique could be used for animation of XML drawables.
Keep separate drawables in your res/layout folders as usual.
switchable_drawables.xml ( put this in res/layout too)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Focused-->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/active_button"
/>
<!-- Button Focused Pressed-->
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/active_button"
/>
<!-- Button Pressed-->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/active_button"
/>
<!-- Button Default Image-->
<item android:drawable="@drawable/inactive_button"/>
</selector>
coyttl said:
Thanks, I'll look into that. I had thought I tried to create a bitmap out of it, but it still gave the error - but since my memory is flaky, I'll do it and see what I get.
Cheers-
Mike
Click to expand...
Click to collapse
Hey guys -
Thanks for the info. Unfortunately, same error. The code now looks like:
Code:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferQualityOverSpeed = true;
opts.outHeight = 34;
opts.outWidth = 20;
Bitmap icon = BitmapFactory.decodeResource(MapViewFragment.this.getResources(),
R.drawable.marker, opts);
BitmapDescriptor marker_green_blank = BitmapDescriptorFactory.fromBitmap(icon);
Creating options to generate the bitmap, setting desired height and width, and then using the decodeRecource function, creating a Bitmap, then using the .fromBitmap to turn it into a Descriptor. When the GMaps tries to access it, same null error. I may post this over on StackOverflow, since looking at the docuemtation and other examples, this should work.

Custom adapter layout

In my application I use a custom adapter to show a list of items. This is shown in my DashboardActivity. To add user input I want to place multiple EditText input fields and a button to add items. However when I am trying to add EditText input fields or buttons in my DashBoardActivity it will show in every item. This because I am using an custom adapter to show my items.
My question is how can I achieve to use a custom adapter with extra layout items under my custom adapter list?
I tried to illustrate it with a simple example:
Anyone? I don't understand how I can place a button or EditText fields outside my listview. Everything I declare in my activity is getting inside my listview, because of my custom adapter. How can I declare something outside my custom adapter and still using the same activity?
CodeMonkeyy said:
Anyone? I don't understand how I can place a button or EditText fields outside my listview. Everything I declare in my activity is getting inside my listview, because of my custom adapter. How can I declare something outside my custom adapter and still using the same activity?
Click to expand...
Click to collapse
I don't quite understand you. But let's try. The adapter is a "tool" that "adapts" a list that you provide to a listview. So it get the items from the list (ArrayList, Cursor etc) and puts them in the listview according to your implementation. So it just fills the listview. It has nothing to do with the layout. The listview uses a listitem layout for every row. If you don't have the edit text and the button in the list item that you provided, then they won't be in the list view items.
Have you correctly created a layout with a listview, and on top of it (or at the bottom of it) to be your edit text and button?
Thanks for your reply.
Sorry I find it quite difficult to explain. (english is not my native language ).
My layout code:
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/messageList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey"
android:divider="@color/white"
android:dividerHeight="10dp"
android:layout_marginBottom="110dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<TextView
android:id="@+id/message_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:layout_marginTop="10dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/message_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop = "40dp"
android:layout_alignLeft="@+id/message_title"
android:layout_alignStart="@+id/message_title"/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:hint="Title"
android:background="@drawable/gradient"
android:singleLine="true"
android:id="@+id/editText_title"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/message_content"
android:layout_marginBottom="70dp"
android:layout_alignRight="@+id/messageList"
android:layout_alignEnd="@+id/messageList" />
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Message"
android:inputType="textMultiLine"
android:background="@drawable/gradient"
android:singleLine="true"
android:id="@+id/editText_message"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/message_content"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignTop="@+id/editText_message"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
And this (quick example) is what I get:
I don't understand why (In this example) my button is getting placed inside my listview.
I got it working. My problem was that I inflated my whole ActivityDashboard layout like this:
Code:
if(convertView==null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.activity_dashboard, parent,false);
}
And instead I made a new layout for my rows like this:
Code:
if(convertView==null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.row_layout, parent,false);
}
Now its working the way I wanted
Btw maybe you should initialize the layout inflater once in the constructor so you won't need to do it every time getView() is called.
CodeMonkeyy said:
Anyone? I don't understand how I can place a button or EditText fields outside my listview. Everything I declare in my activity is getting inside my listview, because of my custom adapter. How can I declare something outside my custom adapter and still using the same activity?
Click to expand...
Click to collapse
You can use RelativeLayout, place the EditText and button in a LinearLayout ans set the LinearLayout to android:layout_alignParentBottom

Categories

Resources