Layout android not working - java

I wanted to put dynamically the checkboxes with the names that I retrieve from the API one below the other, but when I run the code the checkbox I see just one and the others are in the back of it
JSONObject json = new JSONObject(result);
arr = json.getJSONArray("functionality");
for(int i = 0; i < arr.length(); i++) {
String name = arr.getJSONObject(i).getString("name");
CheckBox check = new CheckBox(rootView.getContext());
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
check.setLayoutParams(lparams);
check.setText(name);
container.addView(check);
}

change below line in your code:
CheckBox check = new CheckBox(rootView.getContext());
to
CheckBox check = new CheckBox(container.getContext());

Related

Eclipse SWT Text Boxes change size

I had an Eclipse plug-in app (ten-year-old code, no documentation, etc.) dropped in my lap and while adding new features to it, I noticed that when a panel is resized, the text boxes change size continuously while the separator is being dragged.
As you can see in the second picture, the text boxes are kind of randomly sized. Is there a setting in SWT that will prevent this from happening?
Here's how I'm creating one of the text boxes. The others are basically clones of this:
Font font = parent.getFont();
setLayout(new FillLayout());
SashForm sashForm = new SashForm(this, SWT.VERTICAL);
FormToolkit toolkit = new FormToolkit(getParent().getDisplay());
Section section = toolkit.createSection(sashForm,
Section.DESCRIPTION | ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
section.setLayoutData(new GridData(GridData.FILL_BOTH));
section.setLayout(new GridLayout());
section.setText("Section Title");
Composite controlComposite = toolkit.createComposite(section);
GridLayout controlLayout = new GridLayout();
controlLayout.numColumns = 2;
controlLayout.verticalSpacing = 20;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Font bold = ResourceManager.getBoldFont(font);
Label textLabel = toolkit.createLabel(controlComposite, "Title:", SWT.BOLD);
GridData gd = new GridData();
gd.horizontalSpan = 1;
textLabel.setLayoutData(gd);
textLabel.setFont(bold);
textBox = new ExtendedText(controlComposite, SWT.BORDER | SWT.SINGLE, false);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 1;
gd.verticalSpan = 2;
textBox.setLayoutData(gd);
The ExtendedText class is an extension of StyledText. The important bits of it are this:
GridData gd_bg = new GridData(GridData.FILL_BOTH);
setLayoutData(gd_bg);
final GridLayout gridLayout = new GridLayout();
gridLayout.verticalSpacing = 0;
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.numColumns = 1;
gridLayout.makeColumnsEqualWidth = true;
sashForm.setWeights(new int[] { 1, 1 });
Okay, after digging in a little deeper, I got it working as expected.
First, the controlComposite and controlLayout objects are now created using
Composite controlComposite = new Composite(section, SWT.NONE)
controlComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
controlComposite.setBackground(section.getBackground());
GridLayout controlLayout = new GridLayout(2, true);
controlLayout.marginHeight = 20;
controlLayout.marginWidth = 0;
controlLayout.verticalSpacing = 10;
controlLayout.horizontalSpacing = 0;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Once I did that, things started to stabilize. I also ended up tweaking the weights to this:
sashForm.setWeights(new int[] { 2, 3 });
It's not perfect, but it'll do for now.
Thanks to #greg-449 and #Baz for taking a look

TransitionDrawable animation in a switch/case statement nested in for loop

The purpose of this is to create SimonSays game and do the logic behind generating and displaying a pattern that needs to be repeated
sequenceArray is already populated with random values from 0 to 3, using Random class and for loop, then what I want to do is read depending on the case of each element of the sequenceArray array, I want to change one of 4 views in a way that the animation lasts 200 ms and it should give the end user a feeling of pressing a button, or in this case, a button flashing. Here's how I did this:
for(int j = 0; j < sequenceArray.length; j++){
switch(sequenceArray[j]){
case 0:
Drawable backgrounds_yellow[] = new Drawable[2];
Resources res_yellow = getResources();
backgrounds_yellow[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow);
backgrounds_yellow[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow_blink);
TransitionDrawable crossfader_yellow = new TransitionDrawable(backgrounds_yellow);
ImageView ivYellow = (ImageView) findViewById(R.id.iv1);
ivYellow.setImageDrawable(crossfader_yellow);
crossfader_yellow.startTransition(0);
crossfader_yellow.reverseTransition(200);
case 1:
Drawable backgrounds_red[] = new Drawable[2];
Resources res_red = getResources();
backgrounds_red[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_red);
backgrounds_red[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_red_blink);
TransitionDrawable crossfader_red = new TransitionDrawable(backgrounds_red);
ImageView ivRed = (ImageView) findViewById(R.id.iv2);
ivRed.setImageDrawable(crossfader_red);
crossfader_red.startTransition(0);
crossfader_red.reverseTransition(200);
case 2:
Drawable backgrounds_blue[] = new Drawable[2];
Resources res_ = getResources();
backgrounds_blue[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_blue);
backgrounds_blue[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_blue_blink);
TransitionDrawable crossfader_blue = new TransitionDrawable(backgrounds_blue);
ImageView ivBlue = (ImageView) findViewById(R.id.iv3);
ivBlue.setImageDrawable(crossfader_blue);
crossfader_blue.startTransition(0);
crossfader_blue.reverseTransition(200);
case 3:
Drawable backgrounds_green[] = new Drawable[2];
Resources res_green = getResources();
backgrounds_green[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_green);
backgrounds_green[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_green_blink);
TransitionDrawable crossfader_green = new TransitionDrawable(backgrounds_green);
ImageView ivGreen = (ImageView) findViewById(R.id.iv4);
ivGreen.setImageDrawable(crossfader_green);
crossfader_green.startTransition(0);
crossfader_green.reverseTransition(200);
default:
return;
}
I've tried the same code with OnClickListener shown bellow and it works, but for some reason, when I run the code above, nothing happens, no animation change takes place.
I've tested the sequenceArray afterwards, it isn't empty, but for some reason beyond my comprehension the current code just won't do what I want it to. What's the main difference between the for loop shown below with the logic above?
iv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable backgrounds[] = new Drawable[2];
Resources res = getResources();
backgrounds[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow);
backgrounds[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow_blink);
TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
ImageView image = (ImageView) findViewById(R.id.iv1);
image.setImageDrawable(crossfader);
crossfader.startTransition(0);
crossfader.reverseTransition(200);
}
});

Blend Mode doesn't work for ImageView

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 !

define multiple labels in javafx at once

I would like to declare multiple labels in javafx all at once, is this possible?
at the moment this is what i type, and i have line 20 to 30 labels!!
fajr_Label_ar = new Label();
fajr_Label_eng = new Label();
zuhr_Label_ar = new Label();
zuhr_Label_eng = new Label();
asr_Label_ar = new Label();
asr_Label_eng = new Label();
maghrib_Label_ar = new Label();
maghrib_Label_eng = new Label();
isha_Label_ar = new Label();
isha_Label_eng = new Label();
hadith_Label = new Label();
fajr_hourLeft = new Label();
Can I declare something along the line of;
label1, label2, label3.... = new Label();
This is not about JavaFX. This question is about Java in general ..
I see some localizations there : en,ar ... so I would suggest this pattern:
Label lbs[];
lbs=new Label[7];
String strings_en[]=new String[]{
"fajr",
"zuhr",
"asr",
"maghrib",
"isha",
"hadith",
"fajr_lefthour"
};
String strings_ar[]=new String[]{
"فجر",
"ظهر",
"عصر",
"مغرب",
"عشاء",
"حديث",
"فجر ساعة لاحقة"
};
String strings[];
//assing lan list
strings=strings_ar; // or strings=strings_en;
for (int i = 0; i < lbs.length; i++) {
lbs[i]=new Label(strings[i]);//initializing labels
//doing what you want here with labels
//...
}
It would be better to isolate data from design as you can see we have labels as Label array and data as String array.. and it would be better and better to define the lang set before retrieving data as we have done when assign object array strings to object array Ar or en .. that would help you to extend/scale/debug your program easily and healthy by adding more labels or languages ..

Associate an image with Radio button in Android

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.

Categories

Resources