Android add partial layout inside layout - android

I have this fragment layout where some content needs to be animated (like showing/hiding input field).
Now I found out I could manage this by using a fancy animation (by using <item name="android:animateLayoutChanges">true</item> on a linear layout).
The problem I have here is that I need to add al content programmatically..
For example
LinearLayout timesheetsWrapper = (LinearLayout)inflatedView.findViewById(R.id.new_timesheet_wrapper);
EditText timeSheetName = new EditText(getContext());
timeSheetName.setHint("Name");
timesheetsWrapper.addView(timeSheetName);
Now image I need to add like 10 GUI components and style each one of them programmatically, which is a bit of a hassle to me.
Is there a way that I could add a partial layout .xml file into the 'wrapper'?
For instance:
LinearLayout partialLayout = ...??
timesheetsWrapper.addView(partialLayout);
Where the partialLayout.xml contains all the GUI components.
Already searched around but it all got a bit confusing to me!
I hope my question is a bit clear, if not (or additional info is needed) I'am happy to edit/provide.
Thanks in advance, regards an android noob

Related

Add un text onto an image programmatically using Kotlin

I have a image "myXmlImage" in my .xml file
In the .kt file, I want to paste another "newImage" image as well as a little text "newText".
myXmlImage.setImageDrawable(newImage.drawable)
myXmlImage.imageMatrix = newImage.imageMatrix
So far it has been working very well and the new image is in place.
I'm completely stuck on how to paste the little "newText" into it
Any ideas will be greatly appreciated, thanks in advance.
Perhaps there's more than what you've described here. But if those are really your only specifications, then what you're asking for is really easy.
I'm assuming that your xml file has a root of ConstraintLayout. If it doesn't, then you're going to either want to change it or at least wrap your ImageView inside of a ConstraintLayout.
Use the layout editor to place a TextView inside of your ImageView (NOT AS A CHILD; when I say "inside", I mean spacially inside). If you haven't used the layout editor very much, it might take you a couple of tries to place the TextView inside. Don't just drag it into the ImageView as if it were a child. Drag it and drop it under the ImageView inside of the ConstraintLayout and then use little circles on the sides to attach it to the sides of the ImageView. Don't forget to set the text of the TextView as "".
(I'm not actually sure if this step is necessary because I don't know what the default background of a TextView is, but I do it out of habit.)When you're done placing that TextView inside of the ImageView, go the code of your xml file and use android:background="#00FFFFFF" (or maybe somewhere in your project you have either a transparent background drawable or a transparent #color that you can reference by name).
I'm sure you know the rest. Just give that TextView an Id like newtext and inside of your activity retrieve the view: newText:TextView=findViewById(R.id.newtext). And then set the text: newText.text="new text".

How to make segmented seekbar/slider look like following?

I was wondering if anyone can provide hint or source to achieve following slider widget used in "Circle – Who's Around?" This is the first time I have ever came across this and I am not sure what to exactly name this widget.:
I was thinking of using custom seekbar background to do this but, I am not sure how do I figure out exact pixels that the seekbar will reach of next step. Since, that will be independent to devices. In my case I am planning to use images, rather than the indicators.
Please don't point to this link http://www.mokasocial.com/2011/02/create-a-custom-styled-ui-slider-seekbar-in-android/ because this is not what I want to achieve. They seem to have used static image footer to show D,W,K. I have tried that app and it doesn't even step to the exact dots or D,W,K. I have looked at AT&T Android Slider Controls but, they don't seem to provide any source for it. I have found some iOS devs achieving that but, I don't really understand obj C code in order to achieve that in Android.
This is just a seekbar with a custom thumb and background. You could use a 9patch for the background so it fills nicely and just set them in your styles
Following #Milanix answer using the library at https://github.com/karabaralex/android-comboseekbar here it is a minimum example code that worked for me:
<com.infteh.comboseekbar.ComboSeekBar
android:id="#+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
custom:color="#000"
custom:textSize="12sp"
custom:multiline="false"
/>
Then in the Activity
private ComboSeekBar mSeekBar;
List<String> seekBarStep = Arrays.asList("All","1","5","10","20");
mDistanceSeekBar.setAdapter(seekBarStep);
This will create a black segmented seekbar using default drawables. If you need to add some customization have a look at ComboSeekBar.onDraw(), CustomDrawable.draw() and CustomThumbDrawable.draw().
This project is all but finished but still a solid starting point.
#Giulio thank you for your post, I have the same problem as Ron Eskinder.
I heve fixed it by removing :"custom:color" , "custom:textsize" and "custom:multiline" in xml file. then in Java I put this:
mSeekBar = (ComboSeekBar) findViewById(R.id.seekbar);
List<String> seekBarStep = Arrays.asList("All","1","5","10","20");
mSeekBar.setAdapter(seekBarStep);
Hope this will help

Android package/class name in layout XML?

This is probably a trivia question, but why are there package/class names in some people's XML layout files?
(please don't downvote this question if it is something trivial, i don't even know what this is called, so i couldn't look it up).
i was looking at a tutorial, and i saw something like this (in "sample.xml"):
<com.tutorials.foo
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- some buttons and views here -->
</com.tutorials.foo>
My questions are:
1) i'm assuming that "foo" is a custom view? say, like you want to extend TextView with your own version of TextView?
2) what is this pattern/technique even called?
3) what would be the advantages of using this method?
Thanks so much in advance!!!
Yes, <com.tutorials.foo .../> is a custom view.
Calling it will be as same as others.ex:
Foo foo=(Foo)findViewById(R.id.foo);
I assume you mean creating layout static(.xml) or dynamically with code. xml layout would be in advantage when you know that you will use this layout in the program and will not change its format. Of course you can add to it or edit it with code later on. It is also in advantage for readabilty.

Best way to program buttons/images

I am designing something that will have a homepage close to the Google+ android app.
I'm having trouble figuring out the best way to do this? Should I create a button with text and set the background as the image? Should I create an image with the text already programmed in the actual picture or should I program the text and picture to be buttons.
Any suggestions from you guys on past projects?
You can use a Button with text to whatever you like and then place the image for that button above the text (using android:drawableTop)like so:
<Button
android:id="#+id/imageButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Photos"
android:drawableTop="#drawable/button_image" />
replacing buttton_image with your actual image. If you want the image in a different position (i.e. below text etc) use:
android:drawableLeft
android:drawableRight
android:drawableBottom
This would be how I would and do do it...
Since I am a newbie in android development I may be wrong but I suggest why not use a Grid View with each grid item haaving a textview and imageview.
My suggest to layout:
If you want to try something new in Android 4.0 , you can try GridLayout to layout , it can reduce the complicate of the nested layout , check out the blog about GridLayout:
http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html
You should write Buttons this way: http://developer.android.com/resources/articles/layout-tricks-merge.html
I normally use RelativeLayout but this is not important.
Write a class myClass extends RelativeLayout and inflate the XML with
LayoutInflater.from(context).inflate(R.layout.myCustomLayout, this, true);
I think this is best practice by Google.
use a regular button and set the android:drawableTop="#drawable/icon_...." value
Having the text burnt into you image is most certainly not the way to go.
One option for you is a GridView. You could also do this through a combination of Image button and scrollview. In my opinion, GridView is best and involves the least amount of code with most flexibility.
Remember that you can replace the button's background image with a state list drawable.

Scrollable TextView in Android Widget

I would like to create an android widget with a scrollable textview.
The solutions given to this question
Making TextView scrollable on Android
cannot be applied because it is a widget:
1.
This
findViewById(R.id.textview).setMovementMethod(new MovementMethod());
does not work, since findViewById is not available in AppWidgetProvider but only in Activity.
2.Putting a ScrollView around the TextView also does not work, because I get an
InflateException:
android.view.InflateException: Binary XML file line #23: Error inflating class ScrollView
Can anybody give me a hint, how to make a TextView in a Widget scrollable?
I solved this problem by putting the text view inside ListView with single node, which is supported in App widget.
http://developer.android.com/guide/topics/appwidgets/index.html#collections
It looks like this is not possible.
More on this can be found here: http://code.google.com/p/android/issues/detail?id=9580
and here:
How to make a scrollable app widget?
So, probably it is possible to do make appwidgets scrollable in the HTC sense environment but not for normal android environments.
A way around this is to add buttons that go up and down in a list.
I have been working on this with two buttons which increment and decrement through an array. Just having trouble accessing the global variables. Did you make any headway on this?
Another solution is:
Add to textview any web link - for example: www.google.com.
Setting text value with HtmlCompat.fromHtml method:
textView.setText(HtmlCompat.fromHtml("some text" + "\nwww.google.com", HtmlCompat.FROM_HTML_MODE_COMPACT));
After that vertical scrollbar is appeared.
But it's not elegant and full solution. It's temporary workaround maybe...
The complete full solutiion is bat-el-g 's answer - with adding ListView.
Current marked solution (which just tell: "it's not possible") - is wrong.
In mainactivity.java:
after code text view:
TextView txt = (TextView) findViewById(R.id.textView id);
Enter this code:
txt.setMovementMethod(new ScrollingMovementMethod());
and you will be ok.

Categories

Resources