Issue with programmatically trying to set a FrameLayout for Android - android

I am able to successfully a achieve this doing so with the XML layout as seen in the image below:
Then when trying to do so within Java I am not able to get the same result as seen in the screen shot:
Here is the Java code I have that renders the result above:
FrameLayout frameLayout = new FrameLayout(this.context);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
frameLayout.setLayoutParams(layoutparams);
ImageView bodyImageView = new ImageView(this.context);
bodyImageView.setBackgroundResource(R.drawable.niners);
TextView bodyTextView = new TextView(this.context);
bodyTextView.setBackgroundColor(Color.TRANSPARENT);
bodyTextView.setText("SF");
bodyTextView.setTextColor(Color.YELLOW);
bodyTextView.setGravity(Gravity.BOTTOM);
frameLayout.addView(bodyImageView);
frameLayout.addView(bodyTextView);
Can anyone see what I am missing or doing wrong?
UPDATE:
Based on the response below the screen shot results in the following:

You need to add the layout gravity as a LayoutParams to the TextView:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
And change the gravity of the the TextView to the center:
bodyTextView.setGravity(Gravity.CENTER);
Here's the entire layout:
FrameLayout frameLayout = new FrameLayout(this.context);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
frameLayout.setLayoutParams(layoutparams);
ImageView bodyImageView = new ImageView(this.context);
bodyImageView.setBackgroundResource(R.drawable.niners);
TextView bodyTextView = new TextView(this.context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
bodyTextView.setLayoutParams(params);
bodyTextView.setBackgroundColor(Color.GRAY);
bodyTextView.setText("SF");
bodyTextView.setTextColor(Color.YELLOW);
bodyTextView.setGravity(Gravity.CENTER);
frameLayout.addView(bodyImageView);
frameLayout.addView(bodyTextView);
setContentView(frameLayout);

I change to kotlin sorry but, test this code
val frameLayout = FrameLayout(this)
val layoutparams = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL or Gravity.CENTER_HORIZONTAL
)
frameLayout.layoutParams = layoutparams
val bodyImageView = ImageView(this)
bodyImageView.setBackgroundResource(R.drawable.ic_launcher_background)
val params =
FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
params.gravity = Gravity.BOTTOM
val bodyTextView = TextView(this)
bodyTextView.text = "SF"
bodyTextView.gravity = Gravity.CENTER
bodyTextView.setTextColor(Color.YELLOW)
bodyTextView.setBackgroundColor(R.color.purple_700)
frameLayout.addView(bodyImageView)
frameLayout.addView(bodyTextView,params)
You only have to add parameters to your view
in Java
// Add this code
FrameLayout.LayoutParams layoutparams =
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutparams.gravity = Gravity.BOTTOM;
// Add in your code for textView
frameLayout.addView(bodyTextView,layoutparams);

Related

How to Align Two Fields Horizontally in Android Programatically

I was Programmatically Creating Linear Layout and I want to Align two Fields on same Line. But i Want to display the result as like expected.
lView = new LinearLayout(Main2Activity.this);
// lView.setPadding(0,150,0,0);
lView.setBackgroundColor(Color.parseColor("#EDFCFC"));
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Button logout = new Button(Main2Activity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
logout.setLayoutParams(params);
logout.setText("Logout");
logout.setBackgroundColor(Color.parseColor("#F53F37"));
lView.addView(logout);
TextView title = new TextView(Main2Activity.this);
title.setText("ASSOCIATES");
title.setTextColor(Color.parseColor("#2BD865"));
title.setTextSize(25);
title.setTypeface(null, Typeface.BOLD);
title.setGravity(Gravity.CENTER);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title);
TextView title1 = new TextView(Main2Activity.this);
title1.setText("Date :");
title1.setTextSize(20);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title1);
et1 = new EditText(Main2Activity.this);
et1.setWidth(10);
et1.setGravity(Gravity.RIGHT);
et1.setHint("Date");
//lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(et1);
My Actual Output is
Expected
You need to set gravity for your parent LinearLayout to align childs.
set the Linear Layout orientation to horizontal like this:
lView.setOrientation(LinearLayout.HORIZONTAL);
Add LayoutParams to your textview and edittext and you will get the specified view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
TextView title1 = new TextView(Main2Activity.this);
title1.setLayoutParams(params);
title1.setText("Date :");
title1.setTextSize(20);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title1);
params.gravity = Gravity.LEFT;
et1 = new EditText(Main2Activity.this);
et1.setLayoutParams(params);
et1.setWidth(10);
et1.setGravity(Gravity.RIGHT);
et1.setHint("Date");
lView.setOrientation(LinearLayout.HORIZONTAL);
lView.addView(et1);

How to make the textview content HORIZONTAL

I need dynamic create the texview, it works just the text content show VERTICAL but HORIZONTAL
TextView tv = new TextView(this.theContext);
tv.setText(this.getTip(0));
tv.setBackgroundColor(Color.BLUE);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
params.gravity = Gravity.CENTER;
this.addView(tv, params);
mLeftLabel = tv;
also, I set the layout :
tv.layout(l,t,r,b); . but I think that this statement should not effect the text Direction, is it right ?
please give help or tip, thanks.
try to follow the example below:
TextView tv = new TextView(this);
tv.setText("text");
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
params.gravity = Gravity.CENTER;
mainLayout.addView(tv, params);
try changing LayoutParams as
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
setting gravity to center when you are using WRAP_CONTENT for textview does not seem to make sense, though I am not sure.

Set other layout parameters imageview

I would like to have an image on android that appears only when I click a button. So I created it in an onClick method, and set its ressource and position with the following code :
public void createAddButton() {
//create the image
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.add_16);
//setting image position : width and height
imageView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) );
}
But this only allows to define width and height of the image. If I want to define other layout parameters (android:layout_gravity or android:marginLeft for example, what should I do ?
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
lp.bottomMargin = ...
lp.leftMargin = ...
lp.rightMargin = ...
lp.topMargin = ...
imageView.setLayoutParams(lp);
You have to use LayoutParams object to access those layout properties as below...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
params.leftMargin = 10;
imageView.setLayoutParams(params);

How to add multiple views to window manager in Android

I am developing a application like Facebook Chat Heads a know how to add a single view to window manager.
How to add multiple views to window manager? I tried frame layout and relative layout, but how can I move chat head from one place to another place if I am using relative layout?
For adding multiple views I used below code:
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.ic_launcher);
TextView t = new TextView(this);
t.setText("Blessan Mathew");
t.setBackgroundColor(Color.CYAN);
params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.addView(t, params1);
params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.addView(chatHead, params1);
fr.addView(childLayout);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(fr, params);
How can I drag chat head to remove its view?
To add notification on top of ImageView and move both the views together:
Use RelativeLayout as parent Layout and add ImageView and TextView to it.
private RelativeLayout parentlayout;
TextView notification;
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.deals);
chatHead.setId(imageid);
parentlayout = new RelativeLayout(this);
notification = new TextView(this);
notification.setTextColor(Color.parseColor("#494949"));
notification.setText("1");
notification.setId(nameid);
notification.setTextSize(19);
final RelativeLayout.LayoutParams params_imageview = new RelativeLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
params_imageview.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
final RelativeLayout.LayoutParams params_name = new RelativeLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
params_name.addRule(RelativeLayout.ALIGN_RIGHT, imageid);
params_name.addRule(RelativeLayout.ALIGN_TOP, imageid);
parentlayout.addView(chatHead, params_imageview);// adding user image to view
parentlayout.addView(notification, params_name);
Finally make these changes:
mWindowManager.updateViewLayout(parentlayout, params);
and
mWindowManager.addView(parentlayout, params);
PS: Use shapes to style TextView and get exact notification as that of Facebook!! :)

how to set LinearLayout in bottom of FrameLayout programmatically?

I have this code :
FrameLayout game = new FrameLayout(this);
LinearLayout gameWidgets = new LinearLayout (this);
game.addView(gameWidgets);
How to set the LinearLayout in the buttom of the FrameLayout ?
Try this,
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
game.addView(GameWidgets, params);
Use this code:
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM
| Gravity.CENTER_HORIZONTAL);
game.addView(GameWidgets, p);

Categories

Resources