Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying the App version in a TextView

I want to display my current app version in a TextView, the activity the Textview is in is not my MainACtivity. I used the folowing code:

public class informatie extends ActionBarActivity {

    public String versionName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_informatie);

        try {
           versionName = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        Typeface impact = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");

        TextView versieVak = (TextView) findViewById(R.id.versieView);
        versieVak.setText(versionName);
        Toast.makeText(getApplicationContext(), "KLIK", Toast.LENGTH_LONG).show();
        versieVak.setTypeface(impact);
        versieVak.setTextColor(getResources().getColor(R.color.antraciet));
        versieVak.setTextSize(10, TypedValue.COMPLEX_UNIT_SP);

    }

The Layout.xml

<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="com.KnapperDev.knapper.jrw.informatie">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/imageView"
            android:src="@drawable/overeenkomst"
            android:layout_marginBottom="249dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:id="@+id/versieView"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="155dp" />
    </RelativeLayout>

But when i open the activity on my phone there is nothing in the TextView. I made a Toast to check if versieVak.setText(versionName); was used. it just doesn't display anything. Any idea what i am doing wrong?

like image 490
Harjan Avatar asked Apr 16 '15 14:04

Harjan


2 Answers

Try this :

    // Import the BuildConfig at the top of the file
    import yourpackagename.BuildConfig
    
    ...

    // in your method, just use:
    TextView versionName = findViewById(R.id.textName);
    versionName.setText("version : " + BuildConfig.VERSION_NAME);
like image 142
Jéwôm' Avatar answered Nov 09 '22 02:11

Jéwôm'


I tried using your code. It worked fine. Just remove your font related code and try simply. Check if it is working.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String versionName = "";
    try {
           versionName = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    TextView versionname = (TextView) findViewById(R.id.textName);
    versionname.setText(versionName);
}

Also make sure you have assigned unique ids for your textview. If possible, can you share your layout?

like image 26
Santhana Avatar answered Nov 09 '22 03:11

Santhana