I'm new to mobile development and I am having trouble displaying variables as text on to the screen.
public class wifiinfo() {
public string getmac = getBSSID();
}
I have this in my main (blank) activity but I have no clue as to how to code this in with XML. The Android Developer tutorials haven't really helped me. Do I have to index the string in a database, perhaps? Any help would be greatly appreciated.
Use Textview to display text.
Add this in your XML:
<TextView
android:id="#+id/textViewSSID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="Your SSID"/>
And display text in activity like this:
TextView tvSSID = (TextView) findViewById(R.id.textViewSSID);
String ssid = getBSSID();
tvSSID.setText(ssid);
Why you create an inner class to keep the value of ssid is a real mystery to me. You can make the variable getmac as a member variable in the activity and initialize it in your onCreate of your activity.
Update:
As to your comment, your activity should start somewhat like this:
public class ActivityName extends Activity {
private string MacId = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
MacId = getBSSID();
//Other functions
}
You can use TextView to write text and display it on the screen.
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Your text here..."/>
If your text line is so long you should write it in res/values/strings.xml:
<string name="my_text">This is a very long line text I want to show on screen.</string>
And use android:text="#string/my_text" to get it in the TextView.
Hope it helps!
Related
I am new to Android development and I have been looking through Q's/Documentation but unable to find direction on what my end game is.
I am building what could be described as a 'content aggregator' and at current I am struggling to see how I can set an activity that proceeds a click on each position to instigate either an Instagram profile mWebView or an API call. Currently my InstagramList.java is as follows -
public class InstagramList extends Activity {
ListView list;
String[] web = {
"Kyary Pamyu Pamyu",
"Tokyo Girls' Style",
"Haruka Nakagawa",
"Nemu Yumemi",
"Moga Mogami",
"Ayane Fujisaki",
"Koda Kumi",
"Atsuko Maeda",
"Tomomi Itano",
"Haruna Kojima",
"Utada Hikaru",
"Shibasaki Ko",
"Taeyon",
"Tiffany",
"Jessica",
"Sooyoung",
"Sunny",
"Laboum",
"YeEun",
"Yubin",
"Hyelim"
};
Integer[] imageId = {
R.drawable.kyary,
R.drawable.tgs,
R.drawable.haruka,
R.drawable.nemu,
R.drawable.moga,
R.drawable.ayane,
R.drawable.koda,
R.drawable.atsuko,
R.drawable.tomomi,
R.drawable.haruna,
R.drawable.utada,
R.drawable.shibasaki,
R.drawable.taeyon,
R.drawable.tiffany,
R.drawable.jessica,
R.drawable.sooyoung,
R.drawable.sunny,
R.drawable.laboum,
R.drawable.yeeun,
R.drawable.yubin,
R.drawable.hyelim
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instagram_main);
CustomList adapter = new
CustomList(InstagramList.this, web, imageId);
}
}
The xml for this is as follows -
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
Any pointers on how best to either modify this to allow better control of these variables or on a possible solution would be greatly received. Apologies if this has been asked in a similar vein before.
I observed that you are using two arrays. In place of this you can create a class with one member as string (web) and other as int (imageId). In addition to this you can add one variable for possible action as you mention there can be different actions. In adapter you can add tag on view and click listener so you can perform different actions on different item click.
Hope I understand your problem correctly. Let me know if you need more detail.
Check out this. In onItemClick method in MainActivity do what you want.
I have read about accessing xml layout component thorugh opengl renderer and Im still cant do it.. can anyone tell me how to do it?
My xml layout is using relativelayout..
this is a part of my java class for set the renderer
GamePlayActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLSurfaceView = new GameView(this);
mGLSurfaceView.setRenderer(new GameRenderer(this,GamePlayActivity.this));
setContentView(R.layout.gameplay);
sv = (RelativeLayout) findViewById(R.id.gameviewxml1);
sv.addView(mGLSurfaceView, 0);
and this is my renderer constructor
GameRenderer.java
private GamePlayActivity gpa;
public GameRenderer(Context context,GamePlayActivity gp)
{
grContext = context;
gpa = gp;
}
I have a textview I want to change the text when renderer finish doing some animation.. I tried using this in renderer class
gpa.roll.setText("Done");
but the app crash after that.. I wonder where I was wrong?If I missimg some data that I need to show just tell me.. I`ll post it..
I don't understand why you pass in GPA, but that's okay. If you declare the SurfaceView in xml, it should be pretty easy to position everything that way. For example:
<SurfaceView
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/ad_detect" />
<TextView
android:id="#+id/tv_label_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ad_detect"
android:layout_marginRight="10dp"
android:layout_marginTop="18dp"
android:layout_toLeftOf="#+id/tv_value_temp_bkrd"
android:shadowColor="#00FF00"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="#string/temp"
android:textColor="#00FF00"
android:textSize="22sp" />
I am new to android development and I am troubled with something.
I want to create a user interface first and I have a question regarding adding dynamic fields. I am already using XML to setup my interface but I don't know how to proceed.
For example, the user can select 1 2 3 or 4 and based on the selection I would like the dialog box to show that number of EditText. Also the same thing would apply later. A table would show that number of textviews at the header.
Is there a way to do this by using some XML and some java? Because I believe by using only java it will be a pain to style different things.
Please let me know if you need further info.
Thanks in advance
You should check out the visibility attribute of a View.
If you have a fixed set of ui elements (say, 5 buttons) you can just include them in the layout and show them later with setVisibility(View.Visible).
However, if you have a dynamic amount of elements (the user can chose from 1 to n buttons) then you will have to implement it in Java. You can still use XML layouts for parts of the work, but most stuff will have to be done by hand.
I have written a sample code , See if it helps you
public class ActivityMain extends Activity {
LinearLayout main;
private int id = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
main = (LinearLayout) findViewById(R.id.parent);
main.setOrientation(LinearLayout.VERTICAL);
final EditText editText = (EditText) findViewById(R.id.et_count);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
final int count = Integer.parseInt(editText.getText()
.toString());
addEditText(count);
}
});
}
private void addEditText(int count) {
for (int i = 0; i < count; i++) {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
}
}
}
and the layout test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent"
android:padding="10dp" >
<EditText
android:id="#+id/et_count"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
To add style
editTextLayout.setTextAppearance(getApplicationContext(), R.style.boldText);
I am trying to make a button, count clicks and display them in a text view.
Tried everything i knew.
XML:
<LinearLayout
android:id="#+id/gpulay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp" >
<TextView
android:id="#+id/master_control_text_gpulay"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Master Control :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/plus_gpulay"
android:layout_width="35dp"
android:layout_height="35dip"/>
</LinearLayout>
Here is my code:
public class Main extends Fragment {
int c=0;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ListView GPU_LAYOUT = (ListView)inflater.inflate(R.layout.gpu, container, false);
TextView text = (TextView) GPU_LAYOUT.findViewById(R.id.text1);
Button plus = (Button) GPU_LAYOUT.findViewById(R.id.butt1);
plus.setOnClickListener(new OnClickListener() { //null pointer this line, but it's from .srtText i think
public void onClick(View v) {
c++;
text.setText(c);
}
});
}
return GPU_LAYOUT;
}
App force closes when i open it, so i don't even see the main layout.
The NPE is coming from here
text.setText(c);
you are using the wrong setText() method. When you place an int in there it looks for a resource with that id. You need to give it a String. You can do this several ways. One way is to change it to
text.setText("" + c);
You could also do
text.setText(String.valueOf(c));
TextView Docs notice the different methods.
Its just wrong to create your app in onCreateView while working in an Activity.
You can still do all this in your onCreate Method.
My Guess would be that GPU_Layout is null or doesnt contain your button. Thats why button (plus) is Null and calling setOnClickListener on it throws a NullPointerException.
Also it would help if you could explain WHEN the error Occurs, during inflating/building the Activity or during actually pressing the Button
1) You need to extend your class from Fragment instead of Activity. Then your method onCreateView will work. Then set this fragment as contentView of your Activity.
2) You can't set int as text of textView or any other widget. You need to convert it into String first using String.valueOf(count);
on android.
my activity is like this.
public class MainActivity extends Activity {
static TextView textview;
static int aaa = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.activity_main);
textview = (TextView)findViewById(R.id.textView6);
//other method,startservice()and so on.
and there is BroadcastReceiver to receive a flag from service.
public static class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intents) {
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
textview.setText("set")//throw null point
Log.d("aaa","aaa is" + aaa);
like this, in onReceiver, textview is null.
as test, int aaa is'nt null.
why textview is null?
EDIT
Texttiew xml is
<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=".MainActivity" >
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/adview"
android:layout_alignParentLeft="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
And method in Activity using same textview.settext isnt null.
Thanks.
EDIT2
I can know i should save this variable.
So, whats the best way to save VIEW variable?
SharedPreferences seem not able to save VIEW variable.
This thread indicates that having static fields might be a problem.
In general, android kills your app.. and on return static variables may not be set.
static variable null when returning to the app
This is likely that you are not referencing the correct textview element. Double check your IDs in your layout file.