I want to fetch Images from my URL and add it to the radio button that is further added to a radio group. However, i could not find metod such as setText to set text for radio button. I wish to add image to my radio button in similar fashion. Is there any hint/method that can do the same for me? Any hint/code example would be really helpful for me,
RadioGroup rg = new RadioGroup (this);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton rb[]= new RadioButton[children.size()];
for (int i = 0; i < var.size(); i++)
{
Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);
MovieName[i]=movieAtt.getAttributeValue( "Title" );
MovieCover[i]=movieAtt.getAttributeValue( "cover" );
ShowTime[i]=movieAtt.getAttributeValue( "showtime" );
rb[i] = new RadioButton(this);
rb[i].setText(movieAtt.getAttributeValue("Title"));
rg.addView(rb[i]);
//Calling this func to get Images
//LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" ));
//rb[i].buildDrawingCache(LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" )));
public static Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
return null;
}
}
Is the problem with the image or the text? As for the image, I believe this should work:
Drawable d = LoadImageFromWebOperations(movieAtt.getAttributeValue("cover"));
rb[i].setButtonDrawable(d);
Make sure to set gravity to Gravity.CENTER or your image won't be centered around the radio button.
You should find information about the text in the RadioButton reference.
Related
I am randomizing the location of a button on a Relative View everytime a button is clicked. I am doing this by changing the margins of the buttons programatically as shown in the code:
Button testbtn = findViewById(R.id.testbtn);
testbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Get Screen size
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//Save dimensions in variables
int widthScreen = metrics.widthPixels;
int height = metrics.heightPixels;
TextView width_height = findViewById(R.id.width_height);
//Declare random
Random randomDimension = new Random();
//Declare layout params
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.rightMargin = 608;
params.leftMargin = 428;
params.topMargin = randomDimension.nextInt(height) +1;
//params.setMargins();
buttonIDs[1].setLayoutParams(params);
width_height.setText(params.leftMargin+" "+params.rightMargin);
}
});
In this case, "buttonIDs[]" is an array i declared earlier in the class and "testbtn" is the button that when pressed, randomizes the position of my button.
However, these specific margins produce the following image
enter image description here
and I need:
enter image description here
Does anyone know why this is happening and how to fix it?
You set:
params.rightMargin = 608;
params.leftMargin = 428;
So the device must be 608+428+width of the button at least wide. Is it?
I have a code which adds buttons and images dynamically to a fragment. The buttons are getting added but the images are not being added.
Here is my code :
for(String f1 : f){
// For Each File Add a View or a button or something
LogUtil.d(TAG,"ocr override" + f1);
Button b = new Button (getActivity());
b.setId (i);
b.setLayoutParams(params2);
b.setText (f1);
b.setOnClickListener(this);
ImageView img_view = new ImageView(getContext());
img_view.setImageBitmap(getBitmapFromAsset("OcrSampleImages"+System.getProperty("file.separator")+f1));
img_view.setLayoutParams(params);
linearLayout.addView(b);
linearLayout.addView(img_view,params);
i++;
}
params and params2 are defined as follows :
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Here is the output on the emulator :
Here is my assets folder :
Better to use iterative strategy..
first, new ImageView(getContext()); -> new ImageView(**getActivity()**);
Secondly, if you don't know where is the problem, then better reading default drawable image
img_view.setImageResource(R.drawable.<any_dummy_image_from_drawable>);
instead of
setImageBitmap()
if your dummy image load properly that means your View is perfect and having problem while reading from assets,
If so, then you can concentrate more on asset reading thing.
I have a specific problem which has not be answered yet on stackoverflow; I have images in the assets folder numbered like 0.jpg, 1.jpg, 2.jpg etc. Using a for loop I select three images from the asssets folder and I am trying to add these images to a gridview but the images are not showing. The activity starts up okay just no images!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
gridView = (GridView) findViewById(R.id.gridview_result);
// Sets the Tag
gridView.setTag(GRIDVIEW_TAG);
/*
* Adapt the image for the GridView format
*/
imageAdapter = new ImageGridViewAdapter(getApplicationContext());
gridView.setAdapter(imageAdapter);
// Set the orientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Retrieve 3 images form the database which appear
// similar
for (int i = 0; i < 3; i++) {
// System.out.println(Retrieval.distances[i][0]);
image = Retrieval.distances[i][0];
int num = (int) image;
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(num);
String imageNum = sBuilder.toString();
System.out.println(imageNum);
String file = imageNum + ".jpg";
try {
// get input stream
InputStream ims = getAssets().open(file);
Log.i("ERROR_IMS", ims + "");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, file);
// set image to ImageView
gridView.setBackground(d);
Log.i("ERROR_d", d + "");
Log.i("ERROR_gridview", gridView+"");
} catch (IOException ex) {
Log.e("I/O ERROR", "Failed when ...");
}
}
}
I believe the issue is occurring in the try/catch. Any help will be much appreciated!
You should get all images first and set it to your adapter.
// set image to ImageView
gridView.setBackground(d);
doesn't affect to your grid items view.
A good tutorial for it: guide
I tried this:
public void addTargetCard(MissionCard mCard) {
int card = mCard.GetID();
leftSide.getChildren().removeAll(targetCardBox);
Image image = new Image(
MainApp.class.getResourceAsStream("images/target" + card
+ ".png"));
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitHeight(81);
imageView.setFitWidth(108);
imageView.setPreserveRatio(true);
imageView.setPickOnBounds(true);
Tooltip.install(imageView, new Tooltip(intToCity(mCard.getStart())
+ " - " + intToCity(mCard.getFinish())));
targetCardBox.getChildren().add(imageView);
leftSide.getChildren().add(targetCardBox);
}
Can somebody explain me why my Tooltip doesn't work - i got no idea what i did wrong. (It's my first time that i use Tooltips's)
somebody else told me that ImageView doesnt work with tooltips and gave me this workaround - but i have again no tooltip when i move with my mouse over the label
public void addTargetCard(MissionCard mCard) {
int card = mCard.GetID();
leftSide.getChildren().removeAll(targetCardBox);
Image image = new Image(
MainApp.class.getResourceAsStream("images/target" + card
+ ".png"));
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitHeight(81);
imageView.setFitWidth(108);
imageView.setPreserveRatio(true);
imageView.setPickOnBounds(true);
Label label = new Label();
label.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
label.setGraphic(imageView);
label.setTooltip(new Tooltip(intToCity(mCard.getStart()) + " - "
+ intToCity(mCard.getFinish())));
targetCardBox.getChildren().add(label);
leftSide.getChildren().add(targetCardBox);
}
Installing a tooltip to image view is working. Try yourself with new sample JavaFX project and see it. When you doubt about some functionality of the used API (JavaFX in this case) try to isolate the doubted use case into new fresh environment/project and observe it closely.
P.S. Why are you removing the targetCardBox from leftSide and adding it again afterwards.
I have two image that is
and this:
I need to place that image (stacked) in a single ImageView. I tried to use blend mode, but doesn't work for ImageView like
Group group = new Group();
group.setBlendMode(BlendMode.SRC_OVER);
// tempImage is array of buffered Images
for(int i=0; i < tempImage.length ;i++){
if(tempImage[i] != null){
ImageView view = new ImageView();
Image im = SwingFXUtils.toFXImage(tempImage[i],
null );
view.setImage(im);
group.getChildren().add(view);
}
}
Just a little trick, instead of using BlendModeuse a HBox, add the images inside the HBox and set the HBox inside the Group
Group group = new Group();
HBox box = new HBox
// tempImage is array of buffered Images
for(int i=0; i < tempImage.length ;i++){
if(tempImage[i] != null){
ImageView view = new ImageView();
Image im = SwingFXUtils.toFXImage(tempImage[i],
null );
view.setImage(im);
box.getChildren().add(view);
}
}
group.getChildren.add(box);
But this wont help you to get the new image, guess you don't even need it !