Using ImageView control creating error in Android - java

I am creating an application in android. And when I try running the app after inserting an ImageView control into my app, I am getting the following error in logCat:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bdisys.android.wsaccess/com.bdisys.boinfoportal.ui.FirstAppUI}:
java.lang.ClassCastException: android.widget.TextView
The following came like a warning after I had inserted an ImageView into my layout/main.xml.
The following classes could not be found:
- ImageView (Change to android.widget.ImageView, Fix Build Path, Edit XML)
Following the xml code portion after I had inserted the ImageView.
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/favicon" />
</TableRow>
Can anyone tell why this error comes?
Am not getting any idea about this. Please help. Thanks in advance.

You are triying to cast a TextView into ImageView. Check your resource xml.

I had the same problem with the ImageView, the message The following classes... was showing up in the design tab of my main.xml
In my case the problem was that the drawable had no extension, however it appeared in #drawable/ section and was appearing in the autocomplete too as if nothing was wrong.
Cannot provide any info on the classcast exception though.
To sum up: Make sure your favicon file has the correct extension

if you have code like this
ImageView img;
img = (ImageView) findViewById(R.id.TextView1);
change to
TextView txt;
txt = (TextView) findViewById(R.id.TextView1);

Related

How and Where to define this ImageView in xml?

In this Android Navigation Icon - Profile picture instead of hamburger icon stackoverfolw post
ImageView profileImage = (ImageView)
binding.navView.getHeaderView(0).findViewById(R.id.iv_profile_image);
How an Where to define R.id.iv_profile_image in which xml file ?
I couldn't find the XML code in the example you linked. However, I noticed the following line in the Activity's onCreate() method. This would indicate that the XML file being used is called activity_maps.xml. Most likely this is where the ImageView was placed.
binding = DataBindingUtil.setContentView(this, R.layout.activity_maps);
Assuming this assumption is correct, the ImageView can be declared something like this. The answer to "where" within the XML depends as XML is a declarative language.
<ImageView android:id="#+id/iv_profile_image"
android:layout_width="60dp"
android:layout_height="60dp"
...
android:contentDescription="#string/to_replace" />

Can't get rid of the ugly border around ImageButton

I'm working on an Android App which has a few (Next, Back, ... etc) Buttons which are all round and transparent and I use ImageButton for those Buttons, however the problem is there is always a white border around the Button (I use black background so it is very ugly) and the button never appears to be round is appears as some sort of a square-ish shape.
Here is what I tried so far :
Setting the background of the ImageButton in the activity_mypage.xml file to
android:background="#android:color/transparent"
and to
android:background="?android:selectableItemBackground"
and to
android:background="?android:selectableItemBackgroundBorderless"
even to
android:background="#null"
but nothing seems to work on the .xml side.
and I tried the following on the MyPage.java file :
myBtn.setBackgroundResource(0);
and also
myBtn.setBackground(null);
Nothing seems to work anything I do keeps resulting the same (although it removes a gray border from around the Button but none of them makes the Button completely transparent)
This is a screenshot of the button before applying any of above :
And this is a screenshot of the same Button after applying any of those attributes (doesn't really matter if I applied it only to the xml or only to the java file or to both because all results are the same):
This is my xml code for one of my Buttons in xml :
<ImageButton
android:id="#+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="26dp"
android:layout_marginEnd="37dp"
android:layout_marginRight="37dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="#mipmap/my_btn"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintRight_creator="1"
android:background="#android:color/transparent"
/>
And here is the code for it in the .java file :
ImageButton myBtn= (ImageButton) findViewById(R.id.my_btn);
myBtn.setBackground(null);
myBtn.setBackgroundResource(0);
I'm really lost at this every result I found for this query has suggested one of the above methods but none of these seems to work for me..
Just change
android:background="#android:color/transparent"
to
android:background="#null"
Try with this
android:src="#mipmap/yourimage"
instead of
app:srcCompat="#mipmap/my_btn"
add this to your button
android:background="#drawable/template"
and create a file name it template in your drawable folder
and add this code to it
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/btn2imagePressed"></item>
<item android:drawable="#drawable/btn1imageNotPressed"></item> </selector>
You can use ImageView instead of ImageButton, so background issue will not occur
<ImageView
android:id="#+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="26dp"
android:layout_marginEnd="37dp"
android:layout_marginRight="37dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:src="#mipmap/my_btn"
/>
So after reading some of the posted answers and playing around with the suggestions in my code I noticed that the icon was incorrectly loaded into my mipmap directory instead of drawables and that is because when I imported the icons I selected New -> Image Asset instead of Vector Asset which caused my 'icon' to lose its transparency ...
Thanks to everyone for their replies and answers.

android change imageview programmatically

I am making an android app where the same imageview is displayed every time the user enters a value. but I want the imageview to be changed every time the user enters a value. I wrote a code to do this but the problem is after the first image is displayed I face black screen and it takes me to the first page in the app, it doesn't crash just shows black screen.
here is the imageview code in xml file:
<ImageView
android:id="#+id/ImageSuccess"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:scaleType="centerInside"
android:src="#drawable/image1" />
and here where I changed the imageview in java file:
ImageView myImage= (ImageView) findViewById(R.id.ImageSuccess);
if(userVlue.equals("SCHOOL")){
myImage.setImageResource(R.drawable.image1);
}
else if(userVlue.equals("CAR")){
myImage.setImageResource(R.drawable.image2);
}
else if(userVlue.equals("TOY")){
myImage.setImageResource(R.drawable.image3);
}
One of myImage or userVlue is not set properly and probably is null.
Debug and share logs. Your partcular activity is getting distroyed probably because of nullpointexception hence returning to main activity in backtrack.
I tried with a sample app and your logic is working perfectly.
You are handling user action on some UI element, when you do findViewById, cotext is that UI element, so this method searches ImageSuccess in children of it. Try giving page layout as rootlayout and do rootlayout.findViewById("ImageSuccess");
I found the solution finally, the black screen appeared because the images were only in the drawable file, so I needed to add the images in all drawbridge folders such as drawable-ldp, drawable-ldpi etc...., so that the app can work in different phone sizes.

Setting CustomView in Android ActionBar; CustomView does not appear

I am trying to set a custom view in an ActionBar. I am using the support.v7 ActionBar - min API is 9. For some odd reason, I cannot get the custom view to appear in the ActionBar. Am I missing something painfully obvious? Any help appreciated.
Here is the layout I am inflating:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="flsfjkelsjek"
android:background="#color/black"
android:textColor="#color/white"
/>
...Very simple, just a TextView for testing.
Here is the code to set this view as my custom ActionBar view:
TextView customView = (TextView) myInflater.inflate(R.layout.my_test_textview, null);
myActionBar.setDisplayShowTitleEnabled(false);
myActionBar.setDisplayHomeAsUpEnabled(true);
myActionBar.setDisplayShowCustomEnabled(true);
myActionBar.setCustomView(customView, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Again, very simple and straightforward. I have tried many different varations on this, such as:
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_SHOW_TITLE);
and have even tried that line along with all of the above. I've also tried with a TextView just created on the fly, rather than inflating one.
The custom view simply doesn't appear. When I check the value of
myActionBar.getCustomView()
it returns my TextView. However, the width of that TextView is 0. Does anyone have any idea why the CustomView is not showing up?

Android TextView Wrap

Here is my problem. How to make text don't wrap?
I already tried to make text smaller. Didn't work. Tried to do singleline="true". Here is what he do(2 screen shot 6-7 textview)
1 Screenshot) Here is I made it in the Eclipse
2 Screenshot) Here is how it showen in the emulator
Try setting android:ellipsize to none for each TextView. For more details see documentation on this attribute.
From xml:
<TextView ... android:ellipsize="none" />
From code:
TextView textView = new TextView(context);
...
textView.setEllipsize(null);
try giving android:maxLines=1... singleLine property should also work...

Categories

Resources