Different Behaviour on Device and AVD - keyboard

Currently I am developing an app based on a custom Keyboard. Basically I have a layout with and edit text and a secondary xml with the keyboard layout. The app is supposed to launch, hide the default keyboard and show the custom one.
A couple of days ago I finished the app, I was testing it with AVDs with 2.1 2.2 and 2.3 all working like a charm!
After that I decided to try it on an actual device but the custom keyboard did't work so I debugged it. The problem happens when setting the onclick listeners for the keys, they all throw nullPointerException.
What really puzzles me is that on the AVD it works perfectly, debugged it also for the AVD and no nullPointerException at all.
Is this normal?
The code following:
public class Main extends Activity implements OnTouchListener, OnClickListener,
OnFocusChangeListener {
private EditText mEt;
private Button mBSpace, mBack, mBorrar;
private RelativeLayout mLayout, mKLayout;
private boolean isEdit = true;
private int w, mWindowWidth;
private String cL[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "Ñ", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", ".", "?", "!"};
private Button mB[] = new Button[40];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
setKeys(); //get ids from xml and setOnClickListeners for every button.
mEt = (EditText) findViewById(R.id.xEt);
mEt.setOnTouchListener(this);
mEt.setOnFocusChangeListener(this);
mEt.setOnClickListener(this);
mLayout = (RelativeLayout) findViewById(R.id.xK1);
mKLayout = (RelativeLayout) findViewById(R.id.xKeyBoard);
hideDefaultKeyboard(); //abrir teclado al prender app
enableKeyboard();
changeCapitalLetters();
changeCapitalTags();
} catch (Exception e) {
Log.w(getClass().getName(), e.toString());
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v == mEt) {
hideDefaultKeyboard();
enableKeyboard();
}
return true;
}
#Override
public void onClick(View v) {
if (v != mBack && v != mBorrar) {
addText(v);
} else if (v != mBorrar && v == mBack) {
isBack(v);
} else if (v != mBack && v == mBorrar) {
isBorrar(v);
}
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == mEt && hasFocus == true) {
isEdit = true;
}
}
private void addText(View v) {
if (isEdit == true) {
String b = "";
b = (String) v.getTag();
if (b != null) {
// adding text in Edittext
mEt.append(b);
}
}
}
private void isBack(View v) {
if (isEdit == true) {
CharSequence cc = mEt.getText();
if (cc != null && cc.length() > 0) {
{
mEt.setText("");
mEt.append(cc.subSequence(0, cc.length() - 1));
}
}
}
}
private void isBorrar(View v) {
if (isEdit == true) {
CharSequence cc = mEt.getText();
if (cc != null && cc.length() > 0) {
{
mEt.setText("");
}
}
}
}
private void changeCapitalLetters() {
for (int i = 0; i < cL.length; i++)
mB[i].setText(cL[i]);
}
private void changeCapitalTags() {
for (int i = 0; i < cL.length; i++)
mB[i].setTag(cL[i]);
}
// enabling customized keyboard
private void enableKeyboard() {
mLayout.setVisibility(RelativeLayout.VISIBLE);
mKLayout.setVisibility(RelativeLayout.VISIBLE);
}
// Disable customized keyboard
private void disableKeyboard() {
mLayout.setVisibility(RelativeLayout.INVISIBLE);
mKLayout.setVisibility(RelativeLayout.INVISIBLE);
}
private void hideDefaultKeyboard() {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEt.getWindowToken(), 0);
//imm.hideSoftInputFromWindow(mKLayout.getApplicationWindowToken(), 0);
}
//get ids del xml y setOnClickListeners
private void setKeys() {
mWindowWidth = getWindowManager().getDefaultDisplay().getWidth();
mB[0] = (Button) findViewById(R.id.xCero);
mB[1] = (Button) findViewById(R.id.xUno);
mB[2] = (Button) findViewById(R.id.xDos);
mB[3] = (Button) findViewById(R.id.xTres);
mB[4] = (Button) findViewById(R.id.xCuatro);
mB[5] = (Button) findViewById(R.id.xCinco);
mB[6] = (Button) findViewById(R.id.xSeis);
mB[7] = (Button) findViewById(R.id.xSiete);
mB[8] = (Button) findViewById(R.id.xOcho);
mB[9] = (Button) findViewById(R.id.xNueve);
mB[10] = (Button) findViewById(R.id.xA);
mB[11] = (Button) findViewById(R.id.xB);
mB[12] = (Button) findViewById(R.id.xC);
mB[13] = (Button) findViewById(R.id.xD);
mB[14] = (Button) findViewById(R.id.xE);
mB[15] = (Button) findViewById(R.id.xF);
mB[16] = (Button) findViewById(R.id.xG);
mB[17] = (Button) findViewById(R.id.xH);
mB[18] = (Button) findViewById(R.id.xI);
mB[19] = (Button) findViewById(R.id.xJ);
mB[20] = (Button) findViewById(R.id.xK);
mB[21] = (Button) findViewById(R.id.xL);
mB[22] = (Button) findViewById(R.id.xM);
mB[23] = (Button) findViewById(R.id.xN);
mB[24] = (Button) findViewById(R.id.xENIE);
mB[25] = (Button) findViewById(R.id.xO);
mB[26] = (Button) findViewById(R.id.xP);
mB[27] = (Button) findViewById(R.id.xQ);
mB[28] = (Button) findViewById(R.id.xR);
mB[29] = (Button) findViewById(R.id.xS);
mB[30] = (Button) findViewById(R.id.xT);
mB[31] = (Button) findViewById(R.id.xU);
mB[32] = (Button) findViewById(R.id.xV);
mB[33] = (Button) findViewById(R.id.xW);
mB[34] = (Button) findViewById(R.id.xX);
mB[35] = (Button) findViewById(R.id.xY);
mB[36] = (Button) findViewById(R.id.xZ);
mB[37] = (Button) findViewById(R.id.xPUNTO);
mB[38] = (Button) findViewById(R.id.xPREGUNTA);
mB[39] = (Button) findViewById(R.id.xEXCLAMACION);
mBorrar = (Button) findViewById(R.id.xBorrar);
mBSpace = (Button) findViewById(R.id.xSpace);
mBack = (Button) findViewById(R.id.xBack);
for (int i = 0; i < mB.length; i++)
mB[i].setOnClickListener(this);
mBorrar.setOnClickListener(this);
mBSpace.setOnClickListener(this);
mBack.setOnClickListener(this);
}
}
And the layout files:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="#+id/xMLayout"
android:background="#000000" android:layout_height="fill_parent"
android:focusable="true"><!-- android:orientation="vertical" -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="#+id/xsubLayout"
android:keepScreenOn="true"
android:layout_height="fill_parent"><!-- android:orientation="vertical" -->
<EditText android:id="#+id/xEt" android:layout_width="fill_parent"
android:focusableInTouchMode="true" android:layout_height="wrap_content" />
<!--<EditText android:id="#+id/et1" android:layout_width="fill_parent"
android:layout_below="#+id/xEt" android:layout_height="wrap_content" />-->
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="#+id/xK1"
android:layout_height="wrap_content"
android:visibility="gone"> <!-- android:orientation="vertical" -->
<include android:id="#+id/xKeyBoard" layout="#layout/keyboard"></include>
</RelativeLayout>
The keyboard layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hebrwKeyboardView" android:layout_width="fill_parent"
android:layout_alignParentBottom="true" android:layout_below="#+id/xsubLayout"
android:orientation="vertical" android:background="#252625"
android:visibility="visible" android:layout_height="225sp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="225sp"
android:orientation="vertical" android:layout_alignParentBottom="true"
android:clipChildren="true">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="225sp"
android:padding="0sp">
<TableRow android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="0sp">
<LinearLayout android:baselineAligned="true"
android:layout_width="fill_parent" android:layout_height="45sp"
android:fitsSystemWindows="true">
<Button android:soundEffectsEnabled="true" android:id="#+id/xCero"
android:layout_width="32sp" android:layout_height="fill_parent"
android:text="0" android:textColor="#000" android:tag="0"
android:padding="0sp" android:textStyle="bold" android:layout_gravity="center"/>
<Button android:soundEffectsEnabled="true" android:id="#+id/xUno"
android:layout_width="32sp" android:layout_height="fill_parent"
android:padding="0sp" android:textColor="#000" android:tag="1"
android:text="1" android:textStyle="bold" android:layout_gravity="center" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xDos"
android:layout_gravity="center" android:layout_width="32sp"
android:padding="0sp" android:layout_height="fill_parent"
android:text="2" android:tag="2" android:textStyle="bold"
android:textColor="#000" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xTres"
android:layout_width="32sp" android:layout_gravity="center"
android:layout_height="fill_parent" android:text="3" android:tag="3"
android:textColor="#000" android:textStyle="bold" android:padding="0sp"
android:fitsSystemWindows="true" /><!-- android:ellipsize="marquee" /> -->
<Button android:soundEffectsEnabled="true" android:id="#+id/xCuatro"
android:layout_width="32sp" android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" android:text="4"
android:tag="4" android:fitsSystemWindows="true"
android:textColor="#000" android:textStyle="bold"
android:ellipsize="marquee" android:padding="0sp"/>
<Button android:soundEffectsEnabled="true" android:id="#+id/xCinco"
android:layout_width="32sp" android:layout_height="fill_parent"
android:tag="5" android:layout_gravity="center" android:text="5"
android:fitsSystemWindows="true" android:textColor="#000" android:padding="0sp"
android:textStyle="bold" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xSeis"
android:layout_width="32sp" android:layout_gravity="center_horizontal"
android:layout_height="fill_parent" android:text="6" android:tag="6"
android:textColor="#000" android:textStyle="bold" android:padding="0sp"
android:fitsSystemWindows="true" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xSiete"
android:layout_width="32sp" android:layout_height="fill_parent"
android:text="7" android:fitsSystemWindows="true" android:tag="7"
android:textColor="#000" android:textStyle="bold" android:padding="0sp"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xOcho"
android:layout_width="32sp" android:layout_height="fill_parent"
android:text="8" android:fitsSystemWindows="true" android:tag="8"
android:textColor="#000" android:textStyle="bold" android:padding="0sp"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xNueve"
android:layout_width="32sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold" android:text="9"
android:fitsSystemWindows="true" android:tag="9" android:padding="0sp"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
</LinearLayout>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="0sp">
<LinearLayout android:baselineAligned="true"
android:layout_width="fill_parent" android:layout_height="45sp"
android:fitsSystemWindows="true">
<Button android:soundEffectsEnabled="true" android:id="#+id/xA"
android:layout_width="32sp" android:layout_height="fill_parent"
android:text="A" android:textColor="#000" android:tag="A"
android:padding="0sp" android:textStyle="bold" android:layout_gravity="center"/>
<Button android:soundEffectsEnabled="true" android:id="#+id/xB"
android:layout_width="32sp" android:layout_height="fill_parent"
android:padding="0sp" android:textColor="#000" android:tag="B"
android:text="B" android:textStyle="bold" android:layout_gravity="center" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xC"
android:layout_gravity="center" android:layout_width="32sp"
android:padding="0sp" android:layout_height="fill_parent"
android:text="C" android:tag="C" android:textStyle="bold"
android:textColor="#000" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xD"
android:layout_width="32sp" android:layout_gravity="center"
android:layout_height="fill_parent" android:text="D" android:tag="D"
android:textColor="#000" android:textStyle="bold" android:padding="0sp"
android:fitsSystemWindows="true" /><!-- android:ellipsize="marquee" /> -->
<Button android:soundEffectsEnabled="true" android:id="#+id/xE"
android:layout_width="32sp" android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" android:text="E"
android:tag="E" android:fitsSystemWindows="true"
android:textColor="#000" android:textStyle="bold"
android:ellipsize="marquee" android:padding="0sp"/>
<Button android:soundEffectsEnabled="true" android:id="#+id/xF"
android:layout_width="32sp" android:layout_height="fill_parent"
android:tag="F" android:layout_gravity="center" android:text="F"
android:fitsSystemWindows="true" android:textColor="#000" android:padding="0sp"
android:textStyle="bold" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xG"
android:layout_width="32sp" android:layout_gravity="center_horizontal"
android:layout_height="fill_parent" android:text="G" android:tag="G"
android:textColor="#000" android:textStyle="bold" android:padding="0sp"
android:fitsSystemWindows="true" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xH"
android:layout_width="32sp" android:layout_height="fill_parent"
android:text="H" android:fitsSystemWindows="true" android:tag="H"
android:textColor="#000" android:textStyle="bold" android:padding="0sp"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xI"
android:layout_width="32sp" android:layout_height="fill_parent"
android:text="I" android:fitsSystemWindows="true" android:tag="I"
android:textColor="#000" android:textStyle="bold" android:padding="0sp"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xJ"
android:layout_width="32sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold" android:text="J"
android:fitsSystemWindows="true" android:tag="J" android:padding="0sp"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
</LinearLayout>
</TableRow>
...
...
<TableRow android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fitsSystemWindows="true"
android:orientation="horizontal">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="45sp" android:gravity="bottom"
android:orientation="horizontal">
<Button android:soundEffectsEnabled="true" android:id="#+id/xBorrar"
android:textColor="#000" android:textStyle="bold"
android:layout_width="115sp" android:layout_height="fill_parent"
android:tag="borrarTodo" android:text="BORRA_TODO" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xSpace"
android:textColor="#000" android:textStyle="bold"
android:layout_width="135sp" android:layout_height="fill_parent"
android:tag=" " android:text="|___ESPACIO___|" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="#+id/xBack"
android:layout_width="70sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold" android:tag="back"
android:layout_gravity="center_horizontal|center_vertical|center"
android:fitsSystemWindows="true" android:text="BORRA" />
</LinearLayout>
</TableRow>
</TableLayout>
</TableLayout>
The app is based on an example I downloaded from:
http://tutorials-android.blogspot.com/2011/06/create-your-own-custom-keyboard-for.html
Any help would be great, I don't know what else to try right now...

I found what the problem was. The AVD I was using had a screen of 640x480 and used the standard layout xml. The physical device I used to test the app was a tablet with a screen of 800x600 and therefore targeted the layout-large xml which didn't which didn't have all the buttons.

Related

Adding view dynamically in fragment not persist after moving back from another fragment through navigation

I'm adding view dynamically in fragment which works fine but when I'm navigating to another fragment and return back to first fragment all layout is set as initial one (dynamically added view is not present). So here I have to save UI state untill my fragment's parent activity is not closed.
Initial Fragment Class Code
package com.hp.billingapp.ui.billfragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText;
import com.hp.billingapp.R;
import com.hp.billingapp.ui.BillingActivity;
public class OrderFragment extends Fragment {
Button pay, buttonAdd;
LinearLayout itemLayout;
Integer item = 1, emptyChk;
Float total;
ConstraintLayout constraintLayout;
Integer layoutId;
public OrderFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_order, container, false);
pay = root.findViewById(R.id.pay);
pay.setOnClickListener(v -> {
datafetch(root);
});
itemLayout = root.findViewById(R.id.itemLayout);
constraintLayout = root.findViewById(R.id.constraintLayout);
buttonAdd = root.findViewById(R.id.button);
buttonAdd.setOnClickListener(v -> addView());
return root;
}
private void datafetch(View root) {
Log.i("heetcount", String.valueOf(itemLayout.getChildCount()));
emptyChk = 0;
total = (float) 0;
for (int i = 0; i < itemLayout.getChildCount(); i++) {
View itemView = itemLayout.getChildAt(i);
TextView number = itemView.findViewById(R.id.number);
AutoCompleteTextView name = itemView.findViewById(R.id.namet);
TextInputEditText price = itemView.findViewById(R.id.pricet);
AutoCompleteTextView type = itemView.findViewById(R.id.typet);
if (name.getText().toString().isEmpty() || price.getText().toString().isEmpty() ||
type.getText().toString().isEmpty()) {
Snackbar.make(constraintLayout, "Please fill the required fields",
Snackbar.LENGTH_SHORT).show();
emptyChk += 1;
} else {
total = total + Float.parseFloat(price.getText().toString().trim());
}
}
if (emptyChk == 0) {
Log.i("heettotal", total.toString());
OrderFragmentDirections.ActionOrderFragmentToBillingFragment action =
OrderFragmentDirections.actionOrderFragmentToBillingFragment(total);
Navigation.findNavController(root).navigate(action);
((BillingActivity) getActivity()).heading("b");
}
}
private void addView() {
item = item + 1;
String text = "Item " + item;
View itemView = getLayoutInflater().inflate(R.layout.item_layout, null, false);
TextView number = itemView.findViewById(R.id.number);
ImageButton remove = itemView.findViewById(R.id.remove);
AutoCompleteTextView nametiet = itemView.findViewById(R.id.namet);
AutoCompleteTextView typetiet = itemView.findViewById(R.id.typet);
TextInputEditText pricetiet = itemView.findViewById(R.id.pricet);
number.setText(text);
remove.setOnClickListener(v -> removeView(itemView));
itemLayout.addView(itemView);
}
private void removeView(View itemView) {
Log.i("heetview", String.valueOf(itemView));
itemLayout.removeView(itemView);
}
}
Initial Fragment Layout Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backdropcenter"
android:theme="#style/Theme.MaterialComponents.NoActionBar"
tools:context=".ui.billfragment.OrderFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/item_layout" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Add Item"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/pay"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Pay"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Dynamically Added Layout Code
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/Theme.MaterialComponents.NoActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="Item 1"
android:textColor="#191b20"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/remove"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#00000000"
android:src="#drawable/ic_close"
app:tint="#191b20" />
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/nametil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Item Name"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/namet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/typetil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Type"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/typet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/pricetil"
style="#style/InputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Price"
app:boxBackgroundMode="filled"
app:endIconMode="clear_text"
app:endIconTint="#AA191b20">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/pricet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="numberDecimal"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
Snaps of issue
Initial state when first fragment starts
Dynamically view added and information filled
First fragment after returning from another fragment

android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>

Here is my fooddetail activity codes
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnCart;
ElegantNumberButton numberButton;
String foodId="";
FirebaseDatabase database;
DatabaseReference food;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
database = FirebaseDatabase.getInstance();
food = database.getReference("Food");
numberButton = (ElegantNumberButton)findViewById(R.id.number_button);
btnCart = (FloatingActionButton)findViewById(R.id.btnCart);
food_name = (TextView)findViewById(R.id.food_name);
food_description = (TextView)findViewById(R.id.food_description);
food_price = (TextView)findViewById(R.id.food_price);
food_image = (ImageView) findViewById(R.id.img_food);
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
if (getIntent() != null)
foodId = getIntent().getStringExtra("FoodId");
if(!foodId.isEmpty())
{
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId)
{
food.child(foodId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Food food = dataSnapshot.getValue(Food.class);
Picasso.with(getBaseContext()).load(food.getImage()).into(food_image);
collapsingToolbarLayout.setTitle(food.getName());
food_price.setText(food.getPrice());
food_name.setText(food.getName());
food_description.setText(food.getDescription());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
and here is my xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.order.m.jersonsordering.FoodDetail">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
android:id="#+id/app_bar_layout">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/collapsing"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#null"
app:layout_collapseMode="parallax"
android:id="#+id/img_food"
android:scaleType="centerCrop" />
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax"
android:id="#+id/toolbar"
app:title="Food Name">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnCart"
android:src="#drawable/ic_shopping_cart_black_24dp"
android:backgroundTint="#android:color/white"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:layout_anchor="#id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:useCompatPadding="true"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/nestedScrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/food_name"
android:layout_marginTop="8dp"
android:padding="12dp"
android:textColor="#000000"
android:text="Food Name"
android:textSize="21sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layout_price"
android:orientation="horizontal">
<TextView
android:layout_width="1dp"
android:layout_weight="9"
android:layout_height="wrap_content"
android:id="#+id/food_price"
android:layout_marginTop="8dp"
android:padding="12dp"
android:textColor="#000000"
android:text="1,000PHP"
android:textStyle="bold"
android:textSize="15sp"/>
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:layout_width="100dp"
android:layout_height="30dp"
android:id="#+id/number_button"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="18dp"
app:textSize="8sp"
app:backGroundColor="#color/colorAccent"
app:textColor="#000000"
app:initialNumber="1"
app:finalNumber="20">
</com.cepheuen.elegantnumberbutton.view.ElegantNumberButton>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/food_description"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
I dont know what is causing the error and i cant resolve it I tried to rebuild and clean the project
I also tried searching for similar error but none is showing
the app keeps on closing and then thats the error(attached photo)
pls help me thank you :)
First make sure that you have included design library implementation 'com.android.support:design:26.+' in your application level build.gradle.
Then replace:
<android.support.design.widget.FloatingActionButton
...
...
android:backgroundTint
/>
with
<android.support.design.widget.FloatingActionButton
...
...
app:backgroundTint
/>

Show PopupWindow on ListItem Click dynamically change ContentView data based on item click

I have a ListView in SomeActivity conatins employee names. On item list click i want to popup a window which displays employee details of clicked item.
i tried this popupwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="First Name"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Last Name"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/firstName" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text=" email address"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/emailAdress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView6"
app:layout_constraintTop_toBottomOf="#+id/lastName" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="phone number"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6" />
<TextView
android:id="#+id/phoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toBottomOf="#+id/emailAdress" />
In SomeActivity i wrote code like this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
String poName = ((TextView)view.findViewById(R.id.rowPOName)).getText().toString();
// View layout = inflater.inflate(R.layout.detailspopup, null);
initiatePopupWindow(poName);
}
});
private void initiatePopupWindow(String name) {
try {
Cursor cursor2 = null;
LayoutInflater inflater = (LayoutInflater) SomeSample.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.detailspopup, null);
String[] selectedArgs = {name};
try {
cursor2 = db.rawQuery("my query to get employee details", selectedArgs);
if (cursor2.moveToFirst()) {
String firstname = cursor2.getString(cursor2.getColumnIndex("firstname"));
String lastname = cursor2.getString(cursor2.getColumnIndex("lastname"));
String email = cursor2.getString(cursor2.getColumnIndex("email"));
String phone = cursor2.getString(cursor2.getColumnIndex("phone"));
TextView fNname = layout.findViewById(R.id.firstName);
TextView lName = layout.findViewById(R.id.lastName);
TextView emailAdd = layout.findViewById(R.id.emailAdress);
TextView phoneNumber = layout.findViewById(R.id.phoneNumber);
fNname.setText(firstname);
lName.setText(lastname);
emailAdd.setText(email);
phoneNumber.setText(phone);
}
} catch (Exception e) {
Log.d(TAG, e.getMessage());
} finally {
if (cursor2 != null && !cursor2.isClosed()) {
cursor2.close();
}
}
popupWindow = new PopupWindow(layout, 370, 450, true);
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
After executing code I got popup window without my employee details populated i.e TextViews are not displaying employee data. It displays only default texts i.e TextView.
need help to solve this

Automatically extension of the layout when i add a new textview

i have a big problem. I am working on a project at the university (in Germany). I have a plain text and a button (look at my code below).
Whenever I enter sth. into the plain text and click the button, I want it to be listed and the layout must be expand.
Did somebody have any idea???
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.alime.beispieltextview1.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:id="#+id/text1"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/frage1"
android:layout_marginTop="14dp"
android:text="Zutat: *"
android:textColor="#android:color/background_dark"
android:textSize="18sp"
android:textStyle="normal|bold" />
<AutoCompleteTextView
android:id="#+id/zutatensuche"
android:layout_width="260dp"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/text1"
android:background="#android:drawable/editbox_dropdown_light_frame"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="true"/>
<Button
android:id="#+id/button"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/zutatensuche"
android:layout_alignBottom="#+id/zutatensuche"
android:layout_alignEnd="#+id/suchbutton"
android:text="+"
android:background="#FF2FC10F"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="108dp"
android:id="#+id/layoutlist">
<TextView
android:id="#+id/zutat1"
android:layout_width="150dp"
android:layout_margin="10dp"
android:layout_height="30dp" />
<TextView
android:layout_height="30dp"
android:id="#+id/zutat2"
android:layout_width="150dp"
android:layout_margin="10dp" />
<TextView
android:layout_height="30dp"
android:id="#+id/zutat3"
android:layout_width="150dp"
android:layout_margin="10dp" />
<TextView
android:layout_height="30dp"
android:id="#+id/zutat4"
android:layout_width="150dp"
android:layout_margin="10dp" />
<TextView
android:layout_height="30dp"
android:id="#+id/zutat5"
android:layout_width="150dp"
android:layout_margin="10dp" />
<TextView
android:layout_height="30dp"
android:id="#+id/zutat6"
android:layout_width="150dp"
android:layout_margin="10dp" />
</LinearLayout>
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button8" />
</LinearLayout>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Die eingegebenen Zutaten werden nach betätigen der Button aufgelistet*/
Button zutatbutton = (Button) findViewById(R.id.button);
zutatbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText e = (EditText) findViewById(R.id.zutatensuche);
String text = e.getText().toString();
TextView t1 = (TextView) findViewById(R.id.zutat1);
TextView t2 = (TextView) findViewById(R.id.zutat2);
TextView t3 = (TextView) findViewById(R.id.zutat3);
TextView t4 = (TextView) findViewById(R.id.zutat4);
TextView t5 = (TextView) findViewById(R.id.zutat5);
TextView t6 = (TextView) findViewById(R.id.zutat6);
if (t1.getText().toString().trim().isEmpty()) {
t1.setText(text);
} else if (t2.getText().toString().trim().isEmpty()) {
String text2 = e.getText().toString();
t2.setText(text2);
} else if (t3.getText().toString().trim().isEmpty()) {
String text3 = e.getText().toString();
t3.setText(text3);
} else if (t4.getText().toString().trim().isEmpty()) {
String text4 = e.getText().toString();
t4.setText(text4);
} else if (t5.getText().toString().trim().isEmpty()) {
String text5 = e.getText().toString();
t5.setText(text5);
} else if (t6.getText().toString().trim().isEmpty()) {
String text6 = e.getText().toString();
t6.setText(text6);
}
e.setText("");
}
});
}
}

Android Grid Menu Layout

i am a beginner in android programming.( i am using android studio for coding)
I am trying to design a dashboard for my android application taking guidance from the following link
and its working well, but i want to make it as per
Like This Image
i need 2 column layout with icon image, Title 1 and title 2 with a Background image.
can anyone help me.
thank you
you have to use customGridView using BaseAdapter. In customGridView show the ImageView with TextView for each list item.
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
then set the adapter with your customized view
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}
Output:
At last, I found the solution...
your XML File`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bg"
android:padding="16dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
`
then AndroidButtonWithIconAndText.java class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class AndroidButtonWithIconAndText extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_button_with_icon_text);
}
}
and finally it may look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>

Resources