I am new to Android development and I have a question regarding custom views and using xml for view customization.
So i my code I am having a view defined using the extended view class i.e.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDisplay();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
draw = new DrawView(this, display.getWidth(), display.getHeight(), vibrator);
setContentView(draw);//custom view called DrawView
}
And in the DrawView class I am performing operations using the canvas.
My question is,
Can I use XML layouts combined with this view that I have defined?
I need to add a few buttons to this custom view, how can I achieve that in this scenario.
Thank you.
You can embed a custom view inside a layout. Here is an example:
<?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="horizontal" android:background="#FFFFFF">
<ListView android:id="#+id/channelsLogos" android:scrollbars="none"
android:layout_height="fill_parent" android:layout_weight=".20"
android:layout_width="100dip">
</ListView>
<test.poc.CustomScrollView
android:id="#+id/scrollViewVertical" android:scrollbars="none"
android:layout_weight=".80" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</test.poc.CustomScrollView>
</LinearLayout>
CustomScrollView is a custom component in package test.poc
Make sure you use the correct constructor while doing this.
Related
I'm fairly new to OpenGL and my problem is when I do SetContentView("R.layout.main") nothing happens, however when I do SetContentView("mGLView")the game is displayed fine. My main.xml file contains an surface view and therefore I would of thought that it would be displayed, since main.xml has an surface view.
I suspect a way to fix this, will be done with addview or findviewbyid, however my implication of this have failed.
Relevent MainActivity Code:
public class OpenGLES20Activity extends Activity {
private GLSurfaceView mGLView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new MyGLSurfaceView(this);
setContentView(R.layout.main);
main.xml code:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="#+id/linearLayout1" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SurfaceView
android:id="#+id/SurfaceView"
android:layout_width="500dp"
android:layout_height="478dp">
</SurfaceView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Timer"
android:id="#+id/Timer"/>
</RelativeLayout>
</FrameLayout>
Any help is appreciated.
You need to set your extended GLSurfaceView instead of just SurfaceView in the xml file so just change SurfaceView to com.example.yourapp.MyGLSurfaceView:
<com.example.yourapp.MyGLSurfaceView
android:id="#+id/SurfaceView"
android:layout_width="500dp"
android:layout_height="478dp"/>
I'm pretty new to Android and need a bit of help with some code.
Basically I have an array of coins and I want to dynamically display images for the coins in a ScrollView. Each time the user adds a coin the view should update appropriately.
Thank you!
Here is a screenshot of what I want to do - http://i.imgur.com/3l2fxKw.png
Try this sample
MainActivity
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
for(int x=0;x<3;x++) {
ImageView image = new ImageView(MainActivity.this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout1.addView(image);
}
}
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</ScrollView>
</LinearLayout>
Look into using SurfaceView.
You will have a canvas object to draw your images on, and then aligning the coins is just a matter of mapping your pixels appropriately.
This is a good tutorial:
http://www.mindfiresolutions.com/Using-Surface-View-for-Android-1659.php
Use a GridView instead and customize your own adapters. Gridview works like ListView
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 am in the process of converting an Android app to use fragments so as to support tablets in landscape mode.
There are two activities that I want to display side-by-side on a tablet in landscape mode. Currently, the first activity (DisplayNotams) has a button that calls an intent to display the second (Chart). The first is a text display of individual items, using a pager to view them one at a time, and the second is a chart on which the items are plotted.
The Chart does not use an xml layout file, as it creates its own ChartView thus:
ChartView mView;
mView = new ChartView(this, mCentreLat, mCentreLong, mRadius);
setContentView(mView);
(The class ChartView extends View, and contains most of the drawing code.)
I am using the github example commonsguy/cw-omnibus/LargeScreen/EU4you to base my revised code on. The problem (for me) is that this uses different layout xml files in layout and layout-large-land to distinguish between the two. Since I only have a layout xml file for DisplayNotams, and none for Chart, I am at a loss as to how to proceed.
Is there any way I can adapt the example to my situation?
For reference, the two xml files in EU4You are as follows.
In layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/countries"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
In layout-large-land:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/countries"
android:layout_weight="30"
android:layout_width="0px"
android:layout_height="fill_parent"
/>
<FrameLayout
android:id="#+id/details"
android:layout_weight="70"
android:layout_width="0px"
android:layout_height="fill_parent"
/>
</LinearLayout>
'countries' would correspond to my DisplayNotams, and 'details' would correspond to my Chart.
Thanks to those who answered. This is what I came up with, replacing
ChartView mView;
mView = new ChartView(this, mCentreLat, mCentreLong, mRadius);
setContentView(mView);
with
ChartView mView;
mView = new ChartView(this, mCentreLat, mCentreLong, mRadius);
setContentView(R.layout.chart);
FrameLayout fl = (FrameLayout)findViewById(R.id.chart_frame);
fl.addView(mView);
and creating a new chart.xml file:
<?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="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/chart_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
There's a group of addView functions available to ViewGroup. You can use that to add children to the FrameLayouts.
ChartView mView;
mView = new ChartView(this, mCentreLat, mCentreLong, mRadius);
FrameLayout frame = (FrameLayout) findViewById(R.id.countries);
frame.addView(mView);
Instead of using setContentView(mView) you can return your ChartView in the onCreateView-Method of your Fragment Class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ChartView mView;
mView = new ChartView(this, mCentreLat, mCentreLong, mRadius);
return mView;
}
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.