I can't add child views to a ViewGroup that I added programmatically.
First I create a GroupView (_view) and add it to the Background RelativeLayout (_root) like this...
_view = new ViewGroup(MainActivity.this) {
#Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
}
};
_view.setBackgroundColor(0xFF00FF00);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200);
params.setMargins(10, 0, 10, 0);
_view.setLayoutParams(params);
_view.setTranslationX(0);
_view.setTranslationY(positionY - 80);
selectView(_view);
_root.addView(_view);
_view.setTag(R.string.viewID, "" + viewNum);
_view.setTag(R.string.viewSelected, "" + 0);
viewNum ++;
That works fine. Then I try to add a view to that new GroupView (_view) like this...
ImageView dragIcon = new ImageView(MainActivity.this);
dragIcon.setImageResource(R.drawable.drabicon);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imgParams.setMargins(0,0,30,0);
dragIcon.setLayoutParams(imgParams);
dragIcon.setTranslationY(_view.getTranslationY());
_view.addView(dragIcon); // This Is What I Want, But This Doesn't Work.
//_root.addView(dragIcon); // Don't Want This, But This Works.
But it doesn't get added, or it isn't visible or something. Now if i use the Background RelativeLayout (_root) like this _root.addView(dragIcon); It works fine, the ImageView gets added.
So why doesn't work with the _view ViewGroup, _view.addView(dragIcon)?
Since you are using ViewGroup, which is an abstract class you should implement the abstract method onLayout in which each layout method of each child of the viewgroup must be called. For more info check this answer.
I think you can use FrameLayout which is a subclass ViewGroup. It has the onLayout method implemented so you dont have the burden to implementing it again.
Here is working code for ViewGroup where I plainly called the layout method of the only one child (dragIcon).
ExampleActivity.java
public class ExampleActivity extends AppCompatActivity {
ViewGroup _view ;
RelativeLayout _root;
ImageView dragIcon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
_root = (RelativeLayout) findViewById(R.id.exampleLayout);
_view = new ViewGroup(ExampleActivity.this) {
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
dragIcon.layout(l, t, r, b); //CHECK THIS
}
};
_view .setBackgroundColor(0xFF00FF00);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200);
params.setMargins(10, 0, 10, 0);
_view .setLayoutParams(params);
_view .setTranslationX(0);
_view .setTranslationY(80);
// selectView(_view);
_root.addView(_view );
_view .setTag(R.string.app_name, "" );
_view .setTag(R.string.app_name, "" + 0);
dragIcon = new ImageView(ExampleActivity.this);
dragIcon.setImageResource(R.drawable.heart1);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
_view .addView(dragIcon); // This Is What I Want, But This Doesn't Work.
//sample_root.addView(dragIcon); // Don't Want This, But This Works.
}
}
activity_example.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/exampleLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.anil.raceorganizer.ExampleActivity">
</RelativeLayout>
Related
I've created a RelativeLayout within my activity, and I'm dynamically creating ImageView objects that I'm adding to the RelativeLayout using addView along with RelativeLayout.LayoutParams. When I then go to get the position of the child view, it always returns the coordinates of the parent view. Here are the pertinent parts of the code:
public class MainActivity extends AppCompatActivity {
public RelativeLayout parentLayout;
RelativeLayout.LayoutParams layoutParams;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentLayout = findViewById(R.id.parentRelativeLayout);
}
// Method to dynamically add the ImageView to the parent
public int addObject(Drawable drawing, int x, int y) {
ImageView newObj = new ImageView(getApplicationContext());
newObj.setBackground(drawing);
layoutParams = new RelativeLayout.LayoutParams(50, 50);
layoutParams.leftMargin = x;
layoutParams.topMargin = y;
int childIndex = parentLayout.getChildCount()-1;
parentLayout.addView(newObj, childIndex,layoutParams);
return childIndex;
}
public int[] getChildLocation(int childIndex) {
View childView;
int[] outlineCoord = new int[2];
childView = parentLayout.getChildAt(childIndex);
childView.getLocationOnScreen(outlineCoord);
return outlineCoord;
}
}
If I then add a child and attempt to retrieve the coordinates of the child view, I always get the location of the parent:
childIndex = addObject(getResources().getDrawable(blackcircle), 100, 200);
outlineCoord = getChildLocation(childIndex);
Log.i("LOC:", "Location of child is X: " + outlineCoord[0] + " Y: " + outlineCoord[1]);
I can't quite figure out what I'm doing wrong here.
I have customized viewGroup with contains Relativelayout as direct child and Button as nested child. whenever I change the layout of Viewgroup,I used the change the layout Relativelayout, But it doesn't affect the child of RelativeLayout.
note: customViewGroup is added as child of mainlayout.
I have used a button to change the layout dynamically in main layout.
main_activity layout.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:layout_height="match_parent"
android:id="#+id/MainLayout"
android:background="#android:color/holo_blue_bright" />
CustomViewGroup.java added as child of mainlayout
internal class CustomViewGroup : ViewGroup {
Button m_button;
private RelativeLayout View;
internal CustomViewGroup(Context context) : base(context) {
Initialize(context);
}
protected override void OnLayout(bool changed, int l, int t, int r, int b) {
int count = ChildCount;
for (int i = 0; i < count; i++) {
ViewGroup child = GetChildAt(i) as ViewGroup;
child.Layout(0,0 , (int)(r-l), (int)(b-t));
}
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = ChildCount;
for (int i = 0; i < count; i++) {
View child = GetChildAt(i);
this.MeasureChildren(widthMeasureSpec, heightMeasureSpec);
}
this.SetMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
protected override void OnSizeChanged(int w, int h, int oldw, int oldh) {
base.OnSizeChanged(w, h, oldw, oldh);
}
private void Initialize(Context context) {
this.Layout(100, 100, 500, 500);
SetBackgroundColor(Color.Gray);
View = new RelativeLayout(context);
View.SetBackgroundColor(Color.Green);
Button button = new Button(context);
button.SetBackgroundColor(Color.Yellow);
RelativeLayout.LayoutParams lay = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent,
RelativeLayout.LayoutParams.MatchParent);
button.LayoutParameters = lay;
button.Text = "Ashok";
View.AddView(button);
AddView(View);
}
}
MainActivity.java
public class MainActivity : AppCompatActivity {
private CustomViewGroup viewgroup;
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
LinearLayout parentView = (LinearLayout)FindViewById(Resource.Id.MainLayout);
parentView.Orientation = Orientation.Vertical;
Button button = new Button(this);
button.Click += Button_Click;
button.Text = "ChangeLayout";
viewgroup = new CustomViewGroup(this);
LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(300,300);
viewgroup.LayoutParameters = lay;
parentView.AddView(viewgroup);
parentView.AddView(button);
}
private void Button_Click(object sender, EventArgs e) {
viewgroup.Layout(0, 0, 500, 500);
}
}
Change this
viewgroup.Layout(0, 0, 500, 500);
into this
LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(500,500);
viewgroup.LayoutParameters = lay;
I am trying to create a custom view without xml
public class MyView extends ViewGroup {
public MyView (Context context) {
super(context);
setBackgroundColor(0xffff0000);
RelativeLayout base = new RelativeLayout(context);
RelativeLayout.LayoutParams rP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
base.setBackgroundColor(0xff00ff00);
base.setLayoutParams(rP);
RelativeLayout.LayoutParams iP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView icon = new ImageView(context);
icon.setBackgroundResource(android.R.drawable.ic_menu_add);
icon.setLayoutParams(iP);
addView(icon);
addView(base);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
And use it in actitvy
MyView myview = new MyView(this);
WindowManager.LayoutParams baseParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
addContentView(myview, baseParams);
But the LayoutParams does not work, it display nothing
I must use this onLayout to make it display, and the base layout is not MATCH_PARENT and icon is not WRAP_CONTENT
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final View child = getChildAt(0);
final View child2 = getChildAt(1);
child.layout(0, 0, 300, 300);
child2.layout(0, 0, 300, 300);
}
I also tried to add icon to layout, but the app will crash
base.addView(icon);
Is it possible to create this layout without hardcode the size and position?
I checked RelativeLayout.LayoutParams, but I cannot find any method to set it centerInParent to true.
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_add"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Please give this code a try.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create parent RelativeLayout
RelativeLayout relParent = new RelativeLayout(this);
RelativeLayout.LayoutParams relParentParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relParent.setLayoutParams(relParentParam);
// Create child ImageView
ImageView imgView = new ImageView(this);
RelativeLayout.LayoutParams imgViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
imgView.setLayoutParams(imgViewParams);
imgView.setImageResource(android.R.drawable.ic_menu_add);
imgViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
// Add child ImageView to parent RelativeLayout
relParent.addView(imgView);
// Set parent RelativeLayout to your screen
setContentView(relParent, relParentParam);
}
}
I am trying to add a progressbar(circular spinner) inside a RelativeLayout. The relativeLayout is visible, however the progressbar doesn't show up. Following is the code I am using but it doesn't seem to work.
public class MyCustomView extends RelativeLayout
{
RelativeLayout rLayout;
ProgressBar mProgressbar;
public void init()
{
mProgressbar = new ProgressBar(mContext, null, android.R.attr.progressBarStyle);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mProgressbar.setLayoutParams(params);
rLayout = new RelativeLayout(mContext);
rLayout.setBackgroundColor(Color.CYAN);
rLayout.addView(mProgressbar);
addView(rLayout);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(rLayout != null)
{
rLayout.layout(l, t, r, b);
}
}
}
What am I doing wrong?
EDIT:
I am trying to add a progressbar(circular spinner) inside a RelativeLayout. The relativeLayout is visible, however the progressbar doesn't show up. Following is the code I am using but it doesn't seem to work.
public class MyCustomView extends RelativeLayout
{
ProgressBar mProgressbar;
public void init()
{
mProgressbar = new ProgressBar(mContext, null, android.R.attr.progressBarStyle);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mProgressbar.setLayoutParams(params);
addView(mProgressbar);
}
}
Progressbar doesn't even show up now?
My Goals:
Showing ProgressBar in the center of the MyCustomView.
I'm creating a custom linear layout to hold two image views.
I first thought that the layout is not showing at all but I then set its background to black to check if the layout is being inflated and it did. Then understood that the problem is the image views, they simply not showing.
Thanks in advance :-)
This is the class:
public class LoginIcons extends LinearLayout {
private ImageView mImageViewLogo;
private ImageView mImageViewIcons;
private View mView;
boolean test = false;
public LoginIcons(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LoginIcons(Context context) {
super(context);
init();
}
private void init() {
setOrientation(LinearLayout.VERTICAL);
mImageViewLogo = new ImageView(this.getContext());
mImageViewIcons = new ImageView(this.getContext());
mView = new View(getContext());
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mImageViewLogo.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 20);
mImageViewIcons.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 10);
mView.setLayoutParams(params);
mImageViewLogo.setImageResource(R.drawable.splash_logo_placeholder);
mImageViewIcons.setImageResource(R.drawable.login_icons);
mImageViewIcons.setBackgroundColor(Color.BLACK);
mImageViewLogo.setBackgroundColor(Color.BLACK);
addView(mView);
addView(mImageViewLogo);
addView(mImageViewIcons);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setViewsDimensions();
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
private void setViewsDimensions() {
LinearLayout parent = (LinearLayout) mImageViewLogo.getParent();
int screenWidth = LocalSettingsHelper.getScreenWidth(getContext());
// 0.375 percent
int imgDim = (int) ((double) screenWidth * 0.32);
int iconsWidth = (int) ((double) screenWidth * 0.5);
LinearLayout.LayoutParams params = (LayoutParams) mImageViewLogo
.getLayoutParams();
params.width = imgDim;
params.height = imgDim;
mImageViewLogo.setLayoutParams(params);
params = (LayoutParams) mImageViewIcons.getLayoutParams();
params.width = iconsWidth;
mImageViewIcons.setLayoutParams(params);
}
}
And this is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/HeaderBackground"
android:orientation="vertical" >
<Button
android:id="#+id/button_ok"
style="#style/LoginOkButtonStyle"
android:background="#drawable/blue_button_background_selector"
android:text="#string/ok" />
<com.example.me.entities.views.LoginIcons
android:id="#+id/loginIcons"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.example.me.entities.views.LoginIcons>
</LinearLayout>
It seems that the View object acted like match_parent and had height of nearly 400 pixels. This caused the image views to drop outside the boundaries of the screen.
Once I removed the View the image views turned up.