I am trying to run MainActivity which extends View but its not running. Do i have to use MainActivity extends Activity to run the application because i want to use draw() method in MainActivity class.Can i use it directly or i have to use View instead of activity?.
Here is my code
public class MainActivity extends View {
public MainActivity(Context context) {
super(context);
}
Main.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
Yes your main activity (and any other) MUST extend Activity.
If you want to make your own, custom control, just write it (extend view or some of it's descendatns) and put it into your main layout as any other control.
In your custom control you can override onDraw() method if you want.
Take a look at the docs:
Custom controls
Try something like:
class MyActivity extends Activity {
...
#Override
protected void onCreate {
...
setContentView(R.layout.main);
....
}
}
After you setContentView, you can reference it by calling findViewById(<LinearLayout's id>). In your layout you can place the view that you want to do the drawing
Your activity should extend Actvity and you have get your draw image as view which is a object of class that extends View
I hope this will be usefull to you.
Related
I am trying to use Android Bootstrap library. I followed Quick Start. In Quick Start, it says I should override my class like this:
public class SampleApplication extends Application {
#Override public void onCreate() {
super.onCreate();
TypefaceProvider.registerDefaultIconSets();
}
}
How can I use this library without extending Application class? I want to use this library in my Activity classes.
LoginActivity:
public class Login extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TypefaceProvider.registerDefaultIconSets();
setContentView(R.layout.activity_login);
}
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.merve.tev.Login">
<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:bootstrapText="Medium {fa_thumbs_o_up}"
app:bootstrapBrand="regular"
app:roundedCorners="true"
app:bootstrapSize="md"
app:dropdownResource="#array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down"
tools:layout_editor_absoluteY="202dp"
tools:layout_editor_absoluteX="115dp" />
</LinearLayout>
In my MainActivity class, I placed the button. When I click it, I should go LoginActivity class. However, I get an error:
java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.beardedhen.androidbootstrap.BootstrapDropDown
In your activity class:
In onCreate() Method, write this line before setContentView();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TypefaceProvider.registerDefaultIconSets();
}
I hope it will work.
It's recommended to invoke TypefaceProvider.registerDefaultIconSets(); in your application class because that will load the FontAwesome typeface, before any views are displayed on screen.
If you're not loading FontAwesome icons then you can skip this step. If you're worried about startup time then you could try performing it asynchronously.
Finally, if you know for a fact that your app will always launch from a certain activity, then you can call TypefaceProvider.registerDefaultIconSets(); before setContentView is called, and should still be able to use FontAwesome icons.
The only tradeoff here is that most apps have multiple Activities which act as entry points, meaning you might have to add this setup logic to multiple places. That's why the current advice is to set it up in your Application class - you'll only ever need to initialise it once.
I am trying to inflate
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
in List adapter so that I can play video in the listview it self but I am getting error
Error inflating class com.google.android.youtube.player.YouTubePlayerView
while if I am using
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="#+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
It is getting inflated without any error and I am able to display thumbnail in list view
My requirement is when user click on this thumbnail video should play in list view
Please suggest how can I achieve this ?
From the documentation:
Using this View directly is an alternative to using the
YouTubePlayerFragment. If you choose to use this view directly, your
activity needs to extend YouTubeBaseActivity.
Therefore, you must make sure your activity extends YouTubeBaseActivity. Alternatively, if your activity does not need to extend an activity provided by the library, you can use the YouTubePlayerSupportFragment and FrameActivity from android.support.v4.
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="#+id/youtubesupportfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
In your java file MainActivity.java try to replace public class MainActivity extends AppCompatActivity with public class MainActivity extends YouTubeBaseActivity.
In addition to not_a_bot's answer, I'd like to add that you should make sure that you call super.onCreate(Bundle) method, as it seems that YouTubeBaseActivity class doesn't have the #CallSuper annotation.
If you do not want to use fragment in your layout, another solution that you can use is extends your activity from YouTubeBaseActivity() . This will allow you use <com.google.android.youtube.player.YouTubePlayerView/> inside your layout
Example:
class PlayVideoActivity : YouTubeBaseActivity(), YouTubePlayer.OnInitializedListener {...}
i stuck with a unusual problem
i have a restaurant application which includes menu icons fixed at the bottom of every layout,
my problem is i dont want to create onsetclicklistner() method of every icon on each of my activity class....
please give some suggestion so that i can make a common class where i can put all my footer icon click event and activities in it and use it on my every activity class with different setcontentview...
hope you all get my question...
looking forward for your reply
You don't need to setup onClickListeners programmatically. You can also put them in the layout XML like this:
<Button android:id="#+id/my_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_button_text"
android:onClick="myOnClick"
/>
Then you can declare a base activity class that contains this method in it, like this:
public class BaseActivity extends Activity {
public void myOnClick(View v) {
// Do whatever you want to do when the button is clicked
}
}
Then you write all of your activity classes so that they derive from BaseActivity instead of Activity.
I have a SettingsActivity which is a subclass of PreferenceActivity in my library project
The oncreate method looks like this
addPreferencesFromResource(R.xml.preferences);
setContentView(R.layout.main);
The preference.xml file has structure like element PreferenceScreen -> PreferenceCategory -> Preference
My main main.xml Looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!--Listview will be replaced by preferences -->
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The above class SettingsActivity is subclassed in another project and Looks like this
public class SettingsActivityFree extends SettingsActivity
{
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//...
// layout returned will be null ..
LinearLayout layout = (LinearLayout)findViewById(R.layout.main);
// Add the adView to it
layout.addView(adView);
}
The problem is I am trying to get LinearLayout from the parent class so that I can add my stuff in it, but for some reason it returns null , the reason I have LinearLyout in the SettingsActivity class is because I want to put some ads etc at the top of preferences and without putting ad specific code in the Library project
Please advise If I am missing something here,
Thanks
First you need to give your LinearLayout an id using android:id="#+id/whatever". Then you can search for it using findViewById(R.id.whatever).
You should really check the developer site. You are not doing it correctly. Look up the documentation on ListView and ListActivity. Learn how it works before using it.
Also as a hint:
You don't do this (LinearLayout)findViewById(R.layout.main);
When retrieving a view, you will find it in R.id.(whatever) not in R.layout.(whatever)
I have an Activity and i had set activity's content view as "R.layout.main.xml".And i have an another class which contains animation created using openGL. Now i need to use this animation in the background of the Activity.
The code is like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pixie);
mGLView = new ClearGLSurfaceView(this);
setContentView(mGLView);
}
But my app is Crashing ..How can i solve this.
When you call the setContentView() a second time, you replace what had been set the first time, leaving you with only the background. The crash is most likely because you depend on the elements in the main layout, which is removed.
Rather than calling setContentView() twice, you should include the GLSurfaceView in the main layout. Below is an example of how this can be done:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent>
<your.application.package.ClearGLSurfaceView
android:layout_width="match_parent"
android:layout_width="match_parent"/>
<!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie-->
</FrameLayout>
Then you can load this layout in your onCreate() as usual (main_pixie_new refers to the above xml, I just gave it that name to keep things as clear as possible):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pixie_new);
}