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.
Related
I have a horizontal linear layout that has 3 views. An open and close button and a text view. On load the close button is set to
setVisibility(View.GONE);
So it is only showing one imageview, open, and a textview with some text. I am also using
android:animateLayoutChanges="true"
On my parent layout to animate the textview when it opens and closes. Everything functions right except when I open/close I can see the animation for the two buttons being set to GONE and VISIBLE respectively. I do not want animation on those two ImageViews. Any thoughts on how I can remove the animations on those two views? Thanks in advance.
XML File
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/CuFScrollView"
style="#style/ScrollView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#A0A0A0"
>
<LinearLayout
style="#style/LinearLayoutVertical"
android:paddingTop="20dp"
>
<TextView
android:text="Basic Management Plan for Tactical Field Care"
android:id="#+id/header"
style="#style/Header"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#606060"
android:padding="5dp"
android:animateLayoutChanges="true"
>
<ImageView
android:id="#+id/open"
android:src="#drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:onClick="open"
android:layout_gravity="center_vertical"
android:animateLayoutChanges="false"
/>
<ImageView
android:id="#+id/close"
android:src="#drawable/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:onClick="close"
android:layout_gravity="center_vertical"
android:animateLayoutChanges="false"
/>
<TextView
android:id="#+id/stepOne"
android:text="Step 1:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="#style/Bullet"
/>
</LinearLayout>
</LinearLayout>
Java File
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
public class Careunderfirestudy extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.careunderfirestudy);
ImageView close = (ImageView)findViewById(R.id.close);
close.setVisibility(View.GONE);
}
public void open(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
stepOne.setText("Casualties should be extricated from burning vehicles or buildings and moved to places of relative safety. Do what is necessary to stop the burning process.");
ImageView close = (ImageView)findViewById(R.id.close);
ImageView open = (ImageView)findViewById(R.id.open);
close.setVisibility(View.VISIBLE);
open.setVisibility(View.GONE);
}
public void close(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
stepOne.setText("Step 1:");
ImageView close = (ImageView)findViewById(R.id.close);
ImageView open = (ImageView)findViewById(R.id.open);
close.setVisibility(View.GONE);
open.setVisibility(View.VISIBLE);
}
}
The question was: Prevent certain views from being animated with android:animateLayoutChanges=“true” and I don't see the answer to this question in the previous answer.
I had the same problem and I solved it disabling temporary the LayoutTransition for appearing and disappearing type.
Here examples.
Hide a View without animating it:
LayoutTransition lt = ((ViewGroup)view.getParent()).getLayoutTransition();
lt.disableTransitionType( LayoutTransition.DISAPPEARING );
view.setVisibility(View.GONE);
lt.enableTransitionType( LayoutTransition.DISAPPEARING );
Show a View without animating it:
LayoutTransition lt = ((ViewGroup)view.getParent()).getLayoutTransition();
lt.disableTransitionType( LayoutTransition.APPEARING );
view.setVisibility(View.VISIBLE);
lt.enableTransitionType( LayoutTransition.APPEARING );
This does not prevent other views to be animated consequently to the view visibility change (they are animated under LayoutTransition.CHANGE_APPEARING and LayoutTransition.CHANGE_DISAPPEARING transition type).
Not null check to LayoutTransition may be done for safety.
You could just use 1 button and change the image:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#606060"
android:padding="5dp"
android:animateLayoutChanges="true"
android:id="#+id/parent"
>
<ImageView
android:id="#+id/toggleStep"
android:src="#drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:onClick="toggle"
android:layout_gravity="center_vertical"
/>
protected void onCreate(Bundle savedInstanceState) {
...
LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
...
}
private boolean mOpen = false;
public void toggle(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStep);
if(mOpen){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("Casualties should be extricated from burning vehicles or buildings and moved to places of relative safety. Do what is necessary to stop the burning process.");
toggle.setImageResource(R.drawable.close);
}
mOpen = !mOpen;
}
I'm stuck.
The problem is how to place text on a static image and keep position depends on it between different screen dimensions.
To achieve this I've tried the layout 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:padding="#dimen/default_view_padding">
<ImageView
android:id="#+id/iv_background"
android:layout_width="500dp"
android:adjustViewBounds="true"
android:layout_height="900dp"
android:layout_centerInParent="true"
android:src="#drawable/example"/>
<TextView
android:layout_alignLeft="#id/iv_background"
android:layout_alignStart="#id/iv_background"
android:layout_alignEnd="#id/iv_background"
android:layout_alignRight="#id/iv_background"
android:layout_alignTop="#id/iv_background"
android:layout_width="wrap_content"
android:layout_marginTop="190dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Dummy text string"/>
</RelativeLayout>
But on different screens it looks differently.
So, by example, screenshots. The static image background (in example white image with green line), the view with text on nexus5, on nexus7.
As you can see, a text placed on different places over image.
I doesn't know why is it happend, because I'm using dp and relative layout.
I tried wrap_content/match_parent on image sizes, without ajust view bounds etc. And it haven't help.
Ty for answers.
EDIT: I want the text to always be above the green line on the same distance in different screen dimensions. (the same as in the second image)
EDIT2: Someone misunderstood me, sorry if the question isn't clear. As a background in example i'm tried to use imageview instead of background tag of relative layout, because it is'nt help whatever, i tried that before
The line, which I used in example, is only for that. It just needs to explain the issue of the text positioning
use dimens.xml to Support different size of screens.
in your res folder create some value folders Like :
values-xlarge
values-large
values-small
values-sw800dp
...
in these folders youy should have dimens.xml and define your dimensions.
for example this may be your dimensions for normal screens :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="ImageViewWidth">500dp</dimen>
<dimen name="ImageViewHeight">900dp</dimen>
<dimen name="TextViewMarginTop">190dp</dimen>
</resources>
Definitely your dimensions for large or xlarge screens are different!
to use these, simply put them in XML instead of hard-coding dimens
<TextView
android:layout_alignLeft="#id/iv_background"
android:layout_alignStart="#id/iv_background"
android:layout_alignEnd="#id/iv_background"
android:layout_alignRight="#id/iv_background"
android:layout_alignTop="#id/iv_background"
android:layout_width="wrap_content"
android:layout_marginTop="#dimens/TextViewMarginTop"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Dummy text string"/>
If you just want green color as a background of text then use android:background on textview and remove ImageView. Create a 9Patch image with green line at bottom and give that image as background to your text.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/abc_ab_transparent_light_holo"
android:text="Dummy text string"
android:id="#+id/dummy_text" />
I don't know if I understand the question,
but I think solution could be putting RelativeLayout in another contener, ie:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:padding="#dimen/default_view_padding" >
<ImageView
android:id="#+id/iv_background"
android:layout_width="500dp"
android:layout_height="900dp"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="#drawable/example" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#id/iv_background"
android:layout_alignLeft="#id/iv_background"
android:layout_alignRight="#id/iv_background"
android:layout_alignStart="#id/iv_background"
android:layout_alignTop="#id/iv_background"
android:layout_centerHorizontal="true"
android:layout_marginTop="190dp"
android:gravity="center"
android:text="Dummy text string" />
</RelativeLayout>
</LinearLayout>
set android:gravity="center", android:layout_centerInParent="true" and android:layout_alignParentTop="true" of the textview
Create a custom view like my sample:
layout view_product_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_light_blue"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dip"
android:src="#drawable/main0x" />
<TextView
android:id="#+id/tvQty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imgPoduct"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:background="#color/lightBlue3"
android:text="1111"
android:textColor="#color/red"
android:textSize="16dip" />
<TextView
android:id="#+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/imgProduct"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:lines="2"
android:maxLines="2"
android:minLines="2"
android:singleLine="false"
android:text="TextView"
android:textColor="#color/black"
android:textSize="16dip" />
</RelativeLayout>
Class:
package org.mabna.order.ui;
import org.mabna.order.R;
import org.mabna.order.businessLayer.db.BoPreferences;
import org.mabna.order.utils.Farsi;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ImageView_Product extends LinearLayout implements OnTouchListener {
private static final float FONT_SIZE_LARGE = 20;
private static final float FONT_SIZE_NORMAL = 16;
private static final float FONT_SIZE_SMALL = 12;
private RelativeLayout rlParent;
private ImageView imgProduct;
private TextView tvInfo;
private TextView tvQty;
private double qty = 0;
private float mDensity;
private Typeface tf;
public ImageView_Product(Context context) {
super(context);
initialize();
}
public ImageView_Product(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
protected void initialize() {
if (!this.isInEditMode()) {
tf = Farsi.getFarsiFont(this.getContext());
}
mDensity = this.getResources().getDisplayMetrics().density;
LayoutInflater layoutInflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater
.inflate(R.layout.view_product_image, this);
rlParent = (RelativeLayout) view.findViewById(R.id.rlParent);
imgProduct = (ImageView) view.findViewById(R.id.imgProduct);
tvInfo = (TextView) view.findViewById(R.id.tvInfo);
tvQty = (TextView) view.findViewById(R.id.tvQty);
tvInfo.setTypeface(tf);
tvInfo.setTextSize(FONT_SIZE_SMALL);
}
public TextView getTextView() {
return tvInfo;
}
public ImageView getImageView() {
return imgProduct;
}
public TextView getQtyView() {
return tvQty;
}
public void setInfoText(String text) {
tvInfo.setText(text);
}
public String getInfoText(String text) {
return tvInfo.getText().toString();
}
public void setQty(double value) {
this.qty = value;
if (this.qty > 0) {
tvQty.setText(BoPreferences.getStringValueNoGrouping(this.qty,
getContext()));
setStateHasQty();
} else {
tvQty.setText("");
setStateHasNotQty();
}
}
public double getQty() {
return Double.valueOf(tvQty.getText().toString());
}
public void setStateSelected() {
imgProduct.setBackgroundResource(R.drawable.border06);
}
public void setStateNotSelected() {
imgProduct.setBackgroundResource(R.drawable.border05);
}
private void setStateHasQty() {
tvQty.setBackgroundResource(R.color.lightBlueTransparent);
tvQty.setTextSize(FONT_SIZE_LARGE);
// tvInfo.setBackgroundResource(R.color.darkGreen);
rlParent.setBackgroundResource(R.drawable.background_light_green);
}
private void setStateHasNotQty() {
tvQty.setBackgroundResource(0);
tvQty.setTextSize(FONT_SIZE_NORMAL);
// tvInfo.setBackgroundResource(R.color.lightBlue3);
rlParent.setBackgroundResource(R.drawable.background_light_blue);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
public void setImageSize(int width, int height) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)
imgProduct.getLayoutParams();
lp.width = (int) (width * mDensity);
lp.height = (int) (height * mDensity);
imgProduct.setLayoutParams(lp);
tvInfo.setMaxWidth(lp.width);
}
}
I think I understand your problem. Here I've aligned the image to the left and right of the text and set the ImageView and TextView layout_width to wrap_content. I have used android:layout_below and given it a top margin.
This appears to work on all screen sizes.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:padding="20dp">
<TextView
android:id="#+id/tv_example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="190dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Dummy text string"/>
<ImageView
android:id="#+id/iv_background"
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_alignLeft="#+id/tv_example"
android:layout_alignRight="#id/tv_example"
android:layout_marginTop="4dp"
android:layout_below="#+id/tv_example"
android:adjustViewBounds="true"
android:layout_centerInParent="true"
android:src="#drawable/example"/>
</RelativeLayout>
You can change the height and top margin of the ImageView if you wish.
Use LinearLayout instead of RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/iv_background"
android:layout_width="500dp"
android:layout_height="900dp"
android:adjustViewBounds="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="190dp"
android:gravity="center"
android:text="Dummy text string" />
</LinearLayout>
Second Solution is use android:drawablePadding=""and android:drawableBottom="" in TextView.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding=""
android:drawableBottom=""
android:gravity="center"
android:text="Dummy text string" >
</TextView>
I'm trying to get an ImageButton to create another ImageButton in a different layout from an OnClick. My logcat takes me to an error on this line, but provides no further feedback:
ImageButton adab = (ImageButton)findViewById(R.id.abbutton);
My code:
package org.iimed.www;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class Penicillins extends Activity implements OnClickListener{
ImageButton back,addmed;
Context abbuttonplain;
public void onCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
setContentView(R.layout.penicillin);
back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);
back.setOnClickListener(this);
addmed.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.back:
startActivity(new Intent(
Penicillins.this, ImageTextListViewActivity.class));
break;
case R.id.addmed:
RelativeLayout ll=(RelativeLayout)findViewById(R.layout.sundayopen);
LayoutParams param = new
RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton adab = (ImageButton)findViewById(R.id.abbutton);
ll.addView(adab,param);
}
}
}
Thanks for you patience.
Edit: the .xml the button is to create on
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/miimedback" >
<ImageView
android:id="#+id/pillboxm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:contentDescription="#string/pillbox"
android:src="#drawable/pillbox" />
<ImageButton
android:id="#+id/satlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pillboxm"
android:layout_alignRight="#+id/pillboxm"
android:background="#android:color/transparent"
android:contentDescription="#string/satlid"
android:src="#drawable/slid" />
<ImageButton
android:id="#+id/frilid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thulid"
android:layout_toLeftOf="#+id/satlid"
android:background="#android:color/transparent"
android:contentDescription="#string/frilid"
android:src="#drawable/flid" />
<ImageButton
android:id="#+id/thulid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pillboxm"
android:layout_toLeftOf="#+id/frilid"
android:background="#android:color/transparent"
android:contentDescription="#string/thulid"
android:src="#drawable/tlid" />
<ImageButton
android:id="#+id/homebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:background="#android:color/transparent"
android:contentDescription="#string/homebutton"
android:src="#drawable/homebut1" />
<ImageButton
android:id="#+id/wedlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thulid"
android:layout_toLeftOf="#+id/thulid"
android:background="#android:color/transparent"
android:contentDescription="#string/wedlid"
android:src="#drawable/wlid" />
<ImageButton
android:id="#+id/tuelid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pillboxm"
android:layout_toLeftOf="#+id/wedlid"
android:background="#android:color/transparent"
android:contentDescription="#string/tuelid"
android:src="#drawable/tlid" />
<ImageButton
android:id="#+id/monlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pillboxm"
android:layout_toLeftOf="#+id/tuelid"
android:background="#android:color/transparent"
android:contentDescription="#string/monlid"
android:src="#drawable/mlid" />
<ImageButton
android:id="#+id/adbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/abbutton"
android:background="#android:color/transparent"
android:contentDescription="#string/adbutton"
android:src="#drawable/adbutton" />
<ImageButton
android:id="#+id/abbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cobutton"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:contentDescription="#string/abbutton"
android:src="#drawable/antibiotic_buton" />
<ImageButton
android:id="#+id/bppressure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/monlid"
android:layout_alignTop="#+id/adbutton"
android:background="#android:color/transparent"
android:contentDescription="#string/bpbutton"
android:src="#drawable/bpbutton" />
<ImageButton
android:id="#+id/satlidopen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/monlid"
android:layout_toLeftOf="#+id/monlid"
android:background="#android:color/transparent"
android:contentDescription="#string/satlidopen"
android:src="#drawable/satlidopen" />
<ImageButton
android:id="#+id/cobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/satlidopen"
android:layout_marginBottom="133dp"
android:layout_toRightOf="#+id/wedlid"
android:background="#android:color/transparent"
android:contentDescription="#string/cobutton"
android:src="#drawable/cobutton" />
</RelativeLayout>
the logcat:
01-06 23:35:23.725: E/AndroidRuntime(29654): at `org.iimed.www.Penicillins.onClick(Penicillins.java:50)`
You should create a new ImageButton in code, and not try to add the same ImageButton reference (R.id.abbutton). You can't add the same View (you get a reference to same inflated object using findViewbyId). Try something like:
RelativeLayout ll=(RelativeLayout)findViewById(R.layout.sundayopen);
LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton adab = new ImageButton(getApplicationContext());
// Initialise ImageButton properties like the Image here...
ll.addView(adab,param);
Also, as a good practice you should keep the reference to your RelativeLayout ll in a member variable, assign in it once (look it up using findViewbyId) in your onCreate and just use it wherever you need it, that way you save the unnecessary lookups every time you click the R.id.addmed ImageButton.
back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);
you are using this id's to click and getting error for this is
adab = (ImageButton)findViewById(R.id.abbutton);
check id and layout proper as you have no id of abbuttonin your layout
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'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);
I've created a somewhat simple layout with a total of 4 buttons: 3 buttons at the bottom of the layout are side by side (in a RelativeLayout) and on huge button almost in the middle of the screen.
On Ice Cream Sandwich and Jellybean, I can click any of the buttons and everything works fine (i.e.The respective onClick functions are called) but weirdly enough on Gingerbread any of the 3 buttons at the bottom work but not the one in the middle. onClick fires off many instructions, none of which are being executed so as far as I can tell, it's not being called and I don't know why.
Code for layout is given below. Any help is greatly appreciated!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_off"
android:orientation="vertical"
android:weightSum="10" >
<ImageButton
android:id="#+id/button_activate"
android:layout_width="155dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="170dp"
android:layout_weight="7.2"
android:background="#drawable/transparent"
android:clickable="true"
android:contentDescription="#string/app_name"
android:onClick="activate"
android:src="#drawable/transparent" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginTop="80dp"
android:layout_weight="2" >
<ImageButton
android:id="#+id/button_help"
android:layout_width="90dp"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:background="#drawable/transparent"
android:clickable="true"
android:contentDescription="#string/app_name"
android:onClick="openhelp"
android:src="#drawable/transparent" />
<ImageButton
android:id="#+id/button_settings"
android:layout_width="55dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginTop="0dp"
android:layout_toLeftOf="#+id/button_help"
android:background="#drawable/transparent"
android:clickable="true"
android:contentDescription="#string/app_name"
android:onClick="opensettings"
android:src="#drawable/transparent" />
<ImageButton
android:id="#+id/button_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginTop="0dp"
android:layout_toRightOf="#+id/button_help"
android:background="#drawable/transparent"
android:clickable="true"
android:contentDescription="#string/app_name"
android:onClick="opencontact"
android:src="#drawable/transparent" />
</RelativeLayout>
</LinearLayout>
Activity that calls the layout (main.xml):
//bunch of imports
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById (R.id.main_layout);
boolean serviceOn = isMyServiceRunning();
if(serviceOn==true) layout.setBackgroundResource(R.drawable.main_on);
else if (serviceOn==false) layout.setBackgroundResource(R.drawable.main_off);
}
#Override
protected void onResume() {
super.onResume();
LinearLayout layout = (LinearLayout) findViewById (R.id.main_layout);
boolean serviceIsOn = isMyServiceRunning();
if(serviceIsOn==true) layout.setBackgroundResource(R.drawable.main_on);
else if (serviceIsOn==false) layout.setBackgroundResource(R.drawable.main_off);
}
public void activate(View v){
LinearLayout layout = (LinearLayout) findViewById (R.id.main_layout);
Resources res = getResources();
Drawable on = res.getDrawable(R.drawable.main_on);
Drawable off = res.getDrawable(R.drawable.main_off);
if (layout.getBackground().getConstantState() == off.getConstantState()){
layout.setBackgroundResource(R.drawable.main_on);
Toast.makeText(getApplicationContext(), "App has been activated", Toast.LENGTH_SHORT).show();
turnon();
}
else if (layout.getBackground().getConstantState() == on.getConstantState()){
layout.setBackgroundResource(R.drawable.main_off);
Toast.makeText(getApplicationContext(), "App has been deactivated", Toast.LENGTH_SHORT).show();
turnoff();
}
}
}
I can run this fine on my emulator for both 2.2 and 2.3.
The only thing I can suggest is to try to remove drawables (maybe some settings there are screwing things up), or try to make your relative layout height 0dp, since you are using weight_sum