I try to create relative layout programmatically.I wrote some code and I create layout ,I inflated my layout with my own custom view .I can't to control layout witch I created programmatically
addRulle not working compile.
This is a my xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/Bisoutflowproducttxt2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="15dp"
android:background="#drawable/credomoreinfo"
android:gravity="left|center_vertical"
android:hint="moreinfo"
android:paddingLeft="15dp"
android:textColor="#363636"
android:textSize="17dp" />
<ImageView
android:id="#+id/addmore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Bisoutflowproducttxt2"
android:layout_below="#+id/Bisoutflowproducttxt2"
android:layout_marginTop="30dp"
android:background="#drawable/add_icon" />
and this is a my java code
view = mInflater.inflate(R.layout.inflatelayout, null, false);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.addmore);
view.setLayoutParams(lp);
customlayout.addView(view);
As I said added new layout but I can not control my new layout.I want to my layout should be bellow my imageview
if anyone knows solution please help me
thanks
Related
i'm facing a problem in my android project . Here I have a imageButton , on click of this I have to inflate a layout file and dynamically add it inside a Relative layout . adding view is working perfectly . but the problem is after adding this child view I need move the image button below the new child layout . but my imageButton is inflating under the child view , maybe I'm missing some LayoutParameters and rules .
here are my code
private void addDestinationViews() {
View destinationChildView = LayoutInflater.from(getActivity()).inflate(R.layout.destination_city_item, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_TOP, R.id.add_destinationbutton);
destinationChildView.setLayoutParams(params);
mInflated_view_holder.addView(destinationChildView);
}
here is the layout i want to inflate
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/add_destination_button_back"
android:text="afaf"
android:textSize="18sp"
android:paddingLeft="30dp"
android:padding="10dp"
android:gravity="start|center"/>
<ImageButton
android:id="#+id/cancle_destination_imagebutton"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="#drawable/cross_place_icon"
android:scaleType="centerInside"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"/>
this is the relative layout which will host the inflated layout
<RelativeLayout
android:id="#+id/inflated_view_holder_three"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/child_counter_title"
android:layout_marginTop="15dp">
<ImageButton
android:id="#+id/add_destinationbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/add_place_icon"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="#drawable/add_destination_button_back"/>
</RelativeLayout>
P.S I forget to mention this child view will be added upon the imageButton press . so the code needs to be dynamic here .
Use this to host your inflated layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/inflated_view_holder_three"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="15dp">
</RelativeLayout>
<ImageButton
android:id="#+id/add_destinationbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/inflated_view_holder_three"
android:layout_marginRight="5dp"
android:src="#drawable/ic_launcher" />
<!--android:background="#drawable/add_destination_button_back"-->
</RelativeLayout>
Let me know in case of any issues
How can I make this layout in android programmatically.
I want just an idea not whole coding thing.
Here is the answer but not the direct answer. This is what I personally do to create complex layout programatically.
Create the same layout in XML.
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/some_big_image" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#DD000000" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView2"
android:layout_toRightOf="#+id/imageView2"
android:text="TextView"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView2"
android:layout_toRightOf="#+id/imageView2"
android:text="TextView"
android:textColor="#android:color/white" />
</RelativeLayout>
Now start from top parent to child.
RelativeLayout mParent = new RelativeLayout(this);
RelativeLayout.LayoutParams mParentParams = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
mParent.setLayoutParams(mParentParams);
ImageView mBigImageView = new ImageView(this);
mBigImageView.setLayoutParams(mParentParams);
mParent.addView(mBigImageView);
As you practice you can directly code the same without creating xml.
Basic Idea:
Create a RelativeLayout which contains one ImageView and two TextView which is aligned bottom
set the alpha of the RelativeLayout to adjust the transparency
You can have one relative layout which contain one image view for big image and below it one more linear layout containing image and text as "pearl Continental ..." and background of liner layout should be 50-60% transparent.
Create the background for LL using shapes. and set it as align bottom of parent (relative) layout.
Hope this helps you
I am working on an Android application.In my app I have change the textview alignment dynamically. The textview is in relativelayout.
the following is my sample xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/all"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
>
<TextView android:id="#+id/snt_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/Gold"
android:text="df"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="3dp"/>
</RelativeLayout>
And in java i did the following
View vi = inflater.inflate(R.layout.smsconversationsent_row, null);
RelativeLayout nws=(RelativeLayout)vi.findViewById(R.id.all);
TextView message=(TextView)vi.findViewById(R.id.snd_txt);
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)message.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
message.setLayoutParams(params);
But I am getting nullpointer exception for message.getlayoutparams().
Reason of nullpointer exception is the following: You're trying to find R.id.snd_txt, but in layout You have snt_txt, so it's impossible to find view with snd_txt id, because it doesn't exists in the layout.
I'm trying to insert a LinearLayout inside another LinearLayout. I don't know if what I am doing is right or not. I need to try it this way, without using inflation.
LinearLayout address2;
address2 = new LinearLayout(this);
address2 = (LinearLayout)findViewById(R.id.sfsp2_layout);
LinearLayout teste3 = (LinearLayout)findViewById(R.id.se_contentAdressPostal);
LinearLayout teste4 = (LinearLayout)teste3.findViewWithTag("teste");
teste4.addView(address2);
LinearLayout teste3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/se_contentAdressPostal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:background="#drawable/background_tile_address_postal"
android:orientation="vertical" >
<include
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/socio_form_structured_postal" />
LinearLayout teste4
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_tile"
android:orientation="vertical"
android:tag="teste" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp_layout_horizontal"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:layout_gravity="right"
android:background="#drawable/background_tile"
android:orientation="horizontal"
android:tag="teste" >
<Button
android:id="#+id/sfsp_btStructuredPostal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:hint="#string/sfsp_btStructuredPostal" /> .......
LinearLayour address2 ( The layout that i need to insert in layout4)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp2_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_tile"
android:orientation="vertical" >
<EditText
android:id="#+id/sfsp2_etStructuredPostalApartado"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="55dp"
android:layout_marginTop="2dp"
android:layout_weight="0.14"
android:ems="10"
android:hint="#string/sfsp2_etStructuredPostalApartado"
android:inputType="textMultiLine"
android:scrollHorizontally="false" >
The LinearLayout "teste4" is inside "teste3". I need to insert LinearLayout "address2" inside "teste4". Can you tell me what I'm doing wrong?.
I think there is some misunderstanding here about how the findViewById works. I'll try to explain it below. Hope this helps, let me know if it doesn't.
There are two possible findViewById() that can be used.
Activity.findViewById(): this can be used to find a view in the current activity's main content view -- that is, the view that was set using setContentView(). It cannot be used for any other purpose. For example, it cannot find a view from any other layout file -- the function simply would have no way of knowing how to find it. As far as I can tell, this is the function you're trying to use. If the last XML file you have is not the main layout, then the line address2 = (LinearLayout)findViewById(R.id.sfsp2_layout) will fail.
View.findViewById(): this function can be used to find a view that is contained in some other view. E.g. if you have view1 that contains view2, and view2 has ID some_id, then you can find view2 by calling view1.findViewById(R.id.some_id). In order for this to work, view1 has to be fully initialized. E.g. if view1's description is in XML, it has to be fully inflated first.
In essence, if you want to work with a view that is described in a separate XML file, you have to inflate it first. I don't think there is a way around it.
Edition of question
It is already in a separate xml (tablet_shortterm_column.xml) see above.
Anyway, it seems logcat complains that rlo_shortterm_col already has a parent, so it won't allow an another ptr_rlo_rght_middle.addView(rlo_shortterm_col ). Makes no sense.
I spent so many hours on thsi problem and still can't solve it. Can someone please give me a hand? Thanks in advance.
I have an xml file (tablet_shortterm_column.xml) that contains a RelativeLayout that I need to re-use, again and again. Sometimes many times on the same screen stacked one after the other horizontally. I am attempting to insert one into an existing RelativeLayout (ie. one inside the other.)
//exerpts
public class TabletMain extends Activity {
setContentView(R.layout.tablet_main);
public RelativeLayout ptr_rlo_rght_middle;
ptr_rlo_rght_middle = (RelativeLayout) findViewById(R.id.rlo_rght_middle);
//rlo_rght_middle is in tablet_main.xml
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View llo_tmp = (View) inflater.inflate(R.layout.tablet_shortterm_column,null);
RelativeLayout rlo_tmp = (RelativeLayout) llo_tmp.findViewById(R.id.rlo_shortterm_col);
// rlo_shortterm_col is the object I want to reuse it a RelativeLayout and is inside
// tablet_shortterm_column.xml
RelativeLayout.LayoutParams rlo_layoutparams;
rlo_layoutparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlo_layoutparams.addRule(RelativeLayout.RIGHT_OF, R.id.llo_rght_middle_col1);
// llo_rght_middle_col1 is a RelativeLayout inside tablet_main.xml,
// I want to put another RelativeLayout view right next to it.
rlo_tmp.setLayoutParams(rlo_layoutparams);
ptr_rlo_rght_middle.addView(rlo_tmp); //Application crashes right on this line.
} //end Activity
//********************* content of tablet_shortterm_column.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="#+id/rlo_shortterm_col"
android:layout_width="180dp"
android:layout_height="fill_parent"
android:background="#436699"
android:orientation="vertical"
android:layout_margin="3px"
android:weightSum="1"
> <!-- android:background="#32CD32" android:layout_height="365dp" android:layout_margin="30px" -->
<Button
android:id="#+id/btn_shortterm_col"
android:layout_alignParentTop="true"
android:text="Tuesday Afternoon"
android:layout_margin="15px"
android:textSize="12px"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#296699"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
> <!--android:background="#32CD32" -->
</Button>
<ImageView
android:id="#+id/iv_shortterm_col"
android:layout_below="#+id/btn_shortterm_col"
android:src="#drawable/tblet_icon14_med"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
><!-- android:src="#drawable/tblet_shape1" android:layout_gravity="center_horizontal" -->
</ImageView>
<TextView
android:id="#+id/tv_shortterm_col1"
android:layout_below="#+id/iv_shortterm_col"
android:text="-10ÂșC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:background="#DCDCDC"
android:textColor="#000000"
android:textSize="12px"
android:layout_centerHorizontal="true"
> <!-- android:layout_gravity="center_horizontal" -->
</TextView>
<TextView
android:id="#+id/tv_shortterm_col2"
android:layout_below="#+id/tv_shortterm_col1"
android:text="Flurries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:background="#DCDCDC"
android:textColor="#000000"
android:textSize="12px"
android:layout_centerHorizontal="true"
>
</TextView>
<RelativeLayout
android:id="#+id/rlo_shortterm_col_1"
android:layout_below="#+id/tv_shortterm_col2"
android:src="#drawable/tblet_shape2"
android:background="#32CD32"
android:layout_height="113dp"
android:layout_margin="40px"
android:layout_width="125dp"
> <!--android:background="#32CD32" android:orientation="vertical" -->
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Create a separate xml file for your RelativeLayout and then inflate it as many times as you want.
llo_tmp is the parent view of the RelativeLayout you're trying to reuse. Thus, you can't add it to another ViewGroup and you get that logcat error.
You can remove the LinearLayout stuff from your xml file and inflate the xml file in the same way (though maybe instead of returned View you'd return a RelativeLayout). You shouldn't have to change much of the java code since the reference is still the same.
Or, a quick fix may be to add llo_tmp to your ViewGroup instead of rlo_tmp. Either way, rlo_tmp already has a parent and can't be reused. Since you don't have your layout fill the entire screen width, you probably don't want this.