Image Button to New Activity

  • Replies:9
  • OpenNot stickiedUnanswered
  • Forum posts: 5

Apr 12, 2017 8:26:33 AM via Website

Hello All,

Please help me with these codes, same codes working on my other projects but here Image Button doesn't click and not starting other activity. Please find the codes:

import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

ImageButton myImageButton;

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

    myImageButton = (ImageButton) findViewById(R.id.imageButton);

    myImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent beach = new  Intent(MainActivity.this, beachActivity.class);
            startActivity(beach);
        }
    });


    // Font Change Ferries in Andaman
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
    TextView myTextview = (TextView)findViewById(R.id.textView2);
    myTextview.setTypeface(myTypeface);

    // Font Change Watersports in Andaman
    Typeface myTypefaceWS = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
    TextView myTextviewWS = (TextView)findViewById(R.id.textView3);
    myTextviewWS.setTypeface(myTypefaceWS);
  • Forum posts: 54

Apr 12, 2017 8:11:10 PM via Website

Are you sure your layout activity_main.xml has ImageButton with an assigned ID of imageButton? Maybe logcat gives any errors? If yes, please post it.

  • Forum posts: 5

Apr 13, 2017 6:01:37 AM via Website

Hello Sir,

Thank you for your reply. Yes, everything is fine in xml + i am building App with No Errors. Only error (warning) i get when i create new activity which says I have changed dimensions in manifest as 0dp so the new activity may not work as expected or something like that.

I have added Horizontal Scroll View too in xml. I hope that is not the problem.

Please find the xml codes below. If you don't mind i can post full xml code of page. As i have used lots of different layouts in it.

xmlns:app="ht tp://schemas.android.c om/apk/res-auto"
xmlns:tools="ht tp://sc hemas.android.c o m/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="co m. Andaman TravelGuide .ravi. AndamanTravelGuide .MainActivity">

(Gave Space in some places as it was not posting saying it as spam)

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/logo"
    android:id="@+id/imageView"
    tools:ignore="ContentDescription"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="7dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ImageButton
    android:layout_width="175dp"
    android:layout_height="200dp"
    app:srcCompat="@drawable/beaches"
    android:layout_below="@+id/imageView"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="7dp"
    android:id="@+id/imageButton"
    tools:ignore="ContentDescription,RtlHardcoded" />

Please help me out.

— modified on Apr 13, 2017 6:03:14 AM

  • Forum posts: 54

Apr 13, 2017 7:55:26 AM via Website

maybe try to replace your onClickListener with this:
public void openActivity(View view) {
Intent beach = new Intent(MainActivity.this, beachActivity.class);
startActivity(beach);
}

and add this attribute to your ImageButton in xml file:
ImageButton
//...other attributes...
android:onClick="openActivity"
/>

— modified on Apr 13, 2017 7:56:23 AM

  • Forum posts: 5

Apr 13, 2017 10:59:42 AM via Website

I tried but not working. Can't see any errors.

  • Forum posts: 54

Apr 13, 2017 2:54:58 PM via Website

It's weird. Maybe you could send me the project and I will take look? (of course, if it's not a top secret). You can uppload it to Google Drive or Dropbox and give me the link.

  • Forum posts: 5

Apr 14, 2017 12:19:07 AM via Website

Thank you for being so helpful. Please download the package and see what i did wrong (I am not a developer i learned it myself). You are free to tweak anything if you find it wrong.

http s://drive.google.c om/open?id=0B707DSdZUruAc3R2cGpjZUF5d2M

(Adding spaces in between the link, as it is showing as spam)

— modified on Apr 14, 2017 12:22:47 AM

  • Forum posts: 11

Apr 14, 2017 2:59:53 AM via Website

Nice , thanks info

  • Forum posts: 54

Apr 14, 2017 1:48:41 PM via Website

thank you for the project. It would be better if you send it zipped next time. Anyway, I managed to resolve your issue and now Image Button is clickable. The problem was with your Horizontal Scroll View, it covered all the screen so it was impossible to click on a button. This is working xml file of Main Activity:

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.AndamanTravelGuide.ravi.AndamanTravelGuide.MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="7dp"
            app:srcCompat="@drawable/logo"
            tools:ignore="ContentDescription" />
        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="175dp"
            android:layout_height="200dp"
            android:layout_below="@+id/imageView"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="8dp"
            app:srcCompat="@drawable/beaches"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="175dp"
            android:layout_height="200dp"
            android:layout_alignTop="@+id/imageButton"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="7dp"
            android:layout_toEndOf="@+id/imageButton"
            android:layout_toRightOf="@+id/imageButton"
            app:srcCompat="@drawable/sight"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="175dp"
            android:layout_height="200dp"
            android:layout_alignLeft="@+id/imageButton"
            android:layout_alignStart="@+id/imageButton"
            android:layout_below="@+id/imageButton"
            android:layout_marginTop="7dp"
            app:srcCompat="@drawable/events"
            tools:ignore="ContentDescription" />
        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="175dp"
            android:layout_height="200dp"
            android:layout_alignLeft="@+id/imageButton2"
            android:layout_alignStart="@+id/imageButton2"
            android:layout_alignTop="@+id/imageButton3"
            android:layout_marginRight="7dp"
            app:srcCompat="@drawable/museums"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton3"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="10dp"
            android:text="@string/ferries_in_andaman"
            android:textSize="16dp"
            tools:ignore="RtlHardcoded,SpUsage" />
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbarSize="0dp"
            android:layout_alignTop="@+id/textView2">
            <LinearLayout
                android:layout_width="1028dp"
                android:layout_height="145dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="7dp"
                android:layout_marginTop="515dp"
                android:orientation="horizontal"
                tools:ignore="RtlHardcoded">
                <ImageButton
                    android:id="@+id/makruzzButton"
                    android:layout_width="200dp"
                    android:layout_height="145dp"
                    android:layout_marginLeft="0dp"
                    app:srcCompat="@drawable/makk"
                    tools:ignore="ContentDescription,RtlHardcoded" />
                <ImageButton
                    android:id="@+id/greenoceanButton"
                    android:layout_width="200dp"
                    android:layout_height="145dp"
                    android:layout_marginLeft="7dp"
                    app:srcCompat="@drawable/green"
                    tools:ignore="ContentDescription" />
                <ImageButton
                    android:id="@+id/coastalButton"
                    android:layout_width="200dp"
                    android:layout_height="145dp"
                    android:layout_marginLeft="7dp"
                    app:srcCompat="@drawable/coastal"
                    tools:ignore="ContentDescription" />
                <ImageButton
                    android:id="@+id/govtferryButton"
                    android:layout_width="200dp"
                    android:layout_height="145dp"
                    android:layout_marginLeft="7dp"
                    app:srcCompat="@drawable/govt"
                    tools:ignore="ContentDescription,RtlHardcoded" />
                <ImageButton
                    android:id="@+id/makruzzgoldButton"
                    android:layout_width="200dp"
                    android:layout_height="145dp"
                    android:layout_marginLeft="7dp"
                    android:layout_marginRight="7dp"
                    app:srcCompat="@drawable/gold"
                    tools:ignore="ContentDescription,InefficientWeight" />
            </LinearLayout>
        </HorizontalScrollView>
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="665dp"
            android:text="@string/water_sports_and_other_activities"
            android:textSize="16dp"
            tools:ignore="RtlHardcoded,SpUsage" />
        <ImageButton
            android:id="@+id/watersportsButton"
            android:layout_width="175dp"
            android:layout_height="175dp"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="695dp"
            app:srcCompat="@drawable/scuba"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <ImageButton
            android:id="@+id/activitiesButton"
            android:layout_width="175dp"
            android:layout_height="175dp"
            android:layout_alignTop="@+id/watersportsButton"
            android:layout_marginLeft="189dp"
            android:layout_marginRight="7dp"
            app:srcCompat="@drawable/activities"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <TextView
            android:id="@+id/watersportsTextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="871dp"
            android:text="@string/watersports"
            android:textSize="14dp"
            tools:ignore="RtlHardcoded,SpUsage" />
        <TextView
            android:id="@+id/activitiesTextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="189dp"
            android:layout_marginTop="871dp"
            android:text="@string/activities"
            android:textSize="14dp"
            tools:ignore="RtlHardcoded,SpUsage" />
        <TextView
            android:id="@+id/HRtextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="895dp"
            android:text="@string/hotels_and_restaurants"
            android:textSize="16dp"
            tools:ignore="RtlHardcoded,SpUsage" />
        <ImageButton
            android:id="@+id/hotelButton"
            android:layout_width="175dp"
            android:layout_height="175dp"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="925dp"
            app:srcCompat="@drawable/hotels"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <ImageButton
            android:id="@+id/restaurantButton"
            android:layout_width="175dp"
            android:layout_height="175dp"
            android:layout_marginLeft="189dp"
            android:layout_marginRight="7dp"
            android:layout_marginTop="925dp"
            app:srcCompat="@drawable/restaurant"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <TextView
            android:id="@+id/restaurantsTextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="7dp"
            android:layout_marginLeft="189dp"
            android:layout_marginTop="1100dp"
            android:text="@string/restaurants"
            android:textSize="14dp"
            tools:ignore="RtlHardcoded,SpUsage" />
        <TextView
            android:id="@+id/hotelsTextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="7dp"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="1100dp"
            android:text="@string/hotels"
            android:textSize="14dp"
            tools:ignore="RtlHardcoded,SpUsage" />
        <ImageButton
            android:id="@+id/generalButton"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="1125dp"
            app:srcCompat="@drawable/generall"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <ImageButton
            android:id="@+id/emergencyButton"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginLeft="96dp"
            android:layout_marginTop="1125dp"
            app:srcCompat="@drawable/emergencyy"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <ImageButton
            android:id="@+id/bankButton"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginLeft="195dp"
            android:layout_marginTop="1125dp"
            app:srcCompat="@drawable/bank"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <ImageButton
            android:id="@+id/aboutButton"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginLeft="280dp"
            android:layout_marginTop="1125dp"
            app:srcCompat="@drawable/about"
            tools:ignore="ContentDescription,RtlHardcoded" />
        <TextView
            android:id="@+id/InformationtextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="1205dp"
            android:text="@string/information"
            tools:ignore="RtlHardcoded" />
        <TextView
            android:id="@+id/EmergencytextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="105dp"
            android:layout_marginTop="1205dp"
            android:text="@string/emergency"
            tools:ignore="RtlHardcoded" />
        <TextView
            android:id="@+id/banktextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="220dp"
            android:layout_marginTop="1205dp"
            android:text="@string/banks"
            tools:ignore="RtlHardcoded" />
        <TextView
            android:id="@+id/abouttextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="295dp"
            android:layout_marginRight="7dp"
            android:layout_marginTop="1205dp"
            android:text="@string/about_us"
            tools:ignore="RtlHardcoded" />
    </RelativeLayout>
</ScrollView

You can do with you want when the button is clicked inside this method:

ImageButton myImageButton = (ImageButton) findViewById(R.id.imageButton);

        myImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_LONG).show();

                Intent beach = new  Intent(MainActivity.this, beachActivity.class);
                startActivity(beach);
            }
        });

If you have any questions, feel free to ask!

— modified on Apr 14, 2017 1:53:06 PM

  • Forum posts: 5

Apr 16, 2017 12:34:41 PM via Website

Thank you very much. It is working now. Will keep in mind to zip it next time.

Do you have any suggestion regarding the App. I am not a developer, i just learnt everything from Youtube videos. Would appreciate your expert advice (if any).

Thanks again.