Displaying a TextView immediately after another - android

I am a first time developer for Android, so you can say I've been learning as I was developing. For most of my code that doesn't have to do with the XML layout, I had no problem patching my rookie mistakes. With that said, my rookie mistakes has caught up to me in regards to two TextViews when I initially designed them with the GUI interface designer (my major rookie mistake).
My display_city tv and display_today_date tv seem to have a symbiotic relationship with each other. Removal of either one would crash the app. They seem so dependent on each other that changing each other's positioning is impossible (at least from the myriad of things I have tried such as setting layout margins).
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip"
android:layout_above="#+id/display_today_date"
android:layout_below="#+id/get_data"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/display_today_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/display_pollen_type"/>
My question is - how do I simply position display_today_date immediately after my display_city? When I first started this Android app, I relied a lot on the GUI builder. That was my first rookie mistake, which resulted in this symbiotic relationship I explained.
Currently this is what my app looks like:
I have tried changing display_today_date's layout to android:layout_below="#+id/display_city. This results in a crash. I checked logcat, but it did not give me relevant information to the reason of the crash within the XML file.
P.S. get_data is my TextEdit box.

You already have the city to show above the date with the line android:layout_above="#+id/display_today_date". You can't have 2 views in a relative layout each reference the other, or it won't be able to figure out what to do. If you don't want to put the city above the date, delete that line then add the code to place it where you want.

You could use a LinearLayout with the orientation set to horizontal. That way there is no reference to another view. So if you delete one the other one won't cause the app to crash.

Related

Inflating all layouts from res-folder

There are 100+ layouts in a resource folder on android, I can go through them one by one and look at the preview in android studio. They all utilise code like:
tools:text="text for preview"
tools:visibility="visible"
Is there a way to inflate all those one after another in a list/linear layout? And upgrade the tools:xyz attribute to be shown as the real android:xyz attribute in this view
Context&Purpose: To show them in the app under a debug menu dedicated specifically to show available layouts. This is to communicate with both other developers and people without android studio.
I want to avoid doing it manually as it needs to be maintained and that can end very badly for debug functionality such as this. (And that it is a lot of layouts does not help either of course)
After writing it down it feels unlikely, but maybe someone out there have a nice angle on this?
Depending on the complexity of each layout, inflating a hundred of them at once could be quite taxing. You may run out of memory, or get a fair bit of jank. If you are OK with this, then I would say try using the include tag in your debug screen layout
A simple/naive implementation would be something like
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/A"/>
<include layout="#layout/B"/>
<include layout="#layout/C"/>
<include layout="#layout/D"/>
...
</LinearLayout>
I probably wouldn't recommend doing this, but it might get the job done.
As far as the second part of your question, using tools values as actual values, I don't think there is a way to accomplish this. In fact the tools values are not even available during run time as they are stripped out during the build process.

Android auto reduce data table

I am new to Android, and I have a table I made using:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/question_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical"
android:layout_below="#+id/textView7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/linearLayout">
</TableLayout>
added rows to it and it perfectly shows the table. Now I want to implement a search box that reduce the result as I type, I am not sure how to do that. I tried searching, maybe I am missing the term to search. Can someone please add a link to it and/or tell me some technique, because re-populating the table everything someone types is like a bad thing to do.
PS: Data is sent from API as JSON, and I own the API.
Displaying large information in TableLayout is bad practice. You can use ListView. Because, every time when text changed in your search box, you need to clear all views in TableLayout, then you need to create rows that appropriate your search filter and then you need to add these rows to TableLayout again. Creating many views and drawing them on screen is hard process for Android and it can crash your app.

What is the ButtonBarLayout and how should we use it?

When I developed, I found a new widget called android.support.v7.widget.ButtonBarLayout unexpectedly. I tried to search it on the internet, but nothing was found, even on the official development documents site.
In the meantime, I found two ButtonBarLayout when I search ButtonBarLayout everywhere in Android Studio, one is android.support.v7.widget.ButtonBarLayout and the other is com.android.internal.widget.ButtonBarLayout. I tried to read source codes of both, I found that they are the same except package name. So I thought maybe android.support.v7.widget.ButtonBarLayout came from com.android.internal.widget.ButtonBarLayout after the internal ButtonBarLayout was through tests and released. At the same time, ButtonBarLayout is inherited from LinearLayout.
But there are some question:
What can we get from ButtonBarLayout literally and how should we use it?
I noticed the variable of private boolean mAllowStacking. When it changes, orientation of this layout would be changed. But I didn't really understand what it is used for.
So does somebody know ButtonBarLayout well?
P.S.: I used Android Studio of 2.0.0 Preview 4 and Gradle Plugin of 2.0.0-alpha3 and Android Support Library of 23.1.1 and Platform-tools of 23.1 and Build-tools of 23.0.2.
As others pointed out, the class description tells exactly what it is: an extension of LinearLayout that automatically switches to vertical orientation when it can't fit its child views horizontally.
I might add that this was clearly done to fit with the material design specifications about dialogs. They make a distinction between side by side buttons and stacked buttons. See for example:
Side-by-side buttons are recommended when the text of each label does
not exceed the maximum button width, such as the commonly used
OK/Cancel buttons.
While you should go for stacked buttons when the single button is too large, or there's not enough room for both:
When text labels exceed the maximum button width, use stacked buttons
to accommodate the text. Affirmative actions are stacked above
dismissive actions.
So, one possible use of this class, is when designing your own dialogs. For example, AlertDialog and AlertDialog.Builder offer internal support for dialogs with buttons, but sometimes you just want to subclass DialogFragment or AppCompatDialogFragment for a better control.
There, it might be useful to setup a bottom button bar that follows the design guidelines, and have full control on the buttons (like enabling and disabling, things you can't do with an AlertDialog AFAIK).
The source code describes ButtonBarLayout as follows:
/**
* An extension of LinearLayout that automatically switches to vertical
* orientation when it can't fit its child views horizontally.
*/
So, in essence, it is nothing but a smart LinearLayout which manages auto-switching orientations based on available space on screen.
The same ButtonBarLayout.java file describes mAllowStacking in comments as follows:
/** Whether the current configuration allows stacking. */
Source Code Here
You are right first of all. ButtonBar layout does not seem to be featured anywhere in the official Android documentation. I tried myself to search about it, but to no avail. However I have found some information which defines what is a ButtonBar layout and when to use it. Hopefully this will help you.
Most tutorials use the Buttonbar layout in a dialogbox or at the bottom of a screen to confirm or decline an option. The image below is a visual representation of how the ButtonBar layout has been used in a screen.
The screenshot above has the following layout xml:
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button01"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Show" />
<Button
android:id="#+id/Button02"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Change" />
</LinearLayout>
<EditText
android:id="#+id/myView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
So essentially what Android is doing here is simply creating two buttons next to each other in a LinearLayout with each button having the match_parent parameter set to the width. Hence each button takes half the size of the screen. Android have actually taken away the hassle of creating seperate buttons and positioning them correctly to fit different screens, by creating a simple widget handling this altogether.
As with the support library, Android have implemented this for developers using an earlier API. It is normal for them to use the support library for this purpose.
Hope this helps :)
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/com/android/internal/widget/ButtonBarLayout.java
Looking into the code, I think it's a LinearLayout for buttons (duh). You can probably look at it like the Dialog buttons divided by a vertical spacer: | . AllowStacking will change the orientation to vertical and the gravity to the right instead of bottom. I should try it out to give a better answer
ButtonBarlayout is not featured anywhere in the official Android documentation.
it is used for auto-switching orientations according to the space.
Regarding your question:
How should we use it?
I guess it is undocumented because it is not stable yet.
It just popped up because this long lasting complaint originate from poor ROM modification by device vendor.
https://code.google.com/p/android/issues/detail?id=78377
See #270 for the resolution regarding classpath and why all classes inside .internal. were made public.
And nope even that fix a lot of bugs from poor ROM modification are still out there (in lots of device of well known brands). The issue is soon declined by project member.
I don't think we should use it just yet until the document show up.
Just my $.02 though.
Just to add to the other answers, if you guys want to check the orientation of a ButtonBarLayout you should check the orienation AFTER the value has called on measure.
In other words (Kotlin):
buttonBarLayout.post {
val orientation = buttonBarLayout.orientation
val height = buttonBarLayout.measuredHeight
}

Android programming: Adding TextView above pane

Up until 12 hours ago I had never touched Android programming or even XML. As such my question may appear incredibly stupid.
Basically my "app" (if you can call it that) consists of a tabbed fragments with placed immediately below the "app_name" of my app.
What I would like to do is to add some STATIC text immediately below the app_name and (at the same time) immediately above the tabbed fragments -- we are talking about a SINGLE text "element" just be clear. What I have tried so far has been.... unsuccesful. Basically, I thought it would be as follows:
My main.xml currently looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/TextView01"
android:layout_below="#+id/AnalogClock01"
android:layout_width="wrap_content"
android:text="#+id/TextView01" android:layout_height="wrap_content">
</TextView>
</LinearLayout>
where I believe the TextView component would be all the XML required to add the static text-field I want. Subsequently I add:
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("Additional information");
to my MainActivity.java file immediately after calling super.onCreate(savedInstanceState) and my naivity begs me to belive that it should work, but clearly it doesnt (in fact the app crashes when loading it into the emulator).
As this does not work the purpose of my post is to hopefully get one or more of you to offer a solution to "fix" this.
Thanks
Subsequently I add: ... MainActivity.java file immediately after calling super.onCreate(savedInstanceState)
If you're doing that, you're going to have a problem.
You need add
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("Additional information");
after setContentView(R.layout.main);
First you need to give your Activity something to display (a UI) then you can modify the components that make up that UI.
Otherwise findViewById() will return null and you will end up with NullPointerExceptions
Some other things to note:
android:layout_below="#+id/AnalogClock01"
You don't have a AnalogClock01 defined in that layout. If it is defined somewhere else though, you might not crash your app, but things probably won't work as expected.
Even if you did have AnalogClock01 in that layout, you do not access it with #+id/ since you're not assigning an id, but accessing it. Use #id/ instead.
And
android:text="#+id/TextView01"
is also an issue.
To reference Strings from your res/values/strings.xml file, use #string/string_name No + either, and definitely not an id.
You should try to make the activity have multiple layouts in the one activity.
This may help: http://developer.android.com/guide/topics/ui/declaring-layout.html#Position
Also try the .onStart() method instead, although I don't exactly know if it will change anything.

Long text goes to next line on ICS, but not on GB and below

The first time I noticed this was with AlertDialogs putting the entire message on the first line, even when I specified a new line("\n"). On ICS it displays the correct way, but for the life of me, I couldn't get it to work on GB.
Recently I've run into it again. I don't see any reason for it working fine on ICS but not GB and below.
EXAMPLE Project
Heres an example project with a textview, alertdialog, and two standard textviews.
https://github.com/T3hh4xx0r/Text-Example
EDIT
Heres the original question I asked. Seems the problem is more than I originally noticed though.
Android AlertDialog not displaying entire setMessage on certain devices
/EDIT
Here are visual examples of what I mean.
Even specifically setting multiple lines for the textView, the text is still one line, but with extra blank lines below.
Here is the layout that the alertdialog is using to create your view:
<!--
This layout file is used by the AlertDialog when displaying a list of items.
This layout file is inflated and used as the TextView to display individual
items.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/primary_text_light_disable_only"
android:gravity="center_vertical"
android:paddingLeft="14dip"
android:paddingRight="15dip"
android:ellipsize="marquee"
/>
As you can see the ellipsize is set to marquee, so I don't believe it was ever written with the intention to allow multilines.
There is a bug open at the moment that ellipse dots are never shown: http://code.google.com/p/android/issues/detail?id=10554
Therefore it is acting correctly.
If you want it to go onto multiple lines, create your own layout file and pass that to your dialog, that way you have more control anyway.

Categories

Resources