In my application I have to create a button(plus). Upon clicking that button, it should append a view like shown below dynamically below another.
the view has two edittext in a row. I don't know how to do that one. Please suggest me some tutorial for it. Do I have to make custom view for that? Thanks in advance.
Create a layout to generate it dynamically, layout needs to contain your email edittext and your spinner.
Then create your main layout as one linearlayout, oriented as vertical, and one your plus button. OnClick of this button, you can inflate generic layout and add it to above linearlayout, so it will automatically push your plus button.
say this is your main layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
and this is your layout, to be added (in a separate file)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hidden_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="#+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, this is the inflated text of hidden layout"/>
<EditText android:id="#+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, this is your name"/>
</LineraLayout>
in your activity
LinearLayout main = (LinearLayout)findViewById(R.id.main_layout);
now your button.onclick code would look like
View view = getLayoutInflater().inflate(R.layout.hidden_layout, main,false);
main.addView(view);
Related
I have an xml like below.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="#+id/etMsisdn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/allView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/msisdn"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimaryDark"
android:hint="MSISDN"
android:inputType="numberDecimal"
/>
<ImageView
android:layout_width="60px"
android:layout_height="match_parent"
android:src="#drawable/scan"/>
</LinearLayout>
</LinearLayout>
............
Another View
............
</LinearLayout>
How do I add EditText and ImageView programatically inside the horizontal LinearLayout (allView) and add the allView inside Vertical LinearLayout(etMsisdn) while keeping the same attribute as in xml.
The EditText and ImageView r supposed to below the msisdn edittext
here is your solution
LinearLayout allview=(LinearLayout)findViewById(R.id.allView);
EditText edt=new EditText(this);
ImageView img=new ImageView(this);
allview.addView(edt);
allview.addView(img);
put this in your activity
You have to use the addView method on the layout object.
But, if you want to add more times the same "sub layout" in the main layout it's better to create an xml layout with the "sub part" and add it programmatically. Let me know if the second case is what you need to provide the code.
Find LinerLayout and add views:
LinearLayout root = (LinearLayout)findViewById(R.id.allView);
root.addView(someView);
and add the attributes like this on your view:
How to programmatically set textview-s and their properties?
You need to get a reference to the outermost LinearLayout (the first one in your layout), so the best idea is to give it an Id:
<LinearLayout
android:id="#+id/myContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
Then, you need to get the reference to this layout and add the children views, like so:
LinearLayout containerLayout = (LinearLayout)findViewById(R.id.myContainer);
containerLayout.addView(yourView1);
containerLayout.addView(yourView2);
To set the desired layout alignment, you can either manually set the required LayoutParams (see this answer in SO) or you could inflate a layout and add it to your current layout, instead of two individual views (EditText and ImageView) (see this answer in SO).
I have a ListView that is being populated with a custom adapter. I have a pretty basic layout for each row of the the ListView (not even sure if this is applicable to my question):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/BaseStyle"
android:orientation="vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="8dip"
android:paddingBottom="8dip"
>
<TextView
android:id="#+id/text"
style="#style/BaseStyle.Title"
/>
</RelativeLayout>
I'd like to overlay a single TextView at the bottom of the ListView, but not entirely sure how as working with Android layouts can be an exercise in futility. :)
Wrap your main layout with a FrameLayout and put the view that you want to be in overlay as last child.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_container"
android:layout_height="match_parent"
android:layout_width="match_parent">
...
your ListView
...
your text view (it is gonna be on top of your layout)
</FrameLayout>
From your description I am not sure that this is what you are looking for, you should give some more info in your question, let me know.
If instead you are just talking about the TextView inside each list item then you just need to specifiy the alignment in your text view:
android:layout_alignParentBottom="true"
Lets say that I have a simple XML layout such as the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/my_container"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/leftContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Col A - Text 1"
/>
</LinearLayout>
<LinearLayout android:id="#+id/rightContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Col B - Text 1"
/>
</LinearLayout>
</LinearLayout>
Then I want to add a TextView to the rightContainer LinearLayout. I am currently doing this, unsuccessfully:
LinearLayout container = (LinearLayout) findViewById(R.id.rightContainer);
TextView textToAdd = new TextView(this);
textToAdd.setText("Col B - Text 2");
container.addView(textToAdd);
I have looked at LayoutInflater, but am not sure how I would use it here. Any help would be appreciated, thanks! If I try calling setContentView(container), I receive a Force Close error.
Don't call setContentView(), if you are using findViewById() then that view is already inside of your currently set content.
Adding views works fine. All a layout XML is, is a description of the views to create and add to the hierarchy. Make sure you are passing the correct layout params when adding a view -- here since container is a LinearLayout, you want a LinearLayout.LayoutParams.
You don't say in what way your code is "unsuccessful" so it is hard to help further.
Also you can use hierarchyviewer to look at what is actually going on in your view hierarchy.
In my app, I have one (and only one) UI element which isn't referenced in the XML layout file.
That element is a button, instantiated and returned at run-time by a 3rd party library (i.e. I don't have control over that).
My problem is that I would like some of the elements (TextViews) in the XML layout file to be placed relative to that button, using RelativeLayout.
Is it possible to "reserve an empty slot" in the XML layout file for that button such that I can do something like the following?
<TextView android:id="#+id/tv_text_under_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_dynamically_created_button"
android:text="" />
Alternatively, if I were to set the layout at run-time using RelativeLayout.LayoutParams.addRule(), what would be the ID of that dynamically created button, if it has no reference at all in the XML layout file?
For example, in the following call:
layoutParams.addRule(RelativeLayout.BELOW, R.id.btn_dynamically_created_button);
What would I put instead of R.id.btn_dynamically_created_button?
Update: Thanks to the answer below, I created a place holder like this:
<LinearLayout android:id="#+id/btn_dynamically_created_button"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
The challenge now is: How to associate the returned object from getDynamicallyCreatedButton() (returned object is subclass of LinearLayout, not Button), with R.id.btn_dynamically_created_button?
EDIT: This thread seem to address a similar issue, but I am not sure that I understand the solution offered.
I'd suggest:
Put a LinearLayout with width/height set to wrap-content, horizontal orientation and zero padding as the placeholder.
Orient all the other things to that LinearLayout.
When its time to put the button, simply stick it into the LinearLayout.
See if that works for you.
EDIT: attempt at a short example:
The layout (suitably shortened): you can place other components relative to the LinearLayout with id LinearLayout01.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_marginTop="2sp" android:layout_marginBottom="2sp" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="right" style="#style/SimpleButtonBar" android:layout_below="#+id/rootlayout" android:layout_alignParentBottom="true">
</LinearLayout>
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_above="#+id/LinearLayout01" android:fillViewport="true">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/detaillayout">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The code (for example, this would go in onCreate): fetch your button (you need to make sure it has the right Context, but I figure you're doing that alright), fetch the LinearLayout, create a layout parameters object and stick your button into the LinearLayout.
Button b = getButton(); // retrieve your button somehow
LinearLayout l = (LinearLayout)findViewById(R.id.LinearLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
l.addView(b, lp);
I am developing an Android app but I'm still pretty new. I want to have a button, and when you push that button, a few TextViews and Buttons will appear. So I have a main linear layout, and then another linear layout nested inside containing the things I want hidden. I have the nested linear layout set to android:visibility="gone".
The problem I am having is that it only shows the first item inside the hidden linear layout instead of all of them. The way I try to make it appear is
vgAddView = (ViewGroup)findViewById(R.id.add_details);
btnAche.setOnClickListener(new OnClickListener(){
public void onClick(View v){
vgAddView.setVisibility(0);
}
});
My XML file is this
<?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"
>
<Button
android:text="#string/but_stomach_ache"
android:id="#+id/but_stomach_ache"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="#string/but_food"
android:id="#+id/but_food"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<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/add_details"
android:visibility="gone">
<TextView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/when_happen">
</TextView>
<Button
android:text="#string/happen_now"
android:id="#+id/happen_now"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
Your TextView in the LinearLayout is set to android:layout_width="fill_parent" and android:layout_height="fill_parent", which means it will take up the entire space of the LinearLayout, leaving no room for the Button. If you use the hierarchyviewer tool that shipped with the SDK, you can see that when you look at the activity.
You need to set the height of the TextView to be wrap_content or otherwise have it leave room for the Button.
After you are setting its visibility to true set like below:
vgAddView.setVisibility(View.VISIBLE); - to show it
vgAddView.setVisibility(View.GONE); - to hide it
You first TextView has a layout_width="fill_parent".
It's mean,that you TextView don't leave space for Button.
You may try to set layout_weight attribute or set layout_width to wrap_content