I currently add images through xml with the whole R.id.x method with the following function:
public void Image(int ID, int x, int y){
ImageView iv = (ImageView) findViewById(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
}
I've written a new function to get these images on screen programmatically instead of parsing them in XML, with help from afore-mentioned topics/questions I searched and came up with this:
public void ImageRAW(int ID, int x, int y){
ImageView iv = new ImageView(c);
iv.setImageResource(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
rl.addView(iv);
}
But it did not work. I also tried adding the following line, to no avail: iv.setVisibility(View.VISIBLE);
And in regards to the variables rl and c:
private Context c;
private RelativeLayout rl;
public void SetUtilContext(Context context){
c = context;
rl = new RelativeLayout(context);
}
The above function is called in every Activity's onCreate() function and sets the UtilLib's current Context and RelativeLayout for drawing accordingly.
The function ImageRAW() is something I would like to use to replace the old Image() function, to make things easier for me. How would/could I get this working?
Try add this before your SetUtilContext():
RelativeLayout menu = findViewById(R.layout.menu);
And this at the end of your ImageRAW() method:
menu.addChild(rl);
As per Incredible's request, my onCreate function where I'm testing the functions:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
SetUtilContext(this);
Image(R.id.logo, 0, 0);
GetScreenSize();
ImageButton(R.id.start_btn
, (screenWidth/2)-(GetImageWidth(R.id.start_btn)/2)
, 48+GetImageHeight(R.id.logo));
ImageButton(R.id.options_btn
, (screenWidth/2)-(GetImageWidth(R.id.options_btn)/2)
, 96+GetImageHeight(R.id.logo)+GetImageHeight(R.id.start_btn));
ImageButton(R.id.about_btn
, (screenWidth/2)-(GetImageWidth(R.id.about_btn)/2)
, 144+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*2));
ImageButton(R.id.exit_btn
, (screenWidth/2)-(GetImageWidth(R.id.exit_btn)/2)
, 192+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*3));
PlayAudio(R.raw.theme, true);
ImageRAW(R.drawable.head, 0, GetImageHeight(R.id.logo));
}
Related
Transparent activity is there in my app and I want to give left, right, top margin to this activity. For this I am using WindowManager.LayoutParams. But there I am not able to give Gravity top, right, left at a time. There is only two parameter x and y. If I want to set left, right and top then what I need to do.
Code:-
public class SelectedRecipient extends Activity {
private Context context;
private WindowManager.LayoutParams wmParams=null;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("*** SelectedRecipient ***");
context = SelectedRecipient.this;
WindowManager.LayoutParams wmParams = this.getWindow().getAttributes();
wmParams.gravity = Gravity.TOP | Gravity.LEFT;
wmParams.x = 50; // x position
wmParams.y = 40; // y position
ArrayList<String> aListNoOfSelection = new ArrayList<String>();
aListNoOfSelection.add("Vinit");
aListNoOfSelection.add("Vikash");
aListNoOfSelection.add("Jonson");
aListNoOfSelection.add("Nikolesh");
ArrayAdapter<String> aAdapterNoOfSelection = new NoOfSelectionAdapter(context, 0, aListNoOfSelection);
listView = new ListView(context);
listView.setAdapter(aAdapterNoOfSelection);
setContentView(listView);
}
}
I also tried this(which not wwork):-
LayoutParams layoutParams = new LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(50, 0, 50, 0);
listView.setLayoutParams(layoutParams);
You probably want to use a dialog theme like answered above, if so...
put android:theme="#android:style/Theme.Dialog" inside your AndroidManifest.xml
or by calling: setTheme(android.R.style.Theme_Dialog); before calling setContentView(); in your onCreate()
if not you can use something like:
ViewGroup.MarginLayoutParams wmParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
wmParams.setMargins((int left),(int top),(int right),(int bottom));
or wmParams.bottomMargin , wmParams.leftMargin , wmParams.rightMargin , wmParams.topMargin
I'm creating buttons dynamically in my class, I try to position them using 'offsetLeftAndRight()' or '.leftMargin' and '.topMargin' as follows,
public class instruction extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instruct);
final Button btn = new Button(this);
RelativeLayout.LayoutParams paramsd2 =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsd2.leftMargin = 500;
paramsd2.topMargin = 500;
paramsd2.height = 60;
paramsd2.width = 200;
btn.offsetLeftAndRight(300);
btn.setLayoutParams(paramsd2);
addContentView(btn, paramsd2);
}
But the button always stays in the top left corner, how can I position it, what am I doing wrong?
AddContentView() is not the proper way to add a view in an already set layout.
make your main layout a RelativeLayout (check this in the instruct.xml layout file)
use its id to retreive a reference on it in your onCreate() method using
myRelativeLayout = (RelativeLayout) this.findViewById(R.id.itsId)
then add your button to this layout :
myRelativeLayout.addView(myButton);
the layout params of your button seems fine for positioning so it should work.
Set margin on button rather then layout
MarginLayoutParams marginParams = new MarginLayoutParams(backToMainScreenImageView.getLayoutParams());
marginParams.setMargins(0, 0, (int) UIUtil.getRadialButtonMainMargin(this), 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
backToMainScreenImageView.setLayoutParams(layoutParams);
Try something like this :
paramsd2.setMargin(500, 500, 0, 0);
btn.setLayoutParams(paramsd2);
I have a LinearLayout that I am extending and I want to add a ViewFlipper and Views in it dynamically. Can anyone tell me why this is showing a blank screen?
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
ViewFlipper vf = new ViewFlipper(context);
vf.layout(l, t, r, b);
this.addView(vf);
TextView tv = new TextView(context);
tv.setText("FOO");
tv.setBackgroundColor(Color.WHITE);
vf.addView(tv, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
I made this sample code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
ViewFlipper vf = new ViewFlipper(this);
vf.layout(10, 100, 100, 150);
LinearLayout ll = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("Prueba");
ll.addView(tv);
ll.addView(vf);
TextView tv2 = new TextView(this);
tv2.setText("FOO");
tv2.setBackgroundColor(Color.WHITE);
vf.addView(tv2, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
setContentView(ll);
}
And obtained the desired result, not the one you found out.
Maybe you should check the filling type stuff inside you layouts.
Have you overriden the onMeasure() method? I think you need to do this otherwise the View is not correctly sized. Someone please correct me if I am wrong
I'm trying to call onDraw method on Button click ,button the view is updated only once also onDraw method is called . But no change in the position of line.
I have this custom view
public LineSeekbar(Context context) {
super(context);
this.setWillNotDraw(false);
setNewX(130);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("GRAPH","draw");
paint = new Paint();
paint.setARGB(225, 215, 10, 20);
paint.setStrokeWidth(2);
paint.setStyle(Style.FILL);
canvas.drawLine(130,900,getNewX(),100, paint);
setNewX(getNewX()+15);
}
Calling this from activity class
final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView = new LineSeekbar(LineActivity.this);
LineView.setLayoutParams(new LayoutParams(500,500));
LineView.onMeasure(500,500);
LineView.invalidate();
LineView.draw(cv);
// LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);
Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LineView.invalidate();
LineView.draw(cv);
}
});
You should call an instance with small letter because it is not a class: lineView insted of LineView.
Since you are calling it from another thread (UI) you should say
LineView.postInvalidate();
When you create LineView instance for the first time, onDraw will be executed on its own and show the first line.
It is not clear what is LineView.draw(cv); You can draw the background image in onDraw just before drawing the line. You can use onSizeChange method in the custom view to find its real dimensions and resize your bitmap...
In onDraw insert a line
Log.e("LineView", Integer.toString(getNewX()));
and then in DDMS - LogCat watch the output when you press the button.
Try this:
You didn't made an object of the LineView class like you should. You should use small letters for instances.
final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView lineView = new LineSeekbar(LineActivity.this);
lineView.setLayoutParams(new LayoutParams(500,500));
lineView.onMeasure(500,500);
lineView.invalidate();
lineView.draw(cv);
// LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);
Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lineView.invalidate();
lineView.draw(cv);
}
});
Solved it ,I placed custom view in xml layout ,its working fine.
Alright. So I start my activity in Main, grab the FragmentManager and instantiate a Fragment which needs to return a View. OK. So I extended a LinearLayout in order to have something to return. My Activity and Fragment are happy but I am not.
Three LinearLayouts which I create in the parent ViewGroup are there (code below). I have verified this by counting children and by setting the background colors to contrast one another. The parent also changes size depending on how tall I make the children (when I don't declare any LayoutParams on the parent).
public class Mainmenu extends LinearLayout {
private ArrayList<LinearLayout> panes = new ArrayList<LinearLayout>();
private Context context;
private final int
LEFT = 0, CENTER = 1, RIGHT = 2;
public Mainmenu(Context c) {
super(c);
context = c;
setBackgroundColor(Color.WHITE);
setOrientation(HORIZONTAL);
setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
for(int i=0;i<=RIGHT;i++){ //Create the (3) Panes
LinearLayout ll = new LinearLayout(context);
ll.setLayoutParams(
new LinearLayout.LayoutParams(300,
LinearLayout.LayoutParams.FILL_PARENT));
switch(i){
case LEFT | RIGHT:
ll.setBackgroundColor(Color.DKGRAY);
default:
ll.setBackgroundColor(Color.BLACK);
}
ll.setOrientation(LinearLayout.VERTICAL);
ll.setVisibility(VISIBLE);
ll.setWillNotDraw(false);
panes.add(i, ll);
addView(ll);
}
LinearLayout.LayoutParams buttons =
new LinearLayout.LayoutParams(100, 50);
buttons.setMargins(15, 5, 5, 0);
TextView tv1 = new TextView(context);
tv1.setText("hello");
tv1.setTextColor(Color.RED);
panes.get(LEFT).addView(tv1, buttons);
Button button = new Button(context);
button.setText("Launch Editor");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
}
});
panes.get(CENTER).addView(button);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
}
}
Nothing is null, all my elements (3 ViewGroups and 2 Views) are present in the tree but not visible. I've tried bringing children to the front through the parent and the children, creating them in different super.methods and invalidating the view in a similarly shotgunned fashion. What's going on? Is it as simple as not having any idea what I'm doing?
The problem is simply because you are overriding onLayout and doing nothing with it. You only need to override this if you want to layout the children yourself (ie, you were designing some unique custom layout). In this case just remove that method, or call super.onLayout.