How do I make a counter display a variable? - java

So I have been trying to make an app that sort of counts things. Basically its a standard counter app. But, I'm having trouble actually displaying the number. I have the setup for the button:
<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"
tools:context=".MainActivity">
<Button android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
<Button android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_below="#+id/button_1" />
</RelativeLayout>
However, I don't know how to actually create the display of the number increasing or decreasing in java. I've read on some places on how to change text with EditText and TextView, but how would I be able to do that with integers? As in, if the number displayed is 5, how can it be so that I also have an int or float with the same value?
TL;DR: How do I make a counter app? Additionally, How can I make the number displayed be a variable rather than text/a string?

you have to use String.valueOf("Your content") which will convert following values to string : boolean, char, char[], long, float, double, integer Object;

Related

How to remove padding from Rating Bar in Android? [duplicate]

This question already has answers here:
How to reduce the space around rating bar in android
(3 answers)
Closed 4 years ago.
i have rating bar on my application and it appears like this:
I've tried to set padding to 0dp, but nothing changed to the view.
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:layout_marginStart="16dp"
android:isIndicator="false"
style="#style/Widget.AppCompat.RatingBar"/>
Expected result: I am expecting the rating bar with no right padding and bottom padding.
You can simply adjust this
style="#android:style/Widget.DeviceDefault.RatingBar.Small"
instead of
style="#style/Widget.AppCompat.RatingBar
I add this to my xml code and it solved my problems:
android:scaleX="1"
android:scaleY="1"
The built-in Android rating bar is very visually problematic, in which case I would rather use the improvements made by Android users to size each star depending on the size of the object.
Enclosing you a convenient library to work with, I believe this library will solve you a lot of problems with the development.
https://github.com/xckevin/AndroidSmartRatingBar?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=7405
Credit to the developer kevin.liu (xckevin).
use scale x and y to reduce the start size and step size to set star size
Try this xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:isIndicator="false"
style="#style/Widget.AppCompat.RatingBar" />
</LinearLayout>

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.

Dynamically made Layout with EditText - Android

This isn't much of a question, more of an explanation, I like to know whats happening and not treat things like a black box. I'm making an android app, and I created a form where you can dynamically make a new field to add to the form, and the field has 2 edit text next to each other.
formLayout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="#+id/team_form_player_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:background="#color/white"
android:hint="#string/form_hint_team_player_name"
android:inputType="textCapWords"
android:padding="15dp"/>
<EditText
android:id="#+id/team_form_player_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#color/white"
android:hint="#string/form_hint_team_player_number"
android:inputType="number"
android:padding="15dp"/>
</LinearLayout>
And this is inside a button listener when clicked, it'll create the new field
View layout = getLayoutInflater().inflate(R.layout.layout_add_new_field_team, null);
EditText name = (EditText)layout.findViewById(R.id.team_form_player_name);
EditText number = (EditText)layout.findViewById(R.id.team_form_player_number);
playerName.add(name); //arraylist
playerNumber.add(number); //arraylist
containerLayout.addView(layout);
being that its a different layout i had to inflate the view. anyway, my question is this. how does this even work? by code, I'm adding an edit text, each time to its respectful array. im adding the same layout each time, im not even using "new" so its not instantiating anything new. and if I create 10 fields at once, then fill in the fields. and run this method
for(EditText edit : playerNumber) {
String test = edit.getText().toString();
System.out.println(test);
}
it gives me all the correct values in each field. How does that happen? Because on add field click, it instantly adds the edit text to the array with the fields EMPTY. and theres no code inputting the values into the array. I'm just boggled that this works and would like to know how it does. anyone have any idea?
.inflate(layoutResId) == "create new instance of this layout"
So basically every time you use inflate you are creating instances of all the Views in the layout, in your case a LinearLayout and two EditText Views inside it.

Layout issue for messaging app Android XML

I'm trying to create a messaging app in android and have created a custom list adapter that displays the message text and then the date as well. I have got them both displaying, now want them to be able to be displayed in a certain way. I want to be able to have sent messages on one side of the screen but received messages on the other, with the date appearing below/to the side of the message text
This is the custom xml I have used for each item on the list, any ideas on how to make them wrap appropriately and move to sides of the screen would be appreciated!
(Note: I've tried using LinearLayouts with weights and RelativeLayouts with android:layout_alignParentRight and things, but couldn't get it exactly how I wanted it so thought I'd ask here!)
(Don't think other code snippets are needed but can provide if wanted!)
<?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">
<TextView android:id="#+id/msgTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:paddingRight="2dp"
android:textStyle="bold"
android:textSize="15sp" />
<TextView android:id="#+id/msgDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Cheers
I've tried using LinearLayouts with weights
Whats wrong with that? android:layout_weight="0.4"
try to use not 'fill_parent', use 'match_parent';
also, replace this
android:textSize="15sp"
to this:
android:textAppearance="?android:attr/textAppearanceMedium"

Dynamically defining how an icon is drawn

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();

Categories