I have a class called Panel like this:
public class Test extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Panel(this));
...
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private LinearLayout layout;
public Panel(Context context) {
super(context);
layout = //get layout resource in layouts folder
}
public void onDraw(Canvas canvas) {
//I want to draw my xml layout
layout.draw(canvas);
}
}
I have tried to use LayoutInflater in order to get my layout:
LayoutInflater inflater = LayoutInflater.from(context);
layout = new LinearLayout(getApplicationContext());
inflater.inflate(R.layout.mylayout, layout);
... but I cant see anything on the screen, only a black background :S
as far as I know you can't draw a linear layout inside the SurfaceView you can draw anything you want in your SurfaceView if you want to add elements to your view like buttons or something like that I recommend using the XML and putting all your elements there. Something like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView android:id="#+id/mySurfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</SurfaceView>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="myButton"/>
</LinearLayout>
and then you may set your content of your view with setContentView(R.layout.my_xml);
Related
I have an Activity which has a button and by code I draw a Circle, so my Circle class is:
public class Circle extends View {
private float x = 100;
private float y = 100;
private float radius = 50;
public Circle(Context context) {
super(context);
}
protected void onDraw(Canvas canvas){
Paint paint = new Paint();
canvas.drawCircle(x, y, radius, paint);
}
}
And the code of my activity is:
public class AnotherTest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Circle c = new Circle(this);
setContentView(c);
}
}
But when I invoke setContentView, the button seems to be deleted. Any tips to show the circle and preserve the button?
The button you mentioned is in your activity's layout XML file? If so, could you provide the code? setContentView() will only show the circle. If you want to add the circle to your existing layout you have to add it to a ViewGroup in your Activity's XML.
You could do something like this:
public class AnotherTest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.anothertest_activity);
}
}
And the (res/layout/)anothertest_activity.xml file could look like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<your.package.Circle
android:id="#+id/myCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
In Android Studio, right below the xml code are two tabs: "Design" and "Text". Switch to "Text" to paste code, switch back to "Design" to position your elements.
If you drag your views, you have an layout XML file. In the Activity you need to set this file as your content view, otherwise you wouldn't see your views. But you can add views dynamically by doing something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_viewgroup"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
And in your Activity:
public class AnotherTest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Still your xml with your button but without circle
setContentView(R.layout.anothertest_activity);
// First find your ViewGroup to add Views to
RelativeLayout viewGroup = (RelativeLayout) findViewById(R.id.my_viewgroup);
// Add a new circle to existing layout
viewGroup.addView(new Circle());
}
}
You also could add everything dynamically:
public class AnotherTest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup viewGroup = new LinearLayout(this);
setContentView(viewGroup);
viewGroup.addView(new Button());
viewGroup.addView(new Circle());
}
}
But I strongly recommend using xml layouts. Might want to take a look at Android Layouts
I've created a custom view, seen below. I declare this custom view in other xml files. The issue I'm having is that when I call
MyVideoView.setVisibility(View.GONE)
The video that was previously drawn on the SurfaceView is still visible. Shouldn't setting the visibility of the whole View affect the children? Am I missing something?
public class MyVideoView extends FrameLayout {
private SurfaceView videoSurfaceView;
private SurfaceView subtitleSurfaceView;
public MyVideoView(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
View.inflate(context, R.layout.widget_my_video_view, this);
videoSurfaceView = (SurfaceView) findViewById(R.id.svPlaybackSurface);
subtitleSurfaceView = (SurfaceView) findViewById(R.id.svSubtitles);
}
}
widget_my_video_view.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="#+id/svPlaybackSurface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
<SurfaceView
android:id="#+id/svSubtitles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginStart="#dimen/paddingRegular"
android:layout_marginEnd="#dimen/paddingRegular"
android:layout_marginBottom="#dimen/paddingLarge"
android:visibility="invisible"/>
</FrameLayout>
I try to use setContentView (R.layout.main) to display both an imageview and a Custom View. Somehow only the imageview can be shown but the Custom view is not visible (i.e. Bitmap ButtonStart not visible). I tested my code for onDraw and Texture class (responsible to load a bitmap object) somewhere else and it works fine. So I dunno what goes wrong...
Code for Custom view
public class TitleView extends View {
private Bitmap ButtonStart;
public TitleView (Context context, AttributeSet attrs) {
super (context, attrs);
Texture t2 = new Texture(context);
t2.loadFromAsset("button_Start.png");
ButtonStart = t2.getBitmap();
}
#Override
protected void onDraw (Canvas canvas) {
canvas.drawBitmap (ButtonStart, 0, 0, null);
}
}
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature (Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_screen_image);
}
}
main_screen_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainScreenLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="#+id/mainScreenImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/screenimage"
>
</ImageView>
<com.lowbband.chimera.TitleView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/titleview"/>
</LinearLayout>
With the code above, only screenimage was shown...
<ImageView
android:id="#+id/mainScreenImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" //it should be wrap_content otherwise it will take whole space
android:scaleType="centerCrop"
android:src="#drawable/screenimage"
>
</ImageView>
<com.lowbband.chimera.TitleView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/titleview"/>
And if the image is large and still fills the screen then put ScrollView outside LinearLayout
So I'm experimenting with implementing an MVC pattern in Android where my views are subclassed from RelativeLayout, LinearLayout, ScrollView, etc... It's working until I try to get a hold of a view within my view. I get an NPE. I've tried accessing the view in order to set the onClickListener in the constructor and also in onAttachedToWindow(), but I get the NPE in both places.
For example, here's a view class:
public class ViewAchievements extends LinearLayout
{
private RelativeLayout mRelativeLayoutAchievement1;
public ViewAchievements(Context context, AttributeSet attrs)
{
super(context, attrs);
mRelativeLayoutAchievement1 = (RelativeLayout) findViewById(R.id.relativeLayout_achievement1);
mRelativeLayoutAchievement1.setOnClickListener((OnClickListener) context); //NPE on this line
}
#Override
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
mRelativeLayoutAchievement1.setOnClickListener(mOnClickListener); //Also get NPE on this line
}
}
Can someone please tell me the proper way to get a hold of my subviews, in this case mRelativeLayoutAchievement1?
Here's an XML snippet:
<com.beachbody.p90x.achievements.ViewAchievements xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/gray_very_dark"
android:orientation="vertical" >
<!-- kv Row 1 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:baselineAligned="false">
<RelativeLayout
android:id="#+id/relativeLayout_achievement1"
style="#style/linearLayout_achievement"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="#dimen/margin_sm"
android:layout_weight="1" >
<TextView
android:id="#+id/textView_achievement1"
style="#style/text_small_bold_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/margin_large"
android:text="1/20" />
</RelativeLayout>
...
And here's how I'm creating the view from my Activity:
public class ActivityAchievements extends ActivitySlidingMenu
{
private ViewAchievements mViewAchievements;
#Override
public void onCreate(Bundle savedInstanceState)
{
mViewAchievements = (ViewAchievements) View.inflate(this, R.layout.view_achievements, null);
setContentView(mViewAchievements);
...
You're trying to get the child views during the view's constructor. Since they are child views, they haven't been inflated yet. Can you move this code out of the constructor, possibly into View.onAttachedToWindow()?
http://developer.android.com/reference/android/view/View.html#onAttachedToWindow()
my problem I know is simple, but for the life of me I cant get an answer...
I have a LinearLayout that got a scrollview inside it... in the scroll view (in code) it should call a custom ScrollView to show on the application... it only shows black, nothing in it... here is my code
first the xml... simple LL with a textview, scrollview and button
<?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"
>
<TextView android:layout_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:text="test field" android:layout_alignParentTop="true"/>
<ScrollView android:layout_width="600dp"
android:layout_height="300dp"
android:id="#+id/v1"
android:fillViewport="true"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gfx"
android:text="Gfx"
></Button>
</LinearLayout>
Now the onCreate of that activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
cw = getWindowManager().getDefaultDisplay().getWidth();
ch = getWindowManager().getDefaultDisplay().getHeight();
v = Draw2d findViewById(R.id.v1);
context = this;
setContentView(R.layout.gfx);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
}
Draw2d is the class of the custom scrollView declared like so (it does have onDraw and everything. If I set the contentview to the Draw2d object directly it'll show no problem, but I need it withen a LL)
public static class Draw2d extends ScrollView{
public Draw2d(Context context) {
super(context);
}
Please any help is appreciated
Maybe because of you have nothing in the ScrollView? Try putting something in it. Check this link, you see there is another linear layout in the scrollview with "30px" layout height making the scrollview appear. If the layout_height was wrap_content, you would not see the scrollview if there is enough place on the screen for it.