Button setText() wouldn't actually setText - java

I am trying to setText() to
Button btnFloor, btnTable;
Which isn't working ATM, actually I'm trying to send data from
FloorsActivity -> TablesActivity -> NewOrdersActivity
So how I pass Data from a activity to another?
FloorsActivity.java
#Override
public void onFloorItemClicked(int id) {
Intent intent = new Intent(this, TablesActivity.class);
intent.putExtra("FloorId", id);
startActivity(intent);
Toast.makeText(this, "Floor id : " + String.valueOf(id), Toast.LENGTH_SHORT).show();
}
TablesActivity.java
int floorId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_floors_tables);
initViews();
Intent intent = getIntent();
floorId = intent.getIntExtra("FloorId", 1);
}
#Override
public void onTableItemClicked(String name) {
String floorName = "F" + floorId;
Intent intent = new Intent(this, NewOrderActivity.class);
intent.putExtra("FloorId", floorId);
intent.putExtra("TableName", name);
intent.putExtra("FloorName", floorName);
startActivity(intent);
Toast.makeText(this, "Table Name : " + String.valueOf(name), Toast.LENGTH_SHORT).show();
}
NewOrderActivity.java
String floorName;
String tableName;
int floorId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_order);
initViews();
Intent intent = getIntent();
floorName = intent.getStringExtra("FloorName");
tableName = intent.getStringExtra("TableName");
floorId = intent.getIntExtra("FloorId", 1);
}
public void initViews() {
// Fetch view
btnFloor = (Button) findViewById(R.id.btn_floor);
btnTable = (Button) findViewById(R.id.btn_table);
//Set Views
btnFloor.setText(floorName);
btnTable.setText(tableName);
btnFloor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(NewOrderActivity.this, FloorsActivity.class);
startActivity(intent);
}
});
btnTable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(NewOrderActivity.this, TablesActivity.class);
intent.putExtra("FloorId", floorId);
startActivity(intent);
}
});
NewOrdersActivity is where I'm trying to set text
Here is my commit on Github for this full change
Here is the link to this project

You call initViews() before you've overloaded the intent extra's.
Fixed NewOrderActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_order);
Intent intent = getIntent();
floorName = intent.getStringExtra("FloorName");
tableName = intent.getStringExtra("TableName");
floorId = intent.getIntExtra("FloorId", 1);
initViews();
}

Related

applying onDestroy apps

I want to call a method onDestroy to make my apps whenever I kill it and when open the apps still shown the activity I want. But, when I'm applying the onDestroy still nothing happen and didn't show the activity I wanted. What I'm trying to do is make the apps still load the data. Tell me a way, to execute this method. I want to know every detail for my final project.
Here is my activity
public class ChooseJob extends AppCompatActivity {
public RecyclerView recyclerView;
public RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<BothJobs, OrderViewHolder> adapter;
private FirebaseDatabase database;
private DatabaseReference request;
private DatabaseReference history;
//string Intent
String cName;
String cPhone;
String idJob;
String cImage;
String cAirport;
String cAirlines;
String cLuggage;
String fDetail;
String currentTime;
String cPlace;
TextView orderLayout;
MaterialSpinner spinner;
TextView id;
APIService mService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_job);
mService = Common.getFCMService();
database = FirebaseDatabase.getInstance();
request = database.getReference("RequestDetail");
history = database.getReference("History");
recyclerView = findViewById(R.id.listOrders);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
callIntent();
if (getIntent() != null)
loadOrders();
}
#Override
protected void onDestroy() {
super.onDestroy();
callIntent();
if (getIntent() != null)
{
loadOrders();
} else {
Toast.makeText(this, "Nothing HAPPEN!!!", Toast.LENGTH_SHORT).show();
}
}
private void callIntent() {
Intent idJobCarrier = getIntent();
idJob = idJobCarrier.getStringExtra("id");
Intent phoneCarrier = getIntent();
cPhone = phoneCarrier.getStringExtra("carrierPhone");
Intent nameCarrier = getIntent();
cName = nameCarrier.getStringExtra("carrierName");
Intent imageCarrier = getIntent();
cImage = imageCarrier.getStringExtra("carrierImage");
Intent airportCarrier = getIntent();
cAirport = airportCarrier.getStringExtra("carrierAirport");
Intent airlinesCarrier = getIntent();
cAirlines = airlinesCarrier.getStringExtra("carrierAirlines");
Intent locationCarrier = getIntent();
cPlace = locationCarrier.getStringExtra("carrierLocation");
Intent luggageCarrier = getIntent();
cLuggage = luggageCarrier.getStringExtra("carrierLuggage");
Intent flightDetails = getIntent();
fDetail = flightDetails.getStringExtra("flightDetails");
Intent timeStampt = getIntent();
currentTime = timeStampt.getStringExtra("saveCurrentDate");
}
private void loadOrders() {
adapter = new FirebaseRecyclerAdapter<BothJobs, OrderViewHolder>(
BothJobs.class,
R.layout.ob_task,
OrderViewHolder.class,
request.orderByChild("placeSend").equalTo(cPlace)
) {
#Override
protected void populateViewHolder(OrderViewHolder viewHolder, BothJobs model, final int position) {
viewHolder.tvId.setText("ID : " + adapter.getRef(position).getKey());
viewHolder.tvName.setText("Company Name : " + model.getCompanyName());
viewHolder.tvStatus.setText("Status : " + Common.convertCodeToStatus(model.getStatus()));
viewHolder.tvLocation.setText("Location To send : " + model.getPlaceSend());
viewHolder.tvWeight.setText("Weight : " + model.getWeight());
viewHolder.btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// showUpdateDialog(adapter.getRef(position).getKey(), adapter.getItem(position));
// Toast.makeText(ChooseJob.this, "OK BOS.. aku mau ni job", Toast.LENGTH_SHORT).show();
}
});
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}

How to use a variable in a new activity?

I added a variable that pass form the first activity to the second one.
I want to use the info that has accepted from the first activity, at the new activity, and will save it on new double variable, display it as permanent on a textView.
Now, it appears only when I am clicking on the regular button that start the new activity.
As first step, I guess, I need to remove - "startActivity(intent1);".
How should I move on from here?
Java code:
First Activity (Name : settings.java)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
public void onClick (View v){
Intent intent = new Intent(settings.this, WaitressRecord.class);
startActivity(intent);
}
protected void onClickWait (View v) {
//--- Casting & Converting EditText "etSalaryWaitress" to Double "doubleSW".
btnWaitress =(Button)findViewById(R.id.btnWaitress);
etSalaryWaitress = (EditText) findViewById(R.id.etSalaryWaitress);
doubleSW = Double.parseDouble(etSalaryWaitress.getText().toString());
//---Casting Radio Button(s).
rbPercentage = (RadioButton)findViewById(R.id.rbPercentage);
rbShekel = (RadioButton)findViewById(R.id.rbShekel);
if (doubleSW < 100 ) {
if (rbPercentage.isChecked()) {
HafrashaP = 1 - (doubleSW / 100.0);
strHafPer = String.valueOf(HafrashaP);
Toast.makeText(settings.this, strHafPer, Toast.LENGTH_SHORT).show();
// start the SecondActivity
Intent intent1 = new Intent(this, WaitressRecord.class);
intent1.putExtra(Intent.EXTRA_TEXT, strHafPer);
startActivity(intent1);
} else if (rbShekel.isChecked()) {
HafrashaS = -doubleSW;
strHafShek = String.valueOf(HafrashaS);
Toast.makeText(settings.this, strHafShek, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(settings.this, "לא הוזנה סוג ההפרשה", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(settings.this, "מספר שגוי", Toast.LENGTH_SHORT).show();
}
}
New Activity: (Name : WaitressRecord.java)
public class WaitressRecord extends AppCompatActivity {
String strHafPer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waitress_record);
// get the text from MainActivity
Intent intent1 = getIntent();
strHafPer = intent1.getStringExtra(Intent.EXTRA_TEXT);
// use the text in a TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(strHafPer);
}
}
//First Activity
Intent intent= new Intent(this, SecondActivity.class);
Bundle extra = new Bundle();
mBundle.putString(VARIABLE_KEY, value);
intent.putExtras(mBundle);
startActivity(intent);
//on the second Acitivty
intent intent = getIntent();
Bundle bundleExtra;
//Null Checking
if (intent != null ) {
bundleExtra = getIntent().getExtras();
// Be sure your check your "VARIABLE KEY SAME AS in THE FIRST ACTIVITY
String resultString = extras.getString("VARIABLE_KEY");
}

Retrieve contact from mobile's phone directory

This code can open a built-in contacts directory on a mobile device. However, when I select a contact, the app reports "done", but doesn't retrieve the contact number -- the user can't send a message.
How do I retrieve the contact number? List View is not a solution: I need to return just the one number, not extra information.
Here's my code:
public EditText no;
public EditText msg;
public Button cancel;
public Button snd;
public String Number,name,Message;
public int run=0;
public static final int PICK_CONTACT=1;
public void callContacts(View v)
{
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,PICK_CONTACT);
}
#Override
public void onActivityResult(int reqCode,int resultCode,Intent data)
{
super.onActivityResult(reqCode,resultCode,data);
if(reqCode==PICK_CONTACT)
{
if(resultCode== ActionBarActivity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData,null,null,null,null);
if(c.moveToFirst())
{
name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_URI.toString()));
Toast.makeText(this,"done",Toast.LENGTH_SHORT).show();
}
}
}
}
and here is the onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
no = (EditText)findViewById(R.id.num);
msg = (EditText)findViewById(R.id.sms);
cancel = (Button)findViewById(R.id.cancel);
snd = (Button)findViewById(R.id.snd);
no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
msg.setText("");
no.setText("");
callContacts(null);
}
});
snd.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Number = no.toString();
Message = msg.getText().toString();
SmsManager manager = SmsManager.getDefault();
ArrayList<String>long_sms = manager.divideMessage(Message);
manager.sendMultipartTextMessage(Number,null,long_sms,null,null);
Toast.makeText(getApplicationContext(),"Sent Successfully",Toast.LENGTH_SHORT).show();
}
});
}
Here is the code just for selecting one phone number from contact list. i found this code simplest. let me know if there is any problem.
start activity with pick intent on phone data type:
findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(pickContact, PICK_CONTACT_REQUEST);
}
});
Now set onAcitivityResult();
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
switch (requestCode){
case PICK_CONTACT_REQUEST:
if (resultCode == RESULT_OK){
Uri contactUri = intent.getData();
Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null);
if (nameCursor.moveToFirst()){
String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
((EditText)findViewById(R.id.person_name)).setText(name);
((EditText)findViewById(R.id.enter_mobile)).setText(number);
nameCursor.close();
}
}
break;
}
}

How can i use Create Chooser on button click inside a subclass of dialog

Using this code to share audio file through create chooser but not working...
public class ActionDialog extends Dialog {
static String sSongTitle="";
static String sSongPath = "";
static File fSongFile ;
public ActionDialog(final Context context, Message response) {
super(context);
// Inflate our UI from its XML layout description.
setContentView(R.layout.after_save_action);
setTitle(R.string.alert_title_success);
((Button)findViewById(R.id.button_share))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Uri StringUri = Uri.fromFile(new File(sSongPath));
Intent intent = new Intent(android.content.Intent.ACTION_SEND, StringUri);
Intent in = new Intent(android.content.Intent.ACTION_SEND, StringUri, context, null);
intent.setType("audio/*");
startActivity(Intent.createChooser(in, "select any from the list:"));//getting error here as create method startActivity(Intent)
}
});
}
}
your Intent is intent not ishare
Uri is stringUri but you mention ed as StringUri..
can you post entire code.. to see errors if any
see this
code
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button) findViewById(R.id.button_share))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Uri stringUri = Uri.fromFile(new File(""));
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);// intent for sharing
sendIntent.putExtra(Intent.EXTRA_TEXT, stringurl);
sendIntent.setType("audio/*");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, StringTitle);
startActivity(sendIntent);// getting error
// as create
// method
// startActivity(Intent)
}
});
}

Activity called outside the tab

I am developing an app with 5 tabs and my last tab displays a list of menus. The problem appears when I click a menu tab, the menu activity appears nicely below my tab but when I click any of the menus (which it will call LoginActivity), the new Activity appears full screen not under the tab. How can I handle this? Below is my code.
TabActivity
package com.smartag.smarttreasure;
public class NfcSurveyActivity extends TabActivity {
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
int profileCount = db.getContactsCount();
if (profileCount <= 0) {
Intent intent = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(intent);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int profileCount = db.getContactsCount();
if (profileCount <= 0) {
Intent intent1 = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(intent1);
}
Bundle extras = getIntent().getExtras();
if (extras != null) {
tabToDisplay = extras.getString("tab");
if (tabToDisplay.equals("CAMERA")) {
barcodeData = extras.getString("barcodeData");
}
extras.clear();
}
TabHost tabHost = getTabHost();
// Home
TabSpec tbspecHome = tabHost.newTabSpec("Home");
tbspecHome.setIndicator("",
getResources().getDrawable(R.drawable.tab_account_style));
Intent iHome = new Intent(this, HomeActivity.class);
tbspecHome.setContent(iHome);
tabHost.addTab(tbspecHome);
// History
tabHost.addTab(tabHost
.newTabSpec("Fun")
.setIndicator("",
getResources().getDrawable(R.drawable.tab_fun_style))
.setContent(
new Intent(this, NfcSurveyActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
if (tabToDisplay != null && tabToDisplay.equals("REDEEM")) {
if (barcodeData != null && barcodeData.length() > 0) {
tabHost.addTab(tabHost
.newTabSpec("Camera")
.setIndicator(
"",
getResources().getDrawable(
R.drawable.tab_redeem_style))
.setContent(
new Intent(this, NfcSurveyActivity.class)
.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP)
.putExtra("autoLoadBarcodeData",
barcodeData)));
}
else {
tabHost.addTab(tabHost
.newTabSpec("Camera")
.setIndicator(
"",
getResources().getDrawable(
R.drawable.tab_redeem_style))
.setContent(
new Intent(this, NfcSurveyActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
} else {
tabHost.addTab(tabHost
.newTabSpec("Camera")
.setIndicator(
"",
getResources().getDrawable(
R.drawable.tab_redeem_style))
.setContent(
new Intent(this, NfcSurveyActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
// tabHost.setCurrentTab(2);
tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = tabHost
.getTabWidget().getChildAt(2).getLayoutParams().height + 19;
// Search
TabSpec tbspecSearch = tabHost.newTabSpec("Finder");
tbspecSearch.setIndicator("",
getResources().getDrawable(R.drawable.tab_finder_style));
Intent iSearch = new Intent(this, NfcSurveyActivity.class);
tbspecSearch.setContent(iSearch);
tabHost.addTab(tbspecSearch);
// Profile
TabSpec tbspecProfile = tabHost.newTabSpec("Quit");
tbspecProfile.setIndicator("",
getResources().getDrawable(R.drawable.tab_quit_style));
Intent iProfile = new Intent(this, NfcSurveyActivity.class);
tbspecProfile.setContent(iProfile);
tabHost.addTab(tbspecProfile);
for (int i = 0; i <= 4; i++) {
tabHost.getTabWidget()
.getChildTabViewAt(i)
.setBackgroundColor(
getResources()
.getColor(android.R.color.transparent));
if (i == 2) {
tabHost.getTabWidget()
.getChildTabViewAt(i)
.setPadding(
tabHost.getTabWidget().getChildTabViewAt(i)
.getPaddingLeft(),
tabHost.getTabWidget().getChildTabViewAt(i)
.getPaddingTop(),
tabHost.getTabWidget().getChildTabViewAt(i)
.getPaddingRight(), 20);
}
}
if (tabToDisplay != null && tabToDisplay.length() > 0) {
if (tabToDisplay.equals("CAMERA")) {
tabHost.setCurrentTab(2);
} else if (tabToDisplay.equals("HISTORY")) {
tabHost.setCurrentTab(1);
}
}
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
NfcSurveyConfiguration.SelectedTab = tabId;
}
});
}
}
MenuActivity
public class HomeActivity extends ListActivity {
static final String[] Account = new String[] { "Point History", "Scan History",
"Reward/Coupon History", "Share/Transfer History", "Personalise" };
String tabToDisplay = "";
String barcodeData = "";
SharedPreferences nfcSurveyConfiguration;
String profileId;
String pleaseWait = "";
protected boolean _taken;
protected File _directory;
protected String _filename;
protected String _fileExtension;
String profileName = "";
String profileEmail = "";
String profileStatus = "";
String profileLanguage = "";
String profileType = "";
DatabaseHandler db = new DatabaseHandler(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.listview_item_row, Account));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
//Toast.makeText(getApplicationContext(),
// ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(intent1); // This activity appears not in the tab
}
});
}
#Override
public void onPause() {
super.onPause();
NfcSurveyConfiguration.CurrentActiveTab = 0;
}
}
Any suggestion or advice is highly appreciated.
this code inside setOnItemClickListener help me to solve the problem.
View view1 = getLocalActivityManager().startActivity(
"ReferenceName",
new Intent(getApplicationContext(),
YourActivityClass.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
setContentView(view1);

Categories

Resources