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.
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 want to increase the clickable area of the button.But the image in the button should remain of same size.Also i have set image as a background not as source .How can i do that?
<Button
android:id="#+id/backbutton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:background="#drawable/arrow"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/title_gray"
android:textSize="14sp"
android:visibility="visible" />
Just make the parent layout of the button (of larger size or clickable size), and perform click event of that like -
<LinearLayout
android:id="#+id/backbuttonlayout"
android:layout_width="50dp"
android:layout_height="50dp">
<Button
android:id="#+id/backbutton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:background="#drawable/arrow"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/title_gray"
android:textSize="14sp"
android:visibility="visible" />
</LinearLayout>
Now, inside your activity, do like -
LinearLayout backbuttonlayout = (LinearLayout)findViewById(R.id.backbuttonlayout);
and perform setOnClickListener() on backbuttonlayout
Use TouchDelegate
Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds. The view whose touch area is changed is called the delegate view. This class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an instance that specifies the bounds that should be mapped to the delegate and the delegate view itself.
Example
public class LaunchActivity extends Activity {
private Button MyButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
MyButton = (Button)findViewById(R.id.Button1); //Your button ID
View parent = findViewById(R.id.layout); //Your Layout ID
parent.post(new Runnable() {
#Override
public void run() {
Rect delegateArea = new Rect();
Button delegate = MyButton;
delegate.getHitRect(delegateArea);
delegateArea.top -= 600; //Choose yourself
delegateArea.bottom += 600;
delegateArea.left -= 600;
delegateArea.right += 600;
TouchDelegate expandedArea = new TouchDelegate(delegateArea, delegate);
// give the delegate to an ancestor of the view we're
// delegating the
// area to
if (View.class.isInstance(delegate.getParent())) {
((View) delegate.getParent())
.setTouchDelegate(expandedArea);
}
}
});
}
}
I think this will help you out
You can use padding. It will put the space inside the view (margin will put it outside).
For example the following code will provide a clickable area of 20dp but the background will be of 10dp.
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/your_background"
android:padding="10dp" />
Remove margin, use padding around button.
Surround the button with a say a LinearLayout that has the padding round the button.
Add the same onclick to the LinearLayout as the Button.
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 :)
I want to put a layout into another one via layoutInflater.
Here's the main_layout.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/ads_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:id="#+id/host_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/navigation_buttons_layout"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ads_image_view" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/navigation_buttons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
and here's the vitrin_layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
android:text="smallred" />
and finally the onCreate method for my main activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout);
View view = LayoutInflater.from(getBaseContext()).inflate(
R.layout.vitrin_layout, null);
parent.addView(view, LayoutParams.FILL_PARENT);
setTitle(getString(R.string.title_activity_vitrin));
}
The problem is that the vitrin_layout, does not fit into its parent(host_layout); although I've set fill_parent where ever I could!
I can't figure out why.
Edited: I'm really sorry, but i'd pasted wrong vitrin_layout file, I fixed it.
Use this addView method. By the way don't use getBaseContext() unless you know why you are using it, instead use context of an activity (this).
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout);
View view = LayoutInflater.from(this).inflate(R.layout.vitrin_layout, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
parent.addView(view, params);
setTitle(getString(R.string.title_activity_vitrin));
}
parent.addView(view, LayoutParams.FILL_PARENT);
this method was ViewGroup.addView(View child, int index) invoked actually.
you need to invoke ViewGroup.addView(View child, ViewGroup.LayoutParams params) method to set a LayoutParams.
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);
}
}