Reputation: 139
I first have this view in XML, in this file I need an onClick
event to execute a simple Toast, the method is called join:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hrizi.onescore.community">
<include
android:id="@+id/toolbar"
layout="@layout/apptoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="65dp"
android:background="@android:color/white"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="16dp">
<!--First name-->
<!--last name-->
<!--Date of Birth -->
<android.support.design.widget.TextInputLayout
android:id="@+id/ti_dateofbirth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/et_dateofbirth"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/edittextbackground"
android:ems="10"
android:hint="Date of birth"
android:inputType="date"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:singleLine="false"
android:textColorHint="@color/inputcolor" />
</android.support.design.widget.TextInputLayout>
<!--title-->
<android.support.design.widget.TextInputLayout
android:id="@+id/ti_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/edittextbackground"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:singleLine="false"
android:textColorHint="@color/inputcolor" />
</android.support.design.widget.TextInputLayout>
<!--University-->
<android.support.design.widget.TextInputLayout
android:id="@+id/ti_university"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/et_university"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/edittextbackground"
android:ems="10"
android:hint="University"
android:inputType="text"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:singleLine="false"
android:textColorHint="@color/inputcolor" />
</android.support.design.widget.TextInputLayout>
<!--About me-->
<android.support.design.widget.TextInputLayout
android:id="@+id/ti_aboutme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/et_aboutme"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/edittextbackground"
android:ems="10"
android:hint="About me"
android:inputType="text"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:singleLine="false"
android:textColorHint="@color/inputcolor" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btn_join"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="25dp"
android:background="@color/coloronscore"
android:text="Join us"
android:textAllCaps="false"
android:textColor="@android:color/background_light"
android:textStyle="normal"
android:onClick="join"/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_forgot_password"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:visibility="gone" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
In the other Java file I have the following code:
package com.example.hrizi.onescore;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.hrizi.onescore.model.Transcriber;
import com.example.hrizi.onescore.utils.Constants;
import rx.subscriptions.CompositeSubscription;
public class community extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_community);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null){
getSupportActionBar().setTitle("Join Our Community !");
}
//initializeViews();
}
public void join(View view)
{
Snackbar.make(view, Constants.EMAIL ,Snackbar.LENGTH_SHORT).show();
EditText title_Text = (EditText) view.findViewById(R.id.ti_title) ;
String title = title_Text.getText().toString();
Toast.makeText(community.this,"title",Toast.LENGTH_LONG).show();
}
}
And the problem that the Toast never appears and the application crashes.
Upvotes: 0
Views: 59
Reputation: 2641
I can see two things in your code
1) "R.id.ti_title" belongs to "TextInputLayout" not "EditText"
2) You cannot find id of your "EditText" from onClick(View v) [assuming you are passing this view object on button click in join(View view)]
How to resolve
public class community extends AppCompatActivity {
Toolbar toolbar;
EditText title_Text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_community);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
title_Text = (EditText)findViewById(R.id.title); // Modified Line
if(getSupportActionBar()!=null){
getSupportActionBar().setTitle("Join Our Community !");
}
//initializeViews();
}
public void join(View view)
{
Snackbar.make(view, Constants.EMAIL ,Snackbar.LENGTH_SHORT).show();
String title = title_Text.getText().toString();
Toast.makeText(community.this,title,Toast.LENGTH_LONG).show();
}
}
Hope this help you somehow...
Upvotes: 0
Reputation: 1748
ti_title is actually a TextInputLayout not a EditText. This can be the reason for crash. Change the line:-
EditText title_Text = (EditText) view.findViewById(R.id.title) ;
Upvotes: 0
Reputation: 1043
Try this,
public class community extends AppCompatActivity {
Toolbar toolbar;
EditText title_Text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_community);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
title_Text = (EditText)findViewById(R.id.ti_title) ; // here changes
if(getSupportActionBar()!=null){
getSupportActionBar().setTitle("Join Our Community !");
}
//initializeViews();
}
public void join(View view)
{
Snackbar.make(view, Constants.EMAIL ,Snackbar.LENGTH_SHORT).show();
String title = title_Text.getText().toString();
Toast.makeText(community.this,"title",Toast.LENGTH_LONG).show();
}
}
Upvotes: 2