I want to put some imageview and buttons on top of screen using relative layout. When I use android:layout_alignParentTop = "true", it just does this:
instead of what I want to:
Any solutions how to implement this into RelativeLayout?
CODE: http://pastebin.com/NGcherYm
Due to long code I had to put in on pastebin.
Remove these lines
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
Related
I am trying to change the background image of an ImageView for different options on Radio buttons in the calling activity. The calling activity passes the parameters in a bundle to rendering activity and I am receiving the parameters properly (I get Curve1 and Curve2 as parameters in the rendering activity after making choice on radio buttons). But I am not able to change the image background either by setImageResource() or setBackgroundResource().
Here is the snippet doing that:
Rendering Activity
private void validateImage()
{ wave = (ImageView) findViewById(R.id.image_wave);
switch (extras.getString("Curve"))
{
case "Curve1":
wave.setB (R.drawable.sinewave1);
break;
case "Curve2":
wave.setImageResource(R.drawable.sinewave2);
break;
}
}
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="shantanu.concussion.usu.concussiontest.TestActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/image_wave"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Can anybody suggest what am I missing?
Thanks :-)
Try to call invalidate manually. Violently change least something. You need to understand where the problem is (wrongly chosen View, View does not change) Try to change another property for example.
May cause in the resource. (e.g., it is transparent)
Check the entrance to the branch switch. (perhaps wrongly runs comparison)
Change the properties through the editor. Maybe something prevents display background.
It's just a stupid mistake.
try
wave.setImageDrawable(getResources().getDrawable(R.drawable.sinewave2));
So to create a Fragment which contains a single ListView, I must do
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Why do I have to place the ListView inside a FrameLayout? Why can't the ListView be the top level view?
UPDATE
I kind of asked the question wrong: what I meant is: Why is it not best practice to make the ListView itself the top level view? I always see the FrameLayout wrapper in online codes.
It can, as long as you don't expect to put anything else in the layout.
FrameLayout, same as Linear and Relative, are containers for several views, but if you only have one you don't need them.
Most layout files seen online assume you may want to expand on your ui by adding necessities like an empty state or fab. Your empty state needs to be on the same layout too.
So I did an Activity that extends the ListActivity, and it worked fine, but then I found out that to make it work in old mobiles, it should extend ActionBarActivity instead, and when I turned it into that, theres a big margin surrounding my ListView. Thanks
For the old mobiles, I think you can import android.support.v7.app.ActionBar and give it a try. For more details, head to https://developer.android.com/guide/topics/ui/actionbar.html Thanks.
I found the problem was being caused by the following properties in the main.xml layout:
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
So removing them fixed the problem, I assume the default margin must be 0dp.
In IOS, an image which can moved is attached like a magnet to a place to a place. I don't know what is like those method in android.
With an Activity you have a layout file. In that layout file you have multiple items that represent your view. The top most item is what is the same as UIView. You can give this layout an id like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/uiview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
Edit
Today we also have ConstraintLayout, which is like RelativeLayout but uses constraints (like iOS) to layout its childs. You can look here for more information: https://developer.android.com/training/constraint-layout/index.html
I have many custom Views. I want to show a specific custom view on Layout. I am using a View and trying to initialize it with custom view. its not working any help please?
View custom=(View)findViewById(R.id.animation_View);
custom=new CustomeView(this, null);
setContentView(R.layout.activity_animation);
Activity_XMl
<View
android:id="#+id/animation_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
</View>
Unfortunately. your approach is wrong. You are inflating a view from xml and then again assigning a new programmatically created CustomView. If your custom view is CustomView, then there is no need to programmatically create a new CustomView.
Your final code should be,
View custom=(View)findViewById(R.id.animation_View);
setContentView(R.layout.activity_animation);
assuming you have correctly extended a View class and added it to your activity_animation xml. For help regarding creating custom view components, check out-
http://www.vogella.com/articles/AndroidCustomViews/article.html
http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-custom-views-2/
http://developer.android.com/training/custom-views/index.html
There are two ways of using a CustomView
First way is using XML file:-
For example, change the activity_animation.xml file as
<com.myapp.CustomView
android:id="#+id/animation_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
</com.myapp.CustomView>
Note that the com.myapp should be replaced with the package name in which you have CustomView.
Then set this xml in ur activity as
setContentView(R.layout.activity_animation);
Second way, is doing it dynamically, without using any xml:-
CustomView customView = new CustomeView(this, null);
setContentView(customView);