How to get TextView reference from a custom ImageView? - android

I would like to ask if there is any methods to get a View from another View.
My case is that:
I have a custom ImageView call MyImageView.
and my layout is that:
FrameLayout:
MyImageView
LiearLayout:
LiearLayout:
TextView
LiearLayout:
TextView
I have some code in MyImageView and I would like to edit the text in TextView under the LinearLayout.
my code in MyImageView for select the TextView is:
TextView textView = (TextView) findViewById(id.TextView01);
However textView is always null and I can't set the text that I prefered.
More, if I code this:
TextView textView = (TextView) findViewById(R.id.TextView01);
Eclipse will give me a error and said can;t resolve the id.
So Is there any methods to edit TextView from a ImageView?

First of all... that's a horrible idea (I would be freaked out if I were you). And it's big signal that you have to rethink how to do your layout.
Anyway, using the getParent method could work:
// in your MyImageView
FrameLayout parent = (FrameLayout)getParent();
TextView textView = (TextView) parent.findViewById(R.id.TextView01);
It's not working the way you are doing it, since the TextViews are not inside your custom view.

I think you can always call getRootView() and from there you can search for a view by Id. As long as the ids are unique within the current view hierarchy you should be able to grab the textView no matter where it is in the hierarchy.
So similar to the other answer
TextView textView = (TextView) getRootView().findViewById(R.id.textview_01);
if it's on the same screen, it will be there always.

Related

Clarification regarding setContentView

What is difference between:
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
And
TextView textView = new TextView(this);
setContentView(textView);
I found these two pieces of code. In first the setContentView has a I'd passed to it about layout. And in second case it has a view passed as argument. Is textView also an id. I think the difference is that in first case, it is layout of activity_main as described in XML file(which contains textView as well) and in second case it is id of textView. Tell me if I am correct.
Also tell me what does 'this' refer to here. Why we are using findViewById in first case?
In the first code,
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
you are setting the content view of the container and then trying to access the view with ID - text.
For ex:
<RelativeLayout android:height="match_parent"
android:width="match_parent">
<TextView
android:id="#+id/text"
android:text="Hello"
android:height="wrap_content"
android:width="wrap_content"/>
</RelativeLayout>
In this layout file TextView has the id - text
So,in order to access the textview programatically, we make use of findViewById() t to get reference to view.
TextView textView = findViewById(R.id.text);
now we can make use of this view reference to make changes to the view.
For example we can change text like,
textView.setText("This is a test");
As far as
TextView textView2 = new TextView(this);
is concerned, you're creating a TextView dynamically. This can be added to the parent container as and when required.
Activity.setContentView() has 2 signatures. One is taking a layout id as parameter, the other is taking a View as parameter. There is actually a third one taking a View and ViewGroup.LayoutParam as input.
All three methods take what they get (a View or a layout to inflate) and set it as their root element. So in short: There is no real difference here. Just a few options the developer can choose from to tell the Activity about its root UI element
Also see: setContentView description
The line TextView txtView = (TextView)findViewById(R.id.text); is then searching for a TextView with the id "text" within the Activitys Content (in that case every view in R.layout.activity_main).
The line TextView textView = new TextView(this); is creating a new TextView programmatically instead of inflating a layout xml. The this parameter is a Context instance. A Context instance is always needed to create a View. An Activity is a Context.
When you're using the following:
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
you're using the activity_main layout as the content of the activity. whenever you're trying to bind the view with findViewByid(), it only search for the views inside the layout and will giving you an error if you're trying to bind views outside the layout. See setContentView (int layoutResID) for details.
When you're using the following:
TextView textView = new TextView(this);
setContentView(textView);
You're creating a TextView with the activity (this) as the context with new TextView(this);. Please be noted that you always need a context whenever you're creating a View.
Then with setContentView(textView); you're setting the textView as the sole content of the activity. See setContentView (View view) for details.

Is it possible to replace existing textView with new dynamic textView programmatically?

I need to replace textview which is already define in xml layout file with dynamic textview created programmatically in my Activity class file.
You don't need replace it, just get the particular properties from textView (from Java) and set them on text_view (from XML).
For example:
text_view.setText(textView.getText().toString())
text_view.setColor(textView.getColor())
...
text_view.setAnything(textView.getAnything())
Maybe it helpfull to you
I didn't get why you would do this, but anyhow will layout the steps:
get reference to the TextView containing ViewGroup
call removeView with TextView Reference.
call addView with new TextView.
TextView old = (TextView) findViewById(R.id.old_text_view)
LinearLayout layout = (LinearLayout) findViewById(R.id.container)
TextView new = new TextView()
layout.removeView(old);
layout.addView(new, position);

Accessing Android Views in Java files

I've designed a RelativeLayout with children elements such as TextView and ImageView. What I want to do is to create a RelativeLayout object from my created RelativeLayout in XML, that way I can access to its children elements and modify them (change image from ImageView and change the text from TextView). How can I do this? It would be kind of like this.
RelativeLayout lay = new "myrelativelayout";
ImageView img = lay.children[0];
TextView txt = lay.children[1];
Thanks!
XML:
<RelativeLayout
android:id="#+id/relative_layout_id"
...>
<TextView
android:id="#+id/textview_id"
.../>
</RelativeLayout>
Activity onCreate:
RelativeLayout lay = (RelativeLayout) findViewById(R.id.relative_layout_id);
To change children use:
TextView child = (TextView) findViewById(R.id.textview_id);
child.setText(text);
or:
View child = lay.getChildAt(i);
To access the layout we use the findViewById method. Therefore to change the image in the ImageView you do not really need access to the RelativeLayout.
The way we access any element through its id is as follows:
View v = (View)findViewById(R.id.view_id);
where view_id is the ID of the view.
To access the RelativeLayout, therefore, the piece of code would be:
RelativeLayout lay = (RelativeLayout) findViewById(R.id.relative_layout_id);
But if you only want to change the text in the TextView, you really don't need the above code. It is sufficient if you do a similar access for the TextView:
TextView textView = (TextView) findViewById(R.id.id_of_textview);
textView.setText(text);
A general method of accessing any resource can be found here.

Android Linkify in nested RelativeView

I have a LinearLayout/Scrollview/LinearLayout in which I need two links.
first is a regular TextView - that works fine.
SECOND TextView is in a RelativeLayout with an ImageView left from it, that's not working.
(LinearLayout/Scrollview/LinearLayout/RelativeLayout
ImageView TextView*
)
TextView first = (TextView) v.findViewById(R.id.first_tv);
TextView second = (TextView) v.findViewById(R.id.second_tv);
first.setText(Html.fromHtml(getString(R.string.url_first)));
Linkify.addLinks(first, Linkify.ALL);
second.setText(Html.fromHtml(getString(R.string.url_second)));
Linkify.addLinks(second, Linkify.ALL);
first textview click works.
Why does the second textview look like it's a link but it's not opening the browser on click?
add
second.setMovementMethod(LinkMovementMethod.getInstance());

Android : How to adjust Textview

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.

Categories

Resources