I'm using XML for the layout but I need to programmatically position the ImageButton. Can anyone gives me an idea how to do it?
XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainScreenLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/mainScreenImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/screenimage"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/titleview"
android:background="#00000000"
android:adjustViewBounds="true"
android:src="#drawable/buttonstart"/>
</FrameLayout>
First of all, wrap the ImageButton with a FrameLayout as
....
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/titleview"
android:background="#00000000"
android:adjustViewBounds="true"
android:src="#drawable/buttonstart"/>
</FrameLayout>
....
This is not a must, to be a FrameLayout. Just wrap the image with some layout. But for other layouts, positioning with X and Y will vary.
Now, I can change the position of ImageButton as,
FrameLayout mFrame=(FrameLayout) findViewById(R.id.frameLayout1);
mFrame.setPadding(fromLeftX, fromTopY, fromRight, fromBottom);
i think LayoutParams will work fine
try this code
Button button = (Button)findViewById(R.id.my_button);
AbsoluteLayout.LayoutParams absParams =
(AbsoluteLayout.LayoutParams)button.getLayoutParams();
absParams.x = myNewX;
absParams.y = myNewY;
button.setLayoutParams(absParams);
and if you want to set image on it use setBackgroundResources property
button.setBackgroundResource(R.drawable.new_image);
Related
I'm trying to make a trimmerbar to my video player
trimmer_bar.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/thumbsBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
tools:background="#color/trimmerBarSelected">
<ImageView
android:id="#+id/leftThumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription=""
android:scaleType="fitStart"
android:src="#drawable/ic_left_thumb" />
<ImageView
android:id="#+id/rightThumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:contentDescription=""
android:scaleType="fitEnd"
android:src="#drawable/ic_right_thumb" />
</RelativeLayout>
<ImageView
android:id="#+id/progressIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="60dp"
android:layout_marginTop="3dp"
android:contentDescription=""
android:scaleType="centerCrop"
android:src="#android:drawable/btn_star_big_on" />
</FrameLayout>
activity_video_viewer:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="match_parent"
android:orientation="vertical"
android:background="#a8c71c"
tools:context=".activities.VideoTrimmerActivity">
<com.tomatedigital.instagram.longstories.ui.TrimmerBar
android:id="#+id/trimmerBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/trimmerBarSelected"
/>
</FrameLayout>
it's logic is working great: when the user touches, i detect if it was on the any of the thumbs and case on the left thumb I adjust the left margin of the thumbsBar, case on the right I adjust the right margin...
As the two imageview are aligned to the edges this behavior makes them to move... but what is actually moving is the whole RelativeLayout
The problem is even adjuting the margins and moving the imageviews the relative layout is still showing the background in the margin area
ignoring the whole calculation i set the margins using:
this.thumbsBar= (RelativeLayout) findViewById(R.id.thumbsBar);
this.thumbsBarLayout = (LayoutParams) this.thumbsBar.getLayoutParams();
this.thumbsBarLayout.leftMargin = this.leftMargin;
this.thumbsBarLayout.rightMargin = this.rightMargin;
this.thumbsBar.setLayoutParams(this.thumbsBarLayout);
===============================================================
UPDATE:
to make it easier to understand
I've this:
I should have:
com.tomatedigital.instagram.longstories.ui.TrimmerBar has the same background as your RelativeLayout and has a width of match_parent. You don't really specify how your layout is being loaded and I assume it is in your custom view.
For a test, change the color of the background of the custom view to see if the background you are seeing in the margins is coming from the RelativeLayout or the custom view. My guess is that it's coming from the custom view and changing the background color in the custom view will solve your problem.
I have a RelativeLayout inside of another Relative Layout.When i use the inner layout to be LinearLayout the code works fine however.I read about many such questions here on SO.Sometimes it was mentioned that the layout params used should be of the parent layout. I wasnt able to quite understand it.Is this where I am going wrong?
Thanks in advance guys !
MY XML FILE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rel1" >
<RelativeLayout
android:id="#+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="#drawable/helo" />
</RelativeLayout>
</RelativeLayout>
Function Where I use margins
public void showcopter()
{
ImageView image=(ImageView)findViewById(R.id.imageView1);
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)(image.getLayoutParams());
lp.leftMargin =0;//(int)((width-150)*density);
lp.topMargin=(int)(hg-(30*density)); //hg is a global variale which stores the screen height
// Log.d("hello_girl",hg+" ");
// lp.setMargins((int) ((width-150)*density),(int)(hg-(30*density)),0,0);
image.setLayoutParams(lp);
image.requestLayout();
}
Since you have set android:layout_alignParentRight to be true, leftMargin won't take effect, and while you set android:layout_centerVertical to be true, topMargin won't take effect either.
I made an android app that contain scrollview layout and inside it AbsoluteLayout, the problem is I can't display the bottom of the layout it only display the top of the layout. I tried to use layout_marginBottom in the AbsoluteLayout but it didn't work.
Use RelativeLayout instead of AbsoluteLayout. Also don't scroll the total layout. Just scroll the View only.
Check it once:
<?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"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp"
android:background="#drawable/bg" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</ScrollView>
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btn1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="27dp"
android:text="#string/s3"
android:textSize="16sp"
android:text="TextView" />
</RelativeLayout>
Use RelativeLayout and set left and right margins like
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.leftMargin = x;
lp.topMargin = y;
True , absolute layout is deprecated . Also , here is a tip :
When creating an xml layout for Android , the easiest way to check for error is using the "graphical layout" tab of the xml layout file . . Use an appropriate device configuration ( match your device ) and the API version , reproduce that error and see if changing the layout parameters help .
use property android:fillViewPort=true inside ScrollView as follows
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical" >
Here's the Android ImageButton that I am trying to rotate.
It does rotate but as you can see from the screenshot, part of it goes to background/invisible during the rotation. How do I ensure that the ImageButton stays visible completely during the entire rotation?
Here's the Activity code:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imgbt = (ImageButton)findViewById(R.id.mybutton);
RotateAnimation ra =new RotateAnimation(0, 360);
ra.setFillAfter(true);
ra.setDuration(2000);
imgbt.startAnimation(ra);
}
}
And the XML:
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mybutton" />
</LinearLayout>
In your LinearLayout, change this:
android:layout_height="wrap_content"
to this:
android:layout_height="match_parent"
So, you will ensure that the container is big enough to show the image button completely, even when it's vertical.
Change your LinearLayout height and width to fill_parent as below:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
You can use :
imgbt.bringToFront();
Set match_parent to your LinearLayout height.
If you are not using weight then don't need to include it. So change
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
chnage your width and height parameter of the linear layout to fill_parent cause the use code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:layout_gravity="center"
android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mybutton"
/>
My main layout main.xml simply contains two LinearLayouts:
The 1st LinearLayout hosts a VideoView and a Button,
The 2nd LinearLayout hosts an EditText, and this LinearLayout has set the visibility value to "GONE" (android:visibility="gone")
like below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/first_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<VideoView
android:id="#+id/my_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
/>
<Button
android:id="#+id/my_btn"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_gravity="right|bottom"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/second_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:visibility="gone"
>
<EditText
android:id="#+id/edit_text_field"
android:layout_height="40dip"
android:layout_width="fill_parent"
android:layout_weight="5"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
</LinearLayout>
I successfully implemented the feature that when the Button (with id my_btn) is pressed, the 2nd LinearLayout with EditText field is shown, with the following Java code:
LinearLayout secondLL = (LinearLayout) findViewById(R.id.second_ll);
Button myBtn = (Button) findViewById(R.id.my_btn);
myBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
int visibility = secondLL.getVisibility();
if(visibility==View.GONE)
secondLL.setVisibility(View.VISIBLE);
}
});
With the above Java code, the 2nd LinearLayout with EditText is shown like appending below the 1st LinearLayout which makes sense.
BUT, What I need is: when Button(id: my_btn) is pressed, the 2nd LinearLayout with EditText is shown on top of the 1st LinearLayout, which looks like the 2nd LinearLayout with EditText is rising from the bottom of screen, and the 2nd LinearLayout with EditText only occupy part of the screen from bottom, that's the 1st LinearLayout still visible, like the image below showed:
So, when Button(id: my_btn) is pressed how to show the 2nd LinearLayout with EditText on top of the 1st LinearLayout instead of appending 2nd LinearLayout below 1st LinearLayout programmatically?
Use a FrameLayout with two children. The two children will be overlapped. This is recommended in one of the tutorials from Android actually, it's not a hack...
Here is an example where a TextView is displayed on top of an ImageView:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/golden_gate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</FrameLayout>
FrameLayout is not the better way to do this:
Use RelativeLayout instead.
You can position the elements anywhere you like.
The element that comes after, has the higher z-index than the previous one (i.e. it comes over the previous one).
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
app:srcCompat="#drawable/ic_information"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a text."
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#A000"
android:textColor="#android:color/white"/>
</RelativeLayout>
The answer, given by Alexandru is working quite nice. As he said, it is important that this "accessor"-view is added as the last element. Here is some code which did the trick for me:
...
...
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
<!-- place a FrameLayout (match_parent) as the last child -->
<FrameLayout
android:id="#+id/icon_frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</TabHost>
in Java:
final MaterialDialog materialDialog = (MaterialDialog) dialogInterface;
FrameLayout frameLayout = (FrameLayout) materialDialog
.findViewById(R.id.icon_frame_container);
frameLayout.setOnTouchListener(
new OnSwipeTouchListener(ShowCardActivity.this) {