Tuesday, November 29, 2022

How to change title of Activity in Android ?

When you are using the fragments and activities together in an application, it takes the title for the fragments automatically as the names of the fragments. But for the activities, it takes the app name itself as the title of the activity, So how to change the title of the activity.

 There are two ways to change the title of the activity. 

1. You can do it programmatically through activity by setting the title as below:

public class QuestionActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
setTitle("Questions ?");

}
}

Before:                                       After: 




2. You can do it through androidmanifest.xml by setting the label there.  Open Androidmanifest.xml, in the activity you can add the label as the string or from the string file below to display the title:


<activity
android:name=".QuestionActivity"
android:label="@string/Questions"
android:exported="false" />

Here is the title shown:




That's it. Happy coding


Wednesday, November 23, 2022

"No Application Encryption Key Has Been Specified" Error in Laravel Application

 While creating a new laravel application from the begining, you only need the below things:

  • A terminal
  • PHP-7.0 installed
  • composer installed and available in your path
  • npm installed aad available in your path.

After all the above installed in your computer , You can create a new Laravel project from the command prompt:

composer create-project laravel/laravel  Myapplication

Now you can run:

php artisan serve

This should give you a URL to view, so click on this and open it in your browser. This should show the default Laravel screen or Page. But some time you get a Error in the browser as " No application Encryption Key has been specified" 



This will happen if the application key is empty in .env file or the key is corrupted.
In such case you need to follow the below steps to solve this issue:

1. Generate application key by running the below command in your command prompt:
    php artisan key:generate




2. Now you can see. .env file your app key is set to a key if its empty. or its updated.

3. Now you can run php artisan serve



 4. And go to the browser and check the link, You will be able to see the default laravel page showing



Even if you see the error, you need to clear the cache with the command 
php artisan config:cache

And after this, run php artsan serve and you will be able to see the laravel default page. Hope this helps.

Tuesday, November 22, 2022

Playing a YouTube video in the Android application

We  want to play Youtube video by streaming it directly from YouTube without downloading. Also, while playing videos, I want provide menu options. I don't want to play video using default intent. How can we do this?

There are multiple ways to play the youtube video in your android application. One of the easiest way is to start the activity View by parsing the URI. Eg as below:

    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("youtube URL")));

that's 
"startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.youtube.com/watch?v=4TmBlhfJVnc")));



if this should happen on click of a button, you can add this inside onclick of the button:

btnvideo.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.youtube.com/watch?v=4TmBlhfJVnc")));
Log.i("Video", "Video Playing...."); } });

Now if  the video names are shown as the list of items. 
And on click of the list item, we need to fetch the URL from mysql database and 
pass it to the URL Parse.


BhagvadgeetaViewModel slideshowViewModel =
new ViewModelProvider(this).get(BhagvadgeetaViewModel.class);


binding = FragmentSlideshowBinding.inflate(inflater, container, false);
View root = binding.getRoot();

final TextView textView = binding.textSlideshow;
slideshowViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
ProgressBar progressBar = binding.progress;
ListView listview =binding.list;
final String[] videoName = new String[1];
progressBar.setVisibility(View.VISIBLE);

slideshowViewModel.getVideoList().observe(getViewLifecycleOwner(), videoList->{
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_2, android.R.id.text1, videoList);
listview.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
});

listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView1 = view.findViewById(android.R.id.text1);
String selectedItemname = (String) textView1.getText();
slideshowViewModel.getVideoPath(selectedItemname).observe(getViewLifecycleOwner(), S-> {
String videoPath = S;

Log.i("Video", "Video Playing....: "+videoPath);
// Toast.makeText(getActivity(),"Selected Path: "+videoPath,Toast.LENGTH_SHORT).show();
if(videoPath !=null) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(videoPath)));
}

});

}
});

return root;
}




And in the viewmodel, you need to write the code to get the video path:
You need to have a api which returns the youtube url by sending the youtube video name.
I have that in getpath.php file in my server.


Here we go:

LiveData<String> getVideoPath(String videoName){
mVideoPath = new MutableLiveData<>();
mVideoPath.setValue(loadVideoPath(videoName));
if(mVideoPath == null){
mVideoPath.setValue(loadVideoPath(videoName));
}
return mVideoPath;
}
private String loadVideoPath(String videoName) {
Handler myHandlerVideoPath = new Handler();

myHandlerVideoPath.post(new Runnable() {
@Override
public void run() {
String[] field = new String[1];
field[0] = "name";
String[] data = new String[1];
data[0] = videoName;
PutData putData = new PutData(SERVER+"getPath.php", "POST", field, data);
if (putData.startPut()) {
if (putData.onComplete()) {

String result = putData.getResult();
if(result.equals("All fields are required")){
//Toast.makeText(getActivity(), result,Toast.LENGTH_SHORT).show();
mText.setValue(result);
}
else {
videoPath = result ;

}
mVideoPath.setValue(videoPath);


}

}
}

});
return videoPath;
}