Hi I am making a custom view and giving other component id as a reference in it like this
<com.example.timerview.CustomView
android:id="#+id/custom_view"
android:layout_width="100dip"
android:layout_height="100dip"
mtv:associatedTextView="#+id/text_view"
/>
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
now I want to use this TextView in CustomView.java so I am doing this in constructor
int tId = a.getResourceId(R.styleable.CustomView_associatedTextView, 0);
TextView tText = (TextView) findViewById(tId);
but I found tText is null please advise me where I am wrong or How can I do such thing. Please help me.
Your TextView is not a child of your custom view, which is why findViewById cannot find it.
Call findViewById from the root view, after the custom view has been attached:
protected void onAttachedToWindow() {
super.onAttachedToWindow();
int tId = a.getResourceId(R.styleable.CustomView_associatedTextView, 0);
TextView tText = (TextView) getRootView().findViewById(tId);
}
Related
I want to get a view from his ID, from the attribute in the xml. I have tried with getParent() but it's return null.
XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ntwldg="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#drawable/background_settings"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/revealView"
android:gravity="center"
android:text="TEST"
android:background="#android:color/darker_gray"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.dot.networkloading.NetworkLoading
android:id="#+id/network_loading"
ntwldg:text="Youtube"
ntwldg:image="#drawable/ic_youtube"
ntwldg:imageBackground="#drawable/ic_youtube"
ntwldg:revealView="#id/revealView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
The attribute revealView is referencing the View above.
Code - NetworkLoading (init())
title = (TextView) findViewById(R.id.title);
image = (ImageView) findViewById(R.id.image);
imageBackground = (ImageView) findViewById(R.id.imageBackground);
finishView = findViewById(R.id.revealView);
String text = attributes.getString(R.styleable.network_loading_text);
imageId = attributes.getResourceId(R.styleable.network_loading_image, 0);
imageBackgroundId = attributes.getResourceId(R.styleable.network_loading_imageBackground, 0);
finishView = ((View) getParent()).findViewById(attributes.getResourceId(R.styleable.network_loading_revealView, R.id.revealView));
It seems that you're trying to find your revealView too early. In the constructor, the View will not have yet been added to the layout it's in, so getParent() will return null. Keep the resource ID for the revealView as a field, get its value in the constructor, and then find the View in the onAttachedToWindow() method.
In the code you've posted, remove the last line - the last finishView = ... line - and instead save the resource ID to your field.
revealViewId = attributes.getResourceId(R.styleable.NetworkLoading_revealView, R.id.revealView);
Then find the View in the onAttachedToWindow() method instead.
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
finishView = ((View) getParent()).findViewById(revealViewId);
...
}
so I have this getter function in a Class called ItemEnergyGreen:
public TextView getTextView(FrameLayout fl) {
textView = (TextView) fl.findViewById(R.id.textview_energyOrange_quantity);
return textView;
}
And I want now to call this methode from another Class:
//Item Energy Green
itemEnergyGreenFrameLayout = (FrameLayout) inflater.inflate(R.layout.item_energy_green_layout, null, false);
itemEnergyGreenQuantityTextView = itemEnergyGreen.getTextView(itemEnergyGreenFrameLayout);
btn_ItemEnergyGreen = (ImageButton) itemEnergyGreenFrameLayout.findViewById(R.id.btn_energyGreenItem_use);
btn_ItemEnergyGreen.setOnClickListener(this);
The function call is in Line 2, I am passing the frameLayout to the getter Function but textView in my ItemEnergyGreen class is always NULL.
The error occurs when i want to call setText on my view:
public void itemEnergyGreenUpdateNumber() {
int quantity = itemEnergyGreen.getQuantity();
String qstring = String.valueOf(quantity);
itemEnergyGreenQuantityTextView.setText(qstring);
}
This is the XML-FrameLayout that is get inflated:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ItemEnergyGreenFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="#+id/btn_energyGreenItem_use"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/item_energydrink_green" >
</ImageButton>
<TextView
android:id="#+id/textview_energyGreen_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#F00"
android:clickable="false"
android:padding="2dp"
android:textColor="#FFF"
android:textStyle="bold" >
</TextView>
</FrameLayout>
Why is that?
ps. At the point where the getter function is called, the itemEnergyGreen Object is already initialized.
Thank you for your answers, and sorry if that is a dump question but i´m going crazy about this.
textView in my ItemEnergyGreen class is always NULL.
findViewById looks for a view in the current view hierarchy. Probably TextView is not a child of FrameLayout. So initialization fails leading to NullPointerException.
The other reason could be that you referenced wrong id.
Edit:
Id referenced in wrong
<TextView
android:id="#+id/textview_energyGreen_quantity"
Change this
textView = (TextView) fl.findViewById(R.id.textview_energyOrange_quantity);
to
textView = (TextView) fl.findViewById(R.id.textview_energyGreen_quantity);
Try this
private View mView;
mView =inflater.inflate(R.layout.item_energy_green_layout, null, false);
itemEnergyGreenQuantityTextView = itemEnergyGreen.getTextView(mView);
I need to create a view that is moving from one end of screen to other end at the bottom of screen.
Means like in new channels flash news is moving contiguously at bottom.Similar concept is what i want.
I dont know what widget is to be used.I tried flipper but in that only 1 textview is replacing by other only.I need it to move from one end to other and change the content.
Can anyone help?
I tried with the below mentioned answer using marquee..but still it is not moving..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.label);
t.setSelected(true);
}
//xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:text="#string/hello_world"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
/>
it worked.
Problem was actually i gave a string which is very small (hello world) now i give a new lengthy string.so it worked
You can do this.
xml for TextView
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true" />
code for this TextView
If you want to make a ticker
textView.setSelected(true);
If you want to stop it
textView.setSelected(false);
*** * EDIT ** ****
I assume that you are extending your activity from ListActivity. Do the following In your onListItemClick
public void onListItemClick(ListView parent, View row, int position, long id) {
TextView textViewOne = (TextView)row.findViewById(R.id.text_view);
textViewOne.setSelected(true);
for (int i = 0; i < parent.getChildCount(); i++) {
if(i!=position){
View view = (View) parent.getChildAt(i).findViewById(R.id.view);
textViewOne = (TextView)view.findViewById(R.id.text_view);
textViewOne.setSelected(false);
}
}
}
just check it out :
xml file :
<TextView
android:id="#+id/myTextView"
android:ellipsize="marquee"
android:singleLine="true"/>
in code :
tv = (TextView) findViewById(R.id.myTextView);
tv.setSelected(true);
here you have to take textview as single line
I don't totally understand what you want to achieve but apparently you have two options:
create your own view: http://developer.android.com/training/custom-views/index.html
or create some animation and use an existing view: http://developer.android.com/training/animation/index.html
I have a Listview that originally listed items from a menu item with one single TextView. The code I used to retrieve the item selected is
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id)
{
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
});
I've now added another TextView so that I have two values for each item one under the other and my menu_item.xml looks like this
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="#+id/rowid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/exhibitor_header" />
<TextView
android:id="#+id/rowidtwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/listview_background"
android:layout_below="#+id/rowid"/>
</RelativeLayout>
How can I retrieve the value for rowid when the item is selected?
Then instead of casting the whole view, find inside that view the correct TextView object. You can reference it by using the findViewById method and the TextView's id:
TextView textView = (TextView) itemClicked.findViewById(R.id.rowid);
String strText = textView.getText().toString();
TextView textView2 = (TextView) itemClicked.findViewById(R.id.rowidtwo);
String strText2 = textView2.getText().toString();
I suggest to use custom view and custom adapter to have 2 textviews in the list view items and bundle those 2 text views in a LinearLayout.
Now when the onClick called, just cast it in linearlayout and on basis of that by using findviewbyid you can have the access to both of the text views.
I'm trying to retrieve the getText() value of a TextView defined in my ListItem Layout, inside the setOnItemClickListener method.
Basically, I want to log data (Id, LookupKey, DisplayName, or PhoneNumber) allowing me to retrieve the contact selected regardless of the listview/adapter used.
I display a simple TextView and an Imageview which visibility is set according to the data.
here is the layout : contact_entry.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:baselineAligned="false"
android:layout_width="fill_parent" android:padding="5px">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="16px"
android:text="#+id/contactEntryText" android:id="#+id/contactEntryText">
</TextView>
<TableRow android:paddingLeft="5px" android:visibility="visible"
android:paddingRight="5px" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/indicator_logo_table"
android:gravity="right">
<ImageView android:layout_width="wrap_content"
android:id="#+id/imageView1"
android:layout_height="fill_parent"
android:src="#drawable/indicator">
</ImageView>
</TableRow>
</LinearLayout>
the adapter i use is defined below :
SimpleAdapterWithImageView adapter = new SimpleAdapterWithImageView(
this, R.layout.contact_entry, cursor, fields,
new int[] { R.id.contactEntryText });
mContactList.setAdapter(adapter);
( You could have guessed it's for displaying the contact's Display_Name from the ContactsContract ContentProvider. )
and here is finally the mContactList.setOnItemClickListener ...
mContactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);
Log.i("done","linear layout"); // it's not done at all ! findViewById returns null;
//this part is currently useless
TextView displayed_name = (TextView) linear.findViewById(R.id.contactEntryText);
Log.i("done","Displayed name = linear.findViewById contactentrytext");
Toast.makeText(getApplicationContext(),
// displayed_name.getText(), //
//view.getContentDescription(), //
Toast.LENGTH_SHORT).show();
}
As you can see i'm only trying to display a toast for the moment, in the end the name will be put as an extra for an intent.
So : the best result i got these last 36 hours (sigh) is displaying the name of the first contact displayed in the toast, even when clicking on the second item...
TextView displayed_name = (TextView) findViewById(R.id.contactEntryText);
Toast.makeText(getApplicationContext(),displayed_name.getText(),
Toast.LENGTH_SHORT).show();
}
I've been trying stuff like the tutorial google provided here, which (am I wrong? I'm new to Android so don't hesitate to slap me in the face ...) actually casts the whole View in a TextView ...
plus other tricks like view.getChildAt() and so on... for hours.
I'm convinced it's not that hard, yet i'm running in circles !
I don't think it is because of my adapter, but if anyone thinks it could be the cause, i could provide the code in another post.
Thanks in advance !
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);
Here is a mistake - you shan't use R.layout in findViewById. Usually, you need to use R.id there, or replace findViewById by inflating a layout. But in this case you just need the view from arguments.
Replace the quted code with this one:
LinearLayout linear = (LinearLayout) view;