How to Dynamically set text to TextView?
Asked Answered
E

2

5

I want to display the connected ssid and ip address when the activity starts. It contais an image view (for title, because i'm not using the action bar) and 4 text view (ssid label, ssid value, ipaddr label, and ipaddr value)

I've tested the codes for obtaining the ssid and the ipaddr and they worked well. The problem is I can't display them into the TextView.

In this code, there is no error, but when the activity start, the program crash, or stop working.

Here is my java code:

import android.app.Activity;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;

public class ConnectionInfoActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView ssid = (TextView) findViewById (R.id.ssid);
    TextView ip = (TextView) findViewById (R.id.ipaddr);

    ssid.setText(getSsid());
    ip.setText(getIpAddr());

    setContentView(R.layout.activity_connection_info);
}   

// Get the connected network SSID
private String getSsid() {
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ssid = null;

    ssid = wifiInfo.getSSID();

    return ssid;
}

// Get the network IP Address
private String getIpAddr() {
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();

    String ipString = null;
    ipString = String.format(
        "%d.%d.%d.%d",
        (ip & 0xff),
        (ip >> 8 & 0xff),
        (ip >> 16 & 0xff),
        (ip >> 24 & 0xff));

    return ipString;
    }
}

And the layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coninfo_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/title_conInfo"
    android:layout_width="fill_parent"
    android:layout_height="44.5dp"
    android:src="@drawable/header_coninfo" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="SSID" />
        <TextView
            android:id="@+id/ssid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="IP Address" />
        <TextView
            android:id="@+id/ipaddr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="" />
    </TableRow>
</TableLayout>

Emeliaemelin answered 14/10, 2014 at 16:55 Comment(1)
did u loged the values and saw is it loging proper info abt networkBlackmun
B
14

You are setting the content view after you are finding the views. You should set the content view before.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connection_info);
    TextView ssid = (TextView) findViewById (R.id.ssid);
    TextView ip = (TextView) findViewById (R.id.ipaddr);

    ssid.setText(getSsid());
    ip.setText(getIpAddr());
} 
Bede answered 14/10, 2014 at 17:0 Comment(1)
Wow it worked. I spent a couples of week to find the answer to solve this problem, I even tried to code the display layout programmatically. Cant believe the answer is just that simple. Many many thanks to you Johan :)Emeliaemelin
T
1

You cannot act with your view elements before setContentView(R.layout.activity_connection_info);

Telemechanics answered 18/7, 2018 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.