Question

I am a beginner at android development. I am trying to make toasts work but I can't find anything wrong with the code.

This is the xml code.

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/topbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dravaka_dark" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#3333" >
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_marginTop="20dp"
    android:gravity="left"
    android:orientation="vertical"
    android:paddingTop="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/about_icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:layout_marginLeft="20dp"
            android:scaleType="fitXY"
            android:src="@drawable/about_icon" />

        <TextView
            android:id="@+id/about_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="20dp"
            android:clickable="true"
            android:text="@string/about_title"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/schedule_icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:scaleType="fitXY"
            android:src="@drawable/schedule_icon" />

        <TextView
            android:id="@+id/schedule_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="20dp"
            android:clickable="true"
            android:text="@string/schedule_title"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/songs_icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:scaleType="fitXY"
            android:src="@drawable/songs_icon"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/songs_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="20dp"
            android:clickable="true"
            android:text="@string/songs_title"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/contact_icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:scaleType="fitXY"
            android:src="@drawable/contact_icon" />

        <TextView
            android:id="@+id/contact_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="20dp"
            android:clickable="true"
            android:text="@string/contact_title"
            android:textSize="25sp" />
    </LinearLayout>
</LinearLayout>

</LinearLayout>

this is the java code

 public class MainActivity extends Activity implements OnClickListener {

    TextView aboutBtn;
    TextView performancesBtn;
    TextView songsBtn;
    TextView contactBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        aboutBtn = (TextView) findViewById(R.id.about_tab);
        performancesBtn = (TextView) findViewById(R.id.schedule_tab);
        songsBtn = (TextView) findViewById(R.id.songs_tab);
        contactBtn = (TextView) findViewById(R.id.contact_tab);

        aboutBtn.setOnClickListener(this);
        performancesBtn.setOnClickListener(this);
        songsBtn.setOnClickListener(this);
        contactBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.about_icon:
            Toast.makeText(MainActivity.this, "about clicked",
                    Toast.LENGTH_LONG).show();
            break;
        case R.id.schedule_icon:
            Toast.makeText(getApplicationContext(), "schedule clicked",
                    Toast.LENGTH_LONG).show();
            break;

        case R.id.songs_icon:
            Toast.makeText(getApplicationContext(), "songs clicked",
                    Toast.LENGTH_LONG).show();
            break;

        case R.id.contact_icon:
            Toast.makeText(getApplicationContext(), "contact clicked",
                    Toast.LENGTH_LONG).show();
            break;

        }

    }
    }

I am just a beginner. Please do not criticize me for asking a dumb question. I know this might have the easiest solution.

In the toasts I tried 'MainActivity.this' to check if that was the issue. But even then the emulator did not show toasts.

Was it helpful?

Solution

You set the OnClickListeners on the TextViews but switch on the ImageView ids. The ImageViews will never receive clicks and you have no code to handle the TextView clicks.

OTHER TIPS

You should replace this

aboutBtn = (TextView)findViewById(R.id.about_tab);
performancesBtn = (TextView)findViewById(R.id.schedule_tab);
songsBtn = (TextView)findViewById(R.id.songs_tab);
contactBtn = (TextView)findViewById(R.id.contact_tab);

With

ImageView aboutBtn = (ImageView)findViewById(R.id.about_icon);
ImageView performancesBtn = (ImageView)findViewById(R.id.schedule_icon);
ImageView songsBtn = (ImageView)findViewById(R.id.songs_icon);
contactBtn = (ImageView ) findViewById(R.id.contact_icon);

You're passing wrong Ids into your Switch Case. If you want to perform onclick() into ImageView then pass ImageView ids. here you're passing textView Ids and If you want to Perform onClick() txtView then should change your Switch Case with below:

   switch (v.getId()) {
  case R.id.about_tab:
    Toast.makeText(MainActivity.this, "about clicked",
            Toast.LENGTH_LONG).show();
    break;
  case R.id.schedule_tab:
    Toast.makeText(getApplicationContext(), "schedule clicked",
            Toast.LENGTH_LONG).show();
    break;

  case R.id.songs_tab:
    Toast.makeText(getApplicationContext(), "songs clicked",
            Toast.LENGTH_LONG).show();
    break;

  case R.id.contact_tab:
    Toast.makeText(getApplicationContext(), "contact clicked",
            Toast.LENGTH_LONG).show();
    break;

 }

try this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    aboutBtn = (TextView) findViewById(R.id.about_tab);
    performancesBtn = (TextView) findViewById(R.id.schedule_tab);
    songsBtn = (TextView) findViewById(R.id.songs_tab);
    contactBtn = (TextView) findViewById(R.id.contact_tab);

    aboutBtn.setOnClickListener(this);
    performancesBtn.setOnClickListener(this);
    songsBtn.setOnClickListener(this);
    contactBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.about_tab:
        Toast.makeText(MainActivity.this, "about clicked",
                Toast.LENGTH_LONG).show();
        break;
    case R.id.schedule_tab:
        Toast.makeText(getApplicationContext(), "schedule clicked",
                Toast.LENGTH_LONG).show();
        break;

    case R.id.songs_tab:
        Toast.makeText(getApplicationContext(), "songs clicked",
                Toast.LENGTH_LONG).show();
        break;

    case R.id.contact_tab:
        Toast.makeText(getApplicationContext(), "contact clicked",
                Toast.LENGTH_LONG).show();
        break;

    }

}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top