imageview source in the java file and the xml file? - java

Looking at the code below;
ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView1);
theImageView.setImageResource(R.drawable.dot);
Is the second line necessary if we established the source (android:src) of the image in the xml file? XML shown below;
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:src="#drawable/dot"
android:id="#+id/imageView1"/>

No. You either do it in XML or in Java code. You don't have to do it twice.
If you have already define ImageView and set image, then you don't even have to get it's reference in Java code (1st line), unless you want to change something.

Related

Why is TextInputEditText producing this error?

I've been trying to change the fontfamily of my application to advent pro. The problem is only my textinputedittext produces this error
java.lang.RuntimeException: Font not found C:\Users\owner\AndroidStudioProjects\Application\app\src\main\res\font\advent_pro_medium.ttf
Everything works fine on buttons and textviews. I've also tried using edittext, it does not produce this error but the fontfamily doesn't get applied. I have my font in the exact same directory that the error states, so why is textinputedittext producing this error? Below is the code of my textinputedittext..
<android.support.design.widget.TextInputEditText
android:id="#+id/etJoinCode"
android:layout_width="match_parent"
android:layout_height="35dp"
android:fontFamily="#font/advent_pro"
android:hint="#string/join_code"
android:textColor="#ffff"
android:textColorHint="#ffff"
android:textSize="14sp" />
Remove android:fontFamily="#font/advent_pro" in your xml code .
1.create a new fonts directory in the assets directory and put the advent_pro_medium.ttf font file here.
2.You can change to this .
Typeface tf = Typeface.createFromAsset(v.getContext().getAssets(), "fonts/advent_pro_medium.ttf");
etJoinCode.setTypeface(tf);

Displaying text using textView() Android

Unsure of how to display text in the xml file, i have code written to output text in the java file but nothing happens when the application is run
TextView tv = (TextView) findViewById(R.id.my_text_view);
tv.setText("Welcome to the Dungeon");
*
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_text_view"
android:text="#string/my_text_view"/>
Use this:
<TextView android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example"
android:layout_gravity="center"
android:textColor="#656565"
android:textSize="20dp" />
And then setText
why are you setting text twice in xml and java, just add once as:
but whatever try to provide : fontsize, and gravity(center) of layout, either programmatically or in xml

Do not show images of ImageView in real devices

I want show image in ImageView, but don't show it. I use XML code and Java code but don't show. show in simulator but in real devices don't show. tested on LG G2, HTC One X, Samsung Galaxy S3.
my xml code :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:background="#drawable/show_sms_header" />
my java code :
Dialog_Header_Img = (ImageView) findViewById(R.id.sms_dialog_header);
Dialog_Header_Img.setImageResource(R.drawable.show_sms_header);
I used to be separated from each.
Please tell me the solution
First of all you should set image in xml by code:
android:src="#drawable/show_sms_header"
not by android:background, well you can set background to color or other image but your main image you should set by android:src
If you changing something about your image, leave first line in your code that you show, and delete second, if setting image is only thing you set, then you can delete both, because you set source of image in xml.
The "src" attribute should be set to your drawable if you want it to be "centerCrop" correctly.
Check this out :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/show_sms_header" />
Make sure that your file, called "show_sms_header", is in your drawable ( /res/drawable) directory.

How do I use this custom drawable class?

Im fairly new to android programming so im not sure on this.
Im trying to set a png as a background but the image is stretching when i use android:background="#drawable/bkgrnd" in my layout xml file
I found this custom class here http://www.anddev.org/viewtopic.php?p=27178#27178 but im not sure how to use it in my code.
Ive copied the code into its own class file and no errors are present.
How do I set the background to the drawable above using this class?
NB: I have abandoned this approach and gone with something much simpler. The answers below did give clues along with some other stackoverflow questions. Answer is below.
Please try this one it will help you.
android:src="#drawable/bkgrnd"
The result can be achieved using scale drawables instead
bkgrnd.xml (drawable xml file)
<?xml version="1.0" encoding="utf-8"?>`
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/bkgrnd"
android:scaleGravity="clip_horizontal"
android:scaleHeight="100%"
android:scaleWidth="100%"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
layout.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bkgrnd" />
In your XML, declare an ImageView like this
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/bkgrnd"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
/>

Setting margin on an ImageView

I'm trying to set the imageview's layout_marginTop to one value for different density/screen sizes. In my values-mdpi folder I have the following line in dimensions.xml
<dimen name="marginTop">10dp</dimen>
In the MainActivity
ImageView image = (ImageView) findViewById(R.id.s_image);
But there is no setmargin method for imageview. Is there a way to do this?
You don't need to do that in code, you can do it in your XML file where the image view is defined. See this page for more details.
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
You're on the right track. It's probably easiest to refer to your dimension value within the xml (rather than set this up in java code).
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="#dimen/yourMarginTopValue" />
Try try to put android:adjustViewBounds="true" to
It's probably better to do it in XML since you seem to already have the ImageView defined in XML.
However, the layout_* XML attributes refer to the LayoutParams of the parent layout, not the view itself. To change them in code, access them with getLayoutParams(), do your modifications and call requestLayout() to schedule a re-layout pass. For example:
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)imageView.getLayoutParams();
lp.topMargin = 123;
imageView.requestLayout();

Categories