7

So this is my first app and I am trying to code and need some help with buttons. After searching for a answer I just couldn't find one that I understood. I want to be able to make different pages for the app and make imagebuttons that link to these pages. This is the very basic code I have at the minute for my button. Please try to explain where to put the code etc. Thanks in advance.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:background="@drawable/home_button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:nestedScrollingEnabled="true" />
</RelativeLayout>
Shaishav Jogani
  • 1,963
  • 3
  • 20
  • 32
Noobcoder
  • 91
  • 1
  • 1
  • 3

2 Answers2

16

Since this is your first app, let's start it simple with using only Activities.

You start with a MainActivity, which shall contain your ImageButtons. By clicking on one of these buttons, you will be directed to another activity. If you press the back button, you will arrive back at your MainActivity.

I shall demonstrate some code which shows you how to navigate from one activity to another one. First add the two activities so your AndroidManifest.xml will look something like this:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".SecondActivity"
    android:label="@string/title_activity_second_activitity" >
</activity>

If you're using AndroidStudio, it will do this for you when you create a new activity.

Your MainActivity.java will look something like this:

public class MainActivity extends Activity {

    //Define your views
    private ImageButton imageButton;

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

        //Find your views
        imageButton = (ImageButton) findViewById(R.id.image_button);

        //Assign a listener to your button
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Start your second activity
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

Repeat these steps for every Activity you want to add to your application. For more information, you will find the Android Docs an usefull source. Please check this link out as a start.

Good luck!

Hookah_Smoka
  • 334
  • 1
  • 2
  • 1
    Hey thanks for taking it slow with me but my java doesn't look like this. In fact it looks completely different. I've tried copying and pasting your java code in mine but it came up with a lot of red words. Feel like im making a stupid mistake. Please help – Noobcoder Feb 10 '15 at 18:54
  • 2
    Normally you have to import the different Android classes inside your Activity so the compiler knows where to find them. If you're using Android Studio, then a text balloon will pop up to let you import these classes. Otherwhise, you can do it manually trough the shortcut Alt + Enter when your cursor is on the red word. – Hookah_Smoka Feb 10 '15 at 23:24
0

I dont think its a question to be questioned ! Though,make your desired button in your main .xml file and the using java access the button and apply the task which you want to perform from that button .. You use this in .xml to make a button

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
 />

Here's java code for accessing this button

private Button button;

public void addListenerOnButton() {

    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
       @Override public void onClick(View view) {

         //do what you want here              
        }
    });
}
Matt Clark
  • 24,947
  • 16
  • 62
  • 114
  • Hi,sorry i am a beginner and i keep running into problems. When i typed this in the java code i got ALOT of red words etc, dont know if im making a stupid mistake or what but was hoping if you could help me out. – Noobcoder Feb 10 '15 at 18:39
  • http://www.javatpoint.com/android-working-with-button go through this link it will help you –  Feb 10 '15 at 18:40