简体   繁体   中英

How can I display content on TextView?

I've searched around but unfortunately couldn't find what I was actually looking for, referred to developer.android still don't get it. So what I want to do is using a function, get the device model and build, then display it to a TextView .

I have gotten my code for retrieving info -

public String getDeviceName() {
          String manufacturer = Build.MANUFACTURER;
          String model = Build.MODEL;
          if (model.startsWith(manufacturer)) {
            return capitalize(model);
          } else {
            return capitalize(manufacturer) + " " + model;
        }
    }
        private String capitalize(String s) {
          if (s == null || s.length() == 0) {
            return "";
          }
          char first = s.charAt(0);
          if (Character.isUpperCase(first)) {
            return s;
          } else {
            return Character.toUpperCase(first) + s.substring(1);
        }
    }

But now, I wish to display the received information on a TextView

<com.ascendapps.beam.MyTextView
    android:id="@+id/device"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_gravity="center"
    android:textColor="#4c4c4c"
    android:gravity="center"
    android:textSize="25sp"
    android:text="@string/welcome" />

First initialize your MyTextView in your onCreate and then set the text which is returned from your getDeviceName() using setText() method of it.

Try out as below:

  MyTextView text=(MyTextView)findViewById(R.id.device);
  text.setText(getDeviceName());

I suggest you make this change to your textview xml code

<TextView
android:id="@+id/device"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center"
android:textColor="#4c4c4c"
android:gravity="center"
android:textSize="25sp"
android:text="@string/welcome" />

unless you have to use your own custom textview. Then you can do this to display

your_textview_name.setText(getDeviceName());

First, you have to get a reference to the TextView element. For example:

TextView yourTextViewIdentifier = (TextView) findViewById(R.id.idTextView);

where yourTextViewIdentifier is the variable name for the reference of the TextView, and idTextView is the id you defined in your xml to name the TextView in the layout.

Now, you can set the text of the TextView:

yourTextViewIdentifier.setText(getDeviceName());

Hope it helps.

Thanks guys, I managed to do so using setText Method.

private TextView device;

device = (TextView)findViewById(R.id.device);
device.setText(getDeviceName());

In order to find the device model number in your textview do the following

textviewObject.settext(android.os.Build.MODEL);

this will show ur device modelnumber in your textview...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM