0

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:

  • String manufacturer = Build.MANUFACTURER;
  • String model = Build.MODEL;

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;
    }
}

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Have you written anything before in Java? I guess not. Your code is incorrect. Post your layout XML file contents to pastebin.
I don't think so, since your code in syntactically incorrect. Here is corrected version: pastebin.com/zj243sEP
1

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

5 Comments

how can i add the strings and then send them to a text view because im not entireely understanding the answers
Hmmm, not sure - there are 2 answers that show you exactly how to do it. Find your UI resource, get a handle to it, and setText on it. Either mine, or andr's answers are clear IMHO.
and now its saying build can not be resolved as a variable and the same for SavedInstanceState
You have a build error (syntax error somewhere). Look in your XML files - and in your "Problems" tab on the bottom of your IDE (if it's eclipse)
@user1934637 please accept an answer which you feel helped you the most. By doing that you help others who stumble upon a similar problem in picking a working solution.
0
textView.setText("Manufacturer " + manufacturer  + "Model" +model);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.