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.
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);
EDIT
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);
}
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.
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);
Related
I've looked on SO for a solution to this and I can't find one that works for my situation. I'm a beginner with Android development, so let me know if I'm doing something obviously wrong.
First a little background on the structure of my app. I use a navigation drawer with fragments for each of the pages, similar to the navigation drawer example in the Android documentation. On one of the pages I want to dynamically add TextViews to the LinearLayout of the fragment for that page when some event happens. The event isn't important in this case - it just calls a method in my activity.
So in my activity I have a method - addText(). Whenever addText() is called, I want to create a TextView, and then add this to the LinearLayout in the fragment. (the LinearLayout with id diary_layout)
How do I do this?
I have tried the following method. When this method is called, I do not see the text in the page.
public void textAdd(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.fragment_diary, null);
LinearLayout diaryLayout = (LinearLayout) contentView.findViewById(R.id.diary_layout);
TextView newTextView = new TextView(this);
newTextView.setText("Testing");
diaryLayout.addView(newTextView);
}
This is the structure of the layout file for the fragment:
<LinearLayout
android:id="#+id/theLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/diary_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity$PlaceholderFragment">
</LinearLayout>
</ScrollView>
Activity Layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"
android:background="#FFFFFF"/>
Thanks in advance!
You definitely don't want to inflate the content each time you add text. That would load the initial layout again, which is not what you want.
You didn't show how you construct your fragments, but based on your description, that you want to add a new TextView to a LinearLayout in one of the existing Fragments. I would expect you would first fetch the fragment, and then call some method in the fragment to add text. Something like:
public void textAdd(){
FragmentManager fm = getFragmentManager();
MyFragment myFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment);
if (myFragment != null) {
LinearLayout diaryLayout = (LinearLayout) myFragment.findViewById(R.id.diary_layout);
TextView newTextView = new TextView(this);
newTextView.setText("Testing");
diaryLayout.addView(newTextView);
}
}
I think you need to add layout params to your textview.. try it this way..
TextView newTextView = new TextView(this);
newTextView.setText("Testing");
newTextView .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
diaryLayout.addView(newTextView);
I am trying to design a 3D page curl.
In my mainActivity, I have a viewPager which contains pages to be curled. For each page I have a separate layout file.
In the layout file if i just add a text view like below it is showing up fine:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/PageTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:text="VIEW 1" /> -->
But if I wrap the textview inside relative layout, it just shows a white blank screen and textview is not shown.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DemoActivity" >
<TextView
style="#style/PageTitle"
android:id="#+id/sampletextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="VIEW 1" />
</RelativeLayout>
Can someone please help me to know why i am not able to use relative layout in my view layout file:
I inflate the view like this:
LayoutInflater inflater = (LayoutInflater) myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(mViewIds[0], null);
Try changing the second parameter of the inflate() call from null to the ViewGroup that view1 is added to. Or post the code where you add it (and include the part where you define the LayoutParams).
I was trying to fix this for a while and couldn't find the problem. I have a fragment in which I have ProgressBar that is centered. Here is the xml file content of fragment.
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:id="#+id/details_form"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:orientation="vertical"
android:visibility="gone" >
...
</LinearLayout>
</merge>
I also have activity and I just add this fragment to the activity. Here is the xml of activity
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/details_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
tools:context=".MyActivity"
tools:ignore="MergeRootFrame" />
The problem is when I navigate to this activity inside my application the progress is shown on the left side, it is not centered. I am not sure why is this happening, so I thought someone will see something I am missing. Thanks.
EDIT:
In onCreate event of my activity code (MyActivity.java) I have this code:
DetailsFragment detailsFragment = new DetailsFragment();
agreementDetailsFragment.setArguments(arguments);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.details_container,
detailsFragment).commit();
and in my fragment .java file in onCreateView I have this code
View rootView = null;
/*
* Get root view
*/
LinearLayout wrapper = new LinearLayout(getActivity());
inflater.inflate(R.layout.details_fragment, wrapper, true);
rootView = wrapper;
The problem is when I navigate to this activity inside my application
the progress is shown on the left side, it is not centered. I am not
sure why is this happening, so I thought someone will see something I
am missing. Thanks
The layout with the merge tag will have as a parent a LinearLayout. In a LinearLayout the android:layout_gravity will not work and you'll need a more permissive layout like a RelativeLayout or a FrameLayout(I'm assuming that the ProgressBar and the details_form LinearLayout will replace each other using the visibility property):
RelativeLayout wrapper = new RelativeLayout(getActivity());
inflater.inflate(R.layout.details_fragment, wrapper, true);
In my application I'm adding a view dynamically to the layout using addView.
The added view was inflated beforehand using
andorid:layout_width="match_parent"
however, after the adding the view using
parentView.addView(view, index);
The view is addded but dosen't fill the parent's width
I guess this is because the calculation to the "match_parent" size is done beforehand,
when the view is inflated, and therefore it has no reference of how to set the corect width
is this the case?
is there a way for me to tell the view to recalculate the layout params?
my Code:
activity.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"
android:background="#color/blue_bg">
<ImageView android:id="#+id/myImg"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:src="#drawable/myImg"
android:scaleType="fitXY"/>
<LinearLayout android:id="#+id/addressItemContainer"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="#id/myImg" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<View android:id="#+id/placeHolder"
android:layout_width="match_parent" android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
inserted_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#drawable/blue_selector">
.... internal views
</LinearLayout>
java code:
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.inserted_view, null);
... fill inserted view fields ...
View aiv = findViewById(R.id.placeHolder);
ViewGroup parent = (ViewGroup) aiv.getParent();
int index = parent.indexOfChild(aiv);
parent.removeView(aiv);
parent.addView(view, index);
Tx for the help :)
Ok, just add this code
View aiv = findViewById(R.id.placeHolder);
aiv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Mark as resolved, please.
View view = findViewById(R.id.placeHolder);
view .setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//addView(view)
Thanks Dmytro Danylyk. Now its work prefectly.
I'm trying to create a sliding drawer in code, but I don't understand what to do for the AttributeSet part of the constructor.
What do I need to do for that?
Also, how do I define in code where the slider is going to show up?
Thanks,
SlidingDrawer can't new in Java code because it must define handle and content but you can inflate in XML layout as follows:
sliding_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:handle="#+id/handle"
android:content="#+id/content">
<ImageView
android:id="#id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tray_handle_bookmark"
/>
<LinearLayout
android:id="#id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF000000"
/>
</SlidingDrawer>
In Java code:
// you main Layout
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
mainLayout.setOrientation(LinearLayout.VERTICAL);
// add sliding Drawer
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
slidingDrawer = (SlidingDrawer)inflater.inflate(R.layout.sliding_drawer, mainLayout, false);
mainLayout.addView(slidingDrawer);
// get Layout for place your content in sliding drawer
LinearLayout slideContent = (LinearLayout)slidingDrawer.findViewById(R.id.content);
slideContent.addView(.....); // add your view to slideDrawer
It looks like SlidingDrawer cannot be created directly in Java code. You will need to define it in an XML layout and inflate that layout.
Sorry!