Question about android:weight="" - Java for Android App Development

So im following this guys tutoirals:
http://www.youtube.com/watch?v=7OSfxz0iWkI&list=PL4695D1A275CDEE4A
(Watch 1.12 and 1.13)
I understand all of those concepts he used but Im still confused about the android:weight and android:weightSum functions (if that's what you call them). Could someone give a explanation on what they mean?

TwilightLoz said:
So im following this guys tutoirals:
http://www.youtube.com/watch?v=7OSfxz0iWkI&list=PL4695D1A275CDEE4A
(Watch 1.12 and 1.13)
I understand all of those concepts he used but Im still confused about the android:weight and android:weightSum functions (if that's what you call them). Could someone give a explanation on what they mean?
Click to expand...
Click to collapse
It defines how the space which is left after calculating all view sizes is used.
Code:
<Button ...
android:layout_weigth="1" />
<Button ...
android:layout_weight="1" />
The space left over is divided by two and every view will get the half of it.
Code:
<Button ...
android:layout_weigth="1" />
<Button ...
android:layout_weight="2" />
The second button will get twice as much space as the first one.
Weightsum is not necessary. It can define the sum of all weight attributes.
Notice that this just splits the space which is left over.
If you want one View to get a third of the total space and the other two thirds of the space, use this:
Code:
<Button ...
android:layout_weigth="1"
android:layout_width="0px" />
<Button ...
android:layout_weight="2"
android:layout_width="0px" />

Nikwen hit right on the money. As he said, weightsum isn't necessary but is used to define the accumulative weight of its child views. Then each child of said parent view,will have a weight defined to it, adding up to the parent's weightsum. Basically its saying, take up this percentage of parent view.
Sent from my SPH-D710 using Tapatalk 2

Related

Menu Inflate XML problem

Hi, first post here & I did search for an answer first
I'm trying to add a menu to my app, in my onCreateOptionsMenu method I can use
menu.add(0, 1, 0, "Text1");
menu.add(0, 2, 0, "Text2");
etc
It works OK & when I press the menu key it pops up, the onOptionsItemSelected method correctly detects the item selected.
If I try to use the MenuInflater on an XML file, it does not work. When I press the MENU key, it pops up a menu with blank buttons - no text or icons & the number of buttons corresponds to the number of ITEM entries in my XML. The XML looks valid .
Any ideas where I'm going wrong. I would paste the code, but the forum says I cant until I've made more posts.
Thanks.
Still new to this myself, but in my recent menu tries, i had to put in a TextView into the xml to tell it what to say.
<TextView
android:text="@string/menu_button1" />
And define the name you want on that button under the strings file.
<String name="menu_button1">New Game</String>
I may be completly off so take what Im saying with a grain of salt lol
Hmmmm, tried that & it didnt work either. Thxs for the suggestion.
Not a problem the book i have at home had me do some menus, i could post the books complete code later maybe i missed something with what i told you. Its in pdf form so it wouldnt be a big deal. Ill be getting home in around 3 hours.
Maybe its a combination of button and textview together to actually make it work. It was pretty easy last time i did it so im sure its just a small detail being missed. I've slowly learned eclipse isnt able to catch every single error.
is your menu's xml file in the res/menu folder? if not it prob wont work
Yup res/menu
Here is the code for the menu from my book.
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="30dip"
android:orientation="horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="@string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label" />
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_label" />
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_label" />
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_label" />
</LinearLayout>
</LinearLayout>
And the strings file
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
</resources>
Thats pretty close to what I'm doing, but not exactly. The main difference seems to be your using a LinearLayout & my code uses RelativeLayout. OK, I'll try your code EXACTLY when I get home later. Thanks.
When you say menu, you are referring to the options that pop up when you press the hardware menu button, right?
I ask because... what does that linearlayout code have to do with a menu?
Are you implementing this function?
onPrepareOptionsMenu(Menu menu) {
MenuItem option 1 = menu.findItem(R.id.opt1);
...
}
I thought the Layout may affect how the menu text & icons were displayed & was why I couldn't see them.
No I dont implement onPrepareOptionsMenu, only onCreateOptionsMenu and onOptionsItemSelected - I'd post my code but the forum wont let me yet
I'm following the example from Android Developers "Creating Menus" which only has those 2 methods detailed.
OK guys I've fixed it !!! You'll never guess what it was.......
.... OK, I'll tell you then
In my menu.xml, I had the menu namespace xmlns URI as https not http, I had copied & pasted this from another website. Changed it back & we are working again.
Thanks for the assist - Thanks have been awarded.
Oh man thats a killer...I've had that happen to me a couple of times already
Good example android.text instead of android:text...lol
Glad to here its working though,good luck with the rest of your app.
lol - I either need more or less caffeine - not sure which.
Thanks bud.

[Q] addind a button to a secondary view is crashing my program

I am making a game that has a main.xml view (the default one that the game screen is made from. But I have a button for "help" that starts a new view which is just text explaining how to play the game. But I want to add a button to the help menu that will take the user back to the main game. Only problem is when I add a button to the xml file for it, it always crashes as soon as it is called. All the other buttons work fine in main.xml but I cant get the ones in the help.xml to work. Any suggestions?
The way that android's view hierarchy works is actually kind of complicated, and is one thing that I still at times struggle to grasp.
So when you press the help button, and help.xml is supposed to show up, it crashes?
yeah, heres my xml code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidadding="10dip" >
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text goes here" />
</ScrollView>
But it crashes if theres a button inside of it
are you using the new layout in the same Activity or a new one? If the same, try out making a new Activity class (remember to add it to manifest!), it might be a lot easier to then determine your problem if it's separate.
We could do with a logcat output of the crash to help determine what's wrong

Two simple Android SDK things I need help with

I actually yesterday spent some more time with Eclipse Android SDK yesterday and learned how to: center buttons, add buttons, add admob, etc. but I still need to know 2 things:
How do I put buttons side to side?
How do I add sounds to buttons?
P.S. - I am not sure if this is the correct place to post this. I was just spending lots of time trying to the above things out and couldn't, so I figured why not ask the developers pros here at Xda.
I prefer to use RelativeLayouts instead of LinearLayouts.. I feel that LinearLayouts are much more difficult to work with, and are much less intuitive.
When using a RelativeLayout, you could do something like this:
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button2"
/>
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
This would put button1 to the right of button2.
Additionally, if you wanted some space between them, you could specify this in button1:
android:layout_marginLeft="10dp"
To add sounds to a button click... explore the MediaPlayer class.
In the onClickListener of your button, you would have something like media.start()

[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] Where is default app info kept?

When I go to view a video from a webpage or a news app I am always presented with a list of potential players (some of which I don't even have). I check the little box at the bottom of the list that says "always use this" or whatever, and then select RockPlayer. It all works fine but the Nook doesn't seem to remember this choice. It asks each time. Very annoying.
Does anyone know where this default app information is stored? A database file I could look at perhaps? Maybe something is not being set properly. I've made other default selections for other situations and they seem to take. Or maybe it's a RockPlayer issue?
<preferred-activities> inside /data/system/packages.xml
Always in the last place I look
So... this is what is there, but it's repeated over and over (and over--like for every time I've done it):
Code:
<preferred-activities>
<item name="com.redirectin.rockplayer.android.unified/com.redirectin.rockplayer.android.OpenRockPlayerActivity" match="600000" set="5">
<set name="com.estrongs.android.pop/.app.BrowserDownloaderActivity" />
<set name="com.estrongs.android.pop/.app.PopVideoPlayerProxyActivity" />
<set name="com.opera.browser/com.opera.Opera" />
<set name="com.alensw.PicFolder/.StreamingActivity" />
<set name="com.redirectin.rockplayer.android.unified/com.redirectin.rockplayer.android.OpenRockPlayerActivity" />
<filter>
<action name="android.intent.action.VIEW" />
<cat name="android.intent.category.DEFAULT" />
<type name="video/mp4" />
</filter>
</item>
I don't see the BBC News app mentioned in a similar vein, but it behaves in exactly the same way. Nothing in the code above jumps out at me as a novice, but the fact that there are many, many entries in the file all the same does not seem good.
Ideas?
Well, the easiest solution is to just gut the preferred-activities:
Code:
<preferred-activities>
</preferred-activities>
Your Nook may ask you once about a few intents, but that should be it.
It's possible that packages.xml got corrupted.
Also, depending on how a file gets opened there may be different filters for mimetype vs. path filters.
If you are still having problems, narrow it down to exactly how you are trying to open the file.
Use logcat and see what intent generates the picker dialog.

Categories

Resources