Adjust view width and height to 50% - android

I'm creating a gallery with custom adapter (a relative layout in witch i place an imageview as a thumbnail, a textview as Name and other staff).
The gallery displays in fullscreen perfectly, but this is not what i want.. i want the custom relative layout to be scaled to 50% in height and width relatively to the screen size. I tried this :
gallery.getLayoutParams().width = relativelayout.getWidth() / 2;
gallery.getLayoutParams().height = relativelayout.getHeight() / 2;
This was a wrong try, it just set the height and width to the correct size without scaling the content.
This is the original view :
This is what i've expected :
And this is what i've got :
This is the layout Cover :
<?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"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivTourCover"
android:layout_width="640dp"
android:layout_height="240dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/croppedstopgallery_f45d88e4_7864_4dbc_9123_ad7953d8724a" />
<ImageButton
android:id="#+id/ivHorizontalLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tvTourName"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="0dp"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/horizontal_line" />
<TextView
android:id="#+id/tvAuthorName"
style="#style/Cover.AuthorName"
android:layout_width="fill_parent"
android:layout_height="19dp"
android:layout_alignTop="#+id/ivAuthorThumbnail"
android:layout_marginLeft="12dp"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/ivGo"
android:layout_toRightOf="#+id/ivAuthorThumbnail"
android:text="Anna Greenleaf" />
<ImageView
android:id="#+id/ivAuthorThumbnail"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_alignLeft="#+id/ivHorizontalLine"
android:layout_below="#+id/ivHorizontalLine"
android:layout_marginTop="20dp"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/croppedstopgallery_f45d88e4_7864_4dbc_9123_ad7953d8724a" />
<ImageView
android:id="#+id/ivGo"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_alignRight="#+id/ivHorizontalLine"
android:layout_alignTop="#+id/tvAuthorName"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/tour_cover_go" />
<TextView
android:id="#+id/tvAuthorDescription"
style="#style/Cover.AuthorDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvAuthorName"
android:layout_alignRight="#+id/tvAuthorName"
android:layout_below="#+id/tvAuthorName"
android:text="London native and London over!" />
<SurfaceView
android:id="#+id/svFooter"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#color/CoverFooter" />
<TextView
android:id="#+id/tvFooterText"
style="#style/Cover.FooterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/svFooter"
android:layout_alignBottom="#+id/svFooter"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="Individual" />
<com.example.zipdownload.utilities.ui.AutoResizeTextView
android:id="#+id/tvTourName"
style="#style/Cover.TourName"
android:layout_width="wrap_content"
android:layout_height="79dp"
android:layout_below="#+id/ivTourCover"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:gravity="center|top"
android:text="Beautiful London in Winston Churchill&apos;s footsteps" />
</RelativeLayout>
The View Gallery :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Gallery" >
<Gallery
android:id="#+id/gCoverflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="top" />
</RelativeLayout>
And the Activity Gallery :
public class Gallery extends Activity {
private android.widget.Gallery gCoverflow;
private RelativeLayout rlGallery;
private CoverAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
rlGallery = (RelativeLayout) findViewById(R.id.rlGallery);
adapter = new CoverAdapter(this);
gCoverflow = (android.widget.Gallery) findViewById(R.id.gCoverflow);
gCoverflow.setAdapter(adapter);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
gCoverflow.getLayoutParams().width = rlGallery.getWidth() / 2;
gCoverflow.getLayoutParams().height = rlGallery.getHeight() / 2;
}
}

You can try to use ScaleAnimation but in this case touch events will work incorrect:
final ScaleAnimation animation = new ScaleAnimation (
1f, 0.5f, 1f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
);
final LayoutAnimationController controller = new LayoutAnimationController(animation);
animation.setDuration(0);
animation.setFillAfter(true);
gCoverflow.setLayoutAnimation(controller);
This code should be added in onCreate method.

I found it guys, is stead of :
gCoverflow.getLayoutParams().width = rlGallery.getWidth() / 2;
gCoverflow.getLayoutParams().height = rlGallery.getHeight() / 2;
I user this :
gCoverflow.setScaleX((float) 0.5);
gCoverflow.setScaleY((float) 0.5);

Related

Animate a view from 0dp width to MATCH_PARENT

I'm trying to animate my RecyclerView item when its clicked by making a rectangle grow from zero width to 100% (MATCH_PARENT) and become the background of the item.
However I can't see the animation working. I mean, the initial background is white, but the rectangle is gray, so the clicked item would become gray. But this is not happening.
Here's the item xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
android:focusable="true"
android:clickable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="72dp"
android:orientation="horizontal">
<View
android:id="#+id/colored_bar"
android:layout_width="3dp"
android:layout_height="match_parent"
android:background="#drawable/colored_bar_bg1"></View>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/option_background_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#e0e0e0"></View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<ImageView
android:id="#+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="13dp"
app:srcCompat="#drawable/ic_lock" />
<TextView
android:id="#+id/card_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/icon"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="16dp"
android:paddingLeft="8dp"
android:textColor="?android:attr/textColorPrimary"
android:textSize="16sp"
tools:text="#string/option_title_label" />
<TextView
android:id="#+id/card_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/card_title"
android:layout_toEndOf="#+id/icon"
android:layout_toRightOf="#+id/icon"
android:paddingLeft="8dp"
android:textSize="14sp"
tools:text="#string/option_description_label" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
And the code to make the animation:
public class OptionListItemHolder extends RecyclerView.ViewHolder {
private TextView cardTitle;
private TextView cardSubtitle;
private ImageView icon;
private View coloredBar;
private View optionBackground;
public OptionListItemHolder(View v) {
super(v);
cardTitle = (TextView)v.findViewById(R.id.card_title);
cardSubtitle = (TextView)v.findViewById(R.id.card_subtitle);
icon = (ImageView)v.findViewById(R.id.icon);
coloredBar = v.findViewById(R.id.colored_bar);
optionBackground = v.findViewById(R.id.option_background_container);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ObjectAnimator animation = ObjectAnimator.ofInt(optionBackground, "width", 0, view.getWidth());
animation.setDuration(600);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
});
}
}
Why it is not working?
I've found that using a ValueAnimator instead works
ValueAnimator widthAnimator = ValueAnimator.ofInt(view.getWidth(), newWidth);
widthAnimator.setDuration(500);
widthAnimator.setInterpolator(new DecelerateInterpolator());
widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
view.getLayoutParams().width = (int) animation.getAnimatedValue();
view.requestLayout();
}
});
widthAnimator.start();
If the view's width needs to match the parent, you can get the width of the parent by
int parentWidth = ((View)view.getParent()).getMeasuredWidth();

How to translate image across the width of relativelayout in android

I'm trying to animate two images one from left to right and other from right to left all at the same time. For this I have set the left margin for left image equal to negative of its width and same for right image so that they are out of the view.
Now in my mainactivity I'm translating them using translationX but nothing is happening.
Here is xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
tools:context="com.example.BeX.MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="133dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/left"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="-100dp"
android:src="#drawable/left" />
<ImageView
android:id="#+id/right"
android:layout_width="114dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="-114dp"
android:src="#drawable/right" />
</RelativeLayout>
Here is mainactivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppBaseTheme1);
setContentView(R.layout.activity_main);
RelativeLayout r = (RelativeLayout)findViewById(R.id.rLayout);
ImageView left = (ImageView)findViewById(R.id.left);
ImageView right = (ImageView)findViewById(R.id.right);
left.animate().translationX(r.getWidth()).setDuration(2000);
right.animate().translationX(-r.getWidth()).setDuration(2000);
}
}
Please tell me what is the correct or possible way to do that
Thanks in advance
There are two base types of Animation in Android.
The first one.
In order to animate translation use this example:
TranslateAnimation r = new TranslateAnimation(0,0,0,200);
r.setDuration(2000L);
r.setRepeatCount(0);
r.setFillAfter(true);
view.startAnimation(r);
The second one.
In your case, you use ViewPropertyAnimator class.
This is the second implementation of animation in Android.
left.animate().translationX(r.getWidth()).setDuration(2000)
In your case you have setup the Object animator but you haven't started it.
try this:
left.animate().translationX(r.getWidth()).setDuration(2000).start() :)
EDIT
This have to work:
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null">
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="133dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/aqu" />
<ImageView
android:id="#+id/left"
android:layout_width="100dp"
android:layout_height="75dp"
android:src="#drawable/ar" />
<ImageView
android:id="#+id/right"
android:layout_width="114dp"
android:layout_height="75dp"
android:src="#drawable/ari" />
</RelativeLayout>
Java-file
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dat);
RelativeLayout r = (RelativeLayout)findViewById(R.id.rLayout);
ImageView left = (ImageView)findViewById(R.id.left);
ImageView right = (ImageView)findViewById(R.id.right);
float distance = 300.f;
TranslateAnimation anim = new TranslateAnimation(0,0,0,distance);
anim.setFillAfter(true);
anim.setDuration(12000L);
left.startAnimation(anim);
}
But anyway if you want to animate using your ide write this:
ObjectAnimator anim0 = ObjectAnimator.ofFloat(right, "translationX", 0,300);
anim0.setDuration(10000L);
anim0.start();
It really works. I've tested.

TranslateAnimation move tomake other views appear

I'm using a TranslateAnimation to make a fragment (GoogleMap) sliding down to give space to an EditText and a TextView to be visible.
so I used this:
text: TextView
edit: EditText
MapLayout: a LinearLayout that contains the Map
Animation animation = new TranslateAnimation(
MapLayout.getX(), MapLayout.getY(),MapLayout.getY(), text.getHeight()+edit.getHeight());
The problem is that I can't make the slide because text.getHeight()+edit.getHeight() returns 0 so there's no slide!
I tried using a number (100 for exemple), the slide is made, but it's different between the devices, I tested on a Galaxy S3 and the slide is not complete, there's still a part of the EditText which is not visible, as for the emulator it worked ok.
When I tried to make the number a bit bigger, so the slide will be longer (200 for exemple), well... the slide was good for the S3, but i was big for the emulator.
So what I want to know is that if there's any way to make the slide move to a point, without depending on the device, I mean without using pixels; so the slide will work perfectly in any device/
I hope that my problem is clear.
Thank you
Update: I don't if this will help, I added a Toast message, show the height of the EditText and the TextView, in the Emulator it says: 85 and in the S3 it says 181
So yeah, I need to make the map slide down in any device like I said
MainActivity:
protected Animation animation;
protected LinearLayout MapLayout;
protected EditText edit;
protected TextView text;
MapLayout = (LinearLayout)findViewById(R.id.MapLayout);
edit = (EditText)findViewById(R.id.Recherche);
text = (TextView)findViewById(R.id.CaptionRecherche);
Toast.makeText(context, "Height: "+(edit.getHeight()+text.getHeight()), 1000).show();
animation = new TranslateAnimation(MapLayout.getX(), MapLayout.getY(), MapLayout.getY(), text.getHeight()+edit.getHeight());
animation.setDuration(1000);
animation.setFillAfter(true);
MapLayout.startAnimation(animation);
Main XML:
------- I'm using a DrawerLayout...I have a slide menu tu show in the application...just for your information-------
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/ContenuPrincipal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/activity_main_relative"
/>
<!-- ListView... La liste des options du menu -->
<ListView
android:id="#+id/Menu"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:background="#333"
android:divider="#666"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
/>
</android.support.v4.widget.DrawerLayout>
Main2 XML (The one I included above):
<?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"
android:background="#E8E8E8">
<!-- Champs de saisie pour effectuer la recherche: -->
<TextView
android:id="#+id/CaptionRecherche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrer l'emplacement que vous cherchez: "
android:textSize="20sp"
android:layout_marginTop="7dp"
android:layout_marginLeft="20dp"
/>
<EditText
android:id="#+id/Recherche"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:hint="Salle, Deparetement..."
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:maxLength="100"
android:maxLines="1"
android:layout_below="#id/CaptionRecherche"/>
<!-- La map: -->
<LinearLayout
android:id="#+id/MapLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</RelativeLayout>
As a part of my application, I have a status bar which contains some text. This status bar is hidden until the user clicks a button at which point it slides down (hiding the topmost content of the layout below).
The code I use to get the correct height of the hidden status bar:
private int hiddenStatusHeight;
private int currentStatusBarHeight;
private void getStatusBarHeight() {
final ViewTreeObserver observer = hiddenStatus.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#SuppressLint("NewApi") #SuppressWarnings("deprecation") #Override public void onGlobalLayout() {
hiddenStatus.measure(MeasureSpec.UNSPECIFIED,
MeasureSpec.UNSPECIFIED);
hiddenStatusHeight = hiddenStatus.getMeasuredHeight();
currentStatusBarHeight = statusBar.getHeight();
ViewTreeObserver obs = hiddenStatus.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
}
The code that is executed when the button is clicked:
private OnClickListener ExpandClickListener = new OnClickListener() {
#Override public void onClick(View v) {
boolean isExpanded = (Boolean) expandButton
.getTag(R.id.TAG_EXPANDED);
int originalHeight = (Integer) expandButton
.getTag(R.id.TAG_ORIGINAL_HEIGHT);
if (isExpanded) {
expandButton.setTag(R.id.TAG_EXPANDED, false);
expandButton.setImageResource(R.drawable.ic_action_down);
// statusBar.setLayoutParams(new FrameLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, originalHeight));
Log.d(TAG, "Collapsing to " + originalHeight);
ValueAnimator va = ValueAnimator.ofInt(currentStatusBarHeight,
originalHeight);
va.setDuration(500);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
statusBar.getLayoutParams().height = value.intValue();
statusBar.requestLayout();
}
});
va.start();
} else {
expandButton.setTag(R.id.TAG_EXPANDED, true);
expandButton.setImageResource(R.drawable.ic_action_collapse);
currentStatusBarHeight = originalHeight + hiddenStatusHeight;
// statusBar.setLayoutParams(new FrameLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, currentStatusBarHeight + 15));
Log.d(TAG, "Expanding to " + originalHeight + "+"
+ hiddenStatusHeight + "=" + currentStatusBarHeight);
ValueAnimator va = ValueAnimator.ofInt(originalHeight,
currentStatusBarHeight);
va.setDuration(500);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
statusBar.getLayoutParams().height = value.intValue();
statusBar.requestLayout();
}
});
va.start();
}
}
};
And finally my layout XML (it has to be a FrameLayout):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:showDividers="middle" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/displayStatusBar"
style="#style/DisplayStatusBar"
android:layout_width="match_parent"
android:layout_height="65dp" >
<RelativeLayout
android:id="#+id/status_always_visible"
style="#style/StatusBar"
android:layout_width="match_parent"
android:layout_height="20dp" >
<TextView
android:id="#+id/status_received"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/received" />
<TextView
android:id="#+id/status_time_received"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/status_received" />
<TextView
android:id="#+id/status_time_delete_relative_text"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/status_time_delete_relative"
android:text="#string/is_deleted" />
<TextView
android:id="#+id/status_time_delete_relative"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/minutes" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/status_hidden"
style="#style/StatusHidden"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/status_always_visible" >
<LinearLayout
android:id="#+id/status_hydrants_near_address_container"
style="#style/StatusHiddenText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:divider="#android:drawable/divider_horizontal_bright"
android:orientation="vertical"
android:paddingLeft="10dp"
android:showDividers="middle" >
<TextView
style="#style/StatusHiddenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_information" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/optionsBar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#999"
android:orientation="horizontal"
android:paddingTop="5dp" >
<ImageButton
android:id="#+id/button_hydrants"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:alpha="50"
android:contentDescription="#string/module_hydrants"
android:src="#drawable/ic_action_place" />
<ImageButton
android:id="#+id/button_route"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:contentDescription="#string/module_directions"
android:src="#drawable/ic_action_directions" />
<ImageButton
android:id="#+id/button_pdf"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:clickable="false"
android:contentDescription="#string/module_accessplan"
android:src="#drawable/ic_action_attachment" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="#+id/button_more"
style="#style/android:Widget.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_down" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<!-- The "empty" view to show when there are no items in the "list" view defined above. -->
<TextView
android:id="#android:id/empty"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="32dp"
android:text="#string/no_information"
android:textColor="?android:textColorSecondary" />
</FrameLayout>
Hope some of this may be helpful to you.
Just to mention it, I have asked a similar question where the visible layout is pushed downwards as the menu expands. So have a look at that if you rather want this behaviour: How to animate a slide in notification view that pushes the content view down
Happy coding

Drawing a new view into a relative layout

We are writing a "dashboard" for a car. The code for calculating/drawing the speedometer was done separate from the rest of the code, and now I am trying to combine the two.
They both work separately, and with the speedometer view code in, the main portion of the UI is still functioning, I just can't see the speedometer.
I have it compiling, and there are no obvious errors. However, when the app is run, I don't see the speedometer showing up in the main view, even though I have added it using the addView method.
For Reference:
My code to create and add the view:
setContentView(R.layout.main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
myView v = new myView(this);
v.setLayoutParams(lp);
relativeLayout.addView(v, lp);
The code for the myView class:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.View;
public class myView extends View {
private Paint mPaints;
private Paint textpaint;
private boolean mUseCenters;
private RectF mBigOval;
private float mStart;
private float mSweep;
public float SPEED_raw;
public int SPEED = 0; //initialized to 0 just for loop. Remove initialization when IF statement below is deleted
public static Bitmap gaugefront;
public static Bitmap gaugeback;
public myView(Context context) {
super(context);
mPaints = new Paint();
mPaints.setAntiAlias(true);
mPaints.setColor(Color.YELLOW);
textpaint = new Paint();
textpaint.setAntiAlias(true);
textpaint.setColor(Color.WHITE);
textpaint.setTextSize(150);
textpaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
gaugefront = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugefront); //Load gaugefront.png
gaugeback = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugeback); //Load gaugeback.png
mUseCenters = true;
mBigOval = new RectF(400, 10, 880, 490); //left[x-coordinate],top[y-coordinate],right[x],bottom[y]
}
private void drawArcs(Canvas canvas, RectF oval, boolean useCenter, Paint paint) {
canvas.drawArc(oval, mStart, mSweep, useCenter, paint);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(myView.gaugeback, 400, 10, null); //draw gauge background, X coordinate:400, Y coordinate: 10
drawArcs(canvas, mBigOval, mUseCenters, mPaints);
//SPEED_raw = 70; //Raw float data from CCS. Uncomment when the IF statement below is deleted.
SPEED = Math.round(SPEED_raw); //SPEED integer. Units km/h - Top speed of 135. Used for digital readout
mStart = 90; //Start drawing from -90 degrees (Counter clockwise)
mSweep = SPEED_raw*2; //Draw stop point
if(SPEED >= 135){ //JUST FOR SHOW. Delete this for actual speedo
SPEED_raw = 0; // "
} // "
else // "
SPEED_raw += 0.5; // "
canvas.drawBitmap(myView.gaugefront, 400, 10, null); //draw gauge foreground, X coordinate:400, Y coordinate: 10
String speed_string = String.valueOf(SPEED); //Convert int SPEED to String for display
// while (speed_string.endsWith(".0") || speed_string.endsWith(".")){ //Erase trailing ".0" for float types. Don't need if SPEED is an int
// speed_string = (speed_string.substring(0, speed_string.length() - 1));
// }
canvas.drawText(speed_string, 640, 420, textpaint); //arguments: (string, x-coordinate, y-coordinate, paint)
invalidate();
}
}
Here's my XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:keepScreenOn="true"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/solarbg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/solarbg" />
</LinearLayout>
<ImageView
android:id="#+id/l_lamp"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="left"
android:src="#drawable/l_arrow_dim" />
<DigitalClock
android:id="#+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="50dp" />
<ImageView
android:id="#+id/r_lamp"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/r_arrow_dim" />
<TextView
android:id="#+id/setAmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left"
android:text="#string/setAmps"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setAmpsVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_below = "#+id/setAmps"
android:layout_alignParentLeft="true"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setVelocity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/setAmps"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:text="#string/setVelocity"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setVelocityVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_below = "#+id/setVelocity"
android:layout_alignParentRight="true"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/actVelocity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/setVelocityVal"
android:gravity="right"
android:text="#string/actVelocity"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/actVelocityVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below = "#+id/actVelocity"
android:layout_alignParentRight="true"
android:gravity="right"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/busAmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/setAmpsVal"
android:gravity="left"
android:text="#string/busAmps"
android:textColor="#ffffff"
android:textSize="30dp" /><TextView
android:id="#+id/busAmpsVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_below = "#+id/busAmps"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/powerVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom = "true"
android:gravity="right"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/power"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_above = "#+id/powerVal"
android:gravity="left"
android:text="#string/power"
android:textColor="#ffffff"
android:textSize="30dp" />
<TableLayout
android:id="#+id/msgCenterLayout"
android:layout_width="600dp"
android:layout_height="200dp"
android:layout_alignParentBottom = "true"
android:layout_centerHorizontal = "true"
android:orientation = "vertical"
android:visibility = "invisible">
<ScrollView
android:id="#+id/messageCenterText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:text="#string/busAmps"
android:textColor="#ffffff"
android:textSize="30dp" />
</TableLayout>
</RelativeLayout>
edit:
I'm using an XML file for the main layout, and trying to add this new view to a relativelayout inside that XML file.
Try writing
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
v.setLayoutParams(lp);
relativeLayout.addView(v);

Custom ImageView doesn't display a Bitmap

I'm making a simple compass view that's displayed on top of a MapView. Layout for the MapView and other views are declared in a layout xml file.
I'm using this article as an example.
I've written onDraw method for the compass view but it doesn't display anything. Compass object gets invalidated in onSensorChanged method. Why isn't the picture displayed?
XML layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mapviews.MyCompass
android:id="#+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp" />
<com.mapviews.TransparentPanel
android:id="#+id/transparent_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="3dp"
android:padding="5dp" />
<ImageButton
android:id="#+id/map_zoom_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/cd_zoom_out"
android:src="#drawable/zoom_out" />
<ImageButton
android:id="#+id/map_zoom_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/map_zoom_out"
android:contentDescription="#string/cd_zoom_in"
android:src="#drawable/zoom_in" />
<ImageButton
android:id="#+id/map_center_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/map_zoom_in"
android:contentDescription="#string/cd_center_user"
android:src="#drawable/directions" />
<com.mapviews.MyMap
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="MyAPIKey"
android:clickable="true"
android:state_enabled="true" />
Part of MyCompass class
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
canvas.rotate(
-1 * Values.accelValues[0]
, Values.width / 10
, Values.height / 10);
canvas.drawBitmap(
bitmap
, Values.width / 10 - bitmap.getWidth() / 10
, Values.height / 10 - bitmap.getHeight() / 10
, paint);
}
public void setup(Context c) {
paint = new Paint();
bitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.compass1);
}
In onCreate method
compass = (MyCompass) findViewById(R.id.compass);
compass.setup(this);
compass.bringToFront();

Categories

Resources