I have been trying for days to find a solution to my problem, but now I've decided to try to ask you. I'm a noob for programming Android, so please forgive me.
I have a main Activity with a Listview in it.
I am using a Simplecursoradapter to feed it with information from my database and a customized layout I made as an XML.
my problem is, I want to change some of the terms or the units that I used in this custom layout, which is not feed through the database.
But if I use settext to the TextViews in there it wont run, the app will crash, because my setContentView is set to another layout for this class, I guess. I have been looking at inflaters, trying to see if I could change the XML programmable, use a string from String.xml and change that. But as far as I can see these are not an option. Later I found this code on StackOverflow
Activity activity = (Activity)getContext();
TextView t = (TextView)activity.findViewById(R.id.txtDisponible);
t.setText("E-ticket validado");
But I can't get this to work because the first line isn't working for me. The Activity won't give me the getcontext method. And even if I did get this to work, I wouldn't know if this would work. Could you guide me in which direction I need to go? As of now, I don't have a class for my ListView, is that the way to go. I want to keep this as simple as I can.
Thank you for your help.
EDIT 1:
Okay i tried to put this in my code:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_list, null);
TextView t = (TextView) view.findViewById(R.id.kmListview);
t.setText("bok");
The app doesn't crash, but it doesn't change the textview either.
Activity extends ContextWrapper class and you should use this or getBaseContext() because there is no method like getContext() which extends ContextWrapper class.
So use this as Context or getBaseContext() in your Actitiy and I think your problem will be resolved.
Related
I have an app with multiple activities and I would like to modify some of its layout properties to recycle them and avoid creating more activities. I tried with LayoutInflater, but when I modify the layout, the methods atached to that layout won´t work. Before that, I had used LayoutInflater to change the layout of an AlertDialog and it worked fine.
Another thing I could do is send parameters with an Intent and retrieve them in the class of the layout I want to change, but like I said, I have a lot of activities and I think there has to be an easier solution.
I include here my try with LayoutInflater ;act2 is the class with the layout I want to change:
LayoutInflater Inflater = act_main.getLayoutInflater();
lay = Inflater.inflate(R.layout.act2, null);
ImageButton restart = (ImageButton)lay.findViewById(R.id.restart);
restart.setVisibility(View.INVISIBLE);
act_main.setContentView(lay);
PD: Sorry if my English is not that good.
I have created a TextView in the UI designer, but I can't figure out how I should access it from the code. I have tried Go To Declaration but that just brings me to the XML file where the TextView is 'made'. Does anyone know how to do this? Help is very much appreciated!
This is independent of the IDE. First you need to "find" the TextView, then you can modify its properties:
TextView myTextView = (TextView) findViewById(R.id.yourid); // The ID is declared in the XML file as android:id atrribute.
myTextView.setText("New Text");
What do you mean by "access it from the code"? If you're talking about navigating from where it's referenced in the code to viewing it in the UI designer, newer versions of Intellij with Android support enabled put tabs at the bottom of the editor when you're editing XML files to let you switch between a text representation and a visual representation of layout files.
If you're talking about how to instantiate the view in code, post some samples of what you've been trying (the most common way is to use a LayoutInflater).
Edit:
Changing the actual text that's displayed in the TextView isn't an IDE-specific issue. You have two ways to do this (well, three if you count the visual and text views of the XML file as separate methods). You can set the text either in the XML file by setting the android:text attribute on the TextView widget, or in the code by calling setText(). Whichever way you decide to do it, you should consider not referring to your text as a raw string but as a String resource as described here.
Edit 2:
OK, you're looking for instructions on how to inflate the view in the first place to get access to it. This is what I answered initially, but here's a little more code. In your Activity (you do have an Activity set up, right?):
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.<your layout ID>, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.<your TextView's id>);
This may be a dumb question, so my apologies if so; I'm fairly new to Android.
But anyway - I have a working ViewStub, which is replaced by different layouts depending different situations. It's working fine with regards to showing the correct layout when I call the setLayoutResource() method, and then setVisibility to VISIBLE. However, I now need some of the content in this view that is being shows to be dynamic (i.e. I need to set it via code rather than just show a static layout).
Is this even possible? The setLayoutResource() method only takes a static layout-resource ID, but I need that layout XML file to be able to have it's TextViews contain non-static text that comes from some code that I have ready to utilize. How should this be approached if possible? I understand the concept of having a Java class, and inflating the XML to attach itself to it to update the fields, but I can't see how that relates to my code at hand, since it's simply a layout resource int I need to set for the setLayoutResource() method in ViewStub.
I can post existing code if needed, but I'm not sure it do much more than clutter up the post. For reference - All I have is a simple layout XML file with some TextViews, and then my main XML containing the ViewStub, which is part of a custom Dialog. The user is able to instantiate the Dialog and set the layout, which in turn sets the layout of the ViewStub. This is the layout in which I need the dynamic content to be used.
Any advice would be greatly appreciated. Thanks!
Turns out this wasn't too difficult to accomplish. I just needed to use the ID of the TextView layouts after inflating the ViewStub to get a copy of the actual TextViews, then I was easily able to set their text to whatever kind of dynamic/custom text I desired.
I also needed to comment out the code that shows it via the .VISIBLE call, and instead do the following (the .inflate() line of code accomplishes the same thing as setting it to VISIBLE):
View inflatedView = dialog.myStubView.inflate();
TextView myTextView = (TextView) inflatedView.findViewById(R.id.my_text_view);
myTextView.setText("Dynamic/Custom Text");
I need to programmatically create a DialogFragment layout, but I don't know how to proceed. I cannot use xml layout because the DialogFragment will be a part of closed-source JAR file.
Normal Dialogs accept an Activity in their constructor, so it is then possible to instantiate a new Layout like this: layout = new LinearLayout(this);. DialogFragments, on the other hand, do not usually take the Activity as a parameter, so I don't know how to perform this first step.
How should I create the layout?
Is it OK to ask for an Activity in the constructor?
Is creating a layout of DialogFragment any different from creating a layout of normal Dialog?
I will be grateful for any other advice regarding manual DialogFragment design.
How should I create the layout?
How ever you want it to look. If you can't/don't want to use a layout file that you inflate and return from the onCreateView method you'll have to build the entire view hierarchy of your new dialog in code.
Is it OK to ask for an Activity in the constructor?
There is no need for this, the Fragment will get a reference to an Activity, you'll have a reference to that context by using getActivity().
Is creating a layout of DialogFragment any different from creating a
layout of normal Dialog? I will be grateful for any other advice
regarding manual DialogFragment design.
No, it's not different. The documentation for the DialogFragment has a great example on how to build a custom DialogFragment, you should check it out.
Basically what I want to do in my Android app is use TextView to display two different pieces of text at once. So in code, I want to be able to do something like this:
LinearLayout ll = new LinearLayout(this);
TextView text = new TextView(this);
text.setTextColor(0xFF000000);
text.setGravity(Gravity.CENTER_HORIZONTAL);
text.setTextSize(20f);
text.setText("Text1");
text.setTextSize(14f);
text.setColor(0xFF0000FF);
text.setText("\nText2");
ll.addView(text);
To clarify, I am trying to display a black "Text1" and a blue "Text2" at once using only a single TextView. Obviously this doesn't work out using the code above. I've considered using a second TextView but that seems like a waste of effort and memory to me. I'm sure the brilliant minds here can come up with the best solution to this.
Thank you very much in advance for your time and your assistance.
There are two options for you.
One is
Spannable
and other is
fromHtml (String source)
So that you can get your desired output.
I think with the current version of the code, you can see only the latest text (Text2).
If you want to have multiple look and feel for two texts, I would suggest use 2 separate TextViews. It would add more flexibility.
If you are not going to change this UI code later, then you can consider Html.toHtml() in setText() call.
It seems the problem is with:
LinearLayout.addView(text);
You are trying to add a view to a LinearLayout, but the layout doesn't exist (in the current activity). You need to add the TextView to a Layout defined in the .xml you are using. Suppose you have a LinearLayout with id "linearlayout01" in the xml file "activity1.xml", you would do something like:
setContentView(R.layout.activity1);
// Create and adjust TextView text
...
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout01);
layout.addView(text);
Once a View is added to a ViewGroup of which LinearLayout is a descendant you do not need to readd it to update its display. If you preform any changes on a view that requires it to change its display it will handle all the proper notifications about required redraws or relayouts by calling View#invalidate and View#requestLayout where appropriate.
In addition, because all UI changes are handled on the same thread you do not need to worry about calling multiple methods that will update the UI. This is because of two reasons, first, the execution of the redraws will not occur until your code is finished, second, android has optimizations built in that combines multiple invalidate calls into one.
So, the only thing you need to worry about is getting a proper reference to your TextView instance and then you can call all the methods on it that you need to make it display what you wish.
Since you are creating your Views manually and not from xml you need to add your root ViewGroup to the Activity by calling Activity#setContentView.
Edit:
Then you're going to need to learn about SpannableString and SpannableStringBuilder. There is some very brief documentation here: Selecting, Highlighting, or Styling Portions of Text
when do you plan to update the textview ? If it is on click of a button then get a reference to the textview and in the onClickListener() update the text, color, etc whatever you want to do.
After seeing your other comments, I think SpannableString is what you are looking for