Display various images with the ability to draw without switching actions

Buttons on another Layout and images that will be open on buttons click will be open in another layout

As shown in the image, each button has a different image and I don't want to do different actions for view1, view2, view3 and view4 to display the images:

enter image description here

Question:

I am new to android app development. I want to display different images with the ability to draw on different button presses without switching activity for each button! Please help me .. Thanks in advance.

+3


source to share


2 answers


In your activity_main.xml file (or whatever you called it), just enter the id for the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainView" <!--this id-->
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

      

then create a field in your activity

Layout layout;

      

then inject onCreate ():

layout = (Layout) findViewById (R.id.mainView);

      

then in your onClick of your buttons you can change the background using:

layout.setBackground(Drawable drawable);

      



instead of "Layout" you can use your own layout. In my case, this is "RelativeLayout"

EDIT: as per your last rule, if you want to hide buttons, show images. You can use snippets:

http://developer.android.com/guide/components/fragments.html

or you can hide buttons in your onClicks using

yourButton.setVisibility(View.GONE);

      

and then override the onBackPressed () method in your activity like:

@Override
public void onBackPressed()
{
     yourButton.setVisibility(View.VISIBLE);//for each button
     super.onBackPressed();
}

      

Of course, you need to set margins for your buttons and initiate them in onCreate (), just like in Layout above. In both cases, you will see the buttons again if you click Back.

0


source


You can use intent, setOnClickListener for your button:

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
        case id == R.id.buttonView1:
            Intent intent1 = new Intent(this, ShowImage.class);
            intent.putExtra("image", R.drawable.image1);
            startActivities(intent);
        case id == R.id.buttonView2:
            Intent intent2 = new Intent(this, ShowImage.class);
            intent2.putExtra("image", R.drawable.image2);
            startActivities(intent2);
        }
    }

      

And in the ShowImage action, get the image id resource:



int image = getIntent().getIntExtra("image", R.drawable.default_image);
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageResource(image);

      

In the ShowImage layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

      

0


source







All Articles