My question is similar to this
How to stop a GridView from cropping my images?
Iam Using Flowlayout for creating Tag Cloud, I was able to create it, Iam inflating the text view since the text can vary, I have seen all the examples which uses new line has a fixed layout xml. how to implement new line in inflated textview.
It appears that the new line flag on the FlowLayout.LayoutParams object is not modifiable at runtime. You will either need to modify the library itself to add this ability or keep a separate layout file for when you need to inflate a TextView with the newline xml.
If you are looking to break the text across two lines and continue to use the FlowLayout, you will need to inflate two separate TextViews one for the first line and one for the second. You will also need to manually calculated the amount of text you can fit on screen to do this.
Parent layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.customns"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".CustomActivity"
android:orientation="vertical">
<com.example.customns.FlowLayout
android:id="#+id/loadlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
</com.example.customns.FlowLayout>
</RelativeLayout>
Layout to be inflated in the parent
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.customns"
android:id="#+id/buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/blue"
android:orientation="horizontal" >
<TextView
app:layout_newLine="true"
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
layout_newLine is not working in inflated layout, which works if i have all the textview in the parent layout. any suggestion please
Related
I have a layout something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#ff303f"
android:id="#+id/layout">
<ImageView
android:id="#+id/picture"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/cancel"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignRight="#id/picture"
android:background="#drawable/cross_out"/>
</RelativeLayout>
I inflate it using the usual layout inflator service, set up some functionality to the button and an image to the imageview (all images are of same size and aspect ratio).
After that, I add this view to my fragment's layout which is nothing but a ScrollView which is a parent, it has a child linear layout that I call 'map' and simply add it to the map.
Expected : The added layouts should get added properly and I can scroll through it. Actual : If more than 2 are added, the first one gets eaten up. I can see it half or sometimes it is completely eaten up. :\
Any idea whats going on here? Thanks a lot for your time.
EDIT: Here's the layout file I add the inflated layout into:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f6ff45">
<LinearLayout
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#43ff44"
android:orientation="vertical"
android:layout_margin="10dp"
android:layout_gravity="center"
android:layout_marginTop="15dp">
</LinearLayout>
</ScrollView>
I also forgot to mention that after adding 3 or more of these layouts, I realized there's unnecessary empty space in the end. :\
try to define scrollView from Java code like
//Initialize the TextView for vertical scrolling
output = (TextView) findViewById(R.id.textView);
output.setMovementMethod(new ScrollingMovementMethod());
I want to create an application that have 5 buttons in bottom of page, when user clicks
on the any button, the page will changes but 5buttons remain in bottom of page. now i create
this program but when i call setContentView() in other class becuase i call other layout,
buttons removed. have ways for remain 5buttons? or i should create 5buttons again in 5layout?
Thanks
Cheers
Easiest and Simple way is to make a layout having 5 buttons and it in all your layout files
Example
<include layout="#layout/titlebar"/>
Or another way is to use fragments so tht only fragments with change keeping other of your layout stuff same
Edit
OR Use the <merge> Tag
The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another. For example, if your main layout is a vertical LinearLayout in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another LinearLayout as the root for the re-usable layout would result in a vertical LinearLayout inside a vertical LinearLayout. The nested LinearLayout serves no real purpose other than to slow down your UI performance.
To avoid including such a redundant view group, you can instead use the element as the root view for the re-usable layout. For example:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/delete"/>
</merge>
Now, when you include this layout in another layout (using the tag), the system ignores the element and places the two buttons directly in the layout, in place of the tag.
Please go through this link to gain more information about Reusing layouts
From what I can gather, I think you are looking for tabs instead of Buttons. Take a look at this.
Thanks for answer. I read developer link and i add merge and include tag to xml file but the program has errors. Why?
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<include layout="#layout/location"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/button_home"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/home_icon"
android:text="#string/button_home"
android:textColor="#color/text_home" />
<Button
android:id="#+id/button_product"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/product_icon"
android:onClick="Product"
android:text="#string/button_product"
android:textColor="#color/text_product" />
<Button
android:id="#+id/button_places"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/places_icon"
android:onClick="Places"
android:text="#string/button_places"
android:textColor="#color/text_places" />
<Button
android:id="#+id/button_rewards"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/rewards_icon"
android:onClick="Rewards"
android:text="#string/button_rewards"
android:textColor="#color/text_rewards" />
<Button
android:id="#+id/button_more"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/more_icon"
android:onClick="More"
android:text="#string/button_more"
android:textColor="#color/text_more" />
</LinearLayout>
</LinearLayout>
location.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView>
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ"/>
<LinearLayout
android:id="#+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</merge>
what is incorrect?
Thank you
I am fairly new to android programming, but not new to java.
I have been trying to setup a non-trivial view, and I'm having an odd problem. I have designed a layout that horizontally scrolls individual player panels (it's Yet Another ScoreKeeper)
So I created a playermain.xml with the HorizontalScrollView and LinearLayout to contain dynamically added player panels which are a TableLayout.
I want the playerpanel buttons and fields to stretch to consume all available playing space vertically (I also have some goals for horizontal, but let's ignore those for now)
Nothing I tried will get the playing panels to stretch - instead they center, but don't fill. The strange thing is, I tried a quick test, which was to create a flattened version of the xml files (ie. I copied two copies of the playerpanel xml into the main xml. when i simply setContentView to this combined xml I get the view I want.
When I do it programatically, I don't get the stretch, and I must be missing something here. I can also post the combined xml, but I thought it unnecessary, basically when I create a single xml file with Scrolls, Layout and tablelayouts, things stretch the way I want. When I create the Scroll/Layout with one xml file, then add the other items to the top level LinearLayout, they don't stretch.
onCreate:
<----SNIP----->
setContentView(R.layout.playingmain);
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout llayout = (LinearLayout)findViewById(R.id.layout1);
TableLayout tl = (TableLayout)inflater.inflate(R.layout.playerpanel, null);
InitializePlayer(player1,tl);
llayout.addView(tl);
<----SNIP----->
MAINPANEL.XML
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:isScrollContainer="true"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:gravity="center"
>
</LinearLayout>
</HorizontalScrollView>
Individual Panel.xml:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5sp"
android:stretchColumns="*"
android:minWidth="200sp"
>
<TableRow
android:id="#+id/NameRow"
android:minWidth="500sp"
android:layout_weight = "1"
>
<TextView
android:id="#+id/PlayerName"
android:text="PlayerName"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/dbg1"
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</TableRow>
<TableRow
android:id="#+id/scoreRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight = "1"
>
<TextView
android:id="#+id/currentScore"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="10"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="#+id/dbg2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
/>
</TableRow>
<TableRow
android:id="#+id/TableRow17"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight = "1"
>
<Button
android:id="#+id/subtractButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="-"
/>
<EditText
android:id="#+id/scoreEntry"
android:layout_span = "2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number" />
<Button
android:id="#+id/addButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="+"
/>
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight = "1"
>
<Button
android:id="#+id/plusOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="+1" />
<Button
android:id="#+id/plusFive"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="+5" />
<Button
android:id="#+id/minusOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="-1" />
<Button
android:id="#+id/minusFive"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="-5" />
</TableRow>
</TableLayout>
When I add the inflated objects, I needed to specify the proper parent object instead of null. Once I did that, all xml properties made it into the views.
I was able to confirm/debug this using the hierarchy viewer.
TableLayout tl = (TableLayout)inflater.inflate(R.layout.playerpanel, null);
needed to be
TableLayout tl = (TableLayout)inflater.inflate(R.layout.playerpanel, llayout, false);
Akameswaran,
The reason for this discrepency is simple: XML are "statically" defined with dynamic results based on those static paramaters. That is, XML provides an initial inflation using initial state defined by those XML files. The code, allows changes to be made at runtime and is treated by the engine as "dynamic" even if the positions and dimensions are constant. This leads to some very interesting relationships for those who are unfamiliar with Android.
When you have one XML file, during inflation the rendering engine knows what all of the objects are and how they relate to the view hierarchy. However, when you split the XML file, it doesn't know that the properties of the child views are dependant upon the properties of the parent views, unless you tell it so in code. This is, in essence, because the computer does exactly what you tell it to and freaks out if you don't tell it enough. It will "guess" but almost always guess incorrectly. Like HTML, Android is made to work as much as possible providing A RESULT if it can, even if it is the wrong one.
Android provides several ways to overcome this: OnLayout() allows you to dynamically apply positioning to any activity or view object. OnMeasure() allows you to redefine dimensions. OnDraw() allows you to dynamically draw or inflate the object. By overriding these functions, you can get the behavior back to what you need and still keep you XML modular, like you so desire. Since you are using standardized API controls, you'll probably want to override the functions directly in the Activity and save yourself a lot of trouble.
Hope this helps,
FuzzicalLogic
Lets say that I have a simple XML layout such as the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/my_container"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/leftContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Col A - Text 1"
/>
</LinearLayout>
<LinearLayout android:id="#+id/rightContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Col B - Text 1"
/>
</LinearLayout>
</LinearLayout>
Then I want to add a TextView to the rightContainer LinearLayout. I am currently doing this, unsuccessfully:
LinearLayout container = (LinearLayout) findViewById(R.id.rightContainer);
TextView textToAdd = new TextView(this);
textToAdd.setText("Col B - Text 2");
container.addView(textToAdd);
I have looked at LayoutInflater, but am not sure how I would use it here. Any help would be appreciated, thanks! If I try calling setContentView(container), I receive a Force Close error.
Don't call setContentView(), if you are using findViewById() then that view is already inside of your currently set content.
Adding views works fine. All a layout XML is, is a description of the views to create and add to the hierarchy. Make sure you are passing the correct layout params when adding a view -- here since container is a LinearLayout, you want a LinearLayout.LayoutParams.
You don't say in what way your code is "unsuccessful" so it is hard to help further.
Also you can use hierarchyviewer to look at what is actually going on in your view hierarchy.
I have made myself a custom LinearLayout by the name of com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget that hosts a couple of spinner widgets. This custom LinearLayout inflates the following XML layout (You might not need to look at this too carefully but it's here for completeness):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Min value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_min_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/to_text"
android:text="to"
android:textSize="14sp"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="0">
</TextView>
<!-- Max value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_max_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1" />
I have placed one such object in the layout for one of my activities like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include layout="#layout/search_form_section_generic_top"/>
<include layout="#layout/search_form_section_car_specific"/>
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
<include layout="#layout/search_form_section_advanced_options" />
</LinearLayout>
The problem is that my app force closes immediately upon startup. I've checked by putting breakpoints in my custom LinearLayout that none of my custom code is even being run yet. Furthermore, if I copy-paste the layout code for my compound widget in place everything works, which indicates to me that I probably haven't left any important XML attributes out. What could be going wrong?
I fixed it by making the LinearLayout XML element in the widget layout into a merge instead, and moved all of the layout parameters out of the widget XML file and into the activity XML itself, thus replacing
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
with
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
If someone could tell me why this worked, it might help me and others doing it again, and you can take credit.
because you must specify the width and height of every view you use in you xml?