Application crashes when opening alertdialog in android - android

I've look in the android documentation on how to implement alertdialog with a xml layout
here is the documentation
when i run the program and click the todo button the program crashes can anyone help?
here's my source code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StartMoving extends Activity implements OnClickListener {
Button todo;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.start_moving);
todo = (Button) findViewById(R.id.bTodo);
todo.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bTodo:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.to_do_list, null);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
break;
default:
break;
}
}
}
here's the xml file named to_do_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<ScrollView
android:id="#+id/svTips"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="87" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTipsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:text="#string/tips"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvtmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/two_months_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbSAP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_sort_and_purge"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_sap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_sap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_research"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_r_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_r_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbCAMB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_create_a_moving_builder"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_camb_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_camb_content" />
<TextView
android:id="#+id/tvswb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/six_weeks_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_ordersupplies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_order_supplies"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_os_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_os_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_use_it_or_lose_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_use_it_or_lose_it"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_uioli_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_uioli_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_take_measurement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_take_measurement"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_tm_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_tm_content" />
<TextView
android:id="#+id/tvomb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/one_month_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_choose_your_mover_and_confirm_the_arragements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_choose_your_mover_and_confirm_the_arragements"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_cymacta_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_cymacta_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_begin_packing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_begin_paking"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_bp_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_bp_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_label"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_l_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_l_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_separate_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_separate_values"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_sv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_sv_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_do_a_change_of_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_do_a_change_of_address"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_dacoa_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_dacoa_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_notify_important_parties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_notify_all_important_parties"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_naip_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_naip_content" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="13"
android:orientation="horizontal" >
<Button
android:id="#+id/bReturntoTop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Go back to top" />
<Button
android:id="#+id/bgoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Go to" />
</LinearLayout>
</LinearLayout>
this is what the logcat shows

use
Context mContext = v.getApplicationContext();
instead of
Context mContext = getApplicationContext();
or
builder = new AlertDialog.Builder(StartMoving.this);
EDIT :
You are reading button
todo = (Button) findViewById(R.id.bgoto); <-----------
^^^^^
todo.setOnClickListener(this);

AlertDialog.Builder diag = new AlertDialog.Builder(this);
diag.setTitle("DIALOG TITLE")
.setMessage("YOUR MESSAGE")
.setCancelable(false)
.setNegativeButton("WHAT EVER THE BUTTON TEXT",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//enter code here
//do what ever the button in the dialog is clicked
dialog.cancel();
}
});
diag.show();
Easiest way to have a alert dialog
and dont forget to change the Button id in java to R.id.goto

if your are using alertDialog in TabHost then you have to write..
final AlertDialog alertDialog = new AlertDialog.Builder(**getParent()**).create();
//alertDialog.setTitle("Alert....");
alertDialog.setMessage("Your Text");
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
finish();
}
});
alertDialog.show();

It seems that I've been reading the button from the Context and not from the alertDialog. The solution was to create a View that will be set as View for the AlertDialog and declare a button and set it as a child for the View.
final View view;
LayoutInflater inf = LayoutInflater.from(StartMoving.this);
view = inf.inflate(R.layout.rename, null);
final EditText newname = (EditText) view.findViewById(R.id.etNewRoomName);
final Button todo = (BUtton) view.findViewById(R.id.bTodo);
new AlertDialog.Builder(StartMoving.this)
.setView(view)
.setTitle("Rename")
.setMessage("Enter new name for room " + currentRoom)
.show();

Related

How to click button inside listview

I know this problem already asked many times but i still can't get effective way for solving this. I try for use ListView with clickable/editable widget and Android : How to set onClick event for Button in List item of ListView as reference but the result is i need click button several time for executing my program. Here my program
LeadActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".LeadSalesActivity">
<LinearLayout
android:background="#E9ECEB"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/b_menusamping2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="#drawable/menu_button"
android:backgroundTint="#27D01B"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome,"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="#android:color/black"/>
<TextView
android:id="#+id/t_username3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Rizaldi"
android:textColor="#android:color/black"
android:textSize="25sp"
android:textStyle="bold"
android:fontFamily="#font/action_man_bold"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear_refresh2"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white">
<ImageView
android:backgroundTint="#27D01B"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="#drawable/icon_update"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="("
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:textStyle="bold"
android:id="#+id/count_lead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=")"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/btn_add"
android:layout_marginRight="20dp"
android:backgroundTint="#android:color/holo_blue_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tambah Lead"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tanggal"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telp"
android:textColor="#android:color/black"
android:textSize="20sp"/>
</LinearLayout>
<ListView
android:id="#+id/listviewlead"
tools:listitem="#layout/c_lead"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/back6"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</LinearLayout>
c_lead.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_tgl"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Tgl"
android:textSize="15sp"
android:textStyle="italic"
android:textColor="#000000" />
<TextView
android:id="#+id/t_lead4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Lead"
android:textSize="15sp"
android:textColor="#000000" />
<TextView
android:id="#+id/t_numberphone"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="right"
android:text="No.Telp"
android:textSize="15sp"
android:textColor="#android:color/black"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pencil_icon"/>
</LinearLayout>
<LinearLayout
android:id="#+id/btn_delete"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/delete_icon"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_alamat"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Alamat"
android:textColor="#android:color/black"
android:textSize="15sp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
MyAdapter
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.c_lead, null);
}
final HashMap<String, Object> hashMap = _data.get(_position);
final TextView t_tanggal = (TextView) _view.findViewById(R.id.t_tgl);
final TextView t_lead = (TextView) _view.findViewById(R.id.t_lead4);
final TextView t_alamat = (TextView) _view.findViewById(R.id.t_alamat);
final TextView t_numberphone = (TextView) _view.findViewById(R.id.t_numberphone);
final LinearLayout btn_edit = (LinearLayout) _view.findViewById(R.id.btn_edit);
final LinearLayout btn_delete = (LinearLayout) _view.findViewById(R.id.btn_delete);
String dateString = _data.get(_position).get("create_date").toString().replace("-","");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmm");
format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = format1.parse(dateString);
String dateFinal = format2.format(date);
t_tanggal.setText(String.valueOf((long)(_position + 1)).concat(".").concat(dateFinal));
} catch (ParseException e) {
e.printStackTrace();
}
t_lead.setText(_data.get((int)_position).get("type").toString());
t_alamat.setText(_data.get((int)_position).get("street").toString());
if (_data.get((int)_position).get("mobile").toString().equals(true)){
t_numberphone.setText("Tanpa Nomor");
} else {
t_numberphone.setText(_data.get((int)_position).get("phone").toString());
}
btn_edit.setFocusable(false);
btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.setClass(getApplicationContext(), AddLeadSalesActivity.class);
startActivity(i);
}
});
btn_delete.setFocusable(false);
btn_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Data Tidak Bisa Dihapus", Toast.LENGTH_SHORT).show();
}
});
return _view;
}
Is there any mistake inside my program? How to solve this?

Weird behavior on item click

I'm building an app that sends USSD commands to check for balance, etc.
The apps acts weirdly when I try to launch activities from item click, here from image button: I have to touch two times to launch an activity. I don't know what is wrong.
Have a look at my code:
MainActivity.java:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ImageView accountView;
ImageView activationsView;
ImageView servicesView;
ImageView othersView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openAccountView(View view){
accountView = (ImageView) findViewById(R.id.accountView);
accountView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AccountActivity.class);
startActivity(i);
}
});
}
public void openActivationsView(View view){
activationsView = (ImageView) findViewById(R.id.activationsView);
activationsView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ActivationsActivity.class);
startActivity(i);
}
});
}
public void openServicesView(View view){
servicesView = (ImageView) findViewById(R.id.servicesView);
servicesView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ServicesActivity.class);
startActivity(i);
}
});
}
public void openOthersView(View view){
}
}
activity_main.xml:
<?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:id="#id/layout_dashboard_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/c_dashboard_btn_quick_clean_color"
android:orientation="horizontal">
<Button
android:id="#+id/buttonCB"
style="#android:style/Widget.Material.Light.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="2"
android:background="#color/c_main_back_ground_color"
android:elevation="7dp"
android:text="#string/check_balance"
tools:ignore="ButtonStyle"
tools:targetApi="lollipop" />
<Button
android:id="#+id/buttonLB"
style="#android:style/Widget.Material.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_marginStart="2dip"
android:layout_weight="2"
android:background="#color/c_main_back_ground_color"
android:elevation="7dp"
android:text="#string/load_balance"
tools:ignore="ButtonStyle"
tools:targetApi="lollipop" />
</LinearLayout>
<ScrollView
android:id="#id/main_scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/webview_ads"
tools:ignore="UnknownIdInLayout">
<LinearLayout
android:id="#id/layout_dashboard_scroll_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<FrameLayout
android:id="#id/dashboard_alert_section"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<include layout="#layout/dashboard_feature_tip_item" />
</FrameLayout>
<FrameLayout
android:id="#id/main_control_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="345">
<FrameLayout
android:id="#id/dashboard_progress_bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="#dimen/dashboard_clean_progess_mar_top"
android:elevation="#dimen/dashboard_progressbar_elevation"
android:visibility="gone"
tools:ignore="UselessParent"
tools:targetApi="lollipop">
<ImageView
android:layout_width="#dimen/dashboard_progress_clean_all_image_dimension"
android:layout_height="#dimen/dashboard_progress_clean_all_image_dimension"
android:layout_gravity="center"
android:contentDescription=""
android:src="#drawable/btn_ram_checkbox"
tools:ignore="ContentDescription" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#id/layout_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/c_main_back_ground_color">
<RelativeLayout
android:id="#id/layout_account"
android:layout_width="#dimen/dashboard_feature_layout_width"
android:layout_height="#dimen/dashboard_feature_layout_height"
android:layout_marginEnd="#dimen/dashboard_feature_middle_margin"
android:layout_marginLeft="#dimen/dashboard_feature_leftmost_margin"
android:layout_marginRight="#dimen/dashboard_feature_middle_margin"
android:layout_marginStart="#dimen/dashboard_feature_leftmost_margin"
android:layout_marginTop="#dimen/dashboard_feature_top_margin"
android:background="#drawable/dashboard_bg_selector"
android:clickable="false"
android:elevation="6dp"
android:focusable="false"
tools:targetApi="lollipop">
<ImageView
android:id="#+id/accountView"
android:layout_width="130dip"
android:layout_height="130dip"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:adjustViewBounds="false"
android:background="#color/checkbox_button_color"
android:clickable="true"
android:contentDescription="#string/account"
android:cropToPadding="false"
android:onClick="openAccountView"
android:visibility="visible"
app:srcCompat="#drawable/account" />
<TextView
android:id="#+id/accountTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="#string/account"
android:textColor="#color/body_text_color"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/accountView"
android:paddingBottom="2dip"
android:paddingEnd="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingStart="2dip"
android:text="#string/account_detail"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:id="#id/layout_activations"
android:layout_width="#dimen/dashboard_feature_layout_width"
android:layout_height="#dimen/dashboard_feature_layout_height"
android:layout_marginEnd="#dimen/dashboard_feature_rightmost_margin"
android:layout_marginLeft="#dimen/dashboard_feature_middle_margin"
android:layout_marginRight="#dimen/dashboard_feature_rightmost_margin"
android:layout_marginStart="#dimen/dashboard_feature_middle_margin"
android:layout_marginTop="#dimen/dashboard_feature_top_margin"
android:layout_toEndOf="#id/layout_account"
android:layout_toRightOf="#id/layout_account"
android:background="#drawable/dashboard_bg_selector"
android:clickable="false"
android:elevation="#dimen/dashboard_feature_bg_elevation"
android:focusable="false"
android:onClick="openActivationsView"
tools:targetApi="lollipop">
<ImageView
android:id="#+id/activationsView"
android:layout_width="130dip"
android:layout_height="130dip"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:clickable="true"
android:contentDescription="#string/activations_packs"
android:onClick="openActivationsView"
app:srcCompat="#drawable/activations" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/activationsView"
android:elevation="7dp"
android:paddingBottom="2dip"
android:paddingEnd="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingStart="2dip"
android:text="#string/activations_detail"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="12sp" />
<TextView
android:id="#+id/activationsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="#string/activations_packs"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#id/layout_services"
android:layout_width="#dimen/dashboard_feature_layout_width"
android:layout_height="#dimen/dashboard_feature_layout_height"
android:layout_below="#id/layout_account"
android:layout_marginBottom="10dip"
android:layout_marginEnd="#dimen/dashboard_feature_middle_margin"
android:layout_marginLeft="#dimen/dashboard_feature_leftmost_margin"
android:layout_marginRight="#dimen/dashboard_feature_middle_margin"
android:layout_marginStart="#dimen/dashboard_feature_leftmost_margin"
android:layout_marginTop="#dimen/dashboard_feature_ram_security_top_margin"
android:background="#drawable/dashboard_bg_selector"
android:clickable="false"
android:elevation="5dp"
android:focusable="false"
android:onClick="openServicesView"
tools:targetApi="lollipop">
<TextView
android:id="#+id/servicesTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="#string/services"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/servicesView"
android:layout_width="130dip"
android:layout_height="130dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/services"
app:srcCompat="#drawable/services" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/servicesView"
android:paddingBottom="2dip"
android:paddingEnd="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingStart="2dip"
android:text="#string/services_detail"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:id="#id/layout_others"
android:layout_width="#dimen/dashboard_feature_layout_width"
android:layout_height="#dimen/dashboard_feature_layout_height"
android:layout_below="#id/layout_activations"
android:layout_marginBottom="10dip"
android:layout_marginEnd="#dimen/dashboard_feature_rightmost_margin"
android:layout_marginLeft="#dimen/dashboard_feature_middle_margin"
android:layout_marginRight="#dimen/dashboard_feature_rightmost_margin"
android:layout_marginStart="#dimen/dashboard_feature_middle_margin"
android:layout_marginTop="#dimen/dashboard_feature_ram_security_top_margin"
android:layout_toEndOf="#id/layout_services"
android:layout_toRightOf="#id/layout_services"
android:background="#drawable/dashboard_bg_selector"
android:clickable="false"
android:elevation="#dimen/dashboard_feature_bg_elevation"
android:focusable="false"
tools:targetApi="lollipop">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="#string/other_detail"
android:textColor="#color/black"
android:textSize="12sp" />
<ImageView
android:id="#+id/otherView"
android:layout_width="130dip"
android:layout_height="130dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/other"
app:srcCompat="#drawable/help" />
<TextView
android:id="#+id/otherTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:clickable="true"
android:gravity="center_vertical|center_horizontal|center"
android:onClick="openOthersView"
android:text="#string/other"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
You are calling openAccountView method by onClick imageView by this:
android:onClick="openAccountView"
So, no need to add onClicklistener to your imageView.
Remove onClicklistener in openAccountView, Use the below code :
public void openAccountView(View view){
Intent i = new Intent(MainActivity.this, AccountActivity.class);
startActivity(i);
}
Hope this helps.

NullPointerException in dialog - Strange case

I have a stupid problem but i couldnt solve it yet. I have an activity with a button in toolbar, this button open one dialog with some filter params.
Here is what a i did..
Spinner spinner_categoria;
Spinner spinner_vendedor;
SeekBar seek_preco;
TextView tv_filtro;
onCreate method {
...
}
onClickEvent{
dialogFiltro();
}
public void dialogFiltro() {
Context context = SalesActivity.this;
final Dialog dialog;
dialog = new Dialog(context);
//dialog.setContentView(R.layout.dialog_filtro);
// dialog.setTitle("Filtro");
//dialog.show();
VendedorDAO auxVendedor = new VendedorDAO();
final List<String> listVendedor = auxVendedor.getVendedorList();
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_filtro, null, false);
CardView bt_cancelar = (CardView) dialogView.findViewById(R.id.cb_filtro_cancelar);
CardView bt_aceitar = (CardView) dialogView.findViewById(R.id.cb_filtro_aceitar);
seek_preco = (SeekBar) dialogView.findViewById(R.id.sb_preco_max);
tv_filtro = (TextView) dialogView.findViewById(R.id.tv_filtro_preco);
spinner_categoria = (Spinner) dialogView.findViewById(R.id.spinner_categoria);
spinner_vendedor = (Spinner) dialogView.findViewById(R.id.spinner_vendedor);
spinner_categoria.setAdapter(ArrayAdapter.createFromResource(
this, R.array.categoria_array, android.R.layout.simple_spinner_item));
spinner_vendedor.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listVendedor));
String oi = "oi";
tv_filtro.setText(oi);
dialog.setContentView(dialogView);
dialog.show();
seek_preco.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// tv_filtro.setText("R$ " + progress);
// Log.i("RS " , String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
bt_cancelar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
bt_aceitar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
categoria = (String) spinner_categoria.getSelectedItem();
vendedor = (Integer) spinner_vendedor.getFirstVisiblePosition();
Log.e("id vendedor ", String.valueOf(vendedor));
if (vendedor == 0) {
vendedor = -1;
}
int precoAux = seek_preco.getProgress();
precoMax = (float) precoAux;
}
});
}
I have this in dialog_filtro.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="8dp"
android:foreground="#drawable/border_card"
card_view:cardElevation="2dp"
card_view:cardMaxElevation="8dp">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/cb_filtro_cancelar"
android:layout_width="70dp"
android:layout_height="30dp"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:text="X"
android:textColor="#android:color/white" />
<Button
android:id="#+id/cb_filtro_aceitar"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:text="Aceitar"
android:textColor="#android:color/white" />
</FrameLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Filtrar por"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorPrimary" />
<TextView
android:id="#+id/tv_label_categoria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Categoria"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner_categoria"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<TextView
android:id="#+id/tv_label_vendedor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Supermercado"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner_vendedor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Preço máximo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<SeekBar
android:id="#+id/sb_preco_max"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:indeterminate="false"
android:max="100"
android:progress="0" />
<TextView
android:id="#+id/tv_filtro_preco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="5"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:id="#+id/rv_shoppingcart_separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/tv_label_oerdenar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:text="Ordenar por"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorPrimary" />
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Preço:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/group_preco"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_preco_maior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Maior" />
<RadioButton
android:id="#+id/rb_preco_menor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Menor" />
</RadioGroup>
<TextView
android:id="#+id/tv_label_data_Validade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Data de validade:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/group_validade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_validade_maior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Maior" />
<RadioButton
android:id="#+id/rb_validade_menor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Menor" />
</RadioGroup>
<LinearLayout
android:id="#+id/linear_isoffer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_label_ofertas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Apenas ofertas:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cb_offer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:checked="false"
android:text="Selecionar apenas ofertas" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
</LinearLayout>
Everything works fine (spinner, cardview etc) but i always get NullPointerException because the TextView and i don't know why. Here goes my log:
FATAL EXCEPTION: main
Process: com.catafeira.catafeira, PID: 2419
Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.catafeira.catafeira.SalesActivity.dialogFiltro(SalesActivity.java:281)
at com.catafeira.catafeira.SalesActivity.onOptionsItemSelected(SalesActivity.java:432)
at android.app.Activity.onMenuItemSelected(Activity.java:2914)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
EDIT: I know what is a NullPointerException, I already checked the previous posts on the subject, but no problem is the same as mine
I think the problem is with findViewByID. The documentation says
findViewById
Added in API level 1
View findViewById (int id)
Finds a child view with the given identifier. Returns null if the specified child view does not exist or the dialog has not yet been fully created (for example, via show() or create()).
It is just possible that since you are calling show() later the textView might not be created. I would do this
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_filtro, null);
Button bt_cancelar = (Button) dialogView.findViewById(R.id.cb_filtro_cancelar);
Button bt_aceitar = (Button) dialogView.findViewById(R.id.cb_filtro_aceitar);
seek_preco = (SeekBar) dialogView.findViewById(R.id.sb_preco_max);
tv_filtro = (TextView) dialogView.findViewById(R.id.tv_filtro_preco);
spinner_categoria = (Spinner) dialogView.findViewById(R.id.spinner_categoria);
spinner_vendedor = (Spinner) dialogView.findViewById(R.id.spinner_vendedor);
dialog.setContentView(dialogView)
dialog.show();
spinner_categoria.setAdapter(ArrayAdapter.createFromResource(
this, R.array.categoria_array, android.R.layout.simple_spinner_item));
spinner_vendedor.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listVendedor));
tv_filtro.setText("oi");
Change the XML to the following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:foreground="#drawable/border_card"
android:layout_margin="8dp"
card_view:cardElevation="2dp"
card_view:cardMaxElevation="8dp">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/cb_filtro_cancelar"
android:layout_width="70dp"
android:layout_height="30dp"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:text="X"
android:textColor="#android:color/white" />
<Button
android:id="#+id/cb_filtro_aceitar"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:text="Aceitar"
android:textColor="#android:color/white" />
</FrameLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Filtrar por"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorPrimary" />
<TextView
android:id="#+id/tv_label_categoria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Categoria"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner_categoria"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<TextView
android:id="#+id/tv_label_vendedor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Supermercado"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner_vendedor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Preço máximo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<SeekBar
android:id="#+id/sb_preco_max"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:indeterminate="false"
android:max="100"
android:progress="5" />
<TextView
android:id="#+id/tv_filtro_preco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="5"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:id="#+id/rv_shoppingcart_separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/tv_label_oerdenar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:text="Ordenar por"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorPrimary" />
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Preço:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/group_preco"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_preco_maior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Maior" />
<RadioButton
android:id="#+id/rb_preco_menor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Menor" />
</RadioGroup>
<TextView
android:id="#+id/tv_label_data_Validade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Data de validade:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/group_validade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_validade_maior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Maior" />
<RadioButton
android:id="#+id/rb_validade_menor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Menor" />
</RadioGroup>
<TextView
android:id="#+id/tv_label_ofertas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:text="Apenas ofertas:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cb_offer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:checked="false"
android:text="Selecionar apenas ofertas" />
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
Hope this helps!
SOLVED
The problem was something related android material design, I didn't note before that some layouts has 2 files, so some files were outdated -.-
Thanks for everyone that tried to help.

Android custom dialog displays extra space

My custom dialog adds extra space after the Cancel and Save button. I have made a couple of custom dialogs in this app, but this problem appears only in this case. I've colored the view's background to blue, so I think it should be okay.
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/mo"/>
<CheckBox
android:id="#+id/cbMo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/tu"/>
<CheckBox
android:id="#+id/cbTu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/we"/>
<CheckBox
android:id="#+id/cbWe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/th"/>
<CheckBox
android:id="#+id/cbTh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/fr"/>
<CheckBox
android:id="#+id/cbFr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:gravity="center_horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:layout_marginRight="10dp"
android:text="#string/repeat_every"
android:textColor="#android:color/black"
android:gravity="right"
android:layout_gravity="center_vertical"/>
<Spinner
android:id="#+id/spRecurrence"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:prompt="#string/repeat_every"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:layout_marginRight="10dp"
android:text="#string/starts_on"
android:textColor="#android:color/black"
android:gravity="right"/>
<TextView
android:id="#+id/tvDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="#string/pick_a_date"
android:drawableRight="#drawable/ic_calendar"
android:gravity="center_vertical"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.2"
android:visibility="visible"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="right"
android:layout_marginRight="10dp"
android:textColor="#android:color/black"
android:text="#string/recurrences_colon"/>
<EditText
android:id="#+id/etNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:inputType="number"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.3"
android:visibility="invisible"/>
</LinearLayout>
<TextView
android:id="#+id/tvError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_red_dark"
android:visibility="visible"
android:text="Error text"/>
</LinearLayout>
Displaying the dialog:
Context context = getContext();
final NewRecurrencesDialogView recurrencesDialogView = new NewRecurrencesDialogView(context, subject);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(recurrencesDialogView)
.setTitle(R.string.set_recurrence)
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) { }
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
final Dialog dialog = builder.create();
dialog.setCancelable(false);
dialog.show();
((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<Teaching> newRecurrences = recurrencesDialogView.saveRecurrences();
if (newRecurrences != null) {
recurrences.addAll(newRecurrences);
Collections.sort(recurrences, new Comparator<Teaching>() {
#Override
public int compare(Teaching lhs, Teaching rhs) {
if (lhs.getBegin() < rhs.getBegin())
return -1;
else
return 1;
}
});
updateListViewAdapter();
dataChangedListener.setChanged(true);
dialog.dismiss();
}
}
});
((AlertDialog)dialog).getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
I've compared the layout file with the other dialog's files, but can't figure out where the problem is.
I think the problem is with the android:layout_weight you set for your view.
Try change your:
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.2"
android:visibility="visible"/>
To this:
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="visible"/>
I am not really sure what you trying to achieve, but think this will show you the direction (hopefully the right one).

how to set button below while creating textview programatically?

I have one button to create textview and edittext programmatically,everything works fine but the issue is when textview and edittext generated button is appear above the textview and edittext,i want to set button below of them,following is my code can any one tell what is the issue?
addnewdata.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
totalFields++;
lnr = (LinearLayout) findViewById(R.id.addnewlinear);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(25, 0, 0, 0);
valueTV = new TextView(MainActivity.this);
// valueTV.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
valueTV.setText(userInput.getText());
valueTV.setLayoutParams(lp);
valueTV.setTextSize(18);
valueTV.setTag("tv_" + totalFields);
valueTV.setId(totalFields);
valueTV.setTextColor(Color.parseColor("#2d6cae"));
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp1.setMargins(25, 0, 25, 0);
lp1.height = 50;
EditText edtvalues = new EditText(MainActivity.this);
edtvalues.setBackgroundResource(R.drawable.rect_edt);
edtvalues.setLayoutParams(lp1);
edtvalues.setTag("ed_" + totalFields);
lnr.addView(valueTV);
lnr.addView(edtvalues);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
>
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#000000"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="IT ADD$"
android:id="#+id/itaddestxt"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="UP"
android:textColor="#ffffff"
android:layout_toRightOf="#+id/itaddestxt"
android:textSize="20sp" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="#+id/addnewlinear"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD NEW EVENT"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:id="#+id/txtaddnewevent"
android:textColor="#73b5fa"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtaddnewevent"
android:layout_marginTop="10dp"
android:id="#+id/bluelines"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name of Event:*"
android:layout_below="#+id/bluelines"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtnameofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtnameofevent"
android:layout_below="#+id/txtnameofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Event:*"
android:layout_below="#+id/edtnameofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdateofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtdateofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledtdate"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edtdateofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edtdateofevent"
android:layout_marginLeft="10dp"
android:id="#+id/calndrdat"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time of Event:*"
android:layout_below="#+id/reledtdate"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txttimeofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txttimeofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledttime"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edttimeofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edttimeofevent"
android:layout_marginLeft="10dp"
android:id="#+id/timepickrs"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duration of Event:*"
android:layout_below="#+id/edttimeofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdurationofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtdurationofevent"
android:layout_below="#+id/txtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/addnewdata"
android:text="Add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="#android:color/white"
android:layout_gravity="center"
android:id="#+id/btnsubmit"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Add a new layout (linear or relative) above the button.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
>
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#000000"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="IT ADD$"
android:id="#+id/itaddestxt"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="UP"
android:textColor="#ffffff"
android:layout_toRightOf="#+id/itaddestxt"
android:textSize="20sp" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD NEW EVENT"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:id="#+id/txtaddnewevent"
android:textColor="#73b5fa"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtaddnewevent"
android:layout_marginTop="10dp"
android:id="#+id/bluelines"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name of Event:*"
android:layout_below="#+id/bluelines"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtnameofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtnameofevent"
android:layout_below="#+id/txtnameofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Event:*"
android:layout_below="#+id/edtnameofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdateofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtdateofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledtdate"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edtdateofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edtdateofevent"
android:layout_marginLeft="10dp"
android:id="#+id/calndrdat"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time of Event:*"
android:layout_below="#+id/reledtdate"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txttimeofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txttimeofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledttime"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edttimeofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edttimeofevent"
android:layout_marginLeft="10dp"
android:id="#+id/timepickrs"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duration of Event:*"
android:layout_below="#+id/edttimeofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdurationofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtdurationofevent"
android:layout_below="#+id/txtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<!--Empty layout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/addnewlinear"
android:orientation="vertical"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/addnewdata"
android:text="Add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="#android:color/white"
android:layout_gravity="center"
android:id="#+id/btnsubmit"
/>
</LinearLayout>
</ScrollView>

Categories

Resources