Cannot add childs to linearLayout - android

In my app what i want is to dynamically add buttons to a linear layout based on the length of the array.For example if the length of array is 4 then ,4 buttons should be added to the linear layout.I tried doing that but always my app crashes
The linearlayout which is at the bottom of the xml is where i wanna add the buttons
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="match_parent"
android:background="#drawable/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/hsjobslogo"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mrd_home"
android:layout_marginRight="10dp"
android:src="#drawable/home168"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/header"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="0dp"
android:layout_height="33dp"
android:layout_weight="2"
android:layout_marginTop="04dp"
android:text="Resource"
android:gravity="center|start"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:padding="10dp"
android:visibility="gone"
android:layout_marginTop="-5dp"
android:src="#drawable/edit" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:visibility="visible"
android:id="#+id/ll_main_pi"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_pi_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" [ "
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_gender" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=","
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_age" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" ]"
style="#style/Job_on_Call"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profession"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_profession" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" [ "
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yrs"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_exp_yrs" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Months"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_exp_months" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" ]"
style="#style/Job_on_Call"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_area" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_city" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_state" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="true"
android:id="#+id/tv_mrd_country" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="0dp"
android:layout_height="1dp"
android:gravity="bottom"
android:layout_gravity="bottom"
android:background="#drawable/divider_light"
android:layout_weight="0.2" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1.6"
android:layout_height="wrap_content"
android:background="#drawable/rectangle">
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="Personal Info"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/tv_mrd_header"
style="#style/Job_on_Call"
android:textColor="#fff"
android:layout_marginTop="05dp"
android:layout_marginLeft="19dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus_one"
android:visibility="gone"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:id="#+id/iv_mrd_add" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edit_one"
android:visibility="visible"
android:layout_marginRight="5dp"
android:layout_marginLeft="10dp"
android:id="#+id/iv_mrd_edit" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="5dp"
android:id="#+id/iv_mrd_go_left"
android:src="#drawable/left_arrow" />
<ViewFlipper
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#+id/mrd_view_flipper"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:id="#+id/ll_my_resource_personal_details">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nationality"
android:textStyle="bold"
android:id="#+id/tv_mrd_nationality_header"
android:layout_marginTop="02dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:id="#+id/ll_my_resource_refference_details">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
android:id="#+id/tv_mrd_ref_name_header"
android:layout_marginTop="02dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Document Type"
android:textStyle="bold"
android:id="#+id/dumm1"
android:layout_marginTop="05dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/mrd_profile_pic"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ViewFlipper>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="5dp"
android:id="#+id/iv_mrd_go_right"
android:src="#drawable/right_arrow" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/add_doc_circles"
android:orientation="horizontal">
The layout where i want to create buttons dynamically
</LinearLayout>
</LinearLayout>
Code
if (NewDataSet.get("Table2") instanceof JSONArray) {
isDocPresent = true;
JSONArray array = NewDataSet.getJSONArray("Table2");
numOfDocCircles = array.length();
LayoutInflater layoutInflater;
Button button = new Button(context);
for (int k = 0; k < array.length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
button.setLayoutParams(params);
button.setText("" + k);
llAddDocCircles.addView(button);
}
Error
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Please help me out whats the problem?

Move your button creation into the for loop, in your code you are basically just adding the same Button instance to the parent layout several times:
for (int k = 0; k < array.length(); k++) {
Button button = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(convertDpToPixel(30), convertDpToPixel(30));
button.setLayoutParams(params);
button.setText("" + k);
llAddDocCircles.addView(button);
}
Use below method to convert dp to pixel.
public static float convertDpToPixel(float dp){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}

In your for loop call this method as:
for (int k = 0; k < array.length(); k++) {
llAddDocCircles.addView(addMoreButton("" + k));
}
And your addMoreButton() method is:
public Button addMoreButton(String text) {
Button button = new Button(context);
button.setId(buttonID);
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button LayoutParams.setMargins(10, 10, 10, 10); //Add this if you want margin of 10dp
button.setLayoutParams(buttonLayoutParams );
buttonArrayList.add(button);
buttonID++;
return button;
}
And you can easily access these button value by:
private static int buttonID = 0;
private ArrayList<Button> buttonArrayList= new ArrayList<Button>();
// loop to handle each button
for (int i = 0; i < buttonArrayList.size(); i++) {
Button button = buttonArrayList.get(i);
// do your stuff with each button
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}

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?

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.

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>

Show/Hide top bar like chrome, but with some static content and a listview

First let me give a short introduction and then show you what end goal looks:
So far I've added a scrolllistener to my listactivity, so that I can detect scrolling, though it does not give me any animation on android:animateLayoutChanges="true". So when i change the margin's of the view programmatically, it just does these changes instantly, and not that well, any help is most welcomed.
As you can see in my xml, i place the item I'm going to hide slowly first so other will be above it on the Z-axis.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lector="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:background="#color/background_greyish">
<LinearLayout
android:id="#+id/disappearingHeaderLabel"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="60dp"
android:animateLayoutChanges="true"
android:layout_height="50dp"
android:background="#color/solid_white"
android:orientation="horizontal"
android:weightSum="7">
<TextView
android:id="#+id/ProductDetailLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="15dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dip"
android:layout_weight="4"
android:singleLine="true"
android:text="PRODUKTER"
android:textColor="#color/solid_red"
android:textSize="20dp" />
<Button
android:id="#+id/priceChooserButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_weight="3"
android:background="#drawable/buttonbackground"
android:clickable="true"
android:singleLine="true"
android:text="#string/retailprice"
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
<dk.myapp.mobile.views.PageHeaderView
android:id="#+id/header"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></dk.myapp.mobile.views.PageHeaderView>
<LinearLayout
android:id="#+id/expandingListContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:animateLayoutChanges="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="110dp"
android:layout_above="#+id/paginationBarContainer"
android:orientation="vertical">
<LinearLayout
android:id="#+id/headerLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:orientation="horizontal"
android:weightSum="11">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/sortAmountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:singleLine="false"
android:text="1354"
android:textColor="#color/solid_red"
android:textSize="15dp" />
<TextView
android:id="#+id/sortLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:text="Produkter"
android:singleLine="true"
android:textColor="#color/solid_red"
android:textSize="15dp" />
</LinearLayout>
<Button
android:id="#+id/filterBtn"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_weight="4"
android:background="#drawable/button_background_red"
android:clickable="true"
android:singleLine="true"
android:text="#string/retailprice"
android:textColor="#color/solid_white"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/sortSpinner"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/spinner_background"
android:drawSelectorOnTop="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:prompt="#string/sort" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#color/divider_color"></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#color/divider_color"
android:dividerHeight="1dip"
android:paddingLeft="15dip"
android:scrollbarStyle="outsideInset"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int marginRemoved = firstVisibleItem * 30;
if (marginRemoved > 110) {
marginRemoved = 110;
}
if (previousMarginRemoved != marginRemoved) {
previousMarginRemoved = marginRemoved;
LinearLayout disappearingHeaderContainer = (LinearLayout) findViewById(R.id.disappearingHeaderLabel);
RelativeLayout.LayoutParams disappearingHeaderContainerLayoutParams = (RelativeLayout.LayoutParams) disappearingHeaderContainer.getLayoutParams();
LinearLayout expandingListViewContainer = (LinearLayout) findViewById(R.id.expandingListContainer);
RelativeLayout.LayoutParams expandingListViewContainerParams = (RelativeLayout.LayoutParams) expandingListViewContainer.getLayoutParams();
if (disappearingHeaderStartMargin == -1 && expandingHeaderStartMargin == -1) {
disappearingHeaderStartMargin = disappearingHeaderContainerLayoutParams.topMargin;
expandingHeaderStartMargin = expandingListViewContainerParams.topMargin;
}
float disappearingHeaderTopMarginFloat = disappearingHeaderStartMargin - (marginRemoved * pixelDensity);
int disappearingHeaderTopMargin = Math.round(disappearingHeaderTopMarginFloat);
if (marginRemoved > 50) {
marginRemoved = 50;
}
float expandingContainerTopMarginFloat = expandingHeaderStartMargin - (marginRemoved * pixelDensity);
int expandingContainerTopMargin = Math.round(expandingContainerTopMarginFloat);
disappearingHeaderContainerLayoutParams.setMargins(disappearingHeaderContainerLayoutParams.leftMargin,
disappearingHeaderTopMargin,
disappearingHeaderContainerLayoutParams.rightMargin,
disappearingHeaderContainerLayoutParams.bottomMargin);
expandingListViewContainerParams.setMargins(expandingListViewContainerParams.leftMargin,
expandingContainerTopMargin,
expandingListViewContainerParams.rightMargin,
expandingListViewContainerParams.bottomMargin);
if ((lastChangeDate < System.currentTimeMillis()-100)) {
lastChangeDate = System.currentTimeMillis();
disappearingHeaderContainer.setLayoutParams(disappearingHeaderContainerLayoutParams);
expandingListViewContainer.setLayoutParams(expandingListViewContainerParams);
}
}
}
};

ImageView and textView getting overlapped when trying to inflate xml

I have an xml which contain imageview and textview.Textview is placed toRightOf ImageView , but when i try to inflate the xml imageView and textview is getting overlapped.Any help is appreciated
data.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingBottom="5dp"
android:src="#drawable/payback" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/img"
android:paddingTop="5dp"
android:text="33%" />
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img"
android:paddingBottom="10dp"
android:text="Important and Urgent" />
</RelativeLayout>
Activity Code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
content=(LinearLayout)findViewById(R.id.content);
today=(TextView)findViewById(R.id.today);
tomorrow=(TextView)findViewById(R.id.tomorrow);
today.setOnClickListener(new TextView.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(ToDo.this, "Before"+content.getChildCount(), Toast.LENGTH_LONG).show();
content.removeAllViews();
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int id=0;
for (int idx = 0; idx < 4; idx++) {
id = idx + 1;
rel = (RelativeLayout) inflater.inflate(R.layout.data, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 50, 0, 0);
txt = (TextView) rel.findViewById(R.id.txt);
txt.setText("AAA"+idx);
img = (ImageView) rel.findViewById(R.id.img);
img.setImageResource(R.drawable.payback);
img.setId(id);
content.addView(rel, params);
}
content.setVisibility(View.VISIBLE);
}
});
}
check.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/whiteboard"
android:layout_width="250dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:background="#FFFFFF" >
<Button
android:id="#+id/add"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="17dp"
android:layout_marginLeft="20dp"
android:text="Add" />
<View
android:id="#+id/bottomseperator"
android:layout_width="200dp"
android:layout_height="1dp"
android:layout_above="#+id/add"
android:layout_alignLeft="#+id/add"
android:layout_marginBottom="10dp"
android:background="#000000" />
<View
android:id="#+id/topseperator"
android:layout_width="220dp"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:background="#000000" />
<TextView
android:id="#+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="8dp"
android:text="Day :"
android:textSize="15sp" />
<TextView
android:id="#+id/today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/day"
android:layout_alignBottom="#+id/day"
android:layout_toRightOf="#+id/day"
android:text="Today /"
android:textSize="15sp" />
<TextView
android:id="#+id/tomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/today"
android:layout_alignBottom="#+id/today"
android:layout_toRightOf="#+id/today"
android:text="Tomorrow /"
android:textSize="15sp" />
<TextView
android:id="#+id/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tomorrow"
android:layout_alignBottom="#+id/tomorrow"
android:layout_toRightOf="#+id/tomorrow"
android:text="Custom"
android:textSize="15sp" />
<ScrollView
android:id="#+id/contenntscroll"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_alignBottom="#+id/whiteboard"
android:layout_alignTop="#+id/whiteboard"
android:layout_below="#+id/topseperator"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:isScrollContainer="false"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/content"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_marginTop="30dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
The statement:
img.setId(id);
is causing the problem remove this statement the code would work.

Categories

Resources