Im having some trouble with setting textview to invisible/visible.
basicly i want this to happen when an on/off button has been clicked.
what i did is kind of like
textview.setVisibility(TextView.VISIBLE);
textview.setVisibility(TextView.INVISIBLE);
when i try executing this the emultor says that the app has stopped unexcpetedly
Are you building this from XML or programmatically?
I would make it with an XML file then when the Activity runs change the property. Be sure to use setContentView(R.layout.main); before you try to get the TextView with findViewById(...).
Call .setVisibility(View.GONE); on the TextView to hide it.
Call .setVisibility(View.VISIBLE); to on the TextView to show it.
I have an example that does something like this. You can see the code here: https://github.com/ethankhall/Morse-Messenger/blob/master/src/com/kopysoft/MorseMessenger/Translate.java
Without more code or a stack trace, it's hard to say, but it sounds like you haven't initialized the text view. Here's how to do it:
TextView myTextView = (TextView) findViewById(R.id.tv_text);
Where 'tv_text' is the id of the textview as defined in the xml layout file.
Hope that helped!
Read about DDMS and logcat to obtain a stacktrace and to see what the problem is: http://developer.android.com/guide/developing/debugging/debugging-projects.html
This is what you are looking for:
Related
I have an activity called PeopleActivity, with a layout xml of activity_people.xml.
This activity is using the Android Studio Navigation Drawer Activity template.
I have a list view in the content_people.xml file named people_list. When I attempt to associate this control in the back end, it is unable to find the id. I am attempting it as:
peopleList = (ListView) findViewById(R.id.people_list);
Any insight would be greatly appreciated.
EDIT: Did an update to the code section to reflect the correct control referenced.
you should declare your listview like this:
peopleSearch = (ListView) findViewById(R.id.people_list);
not as EditText
I found the answer. You cannot use capital letters in the naming of your controls. Once I fixed this, everything worked fine.
I'm showing a very simple AlertDialog using this little snippet of code:
AlertDialog.Builder bldBuilder = new AlertDialog.Builder(WarningActivity.this);
bldBuilder.setMessage(strTrace).setTitle(strTrace.split("\n")[0]);
AlertDialog dlgTrace = bldBuilder.create();
dlgTrace.show();
The content of my dialog is a stack trace and therefore the lines can be quite long. I'd like the prevent the wrapping of lines in the default alert dialog but I can't seem to figure out how. I've been trying to dig up this answer but I haven't been able to understand how.
You have 2 possible options: First is to create your own layout. Second is to modify the default AlertDialog layouts' textview
//after creating the dialog, get the textview
TextView textView = (TextView) dlgTrace.findViewById(android.R.id.message);
//then set single line to false
textView.setSingleLine(false);
I am working on an android project in which i have to add links i am following the given below example.. But when i am clicking on the link neither it is highlighting nor i am able to access the link. Clicking on the link is causing an exception which says
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I want to open the link in the browser when i am clicking on the link.How to do it efficiently?. I am expecting an answer soon !Thank you
NB: i am using a dynamic textview in my actual project
public class StackOverflowActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView link = new TextView(getApplicationContext);
String linkText = "Visit the <a href='http://stackoverflow.com'>StackOverflow</a> web page.";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());
}
Use android:autoLink="web" in your TextView xml. And if it does not work, try android:linksClickable="true"
Based on your error, if using dynamic TextView make sure you refer your view with this object as
TextView yourView = new TextView(this);
this refers to your Activity context where I think you might have made a little mistake.
You should not use getApplicationContext for creating a view with Activity context.
I believe you have to inflate the layout that contains your TextView before you can use it, like so:
getActivity().getLayoutInflater()
I'm just beginning to learn to develop android apps, using eclipse.
I created multiple .xml files and put a textView in each one. However, they are all called textView1. So in my main activity, if I want to declare a textView, like
TextView hello = (TextView)findViewById(R.id.textView1);
how will the compiler know which xml file to look at? How should I specify?
In the method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
here you are setting one xml to your activity.
and when you are declaring
TextView hello = (TextView)findViewById(R.id.textView1);
so textview1 of main.xml will called.
When you specify a file in the onCreate using setContentView, it knows which xml file you are talking about
You dont need to worry about that. Android system is smart enough to find the exact id.Android gets the view id from the layout you specify in setContentView() method.
So feel free to use same id in other xml layout file.
Note:--
In same xml file, id of any View should be unique.
Situation:
I have a TextView that have the property
android:textAlignment="center"
I am generating another TextView dinamically, based on my TextView from XML Layout, using a clone, cloning all the basic properties to work the way above.
Problem:
To do this i need to use this method:
this.myTextView.setMyTextViewProperty(MyTextView.getMyTextViewProperty());
for example:
this.MyTextView.setText(MyTextView.getText());
Note that this.MyTextViewis a local variable and MyTextView is a private var declared on the top of the file, under class name.
I do this on all the properties of the TextView but when i hit the following line of code from the TextAlignment property...:
this.myTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
I tried to set it to a Custom Aligment instead of getting from my XML TextView It gives me an error:
11-20 15:27:04.460: E/AndroidRuntime(9185): FATAL EXCEPTION: main
11-20 15:27:04.460: E/AndroidRuntime(9185): java.lang.NoSuchMethodError: android.widget.TextView.setTextAlignment
But i see on Ctrl + Space that the method exists, so i cant understand what is happening.
A second try was, to set my TextView property to the property that comes from my TextView:
this.myTextView.setTextAlignment(MyTextView.getTextAlignment());
With no success, too.
Obs: i do not want a Android XML Layout solution, i want a solution on code, because i generate the TextView dinamically on the Activity
I'm using API Level 15
Any help?
The setTextAlignment method was added in API level 17. Maybe your compiler in the IDE is above 17 and the device/emulator which you are testing is less than that. Here the link to setTextAlignment.
Added from the comments:
For API level 15 and below, you want setGravity, per this question.