Ability to use XML and Android programmatically - java

I have a android layout in xml, and I have an image i placed using java/android code. I have placed the image without using xml, but I was wondering if I am able to also use XML to position buttons on the page as well. I don't know how to do this because I am already using setcontentview to draw my image to the screen... and if I use setcontentview(R.exampleXMLfile) it will overwrite my image. Does anyone know a solution or example for this? The reason I am looking is because its a pain to add a button without using XML and eventually I want to have animations on the screen. Thanks,

You could use ImageView in your xml file,
<ImageView
android:id = "#+id/my_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_drawable"
/>
or just View and set Background, or set background on any Layout in your xml.
<View
android:id = "#+id/my_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>

Related

Spacing a textview

I'm trying to make a basic text-based adventure game in Android studio. I'm using a textview to display the map. It has weird spacing, like this:
Current Layout:
However, I want it to be spaced out like this:
Desired Layout:
How can I do this?
If your app supports API 16 and higher, you can use the android:fontFamily attribute on your TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"/>

Setting Spinner background to null programmatically

I'm trying to set the background of a Spinner to null programmatically as would the following XML code:
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/spinner1"
android:background="#null"
/>
I tried either of:
spinner1.setBackground(null);
spinner1.setBackgroundDrawable(null);
spinner1.setBackgroundResource(0);
But none of these three give the same output as the xml. Via xml, the whole arrow region disappears, including padding, whereas via code, only the arrow sign disappears but the full length of the arrow region remains.
Any clue?
Just set it to Transparent instead. Make a style for the spinner, or simply set it's background="#00000000"
or if you want to do it in code you could use a style swap or simply declare a drawable.
Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
mySpinner.setBackground(transparentDrawable);
Note* make sure you use the correct setBackground for the correct OS as it deprecated and moved from setBackgroundDrawable after lollipop I believe it was.

How can I create a view that could hold either an image or a video?

In my app, users can either take a picture or record a video. The file is then saved and its path is passed through an intent to the next activity, which displays it for editing.
My question is, how can I easily use the same view for either a video or an image?
I tried dynamically adding a view at runtime when I know what the file type is, but it turned out to be too hard to configure, not to mention the issues that VideoViews have with being rotated.
Edit:
I forgot to mention that using the same view would be preferable, since I'm not actually going to do anything to the contents that's specific to each kind of view.
Add both the ImageView and VideoView in your layout. Then set the visibility to GONE or VISIBLE for the one you want.
You can create two child views, one for the image and one for the video. And depending on the type of your view you can hide/show your child views.
You can also use FrameLayout for this
By using frame layout / Relative layout you can place a view on top of another view. After creating the views like i mention and using the visibility Gone & Visible you can manage to handle it..
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_drawable"
android:scaleType="fitCenter"
android:visibility="gone"/>
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:padding="5dp"
android:id="#+id/VideoView"
android:visibility="gone"/>
</FrameLayout>
Then based on your usage you can control it dynamically like below
// register the views
ImageView mImageView= (ImageView)findViewById(R.id.ImageView);
VideoView mVideoView= (VideoView)findViewById(R.id.VideoView);
// check your conditions like show video view r image view here
if(VideoView)
{
mVideoView.setVisibility(View.VISIBLE);
mImageView.setVisibility(View.GONE);
}
else if(ImageView)
{
mImageView.setVisibility(View.VISIBLE);
mVideoView.setVisibility(View.GONE);
}
Hope it will work for you :)

How to create a Layout were I can place anything where I want?

I'm designing an app with many images, buttons and textViews strewn across the screen. At the moment I am using the relative layout as it seemed the most flexible of the lot. However were I place my elements and their size is still restricted to being aligned with other elements. Even worse if an element changes size any elements aligned to it will also change size.
There must be a simple solution to this! Apple's nib files perform this so easily with an effortless drag and drop to any location; yet android appears to be stuck with restrictive table/linear/relative/grid layouts.
If possible can the solution be performed via eclipse. If not please guide me to the relevant documentation to learn to create my own layouts via xml, create a huge grid layout or whatever horrors await me :)
Thanks
I think what you want is something like an absolute layout tho these were deprecated a while ago, Im pretty sure you can still do this via a Relative layout, you don't necessarily need to align the via with another view, I guess you could just do something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="70dp"
android:layout_marginTop="82dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="172dp"
android:layout_marginRight="84dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
all I'm doing here is aligning it with the side of the parent and having a margin between it
You can do what you ask with a Frame Layout and setting the position of each object. But do so at your own risk. The reason Apple nib files let you arbitrarily place objects is taht the aspect ratio of all their devices is the same. So your layouts just scale up and down evenly.
Android is a more diverse ecosystem, and you should try to embrace layouts that adapt to different screen sizes and orientations.
Take a look at Android custom layout and Android - How to draw a letter at a specific point?
Are you planning to only use the app on a single android device model? If yes, check AbsoluteLayout's Alternatives. (FrameLayout or RelativeLayout)
It's not a good idea to put "Anything Anywhere you want" since Android devices have a lot of different screen sizes and properties. The only option would be to define your own custom layout.
I actually really like the way Android tries to make your layout compatible with as much devices as it can using alignment and structured layout views.
The reason it's simple for Apple is that you're only targeting iPhone, which has a fixed screen properties accross all devices.
Hope it helps. Good luck.

RelativeLayout and accessebility support

I am adding accessebility support to some application. It works fine with standart UI elements (buttons for example), but for some reason does not work with my custom element, wich is RelativeLayout with ImageView and TextView (it looks like icon). I've defined android:focusable="true" and set contentDescription.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:contentDescription=”my content description”
android:focusable="true">
<ImageView
...
/>
<TextView
...
/>
</RelativeLayout>
Could someone please list here all posible causes?
UPDATE:
Is there some way to know what layouts are on the screen at the moment and what order do they have (some layouts are transparent)?
Use hierarchy viewer for understanding where is your invisible views.
The android docs have a section about designing for accessibility, is this any use to you?

Categories

Resources