user1934637
user1934637

Reputation: 25

Android programming (how to display a string in a textview)

Hi all. I'm working on an app that scans the devices system info and I was wondering if it was possible to add two strings:

to my MainActivity and display them on the screen somehow. Here is my MainActivity:

package com.shadycorp.diji.clock

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Upvotes: 0

Views: 13473

Answers (3)

andr
andr

Reputation: 16054

In your layour activity_main add two TextViews with assigned IDs:

<TextView
    android:id="@+id/manufacturer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left" />
<TextView
    android:id="@+id/model"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left" />

Then, in your onCreate() method, after inflating the layout, find these TextViews using assigned IDs:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView manufacturerTextView = (TextView)findViewById(R.id.manufacturer);
TextView modelTextView = (TextView)findViewById(R.id.model);
// Now you can set TextView's text using setText() method:
manufacturerTextView.setText(Build.MANUFACTURER);
modelTextView.setText(Build.MODEL);

That's all!

Upvotes: 1

Booger
Booger

Reputation: 18725

First step, in your onCreate method, get a reference to your TextView component (using the resource name in XML), then you can set the text on it directly (last 2 lines in this code example show you what you need to do).

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv = (TextView) findViewById(R.id.some_textview_resource_name);
    tv.setText("Text is: " + Build.MANUFACTURER + Build.MODEL);
}

Upvotes: 1

Ramesh Sangili
Ramesh Sangili

Reputation: 1631

textView.setText("Manufacturer " + manufacturer  + "Model" +model);

Upvotes: 0

Related Questions