Android wear - WatchViewStub deprecated - java

I am trying to build an Android Wear app. When I start new project with APK 23 (Marshmallow) and above, the default code I get with the new project are as follows;
public class MainActivity extends Activity {
private TextView mTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener()
{
#Override
public void onLayoutInflated(WatchViewStub stub)
{
mTextView = (TextView) stub.findViewById(R.id.textView);
}
});
}
}
But according to a Android blog, WatchViewStub class is deprecated from API 23 (Marshmallow). According to the blog files rect_activity_main.xml and round_activity_main.xml are replaced by one layout file. But my problem is even when I start a new project with newer APIs I still get the deprecated classes and files (XMLs) in my project. According to the blog, this is how the new default code should look like in main_activity.java;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text);
}
How do I fix this? Please comment before down voting. I have tried a lot of things around the web; Asking at stack overflow was my last resort.

I solved this problem by using one of Android studio's skeleton projects. Then amend the code as needed. I am not still sure how to do this by default when we open a new project.

Related

Error replacing ButterKnife annotation with native ViewBinding

I'm refactoring a lot my app activities and layouts (e.g. ButterKnife annotation -> viewBinding, *Layout --> ConstraintLayout). For a lot of them there were no problem, but at the moment I'm stuck in this situation of my Activity:
Original code:
...
#BindView(R.id.logged_user)
TextView mUserLoggedView;
#BindView(R.id.patient_code)
EditText mPatientCodeView;
#BindView(R.id.search_form)
ScrollView mFormView;
#BindView(R.id.search_progress_layout)
LinearLayout mProgressLayout;
#BindView(R.id.search_progress_text)
TextView mProgressTextView;
App application;
SearchActivityMapper mapper;
CompositeDisposable mCompositeDisposable;
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_search_patient);
ButterKnife.bind(this);
application = (App) getApplication();
application.getAppComponent().inject(this);
...
Code after review:
...
App application;
SearchActivityMapper mapper;
CompositeDisposable mCompositeDisposable;
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
viewBinding = ActivitySearchPatientBinding.inflate(getLayoutInflater());
setContentView(viewBinding.getRoot());
ButterKnife.bind(this);
application = (App) getApplication();
application.getAppComponent().inject(this);
...
I did the same in all the previous Activity and everything went well; when the latter Activity is launched I got this error
java.lang.NoSuchFieldError: No field mUserLoggedView of type Landroid/widget/TextView; in class Lit/company/etmhome/activities/search/SearchActivity; or its superclasses (declaration of 'it.company.etmhome.activities.search.SearchActivity' appears in /data/data/it.company.etmhome.debug/code_cache/.overlay/base.apk/classes27.dex)
I can't understand why it can't find that field that I removed and it's not present in the whole project.
Am I missing something???
Thanks in advance

How to use Custom Font with Youtube Player?

I have successfully integrated YouTube Player in my app but I am not able to use a custom font to other UI elements. On other Activities, the font is working.
public class PlayerActivity extends YouTubeBaseActivity implements CustomAdapter.Listner {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
}
}
I found the solution.
By using YoutubePlayerSupportFragment instead of YoutubePlayerView I can simply use AppCompatActivity and hence use Custom fonts in XML.

Layout rejects to read TextView,ImageViews,Any view at all

The xml code: -->Also tried with ImageView and buttonView, Picker, and so on, it won't read data from this xml.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is a textview."
android:background="#f00"></TextView>
Java code where i set the view to the activity containing this XML.
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
Now, what i SHOULD see is this:
What i REALLY see is this:
Also i have declared the java in the android Manifest.
maybe it'll help but This activity is reached by pressing 2 buttons from 2 different activities, I've tested the same logic with a new project and it works, but it doesn't work on my main project.
Make sure that the part where you start your activity with Intents is right.
It should be something like this :
Intent i = new Intent(context, YourActivity.class):
startActivity(i);
if there's no problem with that, try checking your layout's name for any chance of conflict.
At the last try changing your OnCreate to :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
Looks like there is no problem in your XML, but also check the root of your layout.
ANSWER: There was some weird bug i myself don't understand, i removed the java code and re-added it again, and now it suddenly works, Thanks for suggestions everyone.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}

Extending library activity doesn't call onCreate

I have added a project 'A' as library in my project 'B'. Now in project 'B', I have an activity 'MainActivity' which extends an activity 'SplashActivity' of project A.
When i'm running the app, the onCreate of 'MainActivity'(Project B) is never called, rather the OnCreate of 'SplashActivity'(Project A) is called every time. What could be the problem?
Also what if I want to call the onCreate method of 'SplashActivity'(Project A) and then the onCreate of 'MainActivity'(Project B), is it possible?
The code is given below:
public class MainActivity extends SplashActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_activity_temp);
new SplashActivity().setLogo();//never called
}
}
change your xml ref
setContentView(R.layout.activity_splash_activity_temp);
new SplashActivity().setLogo();
to
setContentView(R.layout.activity_main);
SplashActivity splashactivity = new SplashActivity();
splashactivity.setLogo();
I solved my own answer ,and thanks to #jimit patel and # ρяσѕρєя K , I solved it and and if anyone looking for an answer
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_splash_activity_temp);
ImageView imageView = (ImageView) findViewById(R.id.splash_logo);
super.setLogo(imageView,this);
super.onCreate(savedInstanceState);
}
Just call the super.onCreate at the end of the code , and the code will work just as expected . :)

how to get the cache size of installed applications in android

I'm using the following code to get a list of all installed apps on android, my question is how is it possible to get the size of the cache that's used by those applications,
any help would be appreciated.
public class CacheActivity extends Activity {
private ListView lv;
// private TextView tv;
private List<PackageInfo> pkNames;
// private Context context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cache);
lv = (ListView) findViewById(R.id.cacheList);
// tv = (TextView) findViewById(R.id.text1);
pkNames = getPackageManager().getInstalledPackages(0);
ArrayAdapter<PackageInfo> adapter = new ArrayAdapter<PackageInfo>(this,
android.R.layout.simple_list_item_1, pkNames);
lv.setAdapter(adapter);
}
}
I don't think that's currently possible. As mentioned in that thread there's no public API for that. However there's a little trick that was mentioned here to get the size of an installed application via an hidden function but not directly the cache size. I guess it is a bit normal they want to hide some information about other applications.

Categories