I have a RelativeLayout for a ListView item which contains an image, and to the right of it (from top to bottom) 5 TextViews and a button:
------- textView
| | textView
| | textView
| | textView
| | textView
------- button
The width of the image is fixed, and its height is unknown, so what I want is the height of the image to be equal to the height of the other 5 views combined (as appears in the example above).
I tried doing so in the list_item xml, but I can't seem to be able to do that.
The best I got, was setting the height of the view in the adapter's getView function, but I'm doing that for each item in the list, and that seems wasteful.
Is there any way to set this in the xml file?
Can I set the height to "0dp" in the xml and only set it once in the code somewhere (instead for each item in getView)?
Thanks
Here's a piece of code that worked for me:
MainActivity.java:
package com.example.simon.test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
// Delay the image, so textViews are created and ready for measure
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imageView.setMaxHeight(rLayout.getMeasuredHeight());
imageView.setImageResource(R.drawable.image);
}
}, 100);
}
}
layout/activity_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:contentDescription="hello"/>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there"
android:layout_toRightOf="#id/imageView"/>
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there"
android:layout_below="#+id/tv1"
android:layout_toRightOf="#id/imageView"/>
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there"
android:layout_below="#+id/tv2"
android:layout_toRightOf="#id/imageView"/>
<TextView
android:id="#+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there"
android:layout_below="#+id/tv3"
android:layout_toRightOf="#id/imageView"/>
<TextView
android:id="#+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there"
android:layout_below="#+id/tv4"
android:layout_toRightOf="#id/imageView"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there"
android:layout_below="#+id/tv5"
android:layout_toRightOf="#id/imageView"/>
</RelativeLayout>
Note the scaleCrop for scaling the image and adjustViewBounds so the maxHeight can be set.
Also note that you need to give some time for the other elements of your RelativeLayout to be created so they can be measured.
Set height of your RelativeLayout to wrapcontent should do the work, same forYour image height
Since the items to be inflated are needed in a second activity, I've decided to inflate the layout in the first activity and set its height to a public static member of that activity, to be read by the second activity.
I eventually went with waiting for the layout to finish inflating, saving its height, and setting its visibility to GONE before it is displayed.
public static int listItemLayoutHeight;
...
protected void onCreate(Bundle savedInstanceState) {
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.dummyListItemLayout);
relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
relativeLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
listItemLayoutHeight = relativeLayout.getHeight();
relativeLayout.setVisibility(View.GONE);
}
});
...
}
BTW, another option could be to put this layout inside a FrameLayout, and cover it with the main layout.
Thanks for the help :)
Related
This XML is inflated from my main activity, I would like to know if how do I set the inflated view as full screen or fill_parent as shown in the XML, because when I test my app it will give me a half size screen shown on the right side.
The first pic is from my pc, and the second is from my tab.
Here's my XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal|center_vertical"
android:background="#drawable/splash"
android:paddingBottom="#dimen/_16sdp"
android:paddingLeft="#dimen/_16sdp"
android:paddingRight="#dimen/_16sdp"
android:paddingTop="#dimen/_16sdp" >
<Button
android:id="#+id/g1btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/g1tv2"
android:layout_centerHorizontal="true"
android:text="BUTTON"
android:textSize="#dimen/_70sdp" />
<TextView
android:id="#+id/g1tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""THIS""
android:textSize="#dimen/_70sdp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/g1tv2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="TAP"
android:textSize="#dimen/_70sdp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="GAME 1"
android:textSize="#dimen/_50sdp" />
</RelativeLayout>
Here's my MainActivity.Java
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
View game1 = getLayoutInflater().inflate(R.layout.game1, null);
RelativeLayout item = (RelativeLayout)findViewById(R.id.Main);
item.addView(game1);
}
The problem is not your Wrapping layout, you are adding a view to another View with no layout params with respect to the parent.
Tell your inflated view, this is your parents layout params, for example:
View view = getLayoutInflater().inflate(R.layout.game1, null);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id. Main);
view.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
relativeLayout.addView(view);
this should solve your problem without just putting a LinearLayout wrapping it.
Good Luck and Happy coding!
Add this code before setContentView();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Use Linear layout as the parent View and give android:orientation = "vertical". Relative Layout is a a Layout where the positions of the children are described in relation to each other or to the parent.If you want to use relative layout then you have to use the addrule property or else all the child will be drawn over the previous child.
View game1 = getLayoutInflater().inflate(R.layout.temp, null);
LinearLayout item = (LinearLayout)findViewById(R.id.main);
item.addView(game1);
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 have codes of a RelativeLayout activity.xml
<RelativeLayout
android:layout_height="50dip"
android:layout_width="fill_parent"
android:id="#+id/relativeLayout"
android:background="#686868"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="#FFFFFF"
android:id="#+id/btnBack"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/title_header"
android:text="Header of page"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
/>
</RelativeLayout>
And here is code of Activity:-
public class MainActivity extends Activity {
int valCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
if(valCheck==0)
{
//here set title again for backbutton is "backPreviousPage" and set
//position again of title_header is right of backButton with
//margin=5dip(not set position is centerInParent as code
}
}
}
I don't know the way to get element and set margin in relativeLayout by code..Can you help me?
This may helps you
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.RIGHT_OF, 1001);
params.addRule(RelativeLayout.LEFT_OF, 1000);
Here id 1001 and 1000 is my EditText id which also dynamic added to Relative
To access UI Elements within Android code, you use the findViewById method. For example:
Button btnBack = (Button)findViewById(R.id.btnBack);
Once you have access to all of the elements you need, you can use the setter functions provided in the API to change the layout as need-be. Since most (if not all) UI elements are a child class of the View class, the methods you will need will most likely be defined there. Here is a good reference:
http://developer.android.com/reference/android/view/View.html
All View objects inherit the setLayoutParams method. So you can apply the method suggested by Gunaseelan to your Button and your TextView as well.
Try this code....
<LinearLayout
android:layout_height="50dip"
android:layout_width="fill_parent"
android:id="#+id/linearLayout"
android:background="#686868"
android:orientation="horizontal"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="#FFFFFF"
android:id="#+id/btnBack"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/title_header"
android:text="Header of page"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
/>
</LinearLayout>
and in java code...
public class MainActivity extends Activity {
int valCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button btn = (Button) findViewById(R.id.btnBack);
if(valCheck==0)
{
//here set title again for backbutton is "backPreviousPage" and set
//position again of title_header is right of backButton with
//margin=5dip(not set position is centerInParent as code
btn.setText("backPreviousPage");
}
}
}
Try this way.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
params.setMargins(80, 0, 0, 0); //left, top, right, bottom
params.height = 60; //set height dynamically
params.width = 200; // set width dynamically
relativeLayout.setLayoutParams(params);
I hope this will help you.
Got my TextView in the xml:
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:text="TextView"/>
I want to create multiple TextViews but i want to look them the same as this.
So I tried:
TextView newTextView = new TextView(this);
newTextView.setLayoutParams(myTextView.getLayoutParams());
I think this should get all the layout parameters from myTextView straigthlghly(?) from the xml, and pass them to newTextView to set them.
My problem is: Nothing happens. It does not take effect, why?
here's a sample project which shows that it works.
you can see that it works by looking at the preview of the visual editor , which looks different than what is shown at runtime.
i think that your mistake was that you didn't set 0px (or 0dp, zero is still zero) for the weighted views.
main.xml (layout) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/container">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="0px" android:text="TextView1"
android:layout_weight="1" android:background="#ffff0000" />
<TextView android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="TextView2"
android:background="#ff00ff00" />
</LinearLayout>
TestActivity.java :
public class TestActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv1=(TextView)findViewById(R.id.textView1);
final TextView tv2=(TextView)findViewById(R.id.textView2);
final LayoutParams layoutParams=tv1.getLayoutParams();
tv2.setLayoutParams(layoutParams);
// adding textView programatically:
final TextView tv3=new TextView(this);
tv3.setText("textView3");
tv3.setBackgroundColor(0xff0000ff);
tv3.setLayoutParams(layoutParams);
final ViewGroup root=(ViewGroup)findViewById(R.id.container);
root.addView(tv3);
}
}
I am trying to make a new layout page where I want to put two buttons, and on the above of each button I need to give a frame animation. so on loading the buttons are looking like inside the bubbles. Following is the code I am using to achieve this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_full">
<Button android:id="#+id/btnMusic"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center"
android:layout_marginLeft="215dp"
android:layout_marginTop="140dp"
android:background="#drawable/icon"/>
<ImageView android:id="#+id/imgMusic"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
android:layout_marginLeft="170dp"
android:layout_marginTop="100dp"
android:background="#drawable/button_background"/>
<Button android:id="#+id/btnMovies"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center"
android:layout_marginLeft="405dp"
android:layout_marginTop="140dp"
android:background="#drawable/icon1"/>
<ImageView android:id="#+id/imgMovies"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
android:layout_marginLeft="360dp"
android:layout_marginTop="100dp"
android:background="#drawable/button_background"/>
</RelativeLayout>
My jav code is like this:
public class BubbleActivity extends Activity {
/** Called when the activity is first created. */
/** Called when the activity is first created. */
Button btnMusic, btnMovies ;
ImageView imgMusic,imgMovies;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
btnMusic = (Button)findViewById(R.id.btnMusic);
btnMovies = (Button)findViewById(R.id.btnMovies);
btnMusic.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent intent = new Intent(PixieActivity.this,Splash.class);
startActivity(intent);
}
});
ImageView imgMusic = (ImageView)findViewById(R.id.imgMusic);
imgMusic.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable frameAnimation =(AnimationDrawable) imgMusic.getBackground();
if (frameAnimation.isRunning()) {
frameAnimation.stop();
}
else {
frameAnimation.start();
}
ImageView imgMovies = (ImageView)findViewById(R.id.imgMovies);
imgMovies.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable frameAnimation1 =(AnimationDrawable) imgMovies.getBackground();
if (frameAnimation1.isRunning()) {
frameAnimation1.stop();
}
else {
frameAnimation1.start();
}
}}
But due to the margins the button layout became distracted in different phone resolutions. Is there any other way to achieve the same layout with device resolution independant. Also I want to add the bubble animation to each of the icons i will make in next pages. Please help.
I would suggest not hard-coding the margins, and instead wrap the Buttons and ImageViews in a LinearLayout each, then set the spacing using layout_weight so it is perfectly scalable.
The actual layout choice depends on if you need the button to be exactly 80x80 and the ImageView to be exactly 150x150.
For instance (pseudo-code: obviously many parameters are left out) :
<RelativeLayout>
<LinearLayout id="buttons" >
<LinearLayout layout_width = "0dp" layout_weight = "1"> <!-- 0 width is important! -->
<Button layout_gravity="center" />
</LinearLayout>
<LinearLayout layout_width = "0dp" layout_weight = "1">
<Button layout_gravity="center" />
</LinearLayout>
</LinearLayout>
<LinearLayout id="images" align with #buttons>
<LinearLayout layout_width = "0dp" layout_weight = "1">
<ImageView layout_gravity="center" />
</LinearLayout>
<LinearLayout layout_width = "0dp" layout_weight = "1">
<ImageView layout_gravity="center" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
And just make sure set the LinearLayouts "buttons" and "images" to the same height and full width so that the buttons and images overlap. More about layout_weight: What does android:layout_weight mean?
If you do not need the Buttons and ImageViews to be an exact size, think about sizing them by weight as well. Then no matter what screen you are on, if you tell a button to take up 1/4 of it through layout_weight, it will never be distorted