I have a flying in menu which based on ViewGroup.
I want that there will be a basic layout and in any activity i would be able to insert to the view group new layout and afterwards to erase it.
But it doesn't work!!! Can you help me please.
class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_main, null);
this.setAdditionalLayout(findViewById(R.id.physical_layout));
this.setContentView(root);
}
ViewGroup:
<com.nurielweizmann.calculator.view.viewgroup.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444488"
android:orientation="vertical"
android:id="#+id/menu">
............
</RelativeLayout>
</com.nurielweizmann.calculator.view.viewgroup.FlyOutContainer>
Function:
FlyOutContainer root;
public void setAdditionalLayout(View view){
root.addView(view,1);
}
Thanks in Advance
Try overriding the setContentView(int) instead of the onCreate(Bundle).
Make sure your base layout XML has a ViewGroup (FrameLayout, for example) available to put each Activity's content.
When overriding the setContentView(int), inflate your base layout first then inflate the Activity's layout and place the Activity's layout on the FrameLayout available.
Related
Following is a xml: welcome_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<ImageView
android:id="#+id/welcome_bg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.demo.src.WelcomeLayout
android:id="#+id/welcome_ad"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
This is the MainActivity, I setContentView with layout welcome_view.xml.
class MainActivity extends Activity
{
onCreate()
{
setContentView(R.layout.welcome_view);
ViewGroup view1 = findViewByID(R.id.ad_view);
}
}
WelcomeLayout has been contained in the welcome_view.xml. Please tell me the view in the following class is different with the one in above class??? Why, tell me some the inner mechanism.
class WelcomeLayout extends FrameLayout
{
onCreate()
{
super(context);
View.inflate(context, R.layout.welcome_view, null);
ViewGroup view2 = findViewById(R.id.ad_view);
}
}
Thanks!
reference:Difference between setContentView and LayoutInflater
setContentView is an Activity method only. Each Activity is provided with a FrameLayout with id "#+id/content" (i.e. the content view). Whatever view you specify in setContentView will be the view for that Activity. Note that you can also pass an instance of a view to this method, e.g. setContentView(new WebView(this)); The version of the method that you are using will inflate the view for you behind the scenes.
on the other hand, have a lifecycle method called onCreateView which returns a view (if it has one). The most common way to do this is to inflate a view in XML and return it in this method. In this case you need to inflate it yourself though. Fragments don't have a "setContentView" method
Note:link might be destroy so answer pasted.
I want to display a graph in a fragment, but I didn't found many information about how to use GraphView in general. Can someone please help me to display a simple graph?
I started with this: http://android-graphview.org/#doc_createsimplegraph but don't get it working in a fragment.
What I did was to create a fragment_graph.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1"
android:background="#102f65" />
</LinearLayout>
and add the relevant code in the Fragment class:
public class GraphFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_graph, container, false);
// add the data here if necessary
// ...
LinearLayout layout = (LinearLayout) view.findViewById(R.id.graph1);
layout.addView(graphView);
return view;
}
// ...
}
Then you can use the fragment "as is".
Most credit actually goes to vogella.
define a empty LinearLayout with fixed width and height in your layout.xml file and then add the graphview view to it.
Layout
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1" />
Java
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
Just for the record:
You cannot use it in connection with ScrollViews. If a ScrollView is wrapping your LinearLayout the lib doesn't show anything.
I tried to replace a Fragment in FragmentActivity run-time.
fragment_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MyFragmentActivity.java
public class MyFragmentActivity extends SlidingFragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
}
// Will be called by AsyncTaskLoader's onLoadFinished.
public void selectActiveContent() {
// MyFragment's top level is LinearLayout
Fragment fragment = new MyFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commitAllowingStateLoss();
}
However, I realize the outcome is not optimized.
I realize the FrameLayout supplied through my own fragment_activity.xml, is being attached to another parent FrameLayout.
Instead of
FrameLayout
FrameLayout
LinearLayout
LinearLayout
ListView
I wish to have
FrameLayout
LinearLayout
LinearLayout
ListView
May I know how I can achieve so? Is it possible I can have a fragment_activity.xml without a ViewGroup (FrameLayout) ?
While the <merge> element was specifically designed for removing useless levels of view hierarchy, per this answer, you cannot use merge tags in Fragments and you must instead use a ViewGroup root view as you noticed, the lightest weight (i.e., fastest to render) being a FrameLayout.
There are a number of questions like this but the solutions haven't worked for me.
Anyways, my main activity has a button that, in its onclick method, takes me to another activity, ViewPowerActivity. I have a layout xml file for it called power_view.xml. Inside that I have some layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
...
ViewPowerActivity has the basic onCreateMethod:
public class ViewPowerActivity extends Activity {
private final static Powers powers=new StubPowers();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.power_view);
Power power=powers.getPowers().get(0);
View powerView = findViewById(R.layout.power_view);
...
}
...
}
The findViewById call above is returning null.
If I remove all the code after setContentView(...) and simply return there, it displays the empty layout just fine. I've setContentView, I've cleaned the project, I've tried setting the power view as the main activity, and all sorts of things. What else could it be?
From you code it is clear thar power_view is a layout ie. xml. So R.id.power_view is incorrect.
It seems you want to access the parent view of that layout. Then do the following.
You have to set an id to the parent LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
...
Then,
View powerView = findViewById(R.id.parentLinear);
If you want to take some other view in powerView, you should set id to that view in xml layout and initialize your that view to power_view by findViewById(R.id.your_view)
power_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
public class ViewPowerActivity extends Activity {
private final static Powers powers=new StubPowers();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.power_view);
Power power=powers.getPowers().get(0);
View powerView = (ViewGroup)findViewById(R.id.layout);
...
}
...
}
My abstract class extended from Activity class consists of three Views as described in the following XML snippet:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bg">
<LinearLayout
android:id="#+id/top_border">
...
</LinearLayout>
<View
android:id="#+id/activity_content"
...
/>
<LinearLayout
android:id="#+id/bottom_border">
...
</LinearLayout>
</LinearLayout>
And in onCreate() method I set this XML-layout as content view.
I want the Activitys which extend this one to override onCreate() and there define the activity_content View remaining borders immutable.
For example like this:
abstract public class MyActivity extends Activity {
protected View mContent;
#Override
protected onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.common_layout);
initializeContent();
}
abstract void initializeContent();
}
class OneActivity extends MyActivity {
#Override
protected void initializeContent() {
mContent = View.inflate(this, R.layout.some_view, null); // i.e. concrete View (e.g. LinearLayout, FrameLayout, GridView, etc.)
}
}
But when I do so my mContent remain the same as it was when I defined it in MyActivity's onCreate().
How can I change view's type/content depends on what Activity is in foreground?
First change the view in your main layout into a view group (for example, a LinearLayout). Then you can add views to it. If you add a unique view, it will have exactly the effect you want to achieve.
class OneActivity extends MyActivity {
#Override
protected void initializeContent() {
final ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_content);
viewGroup.addView(View.inflate(this, R.layout.some_view, null));
}
}
In your case that should work. If your custom view group contained other views from higher up in the hierarchy, you can clean it before adding your custom view:
viewGroup.removeAllViews();
It works, I do exactly that in most of my projects.
An alternative is to look at the Fragments API, available for latest versions of the SDK.
You can't override class data members in Java, use methods instead