Reputation: 153
Two Layout files:
activity_maps.xml
<FrameLayout
android:id="@+id/fragment_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jawadh.startthreeproject.MapsActivity" />
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
</FrameLayout >
second.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_container"
android:layout_height="50dip"
android:layout_width="50dip"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
<TextView
android:id="@+id/textViewSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewDest"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
In my activity, once the mapsActivity gets loaded and working fine. I want to load the view of second.xml over the exisiting view. So, I am trying this:
final FrameLayout frameLayout = (FrameLayout)View.inflate(getApplicationContext(),R.layout.second,null);
final Button button = (Button)frameLayout.findViewById(R.id.button);;
button.setBackgroundColor((Color.GRAY));
button.setText("Direction");
button.setY(300);
button.setX(300);
TextView textViewSource = (TextView)frameLayout.findViewById(R.id.textViewSource);
textViewSource.setText(place.getAddress().toString());
textViewSource.setTextColor(Color.BLACK);
textViewSource.setBackgroundColor(Color.WHITE);
frameLayout.setBackgroundColor(Color.BLUE);
Expectation: This last part of code should put te second.xml view(framelayout) over the exisiting view.
Upvotes: 0
Views: 2300
Reputation: 38223
Warning: DO NOT use application context for inflating views!
You created the second layout here:
final FrameLayout frameLayout = (FrameLayout)View.inflate(this, R.layout.second,null);
Now you have to attach it to view hierarchy. Fragment container sounds nice enough:
FrameLayout container = (FrameLayout) findViewById(R.id.fragment_container);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
container.addView(frameLayout, lp);
Alternatively you could inflate AND attach new view in one go like so:
FrameLayout container = (FrameLayout) findViewById(R.id.fragment_container);
final FrameLayout frameLayout = (FrameLayout)View.inflate(this, R.layout.second, container);
...because FrameLayout
will provide you with these LayoutParams
by default:
/**
* Returns a set of layout parameters with a width of
* {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT},
* and a height of {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT}.
*/
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
Upvotes: 1
Reputation: 4623
You can replace view by set visibility to GONE.
In your case, you should create one xml file instead of two. In xml file, take one linearlayout with orientation vertical. In this linearlayout take two framelayouts.When your activity starts, set second framelayout's visibility to GONE. When map is loaded in first framelayout, set it's visibility to GONE and second framelayout's visibility to VISIBLE. When you do this, second framelayout will automatically replace first framelayout.
sample code:
private LinearLayout llLogin;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
llLogin = (LinearLayout) findViewById(R.id.loginlayout);
llLogin.setVisibility(View.GONE);
Upvotes: 0
Reputation: 526
Well, a little more detail was required. And hey, whatever you are doing, you are not doing it the best way.
Anyways, you inflated the view in frameLayout
but you are not adding this to the present parent.
Am not sure what exactly you want to achieve but I think you are missing that part.
Upvotes: 0