Set gravity of a layout - android-layout

I'm trying to set right gravity to a linear layout in the OnPreExecute method,
but the dialog still shows my hebrew text in the left side.
What is the problem with my code ?
private ProgressDialog dialog;
private Context context;
private LinearLayout layout;
public MyTask(Activity activity) {
context = activity;
dialog = new ProgressDialog(context);
layout = new LinearLayout(context);
}
protected void onPreExecute() {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(-1, -2);
layout.setGravity(Gravity.RIGHT);
TextView loadMsg = new TextView(context);
loadMsg.setText("טוען...");
loadMsg.setGravity(Gravity.RIGHT);
layout.addView(loadMsg, params);
dialog.setView(layout);
// dialog.setContentView(R.layout.loaddialog);
// dialog.show();
}

I have tried to fix your problem try to use it. It is working for me.
LinearLayout lp = new LinearLayout(getApplicationContext());
lp.setMinimumWidth(100);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setGravity(Gravity.RIGHT);
TextView loadMsg = new TextView(this);
loadMsg.setText("my msg");
loadMsg.setTextColor(Color.WHITE);
loadMsg.setGravity(Gravity.RIGHT);
lp.addView(loadMsg, params);
Dialog dialog = new Dialog(this);
dialog.setContentView(lp);
dialog.show();

Related

ImageButton to show image of ImageView onClick

I want an ImageButton to change its image when clicked on it. But the image that it changes to has to be the image from an ImageView. Is this possible?
EDIT:
in the first activity where you chose your character to play with, there´s an ImageView. The Image of it is being changed to the Image of the chosen character via onClickListener that is set for an ImageButton:
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
View.OnClickListener onClickButton1 = view -> {
imgV3.setImageDrawable(
((ImageButton) view).getDrawable()
);
};
ImageButton imageButton1 = (ImageButton) findViewById(R.id.plOneChar1);
imageButton1.setOnClickListener(onClickButton);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.plOneChar2);
imageButton2.setOnClickListener(onClickButton);
...
Until here everything's working just fine.
In the 2nd activity, where the two players play against each other, I need the current Image of ImageView3 (and imageView4 for the 2nd character, didn't post it above because it's the same code). this current image is what i want to be shown on several ImageButtons when clicked on them.
This is how i tried to solve it:
public class PlayActivity extends AppCompatActivity {
String random;
String pl1;
String pl2;
List<String> list;
Random rand;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
String pl1 = "Player One";
String pl2 = "Player Two";
List<String> list = new ArrayList<String>();
list.add(pl1);
list.add(pl2);
Random rand = new Random();
String random = list.get(rand.nextInt(list.size()));
Toast.makeText(this, "It´s your turn: " + random, Toast.LENGTH_SHORT).show();
}
public void onClickImgBtn (View view) {
ImageButton imgBtn = (ImageButton) view;
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
ImageView imgV4 = (ImageView) findViewById(R.id.imageView4);
if((random == pl1) && (imgBtn.getDrawable() == null)){
imgBtn.setImageDrawable(imgV3.getDrawable());
random = list.get(rand.nextInt());
} else if((random == pl2) && (imgBtn.getDrawable() == null)) {
imgBtn.setImageDrawable(imgV4.getDrawable());
random = list.get(rand.nextInt());
} else {
Toast.makeText(this,"Chose other section!", Toast.LENGTH_SHORT).show();
}
}
}
I hope this will help to find what I'm doing wrong. Let me know if more info needed.
EDIT SOLUTION:
I found a solution. I used getTag() & setTag() and global variables.
At first i created a new class MyApplication which extends Application to declare 3 global variables:
public class MyApplication extends Application {
Integer defaultTag = R.drawable.white;
Integer resImgV3;
public Integer getResImgV3() {
return resImgV3;
}
public void setResImgV3(int resImgV3) {
this.resImgV3 = resImgV3;
}
Integer resImgV4;
public Integer getResImgV4() {
return resImgV4;
}
public void setResImgV4(int resImgV4) {
this.resImgV4 = resImgV4;
}
}
Then i set an onClickListener to all ImageButtons that changes the Image of the ImageViews (imgV3 and imgV4) by getting the Tags of the ImageButtons which are set to the id of the drawable in each ImageButton and it also sets the value of the global variables resImgV3 and resImgV4 to the id of the now set drawables of imgV3 and imgV4:
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
imgV3.setTag(R.drawable.kid1);
imgV3.setImageResource(R.drawable.kid1);
((MyApplication)this.getApplication()).setResImgV3((Integer) imgV3.getTag());
View.OnClickListener onClickButton = view -> {
imgV3.setTag((Integer) view.getTag());
imgV3.setImageResource((Integer) view.getTag());
((MyApplication) this.getApplication()).setResImgV3((Integer) imgV3.getTag());
};
ImageView imgV4 = (ImageView) findViewById(R.id.imageView4);
imgV4.setTag(R.drawable.kid2);
imgV4.setImageResource(R.drawable.kid2);
((MyApplication)this.getApplication()).setResImgV4((Integer) imgV4.getTag());
View.OnClickListener onClickButton1 = view -> {
imgV4.setTag((Integer) view.getTag());
imgV4.setImageResource((Integer) view.getTag());
((MyApplication) this.getApplication()).setResImgV4((Integer) imgV4.getTag());
};
ImageButton imageButton1 = (ImageButton) findViewById(R.id.plOneChar1);
imageButton1.setImageResource(R.drawable.kid1);
imageButton1.setTag(R.drawable.kid1);
imageButton1.setOnClickListener(onClickButton);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.plOneChar2);
imageButton2.setImageResource(R.drawable.kid2);
imageButton2.setTag(R.drawable.kid2);
imageButton2.setOnClickListener(onClickButton); //and 6 more ImageButtons
And finally in the 2nd activity, where i needed the images of imageView3 and imageView4, i can easily get the resId of each drawable by just accessing the MyApplication-class like following:
ImageView imgV5 = (ImageView) findViewById(R.id.imageView5);
imgV5.setImageResource(((MyApplication) this.getApplication()).getResImgV3());
imgV5.setTag(((MyApplication) this.getApplication()).getResImgV3());
ImageView imgV6 = (ImageView) findViewById(R.id.imageView6);
imgV6.setImageResource(((MyApplication) this.getApplication()).getResImgV4());
imgV6.setTag(((MyApplication) this.getApplication()).getResImgV4());
I hope this will help other ones with the same problem in the future.
Try this:
ImageButton Demo_button = (ImageButton)findViewById(R.id.firstimage);
// when you click this demo button
Demo_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.secondimage);
}
}

Rotating spinner arrow in android by 180

Issue:
I needed to implement Spinner for RTL languages such that text is aligned to right and down arrow towards left.I am able to acheive this alignment but the arrow is pointing upwards.
I do not want to use setRotation since it's available only from API level 11.
PFB the code I am using:
//Creating spinner
Spinner s=new RTLSpinner(this);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataAdapter);
//Fetching the background drawable of Spinner
NinePatchDrawable drawable=(NinePatchDrawable)(((StateListDrawable)s.getBackground()).getCurrent());
//Converting it into Bitmap
Bitmap bmpOriginal = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bmpOriginal);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
//Rotating the image
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(180, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
s.setBackgroundDrawable(new BitmapDrawable(bmResult));
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int textViewResourceId,
List<String> textArray) {
super(context, textViewResourceId, textArray);
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView viewObj = (TextView) super.getView(position, convertView,
parent);
TextView optionView = (TextView) viewObj
.findViewById(android.R.id.text1);
optionView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
return viewObj;
}
}
Thanks in Advance!

JavaFx ProgressBar Bind SimpleDoubleProperty

I am using a progressbar in a tableview, where I'm using bind on his property so that when the value is changed, progressbar is started:
private SimpleDoubleProperty progressBar;
public Tabela(Double progressBar) {
this.progressBar = new SimpleDoubleProperty(progressBar);
}
public Double getProgressBar() {
return progressBar.get();
}
public DoubleProperty getProgressBarProperty() {
return progressBar;
}
public void setProgressBar(Double progressBar) {
this.progressBar.set(progressBar);
}
public void setProgressBar(SimpleDoubleProperty progressBar) {
this.progressBar = progressBar;
}
And in my Hbox am using progressbar as follows:
final ProgressBar progress = new ProgressBar();
progress.setMinWidth(324.0);
progress.progressProperty().bind(tabela.getProgressBarProperty());
So I'm using a so that after a certain time the value is changed, ie I will control the progressbar. The value is changed, but in my tableview nothing happens, but when I change the column position to another, the progressbar is running.
The same happens with "label", i changed for "text" and it work, but if the progressbar I have to use.
have a way to force the 'tableview' refresh?
You need to follow the JavaFX naming standard to get TableViews and other widgets to properly refresh when an observable object changes. For instance, you should rename getProgressBarProperty() to progressBarProperty().
See: How to update TableView Row using javaFx
There would be something wrong in this source?
class table
...
public Tabela(String nome, Double progressBar, String etapa) {
this.nome = nome;
this.progressBar = new SimpleDoubleProperty(progressBar);
this.etapa = new SimpleStringProperty(etapa);
}
....
Add new line.
private void preencheListaNomeTabelas() {
getLista().add(new Tabela("Test", 0.0, "Test Text"));
Add hbox in table.
columTabela.setCellValueFactory(new PropertyValueFactory<Tabela, String>("nome"));
columSituacao.setCellFactory(new Callback<TableColumn<Tabela, Double>, TableCell<Tabela, Double>>() {
public TableCell<Tabela, Double> call(TableColumn<Tabela, Double> p) {
final HBox box = new HBox();
box.setPrefHeight(25.0);
final ProgressBar progressBar = new ProgressBar(-1);
final Text text = new Text();
**text.textProperty().bind(..); //I would use here the**
BorderPane border = new BorderPane();
border.setTop(text);
border.setBottom(progressBar);
BorderPane.setAlignment(text, Pos.CENTER);
box.getChildren().add(border);
final TableCell cell = new TableCell<Tabela, Double>() {
#Override
protected void updateItem(Double t, boolean bln) {
super.updateItem(t, bln);
if (bln) {
setText(null);
setGraphic(null);
} else {
progressBar.setProgress(t);
progressBar.prefWidthProperty().bind(this.widthProperty());
setGraphic(box);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
};
cell.setAlignment(Pos.CENTER);
return cell;
}
});
columSituacao.setCellValueFactory(new PropertyValueFactory<Tabela, Double>("progress"));
columSituacao.setText("Progresso");
tableView.getItems().addAll(lista);
tableView.getSelectionModel().selectFirst();
task
Task t = new Task() {
#Override
protected Object call() throws Exception {
Thread.sleep(10000);
getLista().get(0).setEtapa("Lendo PostgreSQL");
getLista().get(0).setProgressBar(-1.0);
return null;
}
};
new Thread(t).start();

Close tab after dialog confirmation

I created this example of tabs which I want to close after I display confirm dialog and I click Yes button.
public static Tab testconfirmTabClose(Tab tab)
{
tab.setOnClosed(new EventHandler<Event>()
{
#Override
public void handle(Event t)
{
t.consume();
// Dialog Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
Button btnYes = new Button("Yes");
btnYes.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
dialog.close();
}
});
Button btnNo = new Button("No");
btnNo.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
dialog.close();
}
});
// Layout for the Button
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(btnYes);
hbox.getChildren().add(btnNo);
// Layout for the Label and hBox
VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
// Text
Text tc = new Text();
tc.setText("Do you want to quit?");
// Layout for the Button
HBox thbox = new HBox();
thbox.setSpacing(10);
thbox.setPadding(new Insets(20, 20, 20, 90)); // Place the dialog text right
thbox.setAlignment(Pos.CENTER_LEFT);
thbox.getChildren().add(tc);
BorderPane bp = new BorderPane();
bp.setPadding(new Insets(15, 15, 10, 15));
bp.setTop(null);
bp.setLeft(vbox);
bp.setCenter(thbox);
bp.setRight(null);
bp.setBottom(hbox);
Scene scene = new Scene(bp, 500, 140);
dialog.setScene(scene);
dialog.show();
}
});
return tab;
}
I have this issue: When I click on the tab to close it the tab is closed and the confirm dialog is displayed. I cannot "freeze" the tab for the user response. Can you tell me how I can fix this problem?
I'm afraid there is no clean way to do this in JavaFX 2.2. JavaFX 8 will (probably) offer a method called Tab#setOnCloseRequest(...) that will do what you want. For 2.2, the only way I see right now is pulling the source from OpenJDK and creating your own adapted TabPane implementation - sorry :-/.

How to create modal dialog with image

I have this very simple modal dialog:
public class DialogPanels
{
public void initClosemainAppDialog(final Stage primaryStage)
{
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent event)
{
event.consume(); // Do nothing on close request
// Dialog Stage init
final Stage dialog = new Stage();
// If you want to freeze the background during dialog appearence set Modality.APPLICATION_MODAL
// or to allow clicking on the mainstage components set Modality.NONE
// and set dialog.showAndWait();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(primaryStage);
// Frage - Label
Label label = new Label("Exit from the program");
// Button "Yes"
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
//primaryStage.close();
//dialog.close();
//Platform.exit();
System.exit(0);
}
});
// Button "No"
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
primaryStage.show();
dialog.close();
}
});
// Layout for the Button
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(okBtn);
hbox.getChildren().add(cancelBtn);
// Layout for the Label and hBox
VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.getChildren().add(label);
vbox.getChildren().add(hbox);
// Stage
Scene scene = new Scene(vbox, 450, 150, Color.WHITESMOKE);
dialog.setScene(scene);
dialog.show();
}
});
}
}
I want to add image and to make it to look like this:
But I admin that it's too complex for my short knowledge to get the appropriate result. Can you show me how I can split the dialog, add second background and make my code to look the same as this example please?
Have a look at the ControlsFX project, they have some sophisticated dialogs and it's open source, so you can look up how it's done. For example, your dialog looks like this confirmation dialog of ControlsFX:
There is also support for custom dialogs.
€dit:
With the "show Masthead" option enabled it actually looks exactly like it:

Resources