Okay, so I have this code:
<?xml version="1.0" encoding="utf-8?">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/Text"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="Text"/>
</LinearLayout>
(Main Activity) below:
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement!");
This doesn't replace the text, and I do have the class, and main methods. onCreate etc.
It seems that the XML file is the only one it will take a value from, if there's no android:text it shows nothing.
I'm basically trying to just change the text via the .setText function, what am I doing wrong?
Thanks
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement");
}
}
Layouts for files: /storage/emulated/0/AppProjects/New/res/layout/main.xml
/storage/emulated/0/AppProjects/New/gen/com/mycompany/New/R.java
/storage/emulated/0/AppProjects/New/src/com/mycompany/new/MainActivity.java
Move your findViewById after setContentView(), and change your variable names to follow Java convention:
public class MainActivity extends Activity {
#Override
public void onCreate(savedInstanceState){
super.onCreate(Bundle savedInstanceState);
setContentView(R.layout.main);
//this line should be called after you inflate your view with setContentView
//so move it here, instead in MainActivity body
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement");
}
}
Also, there's a typo in your xml layout. Change this line:
<?xml version="1.0" encoding="utf-8?">
to
<?xml version="1.0" encoding="utf-8"?>
You cannot find your views before setContentView() method. Move your lines after setContentView():
TextView Text = (TextView) findViewById(R.id.Text);
Text.setText("Text Replacement);
Also, please follow java naming conventions, as obect names never start with Caps but class names do..
Move the below to onCreate
TextView Text = (TextView) findViewById(R.id.Text);
Like
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.Text);
tv.setText("Text Replacement);
findViewById looks for a view with the id in the current inflated layout. So first you need to set the layout to the activity and then initialize views.
Also consider renaming Text to something more appropriate and meaningfull.
Edit:
Make sure you have the textview with id Text in main.xml. Build and clean your project.
You should innialize your view and set proper properties in andriod after setContentView method is been called.
So this below code should come after setcontent view
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement);
Looks like you have two "" before the "Text"
Try deleting one...
Should look like this:
android:text="Text"/>
First, consider using "wrap_content" or "match_parent" attributes instead of "200dp" for example.
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Text"/>
Then, please rename your "Text" variable into "text". Java or Android may be thinking that Text is not your variable but a class:
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement!");
Ur variable is declared as TextView Text
Instead u should use text because in Java Anything starting with capital letter is treated as a class ,it is a bad programming practice ! The variables should be in lowercase(hungerian notation) !
Related
I am working on Android and have being working on OpenGL ES. I have a xml layout as follows (I have took some things out to show only therelative content:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/myText"/>
<MyGLSurfaceView
android:id="#+id/mySurface"/>
Then in my GlSurfaceView I am trying to access the button that is in the same layout. However this is were I am having a problem.
I have tried the following:
View v = (View) getParent();
myTextViewString = (TextView) v.findViewById(R.id.myText);
this
myTextViewString = (TextView) findViewById(R.id.myText);
and this
myTextViewString = (TextView) ((RelativeLayout) getParent()).findViewById(R.id.myText);
I cannot seem to figure out how to access this button outside of the GLSurfaceView but is in the same activity inside my GLSurfaceView.java.
I do know that it has something to do with not being able to get the parent( I am assuming because it does not extend Activity). I have looked around and I cannot find a way to achieve this.
One clean and straightforward way to do this is to pass the button view to the GLSurfaceView. Aside from avoiding navigation through the view hierarchy, this also makes your view code more generic, since it will not have to know the id of a specific button.
In the onCreate() method of the activity, after calling setContentView(), you could have this:
MyGLSurfaceView glView = (MyGLSurfaceView) findViewById(R.id.mySurface);
TextView textView = (TextView) findViewById(R.id.myText);
glView.setTextView(textView);
In MyGLSurfaceView:
private TextView mTextView;
void setTextView(TextView textView) {
mTextView = textView;
}
Then, in other methods of MyGLSurfaceView, you can use mTextView anytime you need access to the button.
So, I am trying to get reference to a xml layout from the class of a simple widget I made.
So ,my widget contains an ImageView and two TextViews.I will add the code for this widget, just so no one gets confused.
public class Item extends LinearLayout{
TextView tv1,tv2;
ImageView img;
public Item(Context context,int resid, String t1, String t2) {
super(context);
setOrientation(HORIZONTAL);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
img = new ImageView(context);
tv1 = new TextView(context);
tv2 = new TextView(context);
img.setBackgroundResource(resid);
img.setVisibility(View.VISIBLE);
img.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
tv1.setText(t1);
tv1.setTextSize(15);
tv1.setGravity(Gravity.CENTER);
tv1.setLayoutParams(new ViewGroup.LayoutParams(250, 100));
tv2.setText(t2+"lei");
tv2.setTextSize(15);
tv2.setGravity(Gravity.CENTER);
tv2.setLayoutParams(new ViewGroup.LayoutParams(250,100));
tv1.setBackgroundColor(Color.GREEN);
tv2.setBackgroundColor(Color.BLUE);
addView(img);
addView(tv1);
addView(tv2);
}
So, as you can see, there's a clickListener added for each "Item".What I want to do ,is to be able to refer to a xml layout that is a second activity ,so that I can manipulate what is in this layout from within this widget.
The second activity's class:
public class Final extends Activity {
LinearLayout fl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final_layout);
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
fl = (LinearLayout) rootView.findViewById(R.id.fl);
}
}
The XML file for this second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
So, once again, to sum up, I want to be able to add stuff to this second activity ,from the class Item ,but I am not able to take reference to the XML layout coresponding to the second activity.
So,practically ,the only way to do this, is to pass variables through the onclick method.
What I am doing right now is this: I get two String variables and one Int.Strings being used for the text wrote in textViews and int for the resource ID for the imageView's backgroundResource, pass them to the second activity and use them there to re-create the Item.
If anyone needs more details on this, leave a comment here and I'll do my best to help.
I am struggling to make my links clickable within an Android application. I have tried the following:
<ScrollView
<RelativeLayout
<TextView
...
android:id="#+id/textView"
android:text="#string/stringName"
android:linksClickable="true"
android:autoLink="web"
with either
<string name="stringName">String</string>
or
<string name="stringName"><a href="http://www.example.com">String</a></string>
while also incorporating
((TextView) findViewById(R.id.textView)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textView)).setText(Html.fromHtml(getResources().getString(R.string.stringName)));
in my .java
EDIT: I can make my string appear as clickable using the former method in my string.xml file, however, when I click on it nothing happens. I believe it is because I am clicking on either the relativeLayout or even more likely the ScrollView as opposed to the TextView
this work for me:
<string name="stringName">String</string>
TextView TextView=(TextView)findViewById(R.id.textView);
TextView.setMovementMethod(LinkMovementMethod.getInstance());
In Xml file:
Remove this two lines from TextView
android:linksClickable="true"
android:autoLink="web"
This works for me:
TextView text1 = (TextView)findViewById(R.id.textView);
Spanned spanned = Html.fromHtml(getString(R.string.stringName));
text1.setMovementMethod(LinkMovementMethod.getInstance());
text1.setText(spanned);
And my strings.xml, it changes at the end replacing > for >
<string name="stringName"><a href="http://www.example.com">>String</a></string>
I am not too sure about nesting a TextView in a scrollView making it not clickable, but you can always make your TextView clickable in your code.
You set an OnClickListener on your TextView, and inside start a new Intent like this :
First inflate the ScrollView and get your textView in the code like this :
ScrollView layout = (ScrollView) ScrollView.inflate(this, R.layout.campaign_info_layout, null);
TextView textView = (TextView) layout.findViewById(R.id.textView);
Then set the OnClickListener like this ;
textView.setOnClickListener(new OnClickListener (){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(i);
}
});
I am quite new to developing apps. Still I would have thought that this is a basic action, so if there is already a solved thread I would be OK with the link. But since I am searching for over 2 hours for this I am asking anyway:
I want to dynamically add an element to my layout every time the user clicks a button.
By now I have this:
XML (R.layout.game.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit_choice"
android:onClick="submitChoice"/>
</LinearLayout>
Java
public void submitChoice(View view)
{
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
LinearLayout ll = new LinearLayout(this);
ll.addView(View.inflate(ll.getContext(), R.layout.game, null));
ll.addView(textView);
setContentView(ll);
}
Since the XML file does not change, it only works once.
So how can I add a second text when the user clicks the button a second time (without changing the XML file)? Examples are appreciated.
The problem comes from this line, that recreate the whole layout every time:
LinearLayout ll = new LinearLayout(this);
You should define it and setContentView(ll) outside the submitChoice function. Then on click only create and add the textView , then call ll.invalidate(); to see the changes.
Something like:
LinearLayout ll;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
ll = (LinearLayout) findViewById(R.id.ll_game);
}
// More code...
public void submitChoice(View view) {
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
ll.addView(textView);
ll.invalidate();
}
where ll_game is the id you have to set in xml for your LinearLayout.
Once I click the button, I want text of different sizes to be displayed. I tried using the built-in html support for it but I found out that font size tag is not supporI'm using a TextView to do this but I keep getting an error saying "could not execute method of the activity".
Here is my activity main part:
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32px" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rams"
android:onClick="ramsShow" />
I have the header string resource in string.xml:
<string name="header">Header</string>
This is the method in my MainActivity that is called when the button is clicked:
public void ramsShow(View view)
{
TextView test= new TextView(this);
test = (TextView) findViewById(R.id.header);
String tmp = "hi";
test.setText(tmp);
}
As Ketan suggested you should have:
public void ramsShow(View view)
{
TextView test = (TextView) findViewById(R.id.header);
String tmp = "hi";
test.setText(tmp);
}
To adjust sizes have a look here: How to set text size of textview dynamically for different screens
In Activity class, above the onCreate Method, add this line.
TextView test = null;
Within onCreate method, add this code
test = (TextView) findViewById(R.id.header);
Idea is to move the line from ramsShow method to onCreate method.
Within onCreate() write this code...
RelativeLayout lView = new RelativeLayout(this);
myText = new TextView(this);
myText.setText(Html.fromHtml("<b><center><font size='3'><font color=\"#145A14\">Welcome standard\n"</font></font></center></b>"));
lView.addView(myText);
setContentView(lView);
}