-1

Ok y'all, I'm trying to learn here. I just learned how to create a basic appWidget and it works so far. Now try as I might, I do not understand how to add an image button to my appWidget. I mean it's been physically added to the xml file named simple_app_widget.xml. What can't figure out is how do I add an onClick event to the SimpleAppWidget.java file? I have tried it like a normal one click event, but AIDE tells me that it doesn't recognize the info being input.

Please don't just do my work for me. I want to learn, but my search skills are lacking and what I do find is irrelevant or not at me skill level.

1 Answers1

0

as you said... to add an image button first must add the image in your drawable folder ( search it in your proyect tree), then go to the layout where want to use it and write your code like this (note : in, android:background =" " must enter the same name of your image copied in drawable folder):

<ImageButton
    android:id="@+id/arrow"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginBottom="4dp"
    android:background="@drawable/arrow"
    android:contentDescription="@string/test"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.902"
    app:layout_constraintStart_toStartOf="parent" />

After it, go to your Activity.class and type your code:

public class MainActivity extends AppCompatActivity {

ImageButton test;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    test = findViewById(R.id.test);

    test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Here should put what you want to show or where you want to go after click the button.
            
            //For example if you want to go to the next activity use Intents like this:
            
            Intent intent = new Intent(this, MainActivity1.class);
            startActivity(intent);
            finish();
        }
    });
}

}

Rixos
  • 23
  • 5