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);
}
}
Related
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 achieve a layout dynamically which is given below:
I can manage to done all things dynamically. But I can't align item name (Chicken Masala) to right of the ImageView . I am reached at this position as following.
RelativeLayout primary_layout = new RelativeLayout(this);
LayoutParams layoutParam = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
primary_layout.setLayoutParams(layoutParam);
// primary_layout.setOrientation(LinearLayout.HORIZONTAL);
// primary_layout.setBackgroundColor(0xff99ccff);
//String cross = " � ";
String makeString = aOrder.getQuantity() + " "
+ aOrder.getFoodName();
ImageView imageView_remove = createAImageview(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.CENTER_VERTICAL,
10, 20);
TextView item_name = createATextViewWithParam(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_TOP, imageView_remove.getId(),
makeString, 20, 10, 20);
TextView txt_item_price = createATextView(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT,
"" + item_price, 20, 10, 20);
primary_layout.addView(imageView_remove);
primary_layout.addView(item_name);
primary_layout.addView(txt_item_price);
I am share Two methods createAImageview() & createATextViewWithParam() which is necessary for this Layout.
public ImageView createAImageview(int layout_width, int layout_height, int align,
int margin, int padding) {
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
layout_width, layout_height);
_params.setMargins(margin, margin, margin, margin);
_params.addRule(align);
imageView.setLayoutParams(_params);
imageView.setPadding(padding, padding, padding, padding);
imageView.setImageResource(R.mipmap.remove);
return imageView;
}
public TextView createATextViewWithParam(int layout_widh, int layout_height, int align, int align_id,
String text, int fontSize, int margin, int padding) {
TextView textView_item_name = new TextView(this);
// LayoutParams layoutParams = new LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams.gravity = Gravity.LEFT;
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
layout_widh, layout_height);
_params.setMargins(margin, margin, margin, margin);
_params.addRule(align, align_id);
textView_item_name.setLayoutParams(_params);
textView_item_name.setText(text);
textView_item_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
textView_item_name.setTextColor(Color.parseColor("#000000"));
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView_item_name.setPadding(padding, padding, padding, padding);
return textView_item_name;
}
You need to add rule for LEFT_OF/RIGHT_OF :
_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
_params.addRule(RelativeLayout.LEFT_OF, R.id.id_of_textview);
imageView.setLayoutParams(_params);
Try this,
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dynamicviewdemo.MainActivity" >
<RelativeLayout
android:id="#+id/llAddMember"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" />
</RelativeLayout>
MainActivity.java
package com.example.dynamicviewdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
private RelativeLayout llAddMember;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llAddMember = (RelativeLayout) findViewById(R.id.llAddMember);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final RelativeLayout linearLayout = new RelativeLayout(getApplicationContext());
linearLayout.setLayoutParams(layoutParams);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imgView = new ImageView(MainActivity.this);
imgView.setImageResource(R.drawable.ic_launcher);
imgView.setLayoutParams(lparams);
linearLayout.addView(imgView);
lparams.setMargins(0, 15, 0, 0);
LayoutParams lparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView txtResGender = new TextView(MainActivity.this);
txtResGender.setLayoutParams(lparams1);
txtResGender.setText("Hello World");
txtResGender.setTextSize(14);
txtResGender.setTextColor(Color.parseColor("#9C9C9C"));
linearLayout.addView(txtResGender);
lparams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lparams1.setMargins(0, 30, 15, 0);
llAddMember.addView(linearLayout);
}
}
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 have CustomView which is just an arc shape with some lines.
public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;
private Paint paint = new Paint();
public CustomDrawableView(Context context, int startA, int endA) {
super(context);
int x = 0;
int y = 0;
int width = 400;
int height = 400;
paint.setColor(Color.BLACK);
mDrawable = new ShapeDrawable(new ArcShape(startA, endA));
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
canvas.drawLine(0,0,400,0,paint);
canvas.drawLine(0, 0, 0, 400, paint);
canvas.drawLine(400,0, 400,400, paint);
}
}
Afterward I call out my linear layout from the xml file through its id add a framelayout to it. After that, I add CustomDrawableView and try to center it by using gravity. I went through many other questions and tried those solutions, but it just doesn't work for me. If it's worth noting, I notice when I use a regular view like a textview with no custom view in my layout, it centers perfectly.
public class MainActivity extends ActionBarActivity {
private RelativeLayout ll;
private CustomDrawableView testView;
private RelativeLayout.LayoutParams layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load the layout
LinearLayout linLayout = (LinearLayout)findViewById(R.id.ll);
linLayout.setOrientation(LinearLayout.VERTICAL);
/******** Experimental Code **********/
RelativeLayout rl = new RelativeLayout(this);
linLayout.addView(rl);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
CustomDrawableView arc = new CustomDrawableView(this,0,30);
CustomDrawableView arcTwo = new CustomDrawableView(this, 50, 30);
rl.setGravity(Gravity.CENTER_HORIZONTAL);
rl.addView(arc, lp);
rl.addView(arcTwo, lp);
rl.setBackgroundColor(Color.GRAY); }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I use the frame layout in so i can lay views over each other.
There are a few problems here.
First is that you need set the layout params for your RelativeLayout called "rl" when you add it to your LinearLayout called "linLayout." Do this with the following code:
RelativeLayout rl = new RelativeLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
linLayout.addView(rl, params);
Your other issue is that when you add your customViews to rl it is better to add a rule to the layout params than to use gravity. The code below demonstrates this:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
CustomDrawableView arc = new CustomDrawableView(this,0,30);
CustomDrawableView arcTwo = new CustomDrawableView(this, 50, 30);
rl.addView(arc, lp);
rl.addView(arcTwo, lp);
Hope this helps!
Have you try to set rl's layout_width and layout_height to match_parent?
Try this code
/******** Experimental Code **********/
RelativeLayout rl = new RelativeLayout(this);
LayoutParams parems = new LayoutParams(LayoutParams.MATH_PARENT,
LayoutParams.MATH_PARENT);
rl.setParameters(parems);
linLayout.addView(rl);
UPDATE :
Please show your activity_main.xml code and notice your api level. If so, I'm going to run your code.
Otherwise, have you try to set layout size from onMeasure()?
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = 0;
switch (heightMode) {
case MeasureSpec.AT_MOST: // wrap_confent
heightSize = 400;
break;
case MeasureSpec.EXACTLY: // match_parent
heightSize = MeasureSpec.getSize(heightMeasureSpec);
break;
}
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = 0;
switch (widthMode) {
case MeasureSpec.AT_MOST: // wrap_confent
widthSize = 400;
break;
case MeasureSpec.EXACTLY: // match_parent
widthSize = MeasureSpec.getSize(widthMeasureSpec);
break;
}
setMeasuredDimension(widthSize, heightSize);
}
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.