JavaFX2 - Get the ImageView of an IO-Control - java

I'm using JavaFX2.2. Is it actually possible to get the ImageView used as a parameter in the constructor for an UI-control such as Button or Label after the UI-control has been created?
I want to change the image of a control. I could probably also do it with CSS, but I'm not sure of how to use jar-resources there.
Example:
Label label = new Label("", new ImageView(new Image(getClass().getResourceAsStream("test.png"))));
label.getImageView().setImage(...); // there is no such method getImageView()

((ImageView) label.getGraphic()).setImage(...)

Related

Can't resize an ImageView from a dynamic declaration

I'm writing a chess app and I've been able to resize my pieces to match the square size, that itself depends on screen size.
Problem is, it relies on XML. And I need to be able to do it dynamically (adding or removing pieces on the screen after each move)
This is working :
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(sq_size, sq_size);
ImageView klt60 = (ImageView)findViewById(R.id.klt60);
klt60.setImageResource(R.drawable.klt60); // setting the image in the layout
klt60.setLayoutParams(layoutParams); // allows resizing
But this is not : if I declare my imageView without using XML, it just won't resize, and also it won't move when I play a move with that piece.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(sq_size, sq_size);
ImageView klt60 = new ImageView(this); // Create a new image (several pawns to display, for example)
klt60.setImageResource(R.drawable.klt60); // setting the image in the layout
klt60.setLayoutParams(layoutParams); // allows resizing
My view just extends extends AppCompatActivity. Thanks for help

ImageView shows on Emulator but NOT on device. Image on device is invisible

My problem
I have some dynamically created imageViews in which I put a drawable (xml vector, SVG). Everything seems to be OK on the android emulator but when I load the app on my phone there is no image visible. I know the image is there because if I increment its size, the views on the right at on the left move to the side. So there is an image!
What I have tried
Check memory of the phone
Use android:src and app:sourceCompat for the imageViews
Convert Drawable into Bitmap
Load another image (which is doing well in another window)
Put the drawable file in another drawable-folder
Change the layout type from RelativeLayout to LinearLayout
Read all the similar posts... No one resolves my problem :(
My class RankingDialog extends from AlertDialog.Builder
My layout is a RelativeLayout but I don't think that this causes the issue...
Create the views
for (int i = 0; i < LocalNames.size(); i++){
LinearLayout layout = new LinearLayout(context);
layout.setLayoutParams(layoutParams);
TextView position = new TextView(context);
TextView name = new TextView(context);
TextView record = new TextView(context);
position.setTypeface(font);
name.setTypeface(font);
record.setTypeface(font);
position.setText(i+1 + ".");
position.setGravity(Gravity.CENTER);
name.setGravity(Gravity.CENTER);
record.setGravity(Gravity.CENTER);
position.setLayoutParams(tvPosParams);
layout.addView(position);
name.setText(LocalNames.get(i));
name.setLayoutParams(tvNameParams);
layout.addView(name);
//Here is where I create and load the imageView/image
ImageView flag = new ImageView(context);
flag.setLayoutParams(imageParams);
flag.setImageResource(context.getResources().getDrawable(R.drawable.myImage);
layout.addView(flag);
record.setText(LocalPoints.get(i));
record.setLayoutParams(tvPointsParams);
layout.addView(record);
mainLinear.addView(layout);
}
----------Emulator (How it should look like)
How it looks on any real device...
I'm stuck for a couple of weeks here. I can't understand why this is happening...
I will apreciate any help

Android Studio - issue with manipulating Layouts/Views programmatically

I have a few issues with setting LayoutParams and other parameters of my layouts/views programmatically. I cannot specify these in a XML layout file because whether they appear depends on the data held in the database.
The following is a function I use to create a new "Section" which consists of a FrameLayout with its children being View and TextView:
public FrameLayout createSection(long id, String name) {
FrameLayout frame = new FrameLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
params.setMargins(15, 15, 15, 15);
frame.setLayoutParams(params);
View view = new View(this);
LayoutParams viewParams = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
view.setLayoutParams(viewParams);
view.setId(toIntExact(id));
view.setBackgroundResource(R.color.colorButton);
frame.addView(view);
TextView text = new TextView(this);
LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
textParams.setMarginStart(15);
text.setGravity(Gravity.CENTER);
text.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_START);
text.setTextColor(getResources().getColor(R.color.colorTextSecondary));
text.setText(name);
frame.addView(text);
return frame;
}
The parent of this newly created FrameLayout is LinearLayout and so based on the other similar questions on StackOverflow I figured setting parameters for FrameLayout should be done through LinearLayout.LayoutParams. However, this does not make a change. The initial XML page contains this:
Initial XML page
The first "SECTION" is created in the XML file, and the other two are created through 'createSection' function. This is the outcome: Design outcome
The issue is that the margins are not set properly and the TextView doesn't seem to care about the Gravity + TextAlignment combination that I'm using.
I would appreciate any help that I could get to resolve this issue.
I apologise for wasting anyone's time. The code seems to work and the margin sizes are different due to these being set in terms of pixels (px) rather than dp as it is in the XML file.
I also forgot to add text.setLayoutParams(textParams); to the TextView object.

Set transparent background of the taken Snapshot

I use the snapshot() method on the Shape object in order to convert it into ImageView and nest it into Label. The problem is, that when I take a snapshot of the Shape object it is closed into square field with white background. Is there a way to make it transparent? I use the code below in order to convert given Shape into an ImageView object:
WritableImage snapshot = Shape.snapshot(new SnapshotParameters(), null);
ImageView imageView = new ImageView(snapshot);
Label label = new Label();
label.setGraphic(imageView);
Pane.getChildren().add(label);
Here you go
SnapshotParameters parameters = new SnapshotParameters();
parameters.setFill(Color.TRANSPARENT);
WritableImage snapshot = shape.snapshot(parameters, null);
...

Using getDimension() on a Button/TextView always returns the same value

I'm making an app exclusively for android tablets and part of it has buttons dynamically created based on various news headlines. I have a single dimens.xml and I use the scaling factor from DisplayMetrics to make the sizes appropriate for different densities. The problem is that when I change the size in my xml it has absolutely no effect on the dimension that the method returns.
RelativeLayout.LayoutParams announcementButtonLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
announcementButtonLayout.topMargin = topMargin;
announcementButtonLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
announcementButtonLayout.height = this.getResources().getDimensionPixelSize(R.dimen.news_button_height);
announcementButtonLayout.width = getResources().getDimensionPixelSize(R.dimen.dashboard_right_width);
Button button = new Button (this.getActivity());
button.setWidth(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width));
button.setBackgroundResource(R.drawable.photo_edited);
button.setTextColor(getResources().getColor(R.drawable.news_poll_item_text_color));
float f =Utilities.getPixelScaleMultiplier();
f *= getResources().getDimension(R.dimen.news_item_text);
button.setTextSize(f);
button.setText(announcement.getDescription());
button.setMaxLines(1);
button.setTag(announcement);
button.setOnClickListener(this);
button.setLayoutParams(announcementButtonLayout);
button.setId(index);
Based on my button code above, what could be causing this breach of trust?
Thanks,
Adurnari
EDIT: Included my LayoutParams as reference
Found the solution:
"Cleaning" was taking the values that I had changed in my xml and adding them on a later portion of the page where they were located previously. The lower ones were dictating the size.
Try to work with button.setBounds
Use LayoutParams to set view width and height, then add it to viewgroup.
Button button = new Button (this.getActivity());
//button.setWidth(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width));
button.setBackgroundResource(R.drawable.photo_edited);
button.setTextColor(getResources().getColor(R.drawable.news_poll_item_text_color));
float f =Utilities.getPixelScaleMultiplier();
f *= getResources().getDimension(R.dimen.news_item_text);
button.setTextSize(f);
button.setText(announcement.getDescription());
button.setMaxLines(1);
button.setTag(announcement);
button.setOnClickListener(this);
LayoutParams announcementButtonLayout = new LayoutParams(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width), LayoutParams.WRAP_CONTENT);
button.setLayoutParams(announcementButtonLayout);
button.setId(index);
Clean the project after changing dimens.xml value, maybe the R file is not getting updated in your case.

Categories

Resources