Rotating a RelativeLayout in Android 2.3 - java

I am new to android and would like some assistance on rotating my relative layout for a two-player game I am working on. I have seen the posts here and have looked into many other posts on the same subject on SO. My first question is how to I call the new class I just made? For all the posts that dont just tell me to use android:rotation (which is not avaliable in 2.3-) I make a new class, but I get a notification that the class is never called. Do I call the class simmilar to how I call a method? Or is there some command in AndroidManifest? Finialy, how does the new class rotate only one Relative layout and not the other? I just would like to know how its supposted to work.
This is the new Class that I am supposted to make:
public class MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyRelativeLayout(Context context) {
super(context);
init();
}
private void init() {
setStaticTransformationsEnabled(true);
}
#Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
t.setTransformationType(Transformation.TYPE_MATRIX);
Matrix m = t.getMatrix();
m.reset();
m.postRotate(180, child.getWidth() / 2.0f, child.getHeight() / 2.0f);
return true;
Thanks for your help

In the oncreate() method in your activity do setContentView(new MyRelativeLayout(this))

Related

Custom XML/Java Navigation Bar Convert to Jetpack Compose with `obtainStyledAttribues` to Obtain Resources

We have an old custom navigation bar in our tablet code that I am trying to convert to Jetpack Compose.
I have the scaffold set up and it appears on the app. I'm now trying to use the stored resources to display them on my new navigation bar.
The problem is that I can't seem to reference any of these resources when they come from the StyledAttributes.
For example, I am just trying to get the string for the Title of the app to appear and here is the existing code (that will eventually be replaced):
NavigationBar
public class NavigationBar extends RelativeLayout {
...
public NavigationBar(Context context) {
this(context, null, R.attr.navigationBarStyle);
}
public NavigationBar(Context context, AttributeSet attrs) {
this(new ContextThemeWrapper(context, R.style.Material_ActionBar_Light), attrs, R.attr.navigationBarStyle);
}
public NavigationBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = this.getContext().obtainStyledAttributes(attrs, R.styleable.NavigationBar, defStyle, 0);
Drawable iconId = a.getDrawable(R.styleable.NavigationBar_navIcon);
String title = a.getString(R.styleable.NavigationBar_navTitle);
a.recycle();
init(context, iconId, title);
}
...
Is there a good way to reference the Styled Attributes to obtain the icons and strings?
It's especially tricky when using this to reference the NavigationBar because the NavigationBar is the part I'm trying to get rid of.

How to solve draw() it does not draw my screen

I am trying to make a gaze tracking in android studio. And I am trying to draw on screen a rectangle to the corresponding point of gaze. But my method doesn't work. I don't have any experience in drawing in android studio.
This is my draw class
public class drawMargins extends View {
Paint paint;
private volatile Margin margin;
public drawMargins(Context context) {
super(context);
init(null);
}
public drawMargins(Context context, AttributeSet attrs)
{
super(context, attrs);
init(attrs);
}
public drawMargins(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public void updateMargin(Margin margin2) {
margin = margin2;
postInvalidate();
}
private void init(#Nullable AttributeSet set)
{
}
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (margin==null)
return;
paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawRect(
(float)((canvas.getWidth()/8)*margin.getX()),
(float)((canvas.getHeight()/8)*margin.getY()),
(float)((canvas.getWidth()/8)*margin.getX()+canvas.getWidth()/8),
(float)((canvas.getHeight()/8)*margin.getY()+canvas.getHeight()/8),paint);
}
}
and this is how I find the viewbyid("Draw") and update the vars:
DrawMargins = (drawMargins)LayoutInflater.from(context).inflate(R.layout.activity_live_preview,null).findViewById(R.id.Draw);
Margin eyeNose = new Margin(marginX(),marginY());
Margin drawOnScreen = onScreen(correspondingOnScreen(eyeNose,NEMargin,SEMargin,SWMargin,NWMargin,0),0,8,0,8,0 );
DrawMargins.updateMargin(drawOnScreen);
Should I call the draw method? With what canvas?
Try calling invalidate() instead of postInvalidate() in updateMargins(). This will call draw() and should draw the rectangle.
I would suggest you do one step at a time. I don't know exactly how you have your Activity or Fragment set, so I am just gonna go through that as well.
To keep things simple, let me assume that there is only Activity and no Fragments. You need two Java Class to get your objective accomplished: DrawMarginsActivity and DrawMargins.
DrawMargins is a Custom View. Custom views help you create your own custom drawing. Refer this for more details: https://developer.android.com/training/custom-views/custom-drawing . You need to override the onDraw(Canvas canvas) method. This method has the canvas parameter and you will be drawing on to this canvas.
Now you need to add DrawMargins view to your DrawMarginsActivity. You can do this by adding this view to your DrawMarginsActivity's layout file. Then you can get a reference to the DrawMargins view via findViewById().
Be default, when your Activity is created, the DrawMargins's onDraw() method will be called and it will render. But if you want to call the onDraw() again, you have to call DrawMargins.invalidate() method. This will invalidate the view and call the onDraw() method for you.
I am not sure on which action you want the view to draw. Based on that, you need to call the invalidate() method.

Android: How do I make a scrollable Canvas?

I am writing an app. One aspect of it is lines being drawn over an image. Here's the practice code I've been working with:
public class DrawView extends View {
Paint paint = new Paint();
private void init() {
paint.setColor(Color.BLACK);
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
#Override
public void onDraw(Canvas canvas) {
paint.setStrokeWidth(20);
canvas.drawLine(100, 100, 20, 20, paint);
canvas.drawLine(20, 0, 0, 20, paint);
}
}
I want the Canvas to be fairly large, so I need people to be able to scroll in both directions (left/right and up/down). How do I accomplish this? I'm unfamiliar with the Canvas class so any help will be appreciated.
If you want to draw on canvas with your custom height and width you have to call setContentView(android.view.View yourView , android.view.Viewgroup.LayoutParam yourLayout) in your activity class.Because by default setContentView(View view) method use full width and height.So you have to use its overloaded method with two parameter along with your desired. See documentation for more info.And don`t use only LayoutParams() constructor to create its object. Use it by writing its full path like android.view.ViewGroup.LayoutParams. Because there are some other classes with same name in Android SDK.
MyView customView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customView = new MyView(getApplicationContext());
android.view.ViewGroup.LayoutParams lp = new android.view.ViewGroup.LayoutParams(100,200);//100 is width and 200 is height
setContentView(customView, lp);
customView.setOnClickListener(this);
}`

Is there a way in android to set a field via xml android:id into a custom view

For instance I have a custom button and want to connect it to a SeekBar:
public class SeekBarButton extends ImageButton {
SeekBar seekBar;
public SeekBarButton(Context context) {
super(context);
}
public SeekBarButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SeekBarButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setSeekBar(SeekBar seekBar) {
this.seekBar = seekBar;
}
public SeekBar getSeekBar() {
return seekBar;
}
}
I can do it in the code:
sbb = (SeekBarButton) rootView.findViewById(R.id.minus_red);
sbRed = (SeekBar) rootView.findViewById(R.id.sbRed);
sbb.setSeekBar(sbRed);
But 8 buttons will give a lot of boilerplate, and I want something like:
<com.whatever.views.SeekBarButton
...
whatToPutHere:seekbar="#+id/sbRed" // like this? whatToPutHere?
android:id="#+id/minus_red" />
<SeekBar
android:id="#+id/sbRed"
... />
The easiest way to do this is to create a custom ViewGroup that contains both the Button and Seekbar. If you cannot do that, for any reason, here's a solution:
There are a few steps to make this work. First you must define a custom XML attribute that you can then reference and use.
Edit (or create) res/values/attrs.xml. Add:
<declare-styleable name="SeekBarButton">
<attr name="seekbarId" format="integer" />
</declare-styleable>
Then, in SeekBarButton, call this from the constructors:
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SeekBarButton, defStyleAttr, 0);
mSeekbarId = a.getResourceId(R.styleable.SeekBarButton_seekbarId, 0);
a.recycle();
}
}
Finally, in your root ViewGroup of your layout file, add
xmlns:app="http://schemas.android.com/apk/res-auto"
Then,
<com.whatever.views.SeekBarButton
android:id="#+id/minus_red"
app:seekbarId="#+id/sbRed"
... />
<SeekBar
android:id="#+id/sbRed"
... />
Note You will need to call ((ViewGroup) getParent()).findViewById(mSeekbarId) in SeekBarButton to instantiate the SeekBar, but getParent() will be null in SeekBarButton constructors. So, delay findViewById() until you need the SeekBar.
I think you are close. In the first XML tag of your layout file (my example is a RelativeLayout) you need this reference to "custom":
<RelativeLayout
xmlns:custom="http://schemas.android.com/apk/res-auto">
Then farther down wherever your custom ImageButton is, you need this:
<com.whatever.views.SeekBarButton
...
custom:seekbar="#+id/sbRed"
android:id="#+id/minus_red" />
You will also need a seekBarButton.xml file in your project\res\values folder, if you didn't already know that.

How can I support unicode fonts in Android?

I need to show Hindi and Bengali font support in my app. How to do this.Please help.
Also tell me how can I get the support from XML, as I need to get the data through web-services.
What I did is that is put a particular font of choice to assets folder and used this code:
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initFont();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initFont();
}
public CustomTextView(Context context) {
super(context);
initFont();
}
private void initFont() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"myfont.ttf");
setTypeface(tf);
}
}
But the problem what I am facing is that the even if I am getting the fonts in my emulator some of the fonts patterns are not correct.
Reply if anybody wants to add more to my answer, you are welcome.

Categories

Resources