Android Studio Display Long in Button Text

Viewed 111

How would I go about displaying a Long or Int inside of a button element's text field (or a TextView)? Would the same method work for displaying a String inside the text field? Thanks.

Here is my code for MainActivity

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;

import androidx.appcompat.widget.PopupMenu;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import java.io.File;

public class MainActivity extends AppCompatActivity {
Button mainstorage;
private AppBarConfiguration mAppBarConfiguration;
private static final int REQUEST_WRITE_STORAGE = 112;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    StatFs statFs = new 
    StatFs(Environment.getRootDirectory().getAbsolutePath());
    long blockSize = statFs.getBlockSize();
    long totalSize = statFs.getBlockCount()*blockSize;
    long availableSize = statFs.getAvailableBlocks()*blockSize;
    long freeSize = statFs.getFreeBlocks()*blockSize;
    String internalSize = Long.toString(blockSize);
    myBtn.setText(internalSize);

}
}

And my XML for the Fragment.

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<LinearLayout
    android:background="@android:color/transparent"
    android:id="@+id/firstrow"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="start"
    app:layout_constraintHeight_percent="0.22"
    tools:ignore="MissingConstraints">
 <Button
        android:contentDescription="@string/goInternalDesc"
        android:id="@+id/main"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:drawableTop="@drawable/storage"
        android:minWidth="20dp"
        android:text="Main Storage"
        android:minHeight="20dp" />
</LinearLayout>

I am wondering why I am getting a build error when trying to set the text of the button to the String in my code. I thought it might be because I had set the text of the button in the XML but removing that had no effect- I am still getting a "Cannot resolve symbol" error on my MainActivity.

3 Answers

For example:

If you have a button called jButton1, you just need to use this code:

String test = String.valueOf(your_int_or_long);
jButton1.setText(test);

And yes, for the text field is the same method.

You can either use

btn.setText(String.valueOf(myLong));
btn.setText(String.valueOf(myInt));

Or

btn.setText(Long.toString(myLong));
btn.setText(Integer.toString(myInt));

I normally do this way

btn.setText(number+"");
Related