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.
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 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>
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'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.
I am trying to keep two customviews and i kept fadein fadeout but i am not able to see both custom views am able to see only one custom view if i click on fadein i m able to see only one view but not both. but i am not able to see both views at a time please tell me where i did mistake.
MyView myview;
MyView1 myview1;
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myview=new MyView(this);
myview.setBackgroundColor(color.white);
setContentView(myview);
myview = new MyView(this);
myview.setBackgroundColor(Color.WHITE);
myview1=new MyView1(this);
myview1.setBackgroundColor(color.white);
setContentView(myview1);
myview1 = new MyView1(this);
myview1.setBackgroundColor(Color.WHITE);
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setId(0);
relativeLayout.setBackgroundColor(Color.WHITE);
myview = new MyView(this);
myview.setId(004);
RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
myview.setLayoutParams(lp6);
relativeLayout.addView(myview,lp6);
myview1 = new MyView1(this);
myview1.setId(8);
RelativeLayout.LayoutParams lp008 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
myview1.setLayoutParams(lp008);
relativeLayout.addView(myview1,lp008);
Button button1 = new Button(this);
RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
button1.setText("click");
relativeLayout.addView(button1,lp1);
button1.setOnClickListener(this);
setContentView(relativeLayout);
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
protected void onDraw(Canvas canvas) {
Bitmap _scratch = BitmapFactory.decodeResource(getResources(),R.drawable.apple);
canvas.drawBitmap(_scratch, 840, 450, null);
}
}
public class MyView1 extends View{
public MyView1(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
Bitmap scratch=BitmapFactory.decodeResource(getResources(), R.drawable.ant);
canvas.drawBitmap(scratch, 840, 450, null);
}
}
public void onClick(View v) {
if(myview1.getVisibility() == View.VISIBLE) {
Animation out = AnimationUtils.makeOutAnimation(this, true);
myview1.startAnimation(out);
myview1.setVisibility(View.INVISIBLE);
} else {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
myview1.startAnimation(in);
myview1.setVisibility(View.VISIBLE);
}
}
}
Use Views setVisibility method