Image View find by Name - android

HI am trying to get an image view using the approach
int mId = getResources().getIdentifier(s, "id",getPackageName());
ImageView img = (ImageView) findViewById( mId);
img.setImageResource(kBonusImage);
but I am getting 0 in mId.
Here is my xml layout:-
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="35dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:id="#+id/trFirstRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imgFirst"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#drawable/cell_border"
android:gravity="center"
/>....

The problem is that you are trying to load an ImageView with an id with the name"id". However, the XML layout file does not have an ImageView with android:id=#+id/id". This means that findViewById() cannot find the ImageView you are requesting. The specified id must exactly match one from your XML view file.
Using getResources().getIdentifier() is completely unnecessary. The typical way to get a View object in Java code is to use the R.id class which is generated from your XML layout files when you build your app. In this case, you have defined in your XML layout file a ImageView resource with id imgFirst. So you can call findViewById() like this:
ImageView img = (ImageView) findViewById(R.id.imgFirst);
This assumes that you have previously called setContentView() to load the XML layout file which contains this ImageView resource.

Related

Cannot find view by ID when overlaying resources?

I have a layout that contains a TextView that looks like this
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/visual_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor=?android:attr/colorAccent
android:maxLines="3"
android:gravity="center_vertical" />
I have specified an overlay directory and I'm attempting to overlay the layout file using a similar file except with some minor differences
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/visual_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor=?android:attr/colorPrimary
android:maxLines="3"
android:gravity="top" />
My application builds successfully, however, I'm seeing a NullPointerException during runtime when my Java code tries to set the text of the TextView
private void setVisualText(View someView) {
View view = getLayoutInflater().inflate(R.layout.myLayout, someView, false);
TextView myText = (TextView)view.findViewById(R.id.visual_text);
myText.setText(R.string.myString);
}
The exception happens only with the overlay file and the code runs fine without the overlay. Does anyone know why myText is null and why it's not finding the view correctly? My guess is it's related to the ID but I'm not sure why it's not resolving correctly.
So you have a TextView and an overlaying TextView?
If so, you should not use the same ids. Ids should be used only once.
Why are you calling?
getLayoutInflater().inflate(R.layout.myLayout, someView, false);
If you want to get a view from the main contentView, you can just use:
findViewById(id);
because if you want to attach the inflated view to the root you have to call:
getLayoutInflater().inflate(R.layout.myLayout, someView, true);
please try it with (..true).
by using the line above, you are getting the contentView view.

ImageButton displays differently when created dynamically

I have a GridLayout-based View to which I am dynamically adding several ImageButtons. I'm trying to understand why the ImageButtons are styled correctly when I inflate them from a layout xml file, but not when I create them using the ImageButton constructor directly.
The GridLayout and ImageButtons were previously both defined in the same layout .xml file (and rendered as expected):
<ScrollView
style="#style/my_list_style"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="12dp">
<GridLayout
android:id="#+id/my_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<!-- These ImageButtons are being converted to dynamic. -->
<ImageButton
style="#style/my_button_style"
android:src="#drawable/image1" />
<ImageButton
style="#style/my_button_style"
android:src="#drawable/image2" />
</GridLayout>
</LinearLayout>
</ScrollView>
To convert the ImageButtons to dynamic, I first removed them from the layout file and used code like the following to add them at runtime:
ImageButton imageButton = new ImageButton(context, null, R.style.my_button_style);
imageButton.setImageResource(R.drawable.image1);
parent.addView(imageButton);
But the buttons failed to render properly; they are not centered, and their sizes do not appear to be correct/uniform.
I then tried creating a new layout file, containing nothing but the ImageButton and its style:
<ImageButton
style="#style/my_button_style"/>
When I inflate this layout into the GridView at runtime, everything looks as expected:
LayoutInflater inflater = LayoutInflater.from(context);
ImageButton imageButton = (ImageButton) inflater.inflate(
R.layout.my_button_layout, parent, false);
imageButton.setImageResource(R.drawable.image1);
parent.addView(imageButton);
Why does inflating the view with LayoutInflator give different results than creating the button directly from its constructor?
Because when you create ImageButton manually, you do not specify its parent, hence it doesn't know the layout params of its parent and can't be laid out as you expect.
On the other hand, when you inflate it via LayoutInflater, you are specifying the parent. Then correct layout params are being passed to children. That's why you see difference.
Have a look at detailed article by Dave Smith.

setImageAlpha of imageView causing error

I would like to set the alpha of this:
<ImageView
android:id="#+id/DashboardView_upBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:src="#drawable/ab_logo"
android:paddingLeft="#dimen/layoutPaddingExtra"
android:paddingTop="#dimen/layoutPaddingExtra"
android:paddingRight="#dimen/layoutPaddingExtra"
android:paddingBottom="#dimen/layoutPaddingLess"
android:contentDescription="Up"
android:layout_gravity="center_horizontal" />
I have correctly got the resource in code and called it _upButton. This:
_upBtn.setImageAlpha(20);
Gives this:
android.widget.imageview does not contain a definition for setImageAlpha
Why?
The ImageView needs to be casted to an ImageView to allow the imageview to access the functions within the ImageView.
ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);
img.setImageAlpha(20);
If you are using an API below API-16, then please use:
img.setAlpha(20);
instead of:
img.setImageAlpha(20);
Good luck!

Get XML layout inside another XML layout by code [Android]

I have one xml file main_layout.xml and in this I have another xml layout popup_layout.xml which is added by :
<include
android:id="#+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
layout="#layout/popup_layout" />
In the file MainActivity.java I would like to get the popup object so I can change it's textfields etc. What do i write?
I've tested
View v = findViewById(R.id.popup);
TextView tv= (TextView) v.findViewById(R.id.ip_answer);
but it doesn't work
The id attribute for include is not required. Add id attribute to the layout you want to include , then find view by that id an collect it accordingly as the type of layout
eg:
RelativeLayout/LinearLayout= findViewById(id);
then find the text view .
This should work

Custom ListView?

I have a code made by Fedor, it can be found "here".
The first image is what I have now,
and the second image is what I want to accomplish.
Can someone guide me with this. I have been struggling for days trying to solve this problem.
Please help me, Thanks in advance!
Here is an example of something similar. You have to create a custom adapter for your ListView.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
You can probably use most of that example. Just change the row.xml to create tha layout you want and the getView() in the adapter.
You just need to modify the list item layout (in item.xml) to have another ImageView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="#+id/image1" android:layout_width="50dip"
android:layout_height="50dip" android:src="#drawable/stub"
android:scaleType="centerCrop" />
<ImageView android:id="#+id/image2" android:layout_width="50dip"
android:layout_height="50dip" android:src="#drawable/stub"
android:scaleType="centerCrop" />
<TextView android:id="#+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_gravity="left|center_vertical" android:textSize="20dip"
android:layout_marginLeft="10dip" />
</LinearLayout>
and modify LazyAdapter's getView() method to add support for the second ImageView.
A tutorial for creating a Custom ListView can be found here:
http://justcallmebrian.com/?p=139
Just need to change the XML layout for each item to have 2 ImageViews like Robby said. Then in your getView of your Adapter (LazyAdapter if you followed along with the other people's answers) you should have something like this:
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setResource(R.drawable.icon1);
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setResource(R.drawable.icon2);
TextView text = (TextView)findViewById(R.id.text);
text.setText("I have 2 images");
The tutorial I pasted earlier depicts a way to make the generation of the list dynamic (i.e. not having the resource of R.drawable.icon1/2 and not having the text for your Text images). Something like this may work (assuming you have a Model class that will hold all 3 pieces of information):
int resid1 = context.getResources().getIdentifier("com.domain.sub:drawable/" + myList.get(position).getImage1Name, null, null);
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setResource(resid1);
int resid2 = context.getResources().getIdentifier("com.domain.sub:drawable/" + myList.get(position).getImage2Name, null, null);
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setResource(resid2);
TextView text = (TextView)findViewById(R.id.text);
text.setText(myList.get(position).getText());
Of course the snippet above assumes you have an ArrayList called myList that also getters to get the image names and the text to display.

Categories

Resources