In principle, I have a database created in Firebase whose database is connected to the project (Android Studio). I have an Item called "urlmovie" which contains a URL for a movie.
How do I call that Item that contains that URL (it is uploaded in the Firebase database) I want that when I click on the play button it will launch the movie.
This is my MOVIES_ACTIVITY.JAVA Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movies);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ExtendedFloatingActionButton extendedFloatingActionButton =findViewById(R.id.fab);
extendedFloatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String peliculaName = getIntent().getStringExtra("title");
String url="";
switch (peliculaName) {
case "BAD BOYS FOR LIFE (2020)":
url = "IF I PLACE THE URL HERE, PLAY THE FILM BUT I DON'T WANT THE URL TO BE PLACED FROM THE PROJECT IF IT IS ALREADY IN THE DATABASE (FIREBASE FROM GOOGLE)";
break;
case "SCOOBY! (2020)":
url = "";
break;
case "BIRDS OF PREY (2020)":
url = "";
break;
}
Intent intent = new Intent(getApplicationContext(), player.class);
intent.putExtra("title", url);
startActivity(intent);
}
});
PLAYER.JAVA
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
getSupportActionBar().hide();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//VARIABLES
playerView = findViewById(R.id.plaver_view);
progressBar = findViewById(R.id.progress_bar);
//FULLSCREEN
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//VIDEO URL
Uri videoUrl= Uri.parse(getIntent().getStringExtra("title"));
click here to see the image (DATABASE FIREBASE)
Related
for example i have made a Nodemcu server and there are 3 links on the main webpage(192.168.18.100).
the thing i want to try is that when i click an android button this link get accessed without opening any new activity i.e i want to stay on the main activity.
this is my code so far that works but not according to what i want it.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button on = (Button) findViewById(R.id.button);
Button off =(Button) findViewById(R.id.button2);
Button pink = (Button) findViewById(R.id.button3);
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16236607"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_SEND, uri);
startActivity(intent);
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16203967"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
pink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16214167"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
You can try to implement a WebView on your MainActivity. When you click on any of your button, load the WebView with your desired url.
You can find the way to implement WebView in this link
I am trying to save my programmatically created CardView and TextView using OnSaveInstanceState and OnRestoreInstanceState as in the sample code.
While debugging, I notice that values are saved but when I quit the app and reopen it, nothing is being restored.
What am I doing wrong?
**#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("key", userInput.getText().toString());
savedInstanceState.putInt("LayoutId", mConstraintLayout.getId());
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getString("key");
savedInstanceState.getInt("LayoutId");
}**
This are the methods I am using after 'OnCreate'
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mConstraintLayout = findViewById(R.id.top_left);
addButton = findViewById(R.id.add);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View popup = findViewById(R.id.popup);
popup.setVisibility(View.VISIBLE);
}
});
mButton = (Button) findViewById(R.id.create);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CardView sticker = new CardView(mContext);
CardView.LayoutParams params = new CardView.LayoutParams(
CardView.LayoutParams.WRAP_CONTENT,
CardView.LayoutParams.WRAP_CONTENT
);
params.height=500;
params.width=500;
CheckBox privateCalendar = findViewById(R.id.checkPrivate);
CheckBox workCalendar = findViewById(R.id.checkWork);
CheckBox holidayCalendar = findViewById(R.id.checkHoliday);
if (privateCalendar.isChecked()){
sticker.setCardBackgroundColor(Color.parseColor("#FFCC00"));
}
else if (workCalendar.isChecked()){
sticker.setCardBackgroundColor(Color.parseColor("#FF8080"));
}
else if (holidayCalendar.isChecked()){
sticker.setCardBackgroundColor(Color.parseColor("#66B3FF"));
}
else{
sticker.setCardBackgroundColor(Color.parseColor("#FFCC00"));
}
sticker.setId(CardView.generateViewId());
sticker.getId();
sticker.setTag(CARD_VIEW_TAG);
sticker.setLayoutParams(params);
sticker.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) sticker.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData data = new ClipData(sticker.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(sticker);
sticker.startDrag(data
, shadowBuilder
, sticker
, 0
);
sticker.setVisibility(View.INVISIBLE);
return true;
}
});
TextView tv = new TextView(mContext);
tv.setLayoutParams(params);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setGravity(Gravity.CENTER);
userInput = findViewById(R.id.editText);
tv.setText(userInput.getText());
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
tv.setTextColor(Color.BLACK);
sticker.addView(tv);
mConstraintLayout.addView(sticker);
View popup = findViewById(R.id.popup);
popup.setVisibility(View.GONE);
}
});
}
SavedInstanceState and RestoreInstanceState will work if activity is recreated due to config change like orientation change or android system killed that activity due to memory issue, Then only savedInstanceState and restoreInstanceState comes in role.
If you finish activity by yourself and start activity again then there is no use of these methods.
Try to save data in SharedPreference instead savedInstanceState.
SharedPreferences pref =
getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
For Storing data:-
editor.putString("key", userInput.getText().toString());
editor.putInt("LayoutId", mConstraintLayout.getId());
editor.apply();
For retriving data:-
editor.getString("key", null); // getting String
editor.getInt("LayoutId", 0); // getting Integer
First of all if by "close the app" you mean destroy it, this will never work for you.
Second of all in onRestoreInstanceState you don't do anything with those values, you simply read them but don't store them anywhere.
If you mean to recreate the old form after switching back and forth between apps, with your app being destroyed in the background then, you need to only set values in onSaveInstanceState like you did, and in onCreate(Bundle savedInstance), you should do something like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstance == null) {
//read values from getIntent()
key = getIntent().getString("key);
LayoutId = getIntent().getInt("LayoutId");
} else {
//read values from savedInstanceState
key = savedInstanceState.getString("key");
LayoutId = savedInstanceState.getInt("LayoutId");
}
}
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
I have two Java classes. Where I want to retrieve TextView data from DrinkDetail.java into Cart.java in order to save it into Firebase database. The TextView is deliveryOption and wanted to save in Cart.java from Requests table.
How can I do it? Below are my Java codes.
DrinkDetail.java
public class DrinkDetail extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_detail);
private void showAlertDialogChooseOptions() {
alertDialog = new AlertDialog.Builder(DrinkDetail.this);
alertDialog.setTitle("Drop by our kiosk or rider deliver?");
LayoutInflater inflater = this.getLayoutInflater();
View takeaway_deliver = inflater.inflate(R.layout.takeaway_delivery, null);
btnTakeAway = (FButton)takeaway_deliver.findViewById(R.id.btnTakeAway);
btnDelivery = (FButton)takeaway_deliver.findViewById(R.id.btnDelivery);
deliveryOption = (TextView)takeaway_deliver.findViewById(R.id.deliveryOptionChosen);
alertDialog.setView(takeaway_deliver);
alertDialog.create();
btnTakeAway.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deliveryOption.setText("Take Away");
alertDialogTakeAway.show();
}
});
}
Cart.java:
public class Cart extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
private void showAlertDialog() {
alertDialog2 = new AlertDialog.Builder(Cart.this);
alertDialog2.setTitle("Cash on Delivery:");
alertDialog2.setMessage("Choose amount of money will be given to the rider upon order arriving: ");
LayoutInflater inflater2 = this.getLayoutInflater();
View cash_on_delivery = inflater2.inflate(R.layout.cash_on_delivery, null);
btnOrderNow = (FButton)cash_on_delivery.findViewById(R.id.btnOrderNow);
btnOrderNow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Submit to Firebase
Request request = new Request(
Common.currentUser.getPhone(),
Common.currentUser.getName(),
editAddress.getText().toString(),
txtTotalPrice.getText().toString(),
"0", //status
editComment.getText().toString(),
paymentMethod.getText().toString(),
moneySelected.getText().toString(),
//WANTED TO SAVE DELIVERY OPTION "TAKE AWAY" HERE,
cart
);
requests.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
//Order placed
alertDialogOrderPlaced.show();
alertDialog2.setCancelable(true);
}
});
alertDialog2.setView(cash_on_delivery);
alertDialog2.setIcon(R.drawable.icons8_cash_in_hand_filled_50);
}
Make a public method in the class you want to retrieve the data from. Like getData(), something similar to this:
public Data getData(){
return this.data;
}
If your using Intent to navigate from DrinkDetail to Cart then you can pass that string in Intent itself.
like this
Intent intent=new Intent(DrinkDetail.this, Cart.class);
intent.putExtra("Key","String value you want to pass");
startActivity(intent);
and can retrieve that string in Cart.java place below code in onCreate() method
String value;
if(getIntent()!=null)
value=getIntent().getStringExtra("Key");
Data can be stored using SharedPreferences.
In your DrinkDetail.java save the data.
SharedPreferences prefs = this.getSharedPreferences("File", MODE_PRIVATE);
prefs.edit().putString("deliveryOption", deliveryOption.getText()).apply();
In your Cart.java you can retrieve it back.
SharedPreferences prefs = this.getSharedPreferences("File", MODE_PRIVATE);
String deliveryOption = prefs.getString("deliveryOption", "default");
To send the data from one activity to another activity
DrinkDetail.java is first activity from where you want to send data
to another activity
DrinkDetail.java
TextView aText = (TextView )findViewById(R.id.textview);
String text = aText .getText().toString();
Intent myIntent = new Intent(view.getContext(),Cart.java);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
Cart.java is second activity which receive the Intent data whatever
you pass from DrinkDetail.java
Cart.java
public class Cart extends Activity {
TextView mTextview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Cart);
aTextview = (TextView)findViewById(R.id.textView);
aTextview.setText(getIntent().getStringExtra("mytext"));
}
I have made an application where the user enters text into a textbox which gets sent up to azure once a button is clicked. Once the user clicks the button they are taking to a separate java activity where the text from the textbox is added to a listbox. However once I close the app or change screen the text is not saved into the listbox permanently even though it is saved in the Azure. Can anyone help me to add the text from azure to my listbox permanently. The listbox is on my ListDeadlines activity.
My Code is as fallows
AddDeadline Activity
private MobileServiceClient mClient;
private EditText title;
public EditText editText;
public EditText editDate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_deadline);
try {
mClient = new MobileServiceClient(
"https://craigsapp.azure-mobile.net/",
"BTkcgnFQvevAdmmRteHCmhHPzdGydq84",
this
);
} catch (MalformedURLException e) {
e.printStackTrace();
}
editText = (EditText)findViewById(R.id.editText2);
editDate = (EditText)findViewById(R.id.editText3);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final ListDeadlines lst= new ListDeadlines();
lst.txtInput=(EditText)findViewById(R.id.txtinput);
ImageView btAdd=(ImageView)findViewById(R.id.btnAdd);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
title= (EditText) findViewById(R.id.txtinput);
Item item = new Item();
item.Text = title.getText().toString();
Intent returnIntent = getIntent();
returnIntent.putExtra("result", item.Text = title.getText().toString());
setResult(RESULT_OK, returnIntent);
mClient.getTable(Item.class).insert(item, new TableOperationCallback<Item>() {
public void onCompleted(Item entity, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
// Insert succeeded
} else {
// Insert failed
}
}
});
finish();
}
});
ListDeadline Activity
public ArrayList<String> arrayList;
public ArrayAdapter<String> adapter;
public ArrayAdapter<String> newad;
public EditText txtInput;
public EditText txtDate;
public int ADD_DEADLINE_REQUEST=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_deadlines);
final ListView listView = (ListView)findViewById(R.id.listv);
String[] items= {""};
arrayList=new ArrayList<>(Arrays.asList(items));
adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txtitem,arrayList);
listView.setAdapter(adapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Item newitem = new Item();
if (requestCode == ADD_DEADLINE_REQUEST){
if (resultCode == RESULT_OK) {
String item = data.getStringExtra("result");
arrayList.add(item);
adapter.notifyDataSetChanged();
Toast toast = Toast.makeText(getApplicationContext(),
"Deadline has been added", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 100, 0);
toast.show();
}
}
}
you'll have to query azure for the text and then populate your listbox. Or if you want to save the text locally then you can do it using shared preferences.
As #pooja said, you can try to get the text via query Azure Mobile Service or get from the local storage. However, for keeping consistent between local and cloud, you need to add Offline Data Sync to your Android Mobile Services app, please see the offical document https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-offline-data/.
There are two demo video below.
https://channel9.msdn.com/Shows/Cloud+Cover/Episode-155-Offline-Storage-with-Donna-Malayeri
http://azure.microsoft.com/documentation/videos/azure-mobile-services-offline-enabled-apps-with-donna-malayeri/ (For Windows, but feature discussion applies to all platforms.)
And an Android sample for offline data sync on GitHub you can refer to, please see https://github.com/Azure/mobile-services-samples/tree/master/TodoOffline/Android.
It doesn't show any error but when i run it on my phone the app crashes everytime!
`PizzaType.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pizzatype);
RadioButton rButton = (RadioButton) findViewById(R.id.radioButton);
String rText = rButton.getText().toString();
Intent intent = new Intent(Pizzatype.this, Overview.class);
intent.putExtra("Bbread","Brown Bread");
startActivity(intent);
String data = getIntent().getExtras().getString("Bbread");
}
Overview.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
String value= getIntent().getStringExtra("Bbread");
Intent intent = new Intent(this, Pizzatype.class);
intent.putExtra("Bbread", value);
startActivity(intent);
String data= getIntent().getStringExtra("Bbread");
}
Try cleaning and re-running your project. Some times the R.java file get corrupted. and it leads to null pointer exceptions.