Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

ImageButton doesnt work Android Studio Java

ImageButton doesnt work when I change the visibility to visible. The only visible thing is something like a small shadow (which works like a imagebutton) but the image doesnt show up. All images are added to the project.

main activity:

    Button search;
    EditText searchLocation;

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

        search = findViewById(R.id.buttonSearchAll);

        searchLocation = findViewById(R.id.editLocation);
        

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = searchLocation.getText().toString().trim();
                Intent intent = new Intent(getApplicationContext(), MainActivity2.class);
                intent.putExtra("message", str);
                startActivity(intent);

            }
        });
    }

main activity2:

    TextView noResults, searched;
    ImageButton btHotel1, btHotel2;


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

        btHotel1 = findViewById(R.id.btHotel1);
        btHotel1.setVisibility(View.GONE);
        btHotel2 = findViewById(R.id.btHotel2);
        btHotel2.setVisibility(View.GONE);
        noResults = findViewById(R.id.tvNoResults);


        btHotel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity2.this, MainActivity3.class));
            }
        });
        btHotel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity2.this, MainActivity3.class));
            }
        });


        Intent intent = getIntent();
        String str = intent.getStringExtra("message");
        searched.setText(str);

        if (str.equalsIgnoreCase("vienna")){
            btHotel2.setVisibility(View.VISIBLE); //but there's only a small shadow
            return;
        }


        noResults.setVisibility(View.VISIBLE);


    }
}

Im not sure why it doesnt work.

question from:https://stackoverflow.com/questions/65852446/imagebutton-doesnt-work-android-studio-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You call

btHotel1.setVisibility(View.GONE);

it means view is invisible, and it doesn't take any space for layout purposes

Try to use View.Invisible instead of View.GONE

If you want to make your button visible, use View.VISIBLE!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...