I have an int that depends on users input. How do I display that int on the screen?
Here is what I have been attempting:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#int/HW" // What goes here, This is where i am confused
tools:context=".DisplayMessageActivity" />
Also here are my defined variables, you may have seen my post not too long ago about this program:
// Receive messages from options page
Intent intent = getIntent();
int HW = intent.getIntExtra("MESSAGE_HW", 0);
int OTW = intent.getIntExtra("MESSAGE_OTW", 0);
int HPD = intent.getIntExtra("MESSAGE_HPD", 0);
I am trying to display the int HW on the screen, any help is greatly appreciated! Thank you!
android:text="#int/HW" // What goes here, This is where i am confused
A default value can go there. Something that would tell you that the TextView has not been populated yet.
How do i display that int on the screen?
To display the int on the screen inside the TextView you have shown us. You need to get the TextView in your Activity with something similar to the following:
TextView textView = (TextView) this.findViewById(R.id.textViewId);
Then you can set the text on that textView with the following:
textView.setText(String.valueOf(HW));
Here is an example of adding the id to your xml:
<TextView
android:id="#+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#int/HW" // What goes here, This is where i am confused
tools:context=".DisplayMessageActivity" />
When you get the users input, do this:
TextView hwTextView = (TextView)findViewById("R.id.hwTextView")
hwTextView.setText(String.valueOf(yourIntVariable));
Note this means you'll have to add an id attribute to your TextView with the value of "hwTextView"
Building on prolink007's answer, you could also omit that line then call setText, java side.
<TextView
android:id="#+id/hw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".DisplayMessageActivity" />
TextView HWgetText = (TextView)findViewById("R.id.hw");
HWgetText.setText(String.valueOf(HW));
Related
I know this has been posted countless times and im sorry but I have tried to follow the solutions but can't seem to fix it.
Basically I'm trying to read records from Android SQLite Database, but I keep getting the error at setText.
public class Landing extends AppCompatActivity{
public Button buttonProducts;
public void countRecords(){
int recordCount = new TableControllerAppointments(this).count();
ScrollView textViewRecordCount = findViewById(R.id.textViewRecordCount);
textViewRecordCount.setText(recordCount + " records found.");
}
I'm following a tutorial and I added this code to my XML file,
<ScrollView
android:id="#+id/textViewRecordCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:padding="1dp"
android:text="#string/textViewRecordCount"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.182"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/buttonCreateAppointment"
app:layout_constraintVertical_bias="0.146" >
<LinearLayout
android:id="#+id/linearLayoutRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Sorry if im not posting right this is my first time. I think its something to do with my ScrollView but not sure, any help would be much appreciated
Thanks
How to resolve method 'setText(java.lang.String)
FYI
setText() is method of textview not a method of Scrollview because ScrollView does not have any setText()
You can not use as Scrollview.setText("Nilesh");
textview.setText("Nilesh");
It must be like this I hope
public Button buttonProducts;
public void countRecords(){
int recordCount = new TableControllerAppointments(this).count();
TextView textViewRecordCount = findViewById(R.id.textViewRecordCount);
textViewRecordCount.setText(String.ValueOf(recordCount) + " records found.");
}
You can't set text to Scrollview, first you have to understand this, it's basic
You're trying to set text for "Scrollview", which is absolutely wrong. Scroll view is a view which is using for scroll the layout while the application runs. to full fill your requirement you have to declare a TextView. Then you can set the text for it.
At a point in my app I'm cycling through the objects on screen and checking if they're the last object in that line. To do that, I'm checking the Align Parent End property (which is checked for the last widget in each line). Here's part of my activity xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Average Temperature:"
android:id="#+id/AverageTemperatureText"
android:layout_below="#+id/TemperaturesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/AvgTempNum"
android:layout_alignBottom="#+id/AverageTemperatureText"
android:layout_toRightOf="#+id/AverageTemperatureText"
android:layout_below="#+id/TempMiddle"
android:gravity="center|bottom"
android:editable="false"
android:text="0.0"
android:maxLength="7"
android:nextFocusForward="#+id/TotalObservedVolumer"
android:layout_alignRight="#+id/FreeWaterVolume"
android:layout_alignEnd="#+id/FreeWaterVolume"
android:layout_alignParentEnd="true" />
^^^^ Right here. It's true.
You get the idea. Then, in my code, I'm cycling through the relative layout children, then for each view on it, checking it for that particular property. If it's true, I'm supposed to do something. But with the code I have, it's always false. So what am I doing wrong? Here it is:
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
for(int i = 0; i < relLayout.getChildCount(); i++) {
View view = relLayout.getChildAt(i);
// Assess if it's the last field in that line
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
int[] rules = params.getRules();
if (rules[RelativeLayout.ALIGN_PARENT_END] == RelativeLayout.TRUE) {
// my code
}
}
Thanks!!
Fixed, but in a curious way.
In my case, all the last fields on screen had Align Parent End flagged. So I found online this would be the way to do it:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
int[] rules = params.getRules();
if ((rules[RelativeLayout.ALIGN_PARENT_END] == RelativeLayout.TRUE)) {
// Do something about it
}
Where RelativeLayout.ALIGN_PARENT_END stands for 21, according to http://developer.android.com/reference/android/widget/RelativeLayout.html
However, my code was returning false for that check. So I started displaying the value for the property, and it really was false. I tried this for a while, then I saw one of my edittexts returned true for it. But the activity xml showed this
android:layout_alignParentRight="true"
in addition to alignParentEnd. Which didn't make sense, since alignParentRight is supposed to be rule 11, according to the same link above.
So I devised this code to expose which rules where true, and put it in my code:
for(int x = 0; x < 22; x++) {
if (rules[x] == RelativeLayout.TRUE) Log.i("Property true:", String.valueOf(x));
}
Which gave me 11 for all fields, and 11 and 21 for the field that responded TRUE before. Remember, all fields had android:layout_alignParentEnd="true" in the activity xml.
So to fix my code, I'm now checking rules[RelativeLayout.ALIGN_PARENT_RIGHT] and it works, but I strongly believe those two properties are switched somehow. Can anybody confirm? Or please englighten me on where I screwed up, which is more likely :)
This is driving me absolutely crazy and Im not sure what is going on.
I have an xml layout with a TextView within a clickable RelativeLayout.
<RelativeLayout
android:id="#+id/bg_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#color/almost_black"
android:clickable="true"
android:onClick="goToBG"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Go To B.G."
android:textColor="#color/white"
android:textSize="20sp" />
<ImageView
android:id="#+id/bg_arrow"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="-10dp"
android:src="#drawable/arrow_icon" />
<TextView
android:id="#+id/current_bg_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/bg_arrow"
android:text="3"
android:textColor="#color/holo_blue"
android:textSize="22sp" />
</RelativeLayout>
Now in my code I try to update the textview "current_bg_count"
private void updateBGCount(){
try{
RelativeLayout bgSection = (RelativeLayout) findViewById(R.id.bg_section);
TextView bgCountTV = (TextView) bgSection.getChildAt(2);
bgCountTV.setText(tempBG.size());
}
catch(Exception e){
e.printStackTrace();
Logger.d(TAG, "exception in updateBGCount");
}
}
This gives a ResourceNotFountException on the line where I setText although the RelativeLayout is found without a problem. Even when I try finding it just by id like this:
TextView bgCountTV = (TextView) findViewById(R.id.current_bg_count)
bgCountTV.setText(tempBG.size());
it gives the same error.
All the other views in the layout are found easily and updated just fine. Only this one TextView is giving me the problem. Does anyone have any idea what the problem is?
You need to convert your size of the ArrayList to a String at this line
setText(tempBG.size())
Since tempBG.size() returns an int then setText() is looking for a resource with the id of whatever that returns. You are using this setText(ResId int) method which is used for if you have a string resource that you want to use to set the text.
So change it to
setText(String.valueOf(tempBG.size()));
Try setting your tempBG.size() to string like this
bgCountTV.setText(""+tempBG.size());
This should work
Are you using eclipse? Check there is no errors and warning in your project. Sometimes there are errors in xml that let you compile, but doesn't work properly.
the Problem is with this code
bgCountTV.setText(tempBG.size());
tempBG.size() this code is returning an int value which it is assuming as string Resource id something like R.string.VARIABLE_NAME coz it have a corresponding int value which TextView is assuming as id
What you can do
bgCountTV.setText(tempBG.size()+"");
OR
bgCountTV.setText(String.ValueOf(tempBG.size()));
OR
bgCountTV.setText(tempBG.size().toString());
I'm building a new InfoWindow (google maps api v2) and I'm trying to get my layout to be perfect.
A part of that layout is this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtInfoWindowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#ff000000"
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtInfoWindowObstacles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="32"
android:lines="1"
android:singleLine="true"
android:textColor="#ff7f7f7f"
android:textSize="14dp"/>
Now, the problem is with the android:ellipsize="end".
It should draw the three dots at the end but it doesn't do that.
Now I'm getting something like this:
TextTextTextTextTe
Instead of this:
TextTextTextTextTe...
I'm thinking it has something to do with the fact that I'm using layout_width="wrap_content" . But I need that because I'm using a LinearLayout
I tried your code and they work fine, except the second TextView which has the attribute android:maxLength="32", which in this case the text cannot be ellipsized because of 32 characters limit. But if you remove it, it works as expected.
There is another option - you can format your string from code like this:
private static final int STR_MAX_CHAR_COUNT = 32;
private String formatString(String stringToFormat) {
if(stringToFormat.length() > STR_MAX_CHAR_COUNT){
stringToFormat = stringToFormat.substring(0, STR_MAX_CHAR_COUNT - 1) + "...";
}
return stringToFormat;
}
And then just set string like this:
String newText = "some long-long text";
mTextView.setText(formatString(newText))
I have a very simple question about menu control in Android. I'm writing a program that performs some simple numeric calculations and outputs an answer. My question is how do make the program move to second screen when a button is pressed, and how can I let the user move back to the original screen.
Here's a quick example
Screen 1
"Add Numbers"
Input 1st # ____
Input 2nd # ____
(Add)
Screen 2
The answer is "____"
The user inputs two integers. presses add, and then the program moves to the second screen which displays the answer. If they user wants they can return to the first screen and start over.
I know this has been covered but with so many resources I don't know what to look for. Answers or links to resources would be most helpful. Thank you!
You can use the following layout for the screen to input numbers. Name the file first.xml.
<TextView
android:id="#+id/firstnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input Ist:"
android:textSize="20px"
android:textStyle="bold"
/>
<EditText
android:id="#+id/first"
android:layout_height="wrap_content"
android:layout_width="250px"
/>
<TextView
android:id="#+id/secondnumber"
android:text = "Input 2nd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:textStyle="bold"
/>
<EditText
android:id="#+id/second"
android:layout_height="wrap_content"
android:layout_width="250px"
/>
<Button
android:id="#+id/add_button"
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
To add these numbers
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(this);
EditText num1 = (EditText)findViewById(R.id.first);
EditText num2 = (EditText)findViewById(R.id.second);
}
The click listener code
public void onClick(View v) {
int n1 = Integer.parseInt(num1.getText().toString());
int n2 = Integer.parseInt(num2.getText().toString());
int result = n1 + n2;
String res = Integer.toString(result);
Intent intent = new Intent(getApplicationContext(), Result.class);
intent.putExtra("result", result); //Passing the result to the second activity using intent
startActivity(intent);
}
In the Result.java class.
public class Result extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent intent = getIntent();
String result = intent.getStringExtra("result");
TextView t1 = (TextView)findViewById(R.id.result);
t1.setText(result);
}
}
Hope this helps..
May I know what type of language are u using?
I recently just did it with C#.
My flow is that, when I start a program, it will auto be forced to the second screen, with some power options.
But if yours is just moving the program the diff screens, it will be easy in C#.
Screen Srn = Screen.PrimaryScreen; // gives your information of Primary Screen.
Screen[] Srn = Screen.AllScreens; //gives u an array of all your available screen( srn[0] is primary screen, etc)
So, using the intellisense from the IDE, should be able to get the width, height, etc.
Cant remember exactly, but something like src.width or src.bounds.width.
Easiest way to move your program will be to move the program x axis to the respective screen.
ViewFlipper will work well. You could also try "startActivityForResult" to pass your answers to the next screen.
Take a look at the ViewFlipper class. It works like tabs, but without the tabs UI. You can use an xml layout to display your 2 (or more) screens, and showNext() or showPrevious() to flip between the screens.