android buttons and image in the same page - image

I have to create a button to show an image. I created it using two activity, but I want that the button and the image view stay in the same page. So, I think I have to create only one activity, right? can you explain hot to do this? this is my code:
ACTIVITY ONE:
public class HomeWork extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work);
Button getResultButton = (Button) findViewById(R.id.btn1);
getResultButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent resultIntent =new Intent(HomeWork.this,HomeWork2.class);
startActivity(resultIntent);
}
});
}
SECOND ACTIVITY:
public class HomeWork2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work2);
ImageView img=(ImageView)findViewById(R.id.imageView);
img.setBackgroundResource(R.drawable.apple);
}
the apk works correctly, the only problem is that I want button and imageview in the same image

Create a layout which contains both the Button and the ImageView.
Then in the Button's click listener, use
ImageView img=(ImageView)findViewById(R.id.imageView);
img.setBackgroundResource(R.drawable.apple);
instead of
Intent resultIntent =new Intent(HomeWork.this,HomeWork2.class);
startActivity(resultIntent);
Also remove (the now unused) HomeWork2 from your manifest.
[EDIT]
You may add more buttons, to change the contents of the ImageView.
You only have to put in their click listeners:
img.setBackgroundResource(R.drawable.orange);
or
img.setBackgroundResource(R.drawable.pineapple);
or ...

Related

JavaFX: How to remove Pane?

I wasn't really sure how to title this. I have a JavaFX application in which I have two pages (fxml's) which are different sizes. The first is 400x600; the second is maximized. I have a return button which sends the user back to the first fxml. I successfully set it so it goes back to the original size. However, when I go 1->2->1->2 the screen does not maximize. What I imagine is the issue is that it does not rerun the initialize() method the second time it creates this page. Another possibility is that it is caused by the new Runnable() I made, which was necessary to get the stage object.
The second controller:
#Override
public void initialize(URL location, ResourceBundle resources) {
Platform.runLater(new Runnable() {
public void run() {
// Display
stage = (Stage) menuPane.getScene().getWindow();
stage.setResizable(true);
stage.setMaximized(true);
stage.setResizable(false);
}
});
}
Thanks.

How to tell parent to pop current modal and redirect to NavigationPage

I have a Xamarin application, where my MainPage is a navigationpage (contentpage). In this navigationpage I open modals.
In the modals I open in my MainPage, I want to use Navigation.PushAsync but this requires the NavigationPage context which a modal page is not.
I've read that the flow would be:
Tell parent/caller to pop current visible modal
Tell parent/caller
to navigate to the page I want, using Navigation.PushAsync
However, how do you do this? How do you tell the parent to do this and actually do the navigation?
I would use event handlers to get this job done:
Here is an example code:
App.cs
public class App : Application
{
public App()
{
MainPage = new NavigationPage(new PushedNavigationPage());
}
}
PushedNavigationPage Page:
public class PushedNavigationPage: ContentPage
{
public PusehdNavigationPage()
{
}
public void DoPushModalOnButtonClick()
{
var pushedModalPage = new PushedModalPage();
pushedModalPage.DoPush += HandleDoPush;
Navigation.PushModalAsync(pushedModalPage);
}
private void HandleDoPush(object sender, EventArgs e)
{
Navigation.PopModalAsync();
Navigation.PushAsync(//the page you want to push)
}
}
PushedModalPage
public class PushedModalPage : ContentPage
{
public event EventHandler DoPush;
//call this on putton click or whenever you want
private void OnDoPush()
{
if (DoPush!= null)
{
DoPush(this, EventArgs.Empty);
}
}
}
I have not awaiting the async methods but it is better to do so and this is just an example, but I think you should get the idea..

how to apply animation on android activity

i am working on app in which i need some animation on activity i cannot find
any solution. please any one help me
public class MainActivity extends Activity implements AnimationListener {
TextView tv;
Animation animFadein;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
}
}
Animations can be performed through either XML or android code. In this tutorial many of animation are explained that how to do animations using XML notations for an activity.
tutorial link
and source code

About TextField with a small icon

I want to code a TextField component with icon.
So the behavior is as follow:
If the TextField contains an empty string, I use "lens.png".
Otherwise, i use "cross.png".
using the JavaFX Scene Builder, I added a TextFiled and an ImageView in the stack pane.
My code is the following:
#FXML
private TextField textSearch;
#FXML
private ImageView imageView;
final Image lensIcon = new Image("/issue/images/lens.png");
final Image crossIcon = new Image("/issue/images/cross.png");
//initialize () method
textSearch.textProperty().addListener(obs -> {
final String text = textSearch.getText();
Image icon = (text==null || text.isEmpty()) ? lensIcon : crossIcon;
imageView.setImage(icon);
imageView.setMouseTransparent(icon == lensIcon);
}
);
imageView.setOnMouseClicked(evt -> textSearch.setText(null));
my issue is the following:
How to prevent writing caracters below the icon (ImageView). the following figure illustrate my issue.
ControlsFX is an JavaFX API that supplies a ton of advanced controls UI that didn't come with JavaFX out of the box.
ControlsFX - http://fxexperience.com/controlsfx/
FontAwesomeFX supplies hundreds of icons (such as a cross in your case above)
FontAwesomeFX - https://bitbucket.org/Jerady/fontawesomefx/downloads/
Here is a demo solution to your problem after importing both these fantastic APIs
public class TextFields_Demo extends Application {
private Parent createContent() {
Pane root = new Pane();
CustomTextField customTextField = new CustomTextField();
FontAwesomeIconView icon = new FontAwesomeIconView(FontAwesomeIcon.CLOSE);
customTextField.setRight(icon);
root.getChildren().add(customTextField);
return root;
}
#Override
public void start(Stage primaryStage) {
Scene scene = new Scene(createContent());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Closing a Stage from within its controller

I need a way to close a Stage from within itself by clicking a Button.
I have a main class from which I create the main stage with a scene. I use FXML for that.
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Builder.fxml"));
stage.setTitle("Ring of Power - Builder");
stage.setScene(new Scene(root));
stage.setMinHeight(600.0);
stage.setMinWidth(800.0);
stage.setHeight(600);
stage.setWidth(800);
stage.centerOnScreen();
stage.show();
}
Now in the main window that appears I have all the control items and menus and stuff, made through FXML and appropriate control class. That's the part where I decided to include the About info in the Help menu. So I have an event going on when the menu Help - About is activated, like this:
#FXML
private void menuHelpAbout(ActionEvent event) throws IOException{
Parent root2 = FXMLLoader.load(getClass().getResource("AboutBox.fxml"));
Stage aboutBox=new Stage();
aboutBox.setScene(new Scene(root2));
aboutBox.centerOnScreen();
aboutBox.setTitle("About Box");
aboutBox.setResizable(false);
aboutBox.initModality(Modality.APPLICATION_MODAL);
aboutBox.show();
}
As seen the About Box window is created via FXML with a controller. I want to add a Button to close the new stage from within the controller.
The only way I found myself to be able to do this, was to define a
public static Stage aboutBox;
inside the Builder.java class and reference to that one from within the AboutBox.java in method that handles the action event on the closing button. But somehow it doesn't feel exactly clean and right. Is there any better way?
You can derive the stage to be closed from the event passed to the event handler.
new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent actionEvent) {
// take some action
...
// close the dialog.
Node source = (Node) actionEvent.getSource();
Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
}
In JavaFX 2.1, you have few choices. The way like in jewelsea's answer or the way what you have done already or modified version of it like
public class AboutBox extends Stage {
public AboutBox() throws Exception {
initModality(Modality.APPLICATION_MODAL);
Button btn = new Button("Close");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
close();
}
});
// Load content via
// EITHER
Parent root = FXMLLoader.load(getClass().getResource("AboutPage.fxml"));
setScene(new Scene(VBoxBuilder.create().children(root, btn).build()));
// OR
Scene aboutScene = new Scene(VBoxBuilder.create().children(new Text("About me"), btn).alignment(Pos.CENTER).padding(new Insets(10)).build());
setScene(aboutScene);
// If your about page is not so complex. no need FXML so its Controller class too.
}
}
with usage like
new AboutBox().show();
in menu item action event handler.

Resources