I have a ViewFlipper defined that contains 3 views...
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flipper" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="#+id/first" layout="#layout/first_view" />
<include android:id="#+id/second" layout="#layout/second_view" />
<include android:id="#+id/third" layout="#layout/third_view" />
</ViewFlipper>
I also have a custom View defined within my Activity...
private class CompassView extends View {
#Override
protected void onDraw(Canvas canvas) {
...
}
}
Some how, I need these linked together so that the 'third_view' defined in the XML layout file needs to be a CompassView, or have a CompassView added to it.
What I can do is drop the 'third_view' from the layout and then add in the CompassView manually..
viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
viewFlipper.addView(new CompassView(this));
But then I lose the ability to define other view controls within the layout file.
Can I add CompassView to 'third_view' declaratively?
If the question is how to add a custom view in an Android XML layout file, try this:
<com.example.CustomView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.example.CustomView>
The open and close tags should just be the fully qualified class of your custom view.
Related
I have the following code where MyClass basically extends View. I was wondering if I need to use both setContentView(R.layout.activity_mainlcass_app) and setContentView(myDrawing) to show the 2D graphics that I draw in MyClass.
public class MainClass extends Activity {
MyClass myDrawing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainlcass_app);
myDrawing = new myDrawing(this);
setContentView(myDrawing);
myDrawing.requestFocus();
}
}
No, You can't do that. The second layout will override on the parent view.
In you main layout (activity_mainlcass_app) just add MyClass
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.MyClass
android:id="#+id/myclass"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Try like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.firstactivity
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.example.secondactivity
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
For more information check this link and this example
If you want new drawing on top of other (with different layouts sizes maybe) use FrameLayout for original layout and use gravity to position new drawing and use addView methods of activity.
I want to add two webview in a layout..I use frameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webview1"
android:layout_width="350dip"
android:layout_height="350dip" />
<WebView
android:layout_height="250dip"
android:layout_width="250dip"
android:id="#+id/webview2"
/>
</FrameLayout>
And in Main Activity :
web1=(WebView)findViewById(R.id.webview1);
web2=(WebView)findViewById(R.id.webview2);
web1.loadUrl("http://www.google.com");
web2.loadUrl("http://www.youtube.com");
web2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Animation anim=AnimationUtils.loadAnimation(FrameWebViewActivity.this, android.R.anim.slide_in_left);
web2.setAnimation(anim);
}
});
But when run project,it only display webview youtube full screen ..I want to display both two webview..What i must do??
Use another layout , a FrameLayout is used to display only a single child element.
I would suggest using a linear layout and specifying weights to divide the views into the desired proportions.
One thing you need to remember while using Framelayout is "When adding multiple views to a FrameLayout, each will be stacked on top of the previous one." So better use any other parent layout like Linear layout or Relative layout & in that use two framelayouts..
Code:
</RelativeLayout>
<?xml version=”1.0” encoding=”utf-8”?>
<RelativeLayout
android:id=”#+id/RLayout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android”>
<FrameLayout>
<WebView
android:id="#+id/webview1"
android:layout_width="350dip"
android:layout_height="350dip" />
</FrameLayout>
<FrameLayout>
<WebView
android:layout_height="250dip"
android:layout_width="250dip"
android:id="#+id/webview2" />
</FrameLayout>
</RelativeLayout>
I included second layout to first layout like this:
<?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" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="clip_horizontal"
android:layout_alignParentLeft="true"
android:id="#+id/rlMenu"
>
<Button
android:id="#+id/bMusteriler"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Musteriler"
android:textSize="45dp"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/rlEkranlar"
>
<include
android:id="#+id/include1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/ikinci" />
</RelativeLayout>
</RelativeLayout>
Problem is how can I change included layout when clicked a button(on java code)?
I suggest ViewFlipper inside RelativeLayout of your include statements. Try like this:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/vf"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<include android:id="#+id/include1" layout="#layout/ikinci" />
<include android:id="#+id/map" layout="#layout/third_layout" />
</ViewFlipper>
Access ViewFlipper as below.
Initially first layout is output:
ViewFlipper vf = (ViewFlipper)findViewById(R.id.vf);
For Button onClickListener:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
vf.setDisplayedChild(1);
}
});
There is two ways to change layouts in code:
A. Change visibility. Include tow different layouts in your xml:
<include
android:id="#+id/id1"
android:visibility="visible"/>
<include
android:id="#+id/id2"
android:visibility="gone"/>
and in code use this:
findViewById(R.id.id1).setVisibility(View.GONE);
findViewById(R.id.id2).setVisibility(View.VISIBLE);
B. Manually add children. Use the same xml as you use now and add in code you can add or delete children:
yourRelativeLayout.removeAllViews();
yourRelativeLayout.addView(viewToInclude);
Offtopic:
You don't need to write xmlns:android param in RelativeLayout. Just for the most top tag in layout file.
Have you tried to get the instance of the current view of the xml you have included the other xml and use findViewById(). View v is the first layout and against this view you use .findViewById()
I am trying to make some kind of button that can have content added to it like it is done to a FrameLayout. For example:
<ch.pboos.android.ui.ContentButton ...>
<LinearLayout ...>
<TextView .../>
<TextView .../>
</LinearLayout>
</ch.pboos.android.ui.ContentButton>
So far I made a view that xml file that would represent how i want the button to look like:
<?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" android:paddingLeft="#dimen/white_button_margin"
android:paddingRight="#dimen/white_button_margin">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:padding="#dimen/white_button_padding"
android:background="#drawable/channel_bg">
<FrameLayout android:id="#+id/content" ...>
<!-- content -->
</FrameLayout>
<ImageView ... android:src="#drawable/right_triangle" />
</LinearLayout>
</LinearLayout>
Now I would like to make a custom FrameLayout (ch.pboos.android.ui.ContentButton) that will add that view and allow me to add views into the content area of that xml above.
Any suggestions on how to get this working correctly?
1) Dispatch all touch events occurring on ContentButton to custom button via overriding dispatchTouchEvent. And add on click listener to this button (this helps you to recognize and handle only clicks, not e.g. moves)
2) override addView this way:
addView(...) {
if(inflatingInProgress) {
super.addView(view);
}
else {
// add view to #+id/content
}
}
// where flag inflatingInProgress initially = true and is set to false after
// finishing inflating the layout for ContentButton
Hope this helps. Good luck.
I've created my own view by creating a subclass of the SurfaceView class.
However I can't figure out how to add it from the xml layout file. My current main.xml looks like 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"
>
<View
class="com.chainparticles.ChainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
What have I missed?
Edit
More info
My view looks like this
package com.chainparticles;
public class ChainView extends SurfaceView implements SurfaceHolder.Callback {
public ChainView(Context context) {
super(context);
getHolder().addCallback(this);
}
// Other stuff
}
And it works fine like this:
ChainView cview = new ChainView(this);
setContentView(cview);
But nothing happens when trying to use it from the xml.
You want:
<?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"
>
<com.chainparticles.ChainView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Edit:
After seeing the rest of your code it's probably throwing because you can't call getHolder in the constructor while being inflated. Move that to View#onFinishInflate
So:
#Override
protected void onFinishInflate() {
getHolder().addCallback(this);
}
If that doesn't work try putting that in an init function that you call in your Activitys onCreate after setContentView.
It was probably working before because when inflating from xml the constructor:
View(Context, AttributeSet) is called instead of View(Context).
What you missed in your example was the tag name, it supposed to be "view" (first non-capital) not "View". Although you can put your class name as the tag name most of the time, it's impossible to do that if your class is inner class, because "$" symbol, which is used in Java to reference inner classes is restricted in XML tags.
So, if you want to use inner class in your XML you should write like 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"
>
<view
class="com.chainparticles.Foo$InnerClassChainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
The thing is that both "view" and "View" tags exist in the schema. "View" tag (started with capital letter) will generate a View class, while "view" tag, when parsed, will examine the class attribute.