How to Choose Radio Button Value using Java Android? - java

If i'm click OK how to choose pick a value one service in radio button but different value.
void OpenDialogService() {
closeLyt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
cancelTxt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
okTxt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
Bundle b = activity.getIntent().getExtras();
if(b == null)
b = new Bundle();
b.putString("service_id", "1");
startActivity(new Intent(activity, Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
}
});
dialog.show();
}
In this line for primary service :
b.putString("service_id", "1")
How i make conditional if service other give value :
b.putString("service_id", "2")

This is code snippet how you can achieve this with the help of RadioGroups
Your dialog layout should look like this :-
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.test.MainActivity" >
<TextView
android:id="#+id/headerTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#ff669900"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="Pick Service"
android:textColor="#FFF"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_margin="10dp"
android:layout_marginTop="32dp" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/primaryTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:checked="true"
android:text="Primary Service"
android:textSize="18sp" />
<TextView
android:id="#+id/primaryContentTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/primaryTxt"
android:text=" -Dusting \n -Sweeping \n -Washing \n -Cleaning " />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="#+id/service_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/service_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" />
</RadioGroup>
<TextView
android:id="#+id/otherTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/primaryContentTxt"
android:layout_marginTop="10dp"
android:checked="true"
android:gravity="left"
android:text="Other Service(Soon)"
android:textSize="18sp" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/closeLyt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:weightSum="2" >
<TextView
android:id="#+id/cancelTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:padding="5dp"
android:text="CANCEL"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/okTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:padding="5dp"
android:text="OK"
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
And you can inflate this layout in custom dialog box and with the help of radio buttons you can choose service type
int serviceNumber = 0;
void OpenDialogService() {
final Dialog dialog = new Dialog(this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pop_new_order2);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
TextView headerTxt = (TextView) dialog.findViewById(R.id.headerTxt);
TextView okTxt = (TextView) dialog.findViewById(R.id.okTxt);
TextView cancelTxt = (TextView) dialog.findViewById(R.id.cancelTxt);
TextView primaryTxt = (TextView) dialog.findViewById(R.id.primaryTxt);
TextView otherTxt = (TextView) dialog.findViewById(R.id.otherTxt);
TextView primaryContentTxt = (TextView) dialog.findViewById(R.id.primaryContentTxt);
LinearLayout closeLyt = (LinearLayout) dialog.findViewById(R.id.closeLyt);
// Radio Buttons
final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = radio.findViewById(checkedId);
int index = radio.indexOfChild(radioButton);
Toast.makeText(getApplicationContext(), "service" +index, 500).show();
serviceNumber = index+1;
}
});
headerTxt.setText("Pick Services");
//TODO change color here
// headerTxt.setBackgroundResource(R.drawable.bg_dialog_header_success);
// closeLyt.setBackgroundResource(R.color.selector_close_alert_dialog_success);
closeLyt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
cancelTxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
okTxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Bundle b = getIntent().getExtras();
if (b == null)
b = new Bundle();
b.putString("service_id", String.valueOf(serviceNumber));
Toast.makeText(getApplicationContext(), "service " + String.valueOf(serviceNumber), 500).show();
//TODO Handle Activity Transition Here
// startActivity(new Intent(MainActivity.this,
// Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
// overridePendingTransition(R.anim.push_left_in,
// R.anim.push_left_out);
}
});
dialog.show();
}
Result

You can get the selected item id of RadioGroup using, radioGroup.getCheckedRadioButtonId() and use if-else to choose different strings.
int id = radioGroup.getCheckedRadioButtonId();
if(id == radioButton1)
{
b.putString("service_id", "1");
}
else if(id == radioButton1)
{
b.putString("service_id", "2");
}

Related

setOnClickListener not working in fragment Android

I am trying to apply a setOnClickListener on an EditText View in a Fragment and for some reason, It is not working, the code in the listener doesn't execute when I click on the EditText.
The fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<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="16dp"
tools:context=".add_payment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/testpay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:inputType="time" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Table:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Amount:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_amm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
</LinearLayout>
The fragment code:
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_payment_fragment, container, false);
view.findViewById(R.id.add_pay_time).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
}
});
return view;
}
Edit text needs focus to call onClick().
Solution 1
request focus when you set listener:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
}
});
editText.requestFocus(); //request focus
Solution 2
set on touch listener:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction())
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
return false;
}
});
Solution 3
you can use onClickListener and onFocusChangeListener together:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToast();//call your method
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
showToast();//call your method
}
});
EditText edText = view.findViewById(R.id.add_pay_time);
edText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "aaaa", Toast.LENGTH_LONG).show();
}
});
Use getActivity().getApplicationContext() instead

Focus changes to listview once i clear the Edittext

I'm using broadcast receiver to show a dialog.So the flow of code is something like:
Step1 Getting the requestCode value
Step2 Based on this requestCode the broadCast receiver goes to if or else if or else part
Step3 If the value that i entered using some scanner into the EditText(i.e Scan) doesn't matches it shows a Toast "Item Not Available".
Step 4 Once "Item Not Available" toast comes the focus changes to the Listview which is my problem.
Step5 Again if i pass value to the Scan EditText the Listview get click automatically.
So my question is "How to remove focus from the Listview" and set it to the EditText(i.e Scan).
For Reference I'm attaching the snap with code snippet and the layout.xml.Please have a look and drop your suggestions why the focus is going to the listview.
.java snippet
final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
loc = mspinner.getItemAtPosition(mspinner.getSelectedItemPosition())
.toString();
final String ItemNo;
final String Desc;
final String StockUnit;
final String PickSeq;
final String qtyCount;
final String qtyonHand;
final Button mok;
final Button mcancel;
final Button mplus;
final Button mminus;
final EditText medtQtyCount;
final EditText medtItem;
final EditText medtdesc;
final EditText medtuom;
final DatabaseHandler dbHandler;
final String[] UOM = null;
int requestCode;
LayoutInflater li = LayoutInflater.from(InventoryCount.this);
View promptsView = li.inflate(R.layout.quantityupdate, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
InventoryCount.this);
alertDialogBuilder.setView(promptsView);
//requestCode=Integer.parseInt(intent.getStringExtra("idx"));
requestCode=intent.getIntExtra("idx", -1);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
dbHandler = new DatabaseHandler(InventoryCount.this);
medtuom = (EditText) promptsView.findViewById(R.id.edt_mseshipuom_mic);
mok = (Button) promptsView.findViewById(R.id.btn_mseshipOk_mic);
mcancel = (Button) promptsView.findViewById(R.id.btn_mseshipCancel_mic);
mplus = (Button) promptsView.findViewById(R.id.btn_mseshipIncr_mic);
mminus = (Button) promptsView.findViewById(R.id.btn_mseshipDecr_mic);
medtQtyCount = (EditText) promptsView
.findViewById(R.id.edt_shipShiped_mic);
medtdesc = (EditText) promptsView
.findViewById(R.id.edt_mseshipQtyOrd_mic);
medtItem = (EditText) promptsView
.findViewById(R.id.edt_mseshipItemNo_mic);
if (requestCode == 1) {
}
else if (requestCode == 0) {
// ItemNo
/*if (resultCode == RESULT_OK) {
Log.i("Scan resul format: ",
intent.getStringExtra("SCAN_RESULT_FORMAT"));
*/
String itNo = intent.getStringExtra("SCAN_RESULT");
dbhelper.getReadableDatabase();
MIC_Inventory mic_inventory = dbhelper.getMicInventoryDetails(
loc, itNo);
dbhelper.closeDatabase();
if (mic_inventory != null) {
loc = mspinner.getItemAtPosition(
mspinner.getSelectedItemPosition()).toString();
ItemNo = mic_inventory.getItemno();
Desc = mic_inventory.getItemdescription();
PickSeq = mic_inventory.getPickingseq();
StockUnit = mic_inventory.getStockunit();
qtyonHand = mic_inventory.getQoh();// This value gives
// QOHand
qtyCount = mic_inventory.getQc();
medtItem.setText(ItemNo);
medtdesc.setText(Desc);
medtQtyCount.setText(qtyCount);
medtuom.setText(StockUnit);
mplus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String a = medtQtyCount.getText().toString();
int b = Integer.parseInt(a);
b = b + 1;
a = a.valueOf(b);
medtQtyCount.setText(a);
}
});
mminus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = Integer.parseInt(medtQtyCount.getText()
.toString());
c = c - 1;
medtQtyCount.setText(new Integer(c).toString());
}
});
mok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*
* UOM[mspinnerUom.getSelectedItemPosition()] =
* medtQtyCount .getText().toString();
*/
MIC_UOMInternal mic_uom = new MIC_UOMInternal();
mic_uom.setLocation(loc);
mic_uom.setItemno(ItemNo);
String updatedqtyCount = medtQtyCount.getText()
.toString();
if (!qtyCount.equals(updatedqtyCount)) {
mic_uom.setQc(Double
.parseDouble(updatedqtyCount));
mic_uom.setUom(StockUnit);
MIC_Inventory mic_Inventory = new MIC_Inventory();
mic_Inventory.setItemdescription(Desc);
mic_Inventory.setItemno(ItemNo);
mic_Inventory.setLocation(loc);
mic_Inventory.setPickingseq(PickSeq);
mic_Inventory.setQc(updatedqtyCount);
mic_Inventory.setQoh(qtyonHand);
mic_Inventory.setStockunit(StockUnit);
dbHandler.getWritableDatabase();
String result = dbHandler
.insertIntoInternal(mic_uom);
if (result.equals("success")) {
result = dbHandler.updateMIC(mic_Inventory);
}
dbHandler.closeDatabase();
}
Intent i = new Intent(InventoryCount.this,
InventoryCount.class);
i.putExtra("et", 1);
i.putExtra("LOCATION", loc);
// i.putExtra("ID", ID);
startActivity(i);
// InventoryCount.this.finish();
}
});
mcancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.cancel();
}
});
// show it
alertDialog.show();
} else {
/*
* Toast.makeText(this, "Item not available",
* Toast.LENGTH_LONG).show();
*/
toastText.setText("Item not available");
Toast toast = new Toast(getBaseContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
msearchtext.setText("");
/*msearchtext.setFocusableInTouchMode(true);
msearchtext.requestFocus();*/
/*msearchtext.setSelection(0);
lstView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
*/msearchtext.requestFocus();
}
else if (requestCode == 2) {
}
else
{
toastText.setText("Problem in Scanning");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
}
}
Layout.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="#drawable/border_green"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/txt_InvTitle"
style="#style/pageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/invTitle" />
<View
android:id="#+id/txt_InvView"
android:layout_width="match_parent"
android:layout_height="2dip"
android:layout_below="#+id/txt_InvTitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#2E9AFE" />
<LinearLayout
android:id="#+id/invLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/txt_InvView"
android:layout_marginTop="16dp" >
<TextView
android:id="#+id/txtLoc"
style="#style/textRegular"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center"
android:text="#string/location" />
<Spinner
android:id="#+id/sploc"
style="#style/SpinnerItemAppTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:editable="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/invScanType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/invLocation"
android:layout_gravity="center"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="18dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/edt_Search_mic"
style="#style/EditTextAppTheme_Scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_weight=".15"
android:gravity="center"
android:hint="#string/scan" />
<RadioGroup
android:id="#+id/radioScanBasedOn_mic"
style="#style/RadioButtonAppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radioInum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:button="#drawable/radiobutton_selector"
android:checked="true"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/itemno" />
<RadioButton
android:id="#+id/radioNum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/manfno" />
<RadioButton
android:id="#+id/radioUpc_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/upc" />
</RadioGroup>
</LinearLayout>
<HorizontalScrollView
android:id="#+id/scroll_full_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/invScanType" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="25dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lay_fullTitle_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
style="#style/textRegular_list"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="#string/itemno"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="#string/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/pick_seq"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qoh"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/uom"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/lst_msefull_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/ListViewAppTheme.White" >
</ListView>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/lay_PO_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="41dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:visibility="gone" >
<Button
android:id="#+id/btn_OrderLstImport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExit_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Add a textwatcher to edit text and check when text is not blank and it is not equal to expected text then only switch the focus.
/* Set Text Watcher listener */
yourEditText.addTextChangedListener(passwordWatcher);
and check for text once user enter text
private final TextWatcher passwordWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
if (s.length() != 0 && passwordEditText.getText().equals("Your expected text")) {
// show your toast and change focus
}
}
}
You should make your listview not focusable by using setFocusable(false) when not required and when you get response correctly from barcode scanner then you can again make your listview focusable.

Outputting in different layout

I'm currently developing an mobile grocery app. I just want to ask you why my code in layout Y cant throw the details from different activity, but layout X can! As you can see, I used checkbox to checked all items you want to purchase, but in Layout Y, if u checked the item you want and click the "add to cart" button it crashed.
Tho I reedit the code based on respective information and copy to layout Y.
Here is my Code
Baby Diaper (Layout X) Java
public class Baby_Diaper extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoCart;
Intent i = new Intent(this, Shopping_List.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baby__diaper);
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_diaper);
}
public void SelectItem (View view) {
boolean checked = ((CheckBox) view) .isChecked();
switch (view.getId())
{
case R.id.pampers:
if(checked)
{selection.add("Pampers");}
else
{
selection.remove ("Pampers");
}
break;
case R.id.huggies:
if(checked)
{selection.add("Huggies");}
else
{
selection.remove ("Huggies");
}
break;
case R.id.johnsons:
if(checked)
{selection.add("Johnsons");}
else
{
selection.remove ("Johnsons");
}
break;
case R.id.supreme:
if(checked)
{selection.add("Supreme");}
else
{
selection.remove ("Supreme");
}
break;
}
}
public void ocaddtocart(View view){
String final_shopping_selection = "";
for (String Selections : selection){
final_shopping_selection = final_shopping_selection + Selections + "\n";
}
final_text.setText(final_shopping_selection);
final_text.setEnabled(true);}
public void ocgtshoppinglist (View view){
Intent x = new Intent(Baby_Diaper.this, Shopping_List.class);
x.putExtra("items", final_text.getText().toString());
startActivity(x);
}
Baby Food (Layout Y) Java
public class Baby_Food extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoFood;
Intent i = new Intent(this, Shopping_List.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addtoFood = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
setContentView(R.layout.activity_baby__food);
}
public void SelectItem (View view) {
boolean checked = ((CheckBox) view) .isChecked();
switch (view.getId())
{
case R.id.coryandgate:
if(checked)
{selection.add("Cory & Gate");}
else
{
selection.remove ("Cory & Gate");
}
break;
case R.id.gerber:
if(checked)
{selection.add("Gerber");}
else
{
selection.remove ("Gerber");
}
break;
case R.id.hipp:
if(checked)
{selection.add("Hipp");}
else
{
selection.remove ("Hipp");
}
break;
}
}
public void ocaddtocart(View view){
String final_shopping_selection = "";
for (String Selections : selection){
final_shopping_selection = final_shopping_selection + Selections + "\n";
}
final_text.setText(final_shopping_selection);
final_text.setEnabled(true);
}
public void ocgtshoppinglist (View view){
Intent x = new Intent(Baby_Food.this, Shopping_List.class);
x.putExtra("items", final_text.getText().toString());
startActivity(x);
}
Baby Diaper (Layout X) 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="com.example.admin.mobile_grocery.Baby_Diaper"
android:id="#+id/baby_diaper">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pampers"
android:id="#+id/pampers"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:checked="false"
android:onClick="SelectItem"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/huggies"
android:id="#+id/huggies"
android:layout_below="#+id/pampers"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/johnsons"
android:id="#+id/johnsons"
android:layout_below="#+id/huggies"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
android:inputType="textNoSuggestions"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/supreme"
android:id="#+id/supreme"
android:layout_below="#+id/johnsons"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
android:inputType="textNoSuggestions"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/addtocart"
android:id="#+id/addtocart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:inputType="textNoSuggestions"
android:onClick="ocaddtocart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello Shoppers!"
android:id="#+id/final_shopping_diaper"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO SHOPPING LIST"
android:id="#+id/gt_shopping_list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="ocgtshoppinglist"
/>
Baby Food (Layout Y) 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="com.example.admin.mobile_grocery.Baby_Food"
android:id="#+id/baby_food">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cory & Gate"
android:id="#+id/coryandgate"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:checked="false"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gerber"
android:id="#+id/gerber"
android:layout_below="#+id/coryandgate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hipp"
android:id="#+id/hipp"
android:layout_below="#+id/gerber"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD TO CART / REMOVE"
android:id="#+id/addtocart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="ocaddtocart"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello Shoppers!"
android:id="#+id/final_shopping_food"
android:layout_below="#+id/hipp"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO SHOPPING LIST"
android:id="#+id/gt_shopping_list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="ocgtshoppinglist" />
When I clicked the link on Logcat it pointed me to
final_text.setText(final_shopping_selection); of layout Y
try to change it in Y
addtoFood = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
setContentView(R.layout.activity_baby__food);
to
setContentView(R.layout.activity_baby__food);
addtoFood = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
I think your text view is null in addtocart method.

intent to take bold or italic from layout to another layout

my code is to change edittext to bold or italic .
this code to write a text in editText and send it to another layout when touch in image1
this has intent to take text and size of text
i want another intent to take the bold or italic also .
public class Graduation extends ActionBarActivity {
Button button;
ImageView imageView ;
Spinner spinner;
int fontSizeInt;
String[]items = { "12", "16", "18", "20", "24", "28" , "30" };
int fontSizeFamily;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.graduation);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int position = spinner.getSelectedItemPosition();
try
{
fontSizeInt = Integer.parseInt(items[position]);
}
catch (NumberFormatException e)
{
fontSizeInt = 12; // Default size.
}
et.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) fontSizeInt);
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
addListenerOnImageg1() ;
addListenerOnButton1();
addListenerOnButton2();
et = (EditText) findViewById(R.id.et);
}
public void addListenerOnButton1() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
et.setTypeface(null,Typeface.BOLD);
}
}); }
public void addListenerOnButton2() {
final Context context = this;
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
et.setTypeface(null,Typeface.ITALIC);
}
}); }
public void addListenerOnImageg1() {
final Context context = this;
imageView = (ImageView) findViewById(R.id.g1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, g1.class);
startActivity(intent);
Intent intent1 = new Intent(context , g1.class);
intent1.putExtra("fn" , et.getText().toString());
intent1.putExtra("font_size", fontSizeInt);
startActivity(intent1);
// here is the intent I want to take a style also to another layout
}
}); }
also i have .xml file which has all the content i want ..
something like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backf"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your Text Here : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow>
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold"
android:text="B" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:text="I" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choose your card : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/g1"
android:layout_width="122dp"
android:layout_height="111dp"
android:clickable="true"
android:onClick="onClick"
android:src="#drawable/g1" />
<ImageView
android:id="#+id/g2"
android:layout_width="122dp"
android:layout_height="111dp"
android:clickable="true"
android:onClick="onClick"
android:src="#drawable/g2" />
<ImageView
android:id="#+id/g3"
android:layout_width="122dp"
android:layout_height="111dp"
android:clickable="true"
android:onClick="onClick"
android:src="#drawable/g3" />
<ImageView
android:id="#+id/g4"
android:layout_width="122dp"
android:layout_height="111dp"
android:clickable="true"
android:onClick="onClick"
android:src="#drawable/g4" />
<ImageView
android:id="#+id/g5"
android:layout_width="122dp"
android:layout_height="111dp"
android:clickable="true"
android:onClick="onClick"
android:src="#drawable/g5" />
<ImageView
android:id="#+id/g6"
android:layout_width="122dp"
android:layout_height="111dp"
android:clickable="true"
android:onClick="onClick"
android:src="#drawable/g6" />
<ImageView
android:id="#+id/g7"
android:layout_width="122dp"
android:layout_height="111dp"
android:clickable="true"
android:onClick="onClick"
android:src="#drawable/g7" />
</LinearLayout>
</ScrollView>
so my text goes to another layout with no change so when i click bold button the change happen on editText but when i click image the text go to image1 layout without bold

nullpointerexception upon setOnClickListener

I have a button that brings up a dialog with an OK button, and I'm getting a nullpointerexception when I click on the OK button. Does anyone know what's wrong?
Here's my java code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
myListView = (ListView)findViewById(R.id.list);
new GetStuff().execute();
Button importButton = (Button)findViewById(R.id.doButton);
importButton.setEnabled(false);
importButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog pwdDialog = new Dialog(context);
pwdDialog.setContentView(R.layout.pwdentry);
pwdDialog.setTitle("Enter password");
TextView pwdText = (TextView)pwdDialog.findViewById(R.id.pwdText);
pwdText.setText("Enter password");
Button okBut = (Button)findViewById(R.id.okBut);
okBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pwdDialog.dismiss();
}
});
pwdDialog.show();
}
});
}
Here's the XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/pwdText"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Enter password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/pwdEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/pwdText"
android:ems="10"
android:inputType="numberPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/okBut"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_below="#+id/pwdEntry"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:text="OK" />
</RelativeLayout>
Thanks!
The problem is okBut will be null
Button okBut = (Button)findViewById(R.id.okBut);
you need to make this as
Button okBut = (Button)pwdDialog.findViewById(R.id.okBut);
Here you are retrieving a Button by using the id, R.id.okBut from your main layout R.layout.mylist.
Button okBut = (Button)findViewById(R.id.okBut);

Categories