setPivotY() behave strange on Android - android

I have an ImageView which I want it to enlarge once the app is launched
So, firstly I did this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.screenshot198);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
layout.addView(imageView);
imageView.setPivotX(1.0f);
imageView.setScaleX(1.5f);
imageView.setScaleY(1.5f);
}
It works great!
Then I try to set it at bottom by adjusting the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.screenshot198);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imageView.setLayoutParams(params);
layout.addView(imageView);
imageView.setPivotX(1.0f);
imageView.setPivotY(1.0f);
imageView.setScaleX(1.5f);
imageView.setScaleY(1.5f);
}
Hey! What's going on with my tree!?
The only difference between these two paragraphs of code is that, I've added [params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);] in order to put image at bottom, and added [setPivotY(1.0f);] so the image should scale upward from bottom.
But it seems the [setPivotY(1.0f);] doesn't work properly. Anything I'm missing? Thanks!

Comment by #rupps surprisingly helped me. Changing to setPivotY(view.getHeight()) fixed my case.
When using setPivotY(view.getBottom()) I was getting an unexpected offset/margin when using setScaleY().

Related

TextView improperly displaying in Layout in Android 6 and below

I am dynamically creating views in onCreate() and then use setMargins() and setLayoutParams() to position them on the layout.
This works as expected in Android 7 but the parameters are ignored in Android 6 and below.
I assume it has something to do with views not being created in onCreate() or something like that but dont know why it would work in Android 7, or how to get around it.
Below is example code of the problem and some screenshots for visualization.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content);
RelativeLayout.LayoutParams viewParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParam.setMargins(200, 400, 0, 0);
TextView myTextView = new TextView(this);
myTextView.setText("Hello this is my text");
myTextView.setTextSize(
TypedValue.COMPLEX_UNIT_PX, 50);
myTextView.setGravity(Gravity.CENTER);
myTextView.setLayoutParams(viewParam);
viewGroup.addView(myTextView);
}

How to show ImageView Over Background programatically

relativeLayout.setBackgroundResource(R.drawable.b);
setContentView(relativeLayout);
img1.setImageResource(R.drawable.d);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(100,100);
img1.setLayoutParams(parms);
setContentView(img1, parms);
(Part of them)
I set a default background and want to change this b.png after 5 seconds. These lines are in after 5 secs handler method. The above 2 lines make me see new b.png at background. But there is an error when I set imageview over it. I also tried ImageView by adding on xml at origin and to appear as view.visible. But it can't. Only background image is available and no overlapping image on it is denied.
Try this
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout relativeLayout = new RelativeLayout(this);
ImageView img1 = new ImageView(this);
relativeLayout.setBackgroundResource(R.drawable.b);
setContentView(relativeLayout);
img1.setImageResource(R.drawable.d);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(100,100);
img1.setLayoutParams(parms);
relativeLayout.addView(img1, parms);
}

How to design layout programmatically, android?

I need to design the above layout through java coding.
B1 & B2-> Horizontal linear layout
B3 & B4-> Horizontal linear layout
B5 & B6-> Horizontal linear layout
B7 & B8-> Horizontal linear layout
B9,B10 & B11-> Horizontal linear layout
Parent layout is Linearlayout.
This is how I am proceeding
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
l = (LinearLayout) findViewById(R.id.mainl); \\PARENT LAYOUT
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
l1.setLayoutParams(params);
l2.setLayoutParams(params);
l3.setLayoutParams(params);
l5.setLayoutParams(params);
l1.setOrientation(LinearLayout.HORIZONTAL);
l2.setOrientation(LinearLayout.HORIZONTAL);
l3.setOrientation(LinearLayout.HORIZONTAL);
l5.setOrientation(LinearLayout.HORIZONTAL);
l1.addView(btn1);
l1.addView(btn2);
l2.addView(btn3);
l2.addView(btn4);
l3.addView(btn5);
l3.addView(btn6);
l5.addView(btn9);
l5.addView(btn10);
l5.addView(btn11);
l.addView(l1);
l.addView(l2);
l.addView(l3);
l.addView(l5);
}
Though I am not succeeding with my output. Please help me out.
Try this code
LinearLayout.LayoutParams LL_params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LL_params.gravity = Gravity.CENTER_VERTICAL;
LinearLayout.LayoutParams btn_params = new LinearLayout.LayoutParams(0
, LayoutParams.WRAP_CONTENT);
btn_params.weight=1;
l1.setLayoutParams(LL_params);
l2.setLayoutParams(LL_params);
l3.setLayoutParams(LL_params);
l4.setLayoutParams(LL_params);
l1.setOrientation(LinearLayout.HORIZONTAL);
l2.setOrientation(LinearLayout.HORIZONTAL);
l3.setOrientation(LinearLayout.HORIZONTAL);
l4.setOrientation(LinearLayout.HORIZONTAL);
btn1.setLayoutParams(btn_params);
btn2.setLayoutParams(btn_params);
btn3.setLayoutParams(btn_params);
btn4.setLayoutParams(btn_params);
btn5.setLayoutParams(btn_params);
btn6.setLayoutParams(btn_params);
btn7.setLayoutParams(btn_params);
btn8.setLayoutParams(btn_params);
btn9.setLayoutParams(btn_params);
l1.addView(btn1);
l1.addView(btn2);
l2.addView(btn3);
l2.addView(btn4);
l3.addView(btn5);
l3.addView(btn6);
l4.addView(btn7);
l4.addView(btn8);
l4.addView(btn9);
l.addView(l1);
l.addView(l2);
l.addView(l3);
l.addView(l4);

Android custom view and visible button

I made my custom view and I want to add view.button, so I made this solution:
Public void onCreate(Bundle savedInstanceState)
{
Button up;
up = new Button(getApplicationContext());
up.setText("ahoj");
up.setHeight(100);
up.setWidth(100);
up.setTop(200);
up.setLeft(100);
LinearLayout layout = new LinearLayout(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(layout);
myview view = new myview(this);
layout.addView(view);
layout.addView(up);
I only see my view but no button. My view only draw some PNG file. Does anyone know where is the problem? Thanks much.
The most likely reason is that your custom view is added with layout params MATCH_PARENT. It takes the whole of the layout and the button is not visible. Try instead adding your custom view with WRAP_CONTENT params:
MyView view = new myview(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LAYOUTParams.WRAP_CONTENT)
layout.addView(view, lp);
You have the code right but in the wrong order. Try this:
Public void onCreate(Bundle savedInstanceState)
{
Button up;
LinearLayout layout = new LinearLayout(getApplicationContext());
up = new Button(getApplicationContext());
up.setText("ahoj");
up.setHeight(100);
up.setWidth(100);
up.setTop(200);
up.setLeft(100);
myview view = new myview(this);
layout.addView(view);
layout.addView(up);
setContentView(layout);
super.onCreate(savedInstanceState);
}

adding other content in top and bottom of MySurfaceView

I am designing an android application contain MySurfaceView includes a Canvas to draw images in a thread running through the application,
(layout is default main.xml which create with project)
but i have to add a bar/textView in the top and add an AD in the botton of the screen.
then how can i do this ?
my declaration is like this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
}
May be my given information is not enough to understand the situation, so if you find any obscurity in my question please ask me
Thanks in advance
Hmm, one possibility is that you could just put it inside of a RelativeLayout or something similar? This assumes that you don't want to just define it in the xml which would probably look cleaner. The code would probably look something like this (you can definitely make it more efficient/just use a LinearLayout, I just threw down the first thing that came into my head):
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams paramsTop =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the textView to the top
paramsTop.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
TextView textView = new TextView(this);
textView.setText("Hello World");
// Give them id's since we need to position relative to one another
textView.setId(1);
layout.addView(textView, paramsTop);
AdView adView = new AdView(this, AdSize.BANNER, INSERT_ADMOB_ID_HERE);
adView.setId(2);
RelativeLayout.LayoutParams paramsBottom =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the adView to the bottom
paramsBottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layout.addView(adView, paramsBottom);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
SurfaceView view = new SurfaceView( this );
view.setId(3);
RelativeLayout.LayoutParams paramsMiddle =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the surfaceView in between the textView and the adView
paramsMiddle.addRule(RelativeLayout.BELOW, textView.getId());
paramsMiddle.addRule(RelativeLayout.ABOVE, adView.getId());
layout.addView(view, paramsMiddle);
setContentView(layout);

Categories

Resources