I have created some TextView in the view ml, about 5 of them. But the problem is that after I read from xml file the values come, more than 5 sometimes, and there is no limit. It could be better more than 10. However if I can create the TextView from the activity, then I think I can do it somehow.
Assuming you want it add to a view called containingView,
TextView myTextView = new TextView(containingView.getContext());
myTextView.setText("alshareef");
containingView.addView(myTextView);
Related
I want to insert custom layout in android Preference class. My custom layout contains one ImageView and one TextView. There are 7 preferences and all preferences have same custom layout but different images and texts. Now one way to implement such Preference Screen is, make 7 .xml files one for each preference. Is there any other way so that I need to make only one .xml file and I can set image source and text string programatically ?
**EDIT : I need to show all 7 preferences simultaneously. **
You can use one layout with all your views for all preferences, but you need to control them at run time by using:
yourView.setVisibility(View.VISIBLE);
otherView.setVisibility(View.GONE);
thirdView.setVisibility(View.GONE);
fourthView.setVisibility(View.GONE);
. . .
You can use same layout and changing then at runtime.
For example,
TextView tv1 = findViewById(R.id.textview);
tv1.setText("tv1");
layout.addView(tv1);
TextView tv2 = findViewById(R.id.textview);
tv1.setText("tv2");
layout.addView(tv2);
.
.
TextView tv5 = findViewById(R.id.textview);
tv1.setText("tv5");
layout.addView(tv5);
Or you can just change their data at runtime by using only single reference to use same layout.
Hello i am trying to display the content i receive in an activity using TextView but it seems that TextView is overlapping a button that i have put in activity's UI.My goal is to put TextView and the button side by side. I have put the TextView in the UI dynamically like this:
String display = extras.getString("EXTRA_MESSAGE");
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setWidth(20);
textView.setHeight(20);
textView.setText(display);
setContentView(textView);
I know i miss something but i cant find what it is,so can you please suggest a way how to fix that?
Thanks a lot in advance!
When you call setContentView(textView); it changes your layout to just the textView so it isn't overlapping your Button but your Button isn't shown anymore. You need to add it to your layout and put it where you want it.
You can do this by getting a reference to your root View in your xml and calling addView(textVie) on that root View and use addRule() to position your TextView where you want. However, if it isn't necessary to add your TextView dynamically then it is much easier to declare it in your xml.
If you do want to add it dynamically still, then this SO answer, and many more, covers it.
I'm wondering if anyone can shed some insight as to the best practice for dynamically creating controls (inflate vs instantiate).
Inflate:
TextView styledText = (TextView)inflater.inflate(R.layout.styledTextView);
Instantiate:
TextView styledText = new TextView(mContext);
styledText.setTextAppearance(R.style.StyledTextStyle);
The object being created can either contain attributes in the inflated XML file, or be contained in a Style definition which is added to the instantiated object afterwards. (Assume that this styling includes width, background, text color, etc).
Haven't been able to run any time/memory tests of each method, was wondering if anyone knew which was quickest/most efficient.
LayoutInflator has a slight overhead because it has to parse xml in order to build the object. It also temporarily takes more memory for the same reason. Other than that, it builds the View object in the same manner that you would anyway. It may be something to worry about if you call it hundreds of times a second for some reason. 99.9% of the time though you'll never know the difference.
Also to note, any method that accepts an xml resource like "setTextAppearance" will have the same xml parsing overhead. The only difference in the examples you provided is it's not parsing the TextView xml, but it would still have to parse the style attributes.
Though this post asks about controls specifically, I think it's relevant to note that .. for working with a layout you want to dynamically create/add, I found in using the new (aka instantiate) approach , I was not able to get a reference to an inner ImageButton element that was defined in the xml file for which I instantiate the layout object reference.
When I use the inflate approach, the ImageButton was present upon reference.
So in my case:
Works :)
LayoutInflater inflater = LayoutInflater.from(getActivity());
CardView myCardView = (CardView) inflater.inflate(R.layout.my_cardview, null);
ImageView icon = (ImageView) myCardView.findViewById(R.id.iconId);
~~~~~~~~~~~~
Don't Work :( .. variable icon is null in this case
CardView myCardView = new CardView(getActivity());
ImageView icon = (ImageView) myCardView.findViewById(R.id.iconId);
I am trying to add a TextView from the activity when a button has been pressed. I have found how to add a new textview from the activity, however instead of coding the required layout parameters is it possible to copy an existing textviews parameters (in the xml layout) to the new textview?
I have tried:
TextView tv1 = new TextView(this);
TextView tv2 = (TextView) findViewById(R.id.basetext);;
// its this line below which doesn't work
tv1.setLayoutParams(tv2.getLayoutParams());
But it doesn't copy any of the layout parameters...
Any ideas?
You can do One thing.
If you are trying to set just the Value of the Same TextValue every time and if the Layout properties of that TextView is same at all the time then follow below steps:
First Create the One Layout that only contain he TextView layout only with your appropriate properties. (Let name it as layout_textView.xml)
Now, do add that layout_textView.xml dynamically to your main view as per your requirement.
How it will Solve your issue.
If any query then let me know.
Did you call requestLayout() after adding the textview?
Why is
TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();
generating a null pointer exception? The id is correct, testTextView is correctly declared in my XML layout file.
The only reason for findViewById to return null if you are passing a valid id is that you are either setting the wrong content view (with setContentView) or not setting a content view at all.
I think you might have written setContentView(..) after defining the TextView. Reverse these, and it should work.
Change:
TextView test = (TextView) findViewById(R.id.testTextView);
.
.
setContetView(..)
To:
setContetView(..)
.
.
TextView test = (TextView) findViewById(R.id.testTextView);
You probably haven't called setContentView. You can only use findViewById to get elements of views that have already been inflated.
You could also use a layoutinflater to inflate the view, but that's probably not what you want.
Are you sure the TextView is set on the right XML?
For example if you're creating a Dialog that loads a custom XML, to get an element from that xml you have to mention it in dialog.findViewById(R.id.testTextView);
Like Falmarri said, the view has to be inflated.
I understand you solved it by creating a new project, but still thought to mention it for future users.
It can also be that you defined the activity in two files. For example layout and layout-v21 and some information like id is missing on one of them. So check all the activity's layouts
In my case, the layout was not finished inflating. Solved by adding a small delay before trying to access the TextView.
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();
}
}, 100);
I struggled with this for a while and what I realized was, that, if you have more than one layout file version like:
"activity_one.xml" in "layout" folder and one in "layout - small" folder
Which I used for multiple phone layout support, the problem was that one of the TextViews was in both, with the exact same ID and everything, however the difference was that one was higher up in the hierarchy of views in the layout.
When I changed them to be in the same spot it worked.
(I know this is already answered, but maybe this helps someone out there. Very rare though.)