Issue with an icon on Android's PopupWindow - android

I have an app in which I'm opening a PopupWindow, with a couple of images, a text box and an icon of a trash bin below the tex box.
The problem is when I'm presenting the PopupWindow, sometimes when I'm opening the window the icon seems small, and on other times (most of the times) the icon seems as I defined it.
popUpView = inflater.inflate(R.layout.activity_packpresent, (ViewGroup)ma.findViewById(R.layout.activity_main));
pw = new PopupWindow(
popUpView,
ma.f.getView().getMeasuredWidth(), //400
ma.f.getView().getMeasuredHeight(), //800
true);
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
System.out.println("Dismissed!!");
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
//MainActivity.packButton.setVisibility(View.VISIBLE);
try {
image.recycle();
} catch (Exception e) {
}
ma.handlePopUpDissmiss();
}
});
}
});
trash = (ImageButton)popUpView.findViewById(R.id.trashBin);
trash.getLayoutParams().height = (int)((MainActivity.wantedPicSize)*(3.0/4.0));
trash.getLayoutParams().width = (int)((MainActivity.wantedPicSize)*(3.0/4.0));
pw.showAtLocation(map, Gravity.BOTTOM, 0, 0);
pw.update();
The layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lID">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rID"
android:background="#drawable/back5">
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/mImageView"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_below="#+id/mImageView"
android:layout_marginTop="50dp"
android:editable="false"
android:visibility="visible"
android:layout_alignRight="#+id/imageViewR"
android:layout_alignEnd="#+id/imageViewR"
android:layout_alignLeft="#+id/imageViewL"
android:layout_alignStart="#+id/imageViewL" />
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/imageViewRR"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_alignTop="#+id/imageViewR"
android:layout_toRightOf="#+id/button" />
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/imageViewLL"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_alignTop="#+id/imageViewL"
android:layout_toLeftOf="#+id/button" />
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/imageViewR"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_alignTop="#+id/mImageView"
android:layout_toRightOf="#+id/mImageView" />
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/imageViewL"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_above="#+id/editText"
android:layout_toLeftOf="#+id/mImageView" />
<GridView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="#+id/gridView"
android:layout_above="#+id/mImageView"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:layout_marginBottom="10dp"
android:layout_below="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:visibility="invisible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="10dp"
android:id="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_alignRight="#+id/imageViewRR"
android:background="#drawable/line"
android:layout_alignLeft="#+id/imageViewLL"
android:layout_below="#+id/gridView" />
<Button
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/imageViewRR"
android:visibility="invisible" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/trashBin"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/trash_bin" />
</RelativeLayout>
</LinearLayout>

You can try to use a .9 patch image which can be generated here: .9 patch generator
Or you can use any of these icon generators here and see if that works: Android Asset Studio
Both links are very useful.

Related

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: OnClick method for Button doesn't respond

I didn't have this problem until now. I don't know what is the problem here because in the same layout i have several buttons and only buttons which are in LinearLayout won't respond.
This is the layout where my buttons won't respond onClick (EDITED):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:background="#drawable/texture"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/input_first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size">
<requestFocus />
</EditText>
<EditText
android:id="#+id/input_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_first_name"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size" />
<EditText
android:id="#+id/input_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_last_name"
android:ems="10"
android:inputType="number"
android:textSize="#dimen/text_size" />
<ImageView
android:id="#+id/profile_image"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_below="#+id/input_age"
android:layout_centerHorizontal="true"
android:src="#drawable/add" />
<Button
android:id="#+id/save_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/edit_button"
android:onClick="run"
android:text="#string/save_button" />
<Button
android:id="#+id/edit_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/slider"
android:text="#string/edit_button" />
<Button
android:id="#+id/delete_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/save_button"
android:layout_toStartOf="#+id/save_button"
android:text="#string/delete_button" />
<Button
android:id="#+id/edit_birthday_date"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/button_container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cake_image"
android:layout_marginTop="10dp"
android:clickable="true"
android:weightSum="3"
android:orientation="horizontal">
<Button
android:id="#+id/button_movie"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_books"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_tech"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container1"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="3"
android:clickable="true">
<Button
android:id="#+id/button_body_care"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_clothes"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_accessories"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container2"
android:layout_marginTop="10dp"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button_games"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/edit_button"
android:layout_alignTop="#+id/slider"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<ImageView
android:id="#+id/cake_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignStart="#+id/edit_birthday_date"
android:layout_below="#+id/edit_birthday_date"
android:layout_marginTop="20dp"
android:src="#drawable/birthday_cake" />
<TextView
android:id="#+id/turning_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cake_image"
android:layout_toEndOf="#+id/cake_image"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/cake_image" />
</RelativeLayout>
</ScrollView>
I have set for every linear layout visibility when one button which is working is clicked to visible, so i think that is not the problem.
This is the code in java:
if (getMovieCategory.equals("movies")) {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
}
});
} else {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dbh.deleteMovies(getMovieCategory);
}
});
}
Here everything is working except onClick. I'm getting no errors. My button doesn't respond on click.
try by handling setOnClickListener out side the if condition.
.You're coding with a lot of redundancy and risking introducing errors, some of which may not be immediately obvious. Here's a leaner and clearer version of your posted code sample, using one onClick method to handle your if(){else} conditions.
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(
getMovieCategory.equals("movies") ? R.drawable.ic_cancelar : R.drawable.ic_add, 0,0,0
);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
dbh.deleteMovies(getMovieCategory);
}
}
});
EDIT 1: I mistakenly left out your String movies = "movies" and have added it back in. If you're only going to use this String once, to pass it as a parameter for dbh.updateCategoryMovies(birthdayId, movies), then you could simply call dbh.updateCategoryMovies(birthdayId, "movies") and delete String movies = "movies"
EDIT 2: Without seeing your entire source code, I don't know if you're at all changing the value of getMovieCategory somewhere. If not, then the below sample will change it on every click of the button (once you launch the app to test the sample, your buttonCategoryMovie will be a generic Button; once you start clicking the button, it should toggle between showing your R.drawable.ic_cancelar and R.drawable.ic_add resources. If the button toggles between these two drawables, then you at least know that the button is in fact receiving the onClick.
Remember that, if the value of getMovieCategory doesn't change on each click, your button will always perform only one of the sets of onClick actions and it'll look like nothing's happening.
If it works for you as I've described, then you're on your way ;)
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
getMoviesCategory = "not movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
getMoviesCategory = "movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
dbh.deleteMovies(getMovieCategory);
}
}
});
Try changing android:visibility="gone" to android:visibility="visible" in your LinearLayout.
And again add android:clickable="true" to your LinearLayout

Clicking the button of the recyclerview item makes buttons of other recyclerview item respond too

I have created a recyclerview usin cardview and added button in each of the items of the recyclerview. I have already integrated the button click event . Now, when I click the button of one of the recyclerview item it responds normally and the button of other items responds too at the same time as if they are also clicked.what is the problem? how can it be solved?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="95dp"
android:orientation="horizontal"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:cardCornerRadius="1dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true">
<ImageView
android:id="#+id/icon_vendor1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:layout_marginRight="10dp"
android:padding="0dp"
android:scaleType="fitXY"
android:src="#drawable/ic_direction" />
<TextView
android:id="#+id/vendorName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/icon_vendor1"
android:text="Kozzaja Infotech Pvt Ltd"
android:textSize="20sp" />
<RatingBar
android:id="#+id/rating"
style="#style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vendorName1"
android:layout_marginTop="4dp"
android:layout_toEndOf="#+id/imageView2"
android:layout_toRightOf="#+id/icon_vendor1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/vendorName1"
android:layout_marginBottom="3dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button4"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:padding="10dp"
android:src="#drawable/ic_call_black_36dp" />
<ImageButton
android:id="#+id/button3"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:padding="20dp"
android:src="#drawable/ic_messenger_outline_black_36dp" />
<ImageButton
android:id="#+id/button2"
android:layout_width="38dp"
android:layout_height="match_parent"
android:background="#drawable/circle_button"
android:src="#drawable/ic_directions_black_24dp" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/layout"
android:background="#drawable/circle_button"
android:tag="off"
android:text="+"
android:textSize="30sp" />
<View
android:layout_width="11dp"
android:layout_height="38dp"
android:background="#ffffff" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is my adapter's code.
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.title.setText(workers.get(position).getName());
holder.rating.setRating(workers.get(position).getRatings());
Picasso.with(context).load(workers.get(position).getImageLogo()).fit().into(holder.imageView);
holder.plus.setTag("off");
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/// button click event
if (holder.plus.getTag().toString().equals("off")) {
holder.plus.setTag("on");
holder.plus.setText("-");
//show the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_to_right));
holder.layout.setVisibility(View.VISIBLE);
} else if (holder.plus.getTag().toString().equals("on")) {
holder.plus.setTag("off");
holder.plus.setText("+");
//hide the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_to_left));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//disappear the button after 500ms
holder.layout.setVisibility(View.GONE);
}
}, 500);
}
}
});
}

Android - Capture ScreenShot Programmatically without title bar

I am developing an app in that I want to take a screen shot of current screen but with out title bar. I know the code to Capture a ScreenShot, but unable to customize it.
Code:
munchscreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View v1 = L1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});
When I click on Clip then it should capture a screen shot of the screen shown below but should not include the part which I have circled..
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:background="#D8D5AA"
>
<LinearLayout
android:id="#+id/clip_from_web_linearlayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/header"
android:orientation="horizontal"
>
<TextView
android:id="#+id/clip_from_web_textview_back"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Back"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
<TextView
android:id="#+id/clip_from_web_textview_header_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Clip From Web"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
<TextView
android:id="#+id/clip_from_web_textview_clip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Clip"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"
android:layout_below="#+id/clip_from_web_linearlayout1"
/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:layout_below="#+id/munchscreen"
android:contentDescription="#string/app_name"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:id="#+id/clip_from_web_bottom_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#drawable/header" >
<Button
android:id="#+id/clip_from_web_previous"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="4dp"
android:background="#android:drawable/ic_media_previous"
android:layout_marginTop="2dp"
/>
<Button
android:id="#+id/clip_from_web_reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#android:drawable/ic_menu_rotate"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/clip_from_web_next"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_toRightOf="#+id/clip_from_web_reload"
android:layout_marginLeft="102dp"
android:background="#android:drawable/ic_media_next"
android:layout_marginTop="2dp"
/>
</RelativeLayout>
Image before Capturing screen shot..
Image after Capturing screen shot..
Take this to another Linear Layout
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"
/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:contentDescription="#string/app_name"
/>
</LinearLayout>
Now find Id for this Linear Layout on oncreate() method making it global variable
LinearLayout ll;
ll = (LinearLayout)findViewById(R.id.linear);
now capture this screen
ll.setDrawingCacheEnabled(true);
ll.buildDrawingCache(true);
Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());
ll.setDrawingCacheEnabled(false);
Now set this bitmap to your ImageView.
try this...
munchscreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View contentView = getWindow().getDecorView().findViewById(android.R.id.content);
contentView.setDrawingCacheEnabled(true);
contentView.buildDrawingCache();
Bitmap drawingCache = contentView.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), drawingCache);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});
Hey friends I got the solution of above problem.
Follow below steps:-
activity_main.xml
<RelativeLayout
android:id="#+id/toolbar_rl"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="#color/header_color"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="0"
tools:ignore="UselessParent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:padding="#dimen/_8sdp"
android:src="#drawable/ic_back_24" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="#dimen/_10sdp"
android:layout_marginStart="#dimen/_20sdp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/one_time"
android:textColor="#color/white"
android:textSize="#dimen/_13sdp"
tools:ignore="RtlHardcoded" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:padding="#dimen/_8sdp"
android:src="#drawable/ic_menu_icon" />
</LinearLayout>
</RelativeLayout>
<ScrollView
android:id="#+id/tabScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<ImageView
android:layout_width="fill_parent"
android:layout_height="210dp"
android:adjustViewBounds="true"
android:background="#drawable/otp_background"
android:contentDescription="#string/app_name"
android:scaleType="fitXY">
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_16sdp"
android:background="#fff"
android:gravity="center"
android:text="Enter Unique Identification Number"
android:textColor="#color/header_color"
android:textSize="#dimen/_16sdp" />
<LinearLayout
android:id="#+id/layout_otp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_26sdp"
android:gravity="top|center"
android:orientation="horizontal">
<EditText
android:id="#+id/editTextone"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="number"
android:maxLength="1"
android:nextFocusRight="#+id/editText_two"
tools:ignore="LabelFor">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTexttwo"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextthree"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextfour"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextfive"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
</LinearLayout>
<Button
android:id="#+id/btn_submit"
android:layout_width="#dimen/_220sdp"
android:layout_height="#dimen/_40sdp"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/_46sdp"
android:layout_marginLeft="#dimen/_16sdp"
android:layout_marginRight="#dimen/_16sdp"
android:layout_marginTop="#dimen/_26sdp"
android:background="#drawable/editbox_one"
android:text="#string/capture"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="#dimen/_16sdp"
android:typeface="serif" />
</TableLayout>
</ScrollView>
This xml looks like.
in MainActivity Add some code.
3.1 Declare the Button.
Button button_submit;
3.2 Initialize the button.
button_submit = (Button) findViewById(R.id.btn_submit);
3.3 Set set OnClickListener to this button and Call one method i.e
takeScreenshot();
Note:- When you click on this button that time automatically capture the screen without title bar and it will store in file explore with current date, time and.jpg extension(Ex. Fri May 11 151938 GMT+0530 2018.jpg) and this captured image as a bitmap if you want you can set this bitmap to ImageView .
The Code of takeScreenshot().
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
View u = findViewById(R.id.tabScroll);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.tabScroll);
z.setBackgroundColor(getResources().getColor(R.color.white));
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
The output be looks like..
I hope it will helpful for you.

Android, trying to get buttons to be on buttom of screen

I'm trying to make a simple app to show pictures. The images get scaled to fit the screen then there is a next and prev button below the image. I would like the next and prev buttons to be at the button, but they keep getting drawn at the button of the image. Thus they move up and down, depending how big the picture is, each time you display a new image.
I found the following solution here, but it is not working for me:
android:gravity="bottom"
android:layout_alignParentBottom="true"
This is the xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/backFeetGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/viewimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/background" />
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/butLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Left "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butFavrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Favrets "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" email "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Right "
android:textColor="#ff0000ff" />
</LinearLayout>
</LinearLayout>
source code
public class cFeetView extends cBaseView implements OnClickListener {
cFileNames mFileNames;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feet);
// add listeners
View mLeft = findViewById(R.id.butRight);
mLeft.setOnClickListener(this);
// add listeners
View mRight = findViewById(R.id.butLeft);
mRight.setOnClickListener(this);
mFileNames=new cFileNames();
mFileNames.Start();
DrawFeet();
}
public void DrawFeet()
{
int screenHeight;
ImageView picImage = (ImageView) findViewById(R.id.viewimage);// R.id.viewpic);
try {
String FileName = "canon20.png";
FileName=mFileNames.Current();
AssetManager assetManager = getAssets();
InputStream inputStream;
inputStream = assetManager.open(FileName);
Bitmap icon = BitmapFactory.decodeStream(inputStream);
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int bw = icon.getWidth();
int bh=icon.getHeight();
float t = (float) screenWidth / (float) bw;
int iConHeight=(int)((float)bh*t);
picImage.setImageBitmap(icon);
// scale it
picImage.getLayoutParams().width = screenWidth;
picImage.getLayoutParams().height =iConHeight;
Bitmap scaledIcon = Bitmap.createScaledBitmap(icon, screenWidth, iConHeight, false);
} catch (IOException e) {
}
}
// set the top and buttom margins
public void onClick(View v)
{
Intent i;
switch(v.getId())
{
case R.id.butLeft:
mFileNames.Pre();
DrawFeet();
break;
case R.id.butRight:
mFileNames.Next();
DrawFeet();
break;
}
}
} // end class
Instead of LinearLayout, use RelativeLayout in your xml file.
LinearLayout is used only to arrange them in horizontal or vertical directions.
In order to use alignParentBottom you need to use RelativeLayout. And you dont need the gravity attribute.
My suggestion for your particular scenario is that you you have a RelativeLayout and put all your buttons inside a LinearLayout that is aligned at bottom and the ImageView above it.
Psedo:
<RelativeLayout>
<ImageView />
<LinearLayout
android:layout_below="#ImageView_id"
android:layout_alignParentBottom=true>
</LinearLayout>
</RelativeLayout>
Edited with your xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/backFeetGallery"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/viewimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/background" />
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#id/viewimage"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:id="#+id/butLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Left "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butFavrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Favrets "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" email "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Right "
android:textColor="#ff0000ff" />
</LinearLayout>
</RelativeLayout>
You need to have everything in RelativeLayout. For example, look at this sample code which I made for one button which is aligned to bottom center of screen.
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
And here is the complete layout file for your layout
<?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" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/button2"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/button3"
android:text="Button" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:src="#android:drawable/gallery_thumb" />
This is how it looks,

Categories

Resources