Dynamically defining how an icon is drawn - java

I want to have a "comments" button on my Android e-reader app that displays the number of comments currently posted inside the icon... so basically I want a comment bubble with a number inside it.
I could have multiple drawables, each being the bubble with a different number inside of it, and use a switch statement to choose which one to load each time based on the int number_of_comments field of the element being displayed. This approach seems a little wasteful though, and in any case I have a feeling there's a more elegant way to do it.
Any ideas?

You can do better. You can have a textview on top of the image view and keep updating its value everytime a new comment is added. You can define the overlap in xml like below and adjust your code logic accordingly to increase the comment count. For now I have just set up a dummy text Hello to show on top of the ImageView. You can add your comment count using the TextView's setText method.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImageSouce" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
Hope this helps...

check out this 3rd party created widget
Android ViewBadger
You can use it to create the little number bubbles that you are looking for I think. This gives you the benefit of not having modify your layouts so much to achieve what you are trying to get.
Here is the sample code to apply a "badge"
View target = findViewById(R.id.target_view);
BadgeView badge = new BadgeView(this, target);
badge.setText("1");
badge.show();

Related

Add padding to the left of NumberPicker items

How do you add padding to the left of NumberPicker items, resulting in something similar to the picture below.
I was able to add whitespace using RelativeLayouts, but there are issues with that. (mainly that the added RelativeLayout doesn't detect scroll gestures)
I think I need to extend NumberPicker and do something, but I'm not sure what.
Here is my NumberPicker xml:
<NumberPicker
android:id="#+id/pars_left_numberPickerID"
android:background="#color/white"
android:descendantFocusability="blocksDescendants"
android:elevation="10dp"
android:gravity="center"
android:layout_height="190dp"
android:layout_width="90dp"
android:orientation="vertical"
android:theme="#style/numberPicker"
android:visibility="visible" />
`android:paddingLeft="10dp"`
Just type padding and explore ther options.
After giving up on the default NumberPicker, I was able to get this working by importing and modifying this 3rd party NumberPicker: https://github.com/Carbs0126/NumberPickerView
This allowed me to create a paddingLeft attribute that the number picker will respect.
Replace the width being statically set with android:layout_width="90dp" (the reason why it even looks wonky) with wrap_content and you might not be facing this home-made problem. Besides, Widget.Material.NumberPicker can be styled to some degree.

Android make layout and use it again [duplicate]

This question already has answers here:
How to include layout inside layout?
(6 answers)
Closed last year.
I have my main activity. Inside this main activity I want to create an unknown number of layouts that each one of them include one button.
I want smart way to do it - make the layout one time and than use it a lot of time.
What is a good way to do it?
Thanks
Make One Layout Resource File Using Below Code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="#dimen/_100sdp"
android:layout_marginTop="#dimen/_15sdp"
android:gravity="center"
android:textColor="#color/black"
android:textSize="#dimen/_13sdp" />
<Button
android:layout_width="match_parent"
android:layout_height="#dimen/_35sdp"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginTop="#dimen/_20sdp"
android:layout_weight="1"
android:gravity="center"
android:text="Submit"
android:textColor="#color/white"
android:textSize="#dimen/_10sdp" />
</LinearLayout>
And In Your Main Activity Layout You Can Use Only
<include layout="#layout/layoutresourcefilename"/>
Trying to be smart commonly results in the opposite. Better keep it simple;
Which means, just add three buttons and then show either one of them.
This has the advance, that the events are already bound, ready to click.
And also, meanwhile it's a whole lot more common to inflate Fragment
or to data-bind views, which would permit for hiding/showing buttons.

Problem with aligning textView to the end of LinearLayout

I am making chatting app and want to style messages as they are in messenger app.
And this is my code to style them:
textView.setTextColor(Color.parseColor("#FFFFFFFF"));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(400,0,10,10);
params.gravity = Gravity.END;
textView.setLayoutParams(params);
linearLayout.addView(textView);
This is my XML code:
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="12dp"
android:background="#drawable/background"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="#+id/editTextTextPersonName2"
app:layout_constraintTop_toBottomOf="#+id/name">
<LinearLayout
android:id="#+id/chatMessages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
And my TextViews are added to this ^ linear layout.
And this is what I am receiving in app: (don't translate messages they are only for test purposes and they are stupid)
And I do not why those gaps are made, someone please help me.
It is the normal behavior of textView. You can test on the SIGNAL app by typing a long word and it will move it to the next line and on the upper line, you will get to see the same space left as you are getting in yours.
If you mean to change the alignment of text in the textview then you can define the textAlignment property of textView according to your requirement.
That's happening because of the long length of the last word of your messages, if Android sees that he can't put a sequence of chars (word) into the current line, then it types it down on the next one, so to solve this problem you have 2 different solutions (or more):
1- But it may live a gap from the other side
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
2- It centers the text in its container which may live a gap from both sides
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
Good luck.

Make infinite scrollable ImageViews

I'm making an app which allows a user to search the various countries. The user can make filtered searches (such as, for example, search only countries of a specific continent, etc...). All this info (Countries and their continents are stored in my Firebase Realtime Database).
In my FilteredResults.java fragment I want to have a variable number of ImageViews (the number of the size of a List<String>).
This sketch I drew might help you understand it better:
Each one of this rectangles are ImageViews.
This is my XML (I just have a scrollview because I don't know how to create an "array" of ImageViews...)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtResultadosFiltrados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="28dp"
android:layout_marginTop="0dp"
android:text="Resultados Filtrados"
android:textColor="#323B45"
android:textSize="24sp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="28dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</RelativeLayout>
In order to solve your problem in an optimal way, you should use one of:
a plain old ListView,
a newer, more powerful RecyclerView.
These views support so called adapters which map between a model (in your case: List<String>) and a list of items (in your case a list of ImageView's).
Here you have a complete example of a simple app which loads lists of posts from the Firebase Database:
https://github.com/firebase/quickstart-android/tree/master/database
This example is based on RecyclerViews, you can find an a direct usage here:
https://github.com/firebase/quickstart-android/blob/master/database/app/src/main/java/com/google/firebase/quickstart/database/fragment/PostListFragment.java
Use RecyclerView instead of ScrollView. As keeping array of ImageView and adding them in a ScrollView will create a explosive headache.

What would be a method for creating a number counter in Android?

I want to create a scrolling counter that rises at a rate I give it for an Android app, I feel like there is no Android widget that does this well in the SDK and I wanted some idea how this should be accomplished, or even better an open source project around this idea.
Picture example:
This might not be a perfect answer, but at least a starting point. Discounting the animation and general fanciness, you can get most of the way there just by decorating a TextView with some fancy XML. I'll share what I did a few years ago to make a similar-looking screen area for a tip calculator:
Here's the XML. Each single digit is one TextView so you can change them individually. I'm showing two here but obviously there were 5 for the screenshot below.
<TextView
android:id="#+id/tv1"
android:layout_width="60dip"
android:layout_height="140dip"
android:background="#drawable/gradient"
android:textSize="64sp"
android:gravity="center"
android:typeface="serif"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv2"
android:layout_width="60dip"
android:layout_height="140dip"
android:background="#drawable/gradient"
android:textSize="64sp"
android:gravity="center"
android:typeface="serif"
android:textColor="#FFFFFF"
/>
And the "drawable/gradient" I created as a separate drawable file:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dip"
android:color="#fbbb"
/>
<solid android:color="#600000"/>
<layout_margin android:layout_margin="1dip"/>
<gradient
android:startColor="#000000"
android:centerColor="#6D6D6D"
android:endColor="#000000"
android:angle="270"
/>
Here's the final result (inside a linearlayout):
In Addition to raggedToad's answer: You can use the ViewAnimator class to add animations to your counter. Its easy to use. You can extend it, add all the digit textviews in onCreate(). Implement a method increment that handles the increment operation. Just call showNext to flip to the next view.
I'm pretty sure that this particular counter in your screenshot has some short animations (about 5 or 10 frames) for each transition from one number to the next. Maybe there are also some more for showing rapid transitions without actually stopping at the particular number with more blurring.
So the main task would be creating the graphics. Then it's just a matter of displaying the right frame at the right time.
There's an open source project that does the exact thing you are trying to do.
Check: http://code.google.com/p/android-wheel/

Categories