How set a view transparent? - android

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

Related

android view.findViewById and activity.findViewById, one cannot show the data

I've got a problem between view.findViewById and activity.findViewById.
Consisely it's when I use view.findViewById the data will not show and have no error report at all. And when I use the activity.findViewById , everything is fine. I dont know why or if I have made any mistake. Please give me some sugguestions.
MainActivity code
public class MainActivity extends AppCompatActivity {
ViewManager viewManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewManager=new ViewManager(this);
viewManager.setText("hello");
}
}
ViewManger Code
public class ViewManager {
private AppCompatActivity activity;
ViewManager(AppCompatActivity activity){
this.activity=activity;
}
public void setText(String text){
//in this way the word"hello" cannot be shown
View view= LayoutInflater.from(activity).inflate(R.layout.activity_main,null);
TextView textView=view.findViewById(R.id.tv);
//this way it works but i dont know why i m wrong above
//TextView textView=activity.findViewById(R.id.tv);
textView.setText("hello");
}
}
You are creating a separate view hierarchy which has no link with your activity layout so nothing will happen on screen
so you can set view reference as activity's layout
public void setText(String text){
View view= LayoutInflater.from(activity).inflate(R.layout.activity_main,null);
activity.setContentView(view);
//^^^^^^^^^^^^^^^^^^^^^^
TextView textView=view.findViewById(R.id.tv);
//this way it works but i dont know why i m wrong above
//TextView textView=activity.findViewById(R.id.tv);
textView.setText("hello");
}
and setContentView(R.layout.activity_main); no longer be needed
You're setting the text on a view in a layout that you do not show. Once you inflated R.layout.activity_main in ViewManger, you never add the view to the one that got inflated by the activity itself (the layout passed as a parameter to setContentView). The reason that it works with activity is that in that case, the activity will look in the view that was passed by setContentView, the layout that is visible.
The problem is Your activity's layout is already inflated by using this line:
setContentView(R.layout.activity_main);
So if you call in activity:
TextView textView=findViewById(R.id.tv);
textView.setText("hello");
It will work cause the parent of this view if the activity's layout.
The thing you are doing in your ViewManager is inflating a new view which is not bind to any component.So where you suppose to show the text View.
This will be the solution:
public void setText(String text){
TextView textView=activity.findViewById(R.id.tv);
textView.setText("hello");
}

Does Child Activity can also use Parent Activity Layout Elements?

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);
}
}

How to inflate a costum-view on a view

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
}

Which lifecycle method will be invoked when Activity is showing?

Sometimes I need to do some operations(e.g. changing layout) when the activity is just showing. What I do now is using post():
public class MyActivity extends Activity {
#Override
public void onCreate() {
...
container.post(new Runnable(){
resize(container);
});
}
}
Is there any lifecycle method like onCreate can be used to simplify the code, that I don't need to call post?
#Override
public void onX() {
resize(container);
}
I think you mean do something after the UI is displayed.
Using a global layout listener has always worked well for me. It has the advantage of being able to remeasure things if the layout is changed, e.g. if something is set to View.GONE or child views are added/removed.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is
LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.main, null);
// set a global layout listener which will be called when the layout pass is completed and the view is drawn
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
// at this point, the UI is fully displayed
}
}
);
setContentView(mainLayout);
http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html

creating a xml layout from a custom layout. is it possible?

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.

Categories

Resources