I have a question for Android Adding ImageView from Fragment

Asked 2 years ago, Updated 2 years ago, 110 views

I'm using Android Studio. I used tabbed activity. You are about to put an Imageview in the Fragment. I've already written the URL with Button. I want to add Imageview to it. I used rootView. The image is in .XML and is also stored under the name Suttle in drawable. The code is public class tab1sbtosc extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1sbtosc, container, false);

    Button button = (Button) rootView.findViewById(R.id.button);

    ImageView ImageView = (ImageView) getView().findViewById(R.id.imageView);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://nsu.ac.kr/?m1=page%25&menu_id=59%25"));
            startActivity(browserIntent);
        }
    });
        return rootView;
}

It looks like this. I did getView() and I think I should use ImageView, so how should I write the coding here? I'll be waiting for the reply. Thank you. Just in case, I will also write down the ImageView code specified in the XML.!!!

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/suttle"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/button" />

android fragment imageview tabbedactivity

2022-09-21 16:36

1 Answers

You've already obtained a View object, but I'm not sure why you're doing getView().

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1sbtosc, container, false);

    Button button = (Button) rootView.findViewById(R.id.button);
    ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);

    // I'm going to...
    return rootView;
}

Do it like this.

If I don't understand the question, please reply.


2022-09-21 16:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.