I have a MtxVideoView class which contains textview and surfaceview. The MtxVideoView class extends FrameLayout. I want to change size of MtxVideoView . How can do that ?
Following the code of special view
public class MtxVideoView extends FrameLayout implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener,
VideoControllerView.MediaPlayerControl, MediaPlayer.OnErrorListener {
private Context mContext;
private SurfaceHolder mSurfaceHolder;
private SurfaceView mSurfaceView;
private MediaPlayer mPlayer;
private VideoControllerView controller;
private TextView mMessage;
private boolean mbSrcAvailable = false;
private boolean mbSurfaceCreated = false;
private String mSource = "";
private String mUserName = "";
private String mPassword = "";
private boolean mbFullscreen = false;
private FullScreenListener mFullScreenListener;
private int mCount = 0;
static interface FullScreenListener {
void onFullScreen( View view, boolean bFullScreen);
}
public void setFullScreenListener( FullScreenListener listener ){
mFullScreenListener = listener;
}
public MtxVideoView(Context context) {
super(context);
mContext = context;
mbFullscreen = false;
Init();
}
public MtxVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
Init();
}
public MtxVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
Init();
}
private void Init(){
mSurfaceView = new SurfaceView(mContext);
mSurfaceView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));
addView(mSurfaceView);
mMessage = new TextView(mContext);
mMessage.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));
mMessage.setGravity(Gravity.CENTER);
mMessage.setTextColor(Color.WHITE);
this.addView(mMessage);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
}
}
Try this code.
But you have to run it when view is already measured.
{
LayoutParams param = getLayoutParams();
param.width = 100;
param.height = 200;
requestLayout();
}
When defining in xml use layout_width and layout_hight.
If you are adding dynamically then set layout parameters or add this view with layout parameters to its parent.
Related
public class SwipeButton extends RelativeLayout {
private ImageView swipeButtonInner;
private float initialX;
private boolean active;
private TextView centerText;
private ViewGroup background;
private Drawable disabledDrawable;
private Drawable enabledDrawable;
private OnStateChangeListener onStateChangeListener;
private OnActiveListener onActiveListener;
private static final int ENABLED = 0;
private static final int DISABLED = 1;
private int collapsedWidth;
private int collapsedHeight;
private LinearLayout layer;
private boolean trailEnabled = false;
private boolean hasActivationState;
public SwipeButton(Context context) {
super(context);
init(context, null, -1, -1);
}
public SwipeButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, -1, -1);
}
public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, -1);
}
#TargetApi(21)
public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr, int
defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
public boolean isActive() {
return active;
}
public void setText(String text) {
centerText.setText(text);
}
public void setBackground(Drawable drawable) {
background.setBackground(drawable);
}
public void setSlidingButtonBackground(Drawable drawable) {
background.setBackground(drawable);
}
public void setDisabledDrawable(Drawable drawable) {
disabledDrawable = drawable;
if (!active) {
swipeButtonInner.setImageDrawable(drawable);
}
}
public void setButtonBackground(Drawable buttonBackground) {
if (buttonBackground != null) {
swipeButtonInner.setBackground(buttonBackground);
}
}
public void setEnabledDrawable(Drawable drawable) {
enabledDrawable = drawable;
if (active) {
swipeButtonInner.setImageDrawable(drawable);
}
}
public void setOnStateChangeListener(OnStateChangeListener
onStateChangeListener) {
this.onStateChangeListener = onStateChangeListener;
}
public void setOnActiveListener(OnActiveListener onActiveListener) {
this.onActiveListener = onActiveListener;
}
public void setInnerTextPadding(int left, int top, int right, int bottom) {
centerText.setPadding(left, top, right, bottom);
}
public void setSwipeButtonPadding(int left, int top, int right, int bottom) {
swipeButtonInner.setPadding(left, top, right, bottom);
}
public void setHasActivationState(boolean hasActivationState) {
this.hasActivationState = hasActivationState;
}
My XML:`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/AnswerRelativeLayout"
android:layout_height="70dp"
android:orientation="vertical"
tools:context="com.example.dynamicobj.FragmentMain">
<com.example.dynamicobj.SwipeButton
android:id="#+id/test_btn"
android:layout_width="match_parent"
android:layout_height="70dp"
app:button_background="#drawable/shape_button"
app:button_image_disabled="#drawable/ic_launcher_background"
app:button_image_height="60dp"
app:button_image_width="100dp"
app:has_activate_state="true"
app:initial_state="disabled"
app:inner_text="Termine buchen"
app:inner_text_background="#drawable/shape_rounded"
app:inner_text_bottom_padding="18dp"
app:inner_text_right_padding="200dp"
app:inner_text_color="#android:color/black"
app:inner_text_size="16sp"
app:inner_text_top_padding="18dp" />
</LinearLayout>`
F
ragmentMain:
public class FragmentMain extends Fragment {
Context context;
View rootView;
public FragmentMain() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
LinearLayout layout = (LinearLayout)
rootView.findViewById(R.id.AnswerRelativeLayout);
SwipeButton button = new SwipeButton(getContext());
layout.addView(button);
return rootView;
}
}
So I got this custom SwipeButton class from Git (com.ebanx.swipebtn.SwipeButton). Now I want to create an Object from SwipeButton with a layout looking like the one from the xml file. Is there any method, where I just can give the new button the prefinished layout without having to use all these Methods in the SwipeButton class? I am going to create the buttons dynamically later on, but all being the same layout. Help pls?
you forgot to add LayoutParams to your button
use below code before adding button into layout
button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
and set your LinearLayout height wrap_content not 70dp
error in kitkat device not in greater version of android.
after changing gradle file. library used for circle pager indicator. https://github.com/JakeWharton/ViewPagerIndicator
dependencies { classpath 'com.android.tools.build:gradle:1.0.0'}
to dependencies {classpath 'com.android.tools.build:gradle:1.3.0'}
Error is:
Binary XML file line #43: Error inflating class com.viewpagerindicator.CirclePageIndicator
Caused by: java.lang.NoClassDefFoundError: com.app.skedule.R$styleable
10-03 12:29:47.501 28314-28314/? E/AndroidRuntime:
at com.viewpagerindicator.CirclePageIndicator.<init>(CirclePageIndicator.java:93)
10-03 12:29:47.501 28314-28314/? E/AndroidRuntime:
at com.viewpagerindicator.CirclePageIndicator.<init>(CirclePageIndicator.java:74)
CirclePageIndicator.java(inside my project->java->viewpagerindicator package)
package com.viewpagerindicator;
public class CirclePageIndicator extends View implements PageIndicator {
private static final int INVALID_POINTER = -1;
private float mRadius;
private final Paint mPaintPageFill = new Paint(ANTI_ALIAS_FLAG);
private final Paint mPaintStroke = new Paint(ANTI_ALIAS_FLAG);
private final Paint mPaintFill = new Paint(ANTI_ALIAS_FLAG);
private ViewPager mViewPager;
private ViewPager.OnPageChangeListener mListener;
private int mCurrentPage;
private int mSnapPage;
private float mPageOffset;
private int mScrollState;
private int mOrientation;
private boolean mCentered;
private boolean mSnap;
private int mTouchSlop;
private float mLastMotionX = -1;
private int mActivePointerId = INVALID_POINTER;
private boolean mIsDragging;
public CirclePageIndicator(Context context) {
this(context, null);
}
public CirclePageIndicator(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.vpiCirclePageIndicatorStyle);
}
public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (isInEditMode()) return;
//Load defaults from resources
final Resources res = getResources();
final int defaultPageColor = res.getColor(R.color.default_circle_indicator_page_color);
final int defaultFillColor = res.getColor(R.color.default_circle_indicator_fill_color);
final int defaultOrientation = res.getInteger(R.integer.default_circle_indicator_orientation);
final int defaultStrokeColor = res.getColor(R.color.default_circle_indicator_stroke_color);
final float defaultStrokeWidth = res.getDimension(R.dimen.default_circle_indicator_stroke_width);
final float defaultRadius = res.getDimension(R.dimen.default_circle_indicator_radius);
final boolean defaultCentered = res.getBoolean(R.bool.default_circle_indicator_centered);
final boolean defaultSnap = res.getBoolean(R.bool.default_circle_indicator_snap);
//Retrieve styles attributes
`TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirclePageIndicator, defStyle, 0); //Error here`
By reference of this Link
added
dependencies { compile 'com.android.support:multidex:1.0.0'} defaultConfig { multiDexEnabled true}
and
public class MyApplication extends MultiDexApplication {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}`
finally got solution.
I was creating my textview which use as a header in my MainActivity file and everything was okay until I thought I'd better move the code to another class.
While it works and displays the text however when I try to animate it it doesn't work.
The code I was using is in the init() method.
The code is very simple and I'm pasting it here:
public class TextViewHeader extends TextView {
private static final String TAG = "TextViewHeader".toUpperCase();
private Context context;
private FrameLayout frameLayoutWhiteTowerContainer;
private String text;
private int whiteTowerContainerHeight;
private int tower_height;
public TextViewHeader(Context context) {
super(context);
}
public TextViewHeader(Context context, String text, FrameLayout frameLayoutWhiteTowerContainer,
int whiteTowerContainerHeight, int tower_height) {
super(context);
this.context = context;
this.text = text;
this.frameLayoutWhiteTowerContainer = frameLayoutWhiteTowerContainer;
this.whiteTowerContainerHeight = whiteTowerContainerHeight;
this.tower_height = tower_height;
init();
}
public TextViewHeader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init() {
TextView textView = new TextView(context);
textView.setText(text);
textView.setTextSize(25);
textView.setTextColor(Color.parseColor("#FF545454"));
Typeface typefaceCondensed = Typeface.create("sans-serif-condensed", Typeface.BOLD);
textView.setTypeface(typefaceCondensed);
FrameLayout.LayoutParams tv_header_params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
int topMargin = whiteTowerContainerHeight / 2 - tower_height / 2 - 100;
tv_header_params.topMargin = topMargin;
tv_header_params.gravity = Gravity.CENTER_HORIZONTAL;
textView.setLayoutParams(tv_header_params);
frameLayoutWhiteTowerContainer.addView(textView, tv_header_params);
}
}
The code in MainActivity is :
tvHeader = new TextViewHeader(MainActivity.this, "TOWER", flWhiteTowerContainer,
whiteTowerContainerHeight, tower_height ); // create inside onCreate method
tvHeader.animate().y(515).setDuration(1500).start(); //and the animation happens later onMapReady
You are starting an animation before the TextViewHeader is laid out.
Try:
tvHeader = new TextViewHeader(MainActivity.this, "TOWER", flWhiteTowerContainer,
whiteTowerContainerHeight, tower_height );
tvHeader.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
// Layout complete but not drawn yet
tvHeader.getViewTreeObserver().removeOnPreDrawListener(this);
tvHeader.animate().y(515).setDuration(1500).start();
return false;
}
});
The problem is that I'm implementing the TextView but I don't use it. Instead I just create another TextView in the class and I use this instead of my class.
In other words the code :
TextView textView = new TextView(context);
shouldn't exist and just use this to setText, setTextSize, color, etc.
this.setText(text);
this.setTextSize(25);
this.setTextColor(Color.parseColor("#FF545454"));
I have this code in activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MyNumberPicker np = new MyNumberPicker(this);
np.setOnScrollListener(new NumberPicker.OnScrollListener() {
#Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
if (scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
np.setBottomFadingEdgeStrength(0.9f);
np.setBottomFadingEdgeStrength(0.9f);
} else if (scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_IDLE){
np.setBottomFadingEdgeStrength(1.0f);
np.setBottomFadingEdgeStrength(1.0f);
}
//also you can handle SCROLL_STATE_FLING if you want
}
});
}
}
And this code for my custom class:
public class MyNumberPicker extends NumberPicker {
private float bottomFadingEdgeStrength = 1.0f;
private float topFadingEdgeStrength = 1.0f;
public MyNumberPicker(Context context) {
super(context);
}
public MyNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyNumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected float getBottomFadingEdgeStrength() {
return bottomFadingEdgeStrength;
}
#Override
protected float getTopFadingEdgeStrength() {
return topFadingEdgeStrength;
}
void setTopFadingEdgeStrength(float strength){
topFadingEdgeStrength = strength;
}
void setBottomFadingEdgeStrength(float strength){
bottomFadingEdgeStrength = strength;
}
}
The thing is that I don't know how to add this number picker into activity. In other words, I can't get it to work in my program. So how should I change my code in activity to do this.
As far as I can tell, I can't use findViewById(R.id.numberpicker1). Please help.
In your layout, instead using adding a NumberPicker like you normally would:
<NumberPicker android:id="#+id/numberpicker1" ... />
You would use a tag that points to your custom class.
<your.path.to.MyNumberPicker android:id="#+id/numberpicker1" ... />
So, if you added this to your activity_main layout, you could initialize it using View.findViewById:
setContentView(R.layout.activity_main);
final MyNumberPicker np = (MyNumberPicker) findViewById(R.id.numberpicker1);
For some reason, I keep getting an Error inflating Class message because I'm trying to implement
Caused by: java.lang.NullPointerException
at pap.crowslanding.GameView.(GameView.java:49)
Line 49 is this
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
I am doing it this way because I am not in an Activity, any suggestions?
EDIT:
public class GameView extends SurfaceView {
static final long FPS = 10;
public Bitmap bmp;
public SurfaceHolder holder;
public LoopGameThread loopGameThread;
public static Sprite sprite;
public LayoutInflater inflater;
//maze variables
public int width;
public int height;
private float cellWidth;
private boolean[][] north; // is there a wall to north of cell i, j
private boolean[][] east;
private boolean[][] south;
private boolean[][] west;
private boolean[][] visited;
private double size;
boolean done = false;
Display display = new
Paint paint = new Paint();
//maze variables
public GameView(Context context) {
super(context);
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int width = screenSize.x;
int height = screenSize.y;
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
//maze variables
initVars(attrs);
initMaze();
generate(1, 1);
//maze variables
loopGameThread = new LoopGameThread(this);
holder = getHolder();
holder.addCallback(new Callback() {
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
loopGameThread.isStart(true);
loopGameThread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.crow);
sprite = new Sprite(this, bmp);
}
//MAZE VARIABLES
private void initVars(AttributeSet attrs) {
//requires display variables
}
Then within my initVars, I need to use those display variables
EDIT 2 :
package pap.crowslanding;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class Game extends MainActivity{
static boolean pressedUp = false;
protected static GameView gameV;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tester1);
Button moveLeft = (Button) findViewById(R.id.button1);
gameV = (GameView)findViewById(R.id.game_view);
moveLeft.setOnTouchListener(new View.OnTouchListener() {
private Handler mHandler;
#Override public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mHandler != null) return true;
mHandler = new Handler();
mHandler.postDelayed(mAction, 100);
break;
case MotionEvent.ACTION_UP:
if (mHandler == null) return true;
mHandler.removeCallbacks(mAction);
mHandler = null;
break;
}
return false;
}
Runnable mAction = new Runnable() {
#Override public void run() {
System.out.println("Performing action...");
mHandler.postDelayed(this, 100);
GameView.sprite.movementGo();
}
};
});
}
}
You need to pass your context down to whatever class is calling this function.
You could for example, keep local context value in your class which you can refer to in your functions. First, you'll need to make a constructor which will pass in a context to your class to use.
public class Example {
Context mContext;
public Example(Context mContext;){
this.mContext = mContext;
}
public someFunction(){
...
WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
...
}
}
In your Activity class that creates an instance of your class and uses it, you'll need to pass in the Activity's context like so:
public class SomeActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Example test = new Example(this); //You are passing in the Activity as the context here
test.someFunction();
}
}
EDIT to your edit:
Create a new local variable in your GameView class:
private Context mContext;
Then assign a value to mContext in your GameView constructor like so:
public GameView(Context context) {
super(context);
mContext = context;
...
}
You can now use mContext inside of your initVars() function