I got an custom class. Which works great.
public class FocusGameView extends SurfaceView implements Runnable
At the activity itself I want to put the 'FocusGameView' on a view that I already created on the xml file.
so I tried to use the 'inflate' like this:
public class FocusGame extends Activity {
FocusGameView fgv;
View v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fgv= new FocusGameView(this);
v=(View) findViewById(R.id.frame_focus_game);
LayoutInflater mInflater;
mInflater = LayoutInflater.from(fgv.getContext());
v = mInflater.inflate(R.layout.activity_focus_game, null);
setContentView(R.layout.activity_focus_game);
}
The result of this code is opening the activity and set the layout. without put the custom view on the view itself.
I really hope you could help me with that.
Thanks in advance;
Yaniv.
You can only add a view to a view group, not a view, so lets image your View v is a RelativeLayout:
public class FocusGame extends Activity {
FocusGameView fgv;
RelativeLayout v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_focus_game);
v=(View)findViewById(R.id.frame_focus_game);
fgv= new FocusGameView(this);
v.addView(fgv); //You only need to add the view to a parent to make it appear
}
Related
I have created a MainActivity, it has a layout which has different elements; e.g: TextBox, EditBox, Button.
I have created a ChildActivity that is extending from MainActivity, ChildActivity also has a Layout.
My question is, can i use the layout elements of MainActivity and display them in my ChildActivity
The elements you can use depend on what layout file you pass to setContentView(R.layout.my_layout_file); in the onCreate. So yes you can use them in both if you give both activities the same layout file, but they will be treated like separate layouts. E.g. if you set some text in a textview in Main, it will not show in Child.
yes you can access the parent-activity element through add child-activity layout in parent-activity layout.this way you can inflate both layout in child-activity.
ViewGroup viewGroup; is child layout container in parent-activity.
like
parent activity or NormalActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
ViewGroup viewGroup;
protected void onCreate(Bundle savedInstanceState,int res) {
onCreate(savedInstanceState);
viewGroup = (ViewGroup) findViewById(R.id.childContainer);
viewGroup.addView(LayoutInflater.from(this).inflate(res, viewGroup,false));
}
}
Child Activity
public class ChildActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState,R.layout.child_layout);
}
}
I have a Fragment that loads data from web
So I'd like to make a RelativeLayout to show to user that data is still loading
When finished I want to make my RelativeLayout disappear
But here's the problem..
On my Xml of Fragment I put my RelativeLayout with my loading bar visible..
So my fragments goes throught this step:
1)onCreate() {inside this I have Asynctask.execute()
2)onCreateView() {And here I can manage my RelativeLayout with loadingbar through Inflater and View}
3)Asyntask.onPostExecute() {And here I want to make disappear my relativelayout..}
BUT in Asynctask there's no way to access to my relativelayout, and of course if I try app crashes because of NullPointerException [Obvious]
How can I manage this problem?
It seems he did the following:
private RelativeLayout mRelativeLayout;
mRelativeLayout = findViewById(R.Layout.relativeLayoutId);
And use it later on in the class like
mRelativeLayout.doTheMagic();
Modify the constructor of your AsyncTask to accept a reference to the View, then you can modify it during the onPreExecute() and onPostExecute(Result) methods. Why aren't you just using a ProgressDialog instead? Much easier.
That's how I solved:
public class HomeFragment extends Fragment {
RelativeLayout mLoading_Bar;
public void onCreate(Bundle savedInstanceState) {
Asynctask.execute();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View cView = inflater.inflate(R.layout.fragment_home, container, false);
mLoading_Bar = (RelativeLayout) cView.findViewById(R.id.fragment_home_loadingdata_layout);
return cView;
}
class ASynctask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... args) {
//do something
}
protected void onPostExecute(Void string) {
mLoading_Bar.setVisibility(View.GONE);
}
}
}
And that's all folks
I have a class MyView that heritates from View (used to draw on it).
I want to put an image on the background and still be able to draw. Nothing basic works si far
Does any one have a solution?
thanks
public class MyView extends View {....}
in the main actvity :
MyView vueDraw = (MyView)findViewById(R.id.vueDraw);
Here is a very simple example that hopefully can get you started. This is what I do:
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = this.getWindow().getDecorView().getRootView();
view.setBackgroundResource(R.drawable.background_image);
}
}
Make sure that you're not setting the background color of MyView or any of its sub views (or set them to transparent, which is the default).
EDIT: You may have better luck with this approach (setting the background resource on the window and making sure that MyView's background is transparent:
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setBackgroundDrawableResource(R.drawable.background_image);
}
}
The TicTacToe example in Android may also prove to be useful. http://developer.android.com/resources/samples/TicTacToeLib/src/com/example/android/tictactoe/library/GameView.html
I used this code, but when i click on activity at runtime, it never hits in OnTouch() method. Can someone guide me what i am doing wrong? Should i need to setcontentview of this activity? Actually i want the coordinates of activity where user touch during execution.
public class TouchTestAppActivity extends Activity implements OnTouchListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.touch);
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
// TODO Auto-generated method stub
String test = "hello";
}
}
UPDATE:
You need to do this :
In your XML layout file, you need an ID for the root view: android:id="#+id/myView"
In youer onCreate() method, write this:
LinearView v= (LinearView) findViewById(R.id.myView);
v.setOnTouchListener(this);
Assuming that your root view is a LinearView
You should the onTouchListener to relevant GUI components.
For example:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.touch);
TextView someView = (TextView)findViewById(R.id.some_view_from_layout_xml);
someView.setOnTouchListener(this); // "this" is the activity which is also OnTouchListener
}
You have to add a the listener using setOnTouchListener() otherwise who will give call to your onTouch method. any listener works on any view. so you have to add the listener to the view eg button.setOnTouchListener(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.yourxml, null);
setCOntentView(vi);
vi.setOnTouchListener(this);
In onCreate, you need to set the content view (just uncommenting the second line should probably work) and then you need to set your OnTouchListener (your activity) as the onTouchListener for a view in your application.
Let's say you've got a view in your layout called "MainView"; it would look something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.MainView);
view.setOnTouchListener(this);
}
This article has a good example:
http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-2-building-the-touch-example/1763
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle(R.string.app_name);
setContentView(new SampleView(this));
}
}
SampleView.java:
public class SampleView extends View {
#Override
protected void onDraw(Canvas canvas) {
if (certaincondition = true) {
//add elements to canvas etc
} else {
//How do I do the below? The layout is defined in xml.
//I do not want to use Intent. Please help me
//create a layout from resource R.layout.idAbout and transfer control.
}
}
}
Use a layout inflater:
View newRootViewElement;
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newRootViewElement= li.inflate(R.layout.idAbout, null);
You can inflate a layout using
View.inflate(getContext(), R.layout.idAbout, viewParent);
where viewParent is a ViewParent that will be the parent of the inflated view (and can be null).
But what are you trying to do? It's more than a little odd to start a new activity or to modify the view hierarchy from within onDraw(). You might want to post a runnable to a Handler that will do what you want on the next cycle of the event loop. To start a new activity (such as displaying “About” info for the app) you should take a look at the Intent class.