cannot display an image with a do while in the OnCreate method - android

I'm trying to draw an image inside a do while in the OnCreate method but it fails to show.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
drawElements();
}
public void drawElements(){
do{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 60);
params.setMargins(0, 0, 0, 0);
ImageView image = new ImageView(this);
personaje.setImageResource(R.drawable.image);
layout.addView(image, params);
setContentView(layout);
Log.i("MainActivity","here comes");
}while(!esc)
}
It's strange because it comes in the do while, because the log shows constantly on the LogCat but the image does not show.
In fact, it fails to remove the status bar or the title of the application as specified in the onCreate, but if I remove the do while it shows everything correctly.
Any suggestions?

You need to rethink your whole design. What you are doing here does not make any sense.
Your while loop is going to loop forever, blocking the UI thread. Thus, nothing will ever be displayed.
Instead, you should display a single image in onCreate. Add a key listener or button listener that will change the image within the listener.

Related

Table in native app and WCAG 2,0

I would like to work out what is required to make a simple standard table in a native android app compliant to WCAG 2.0.
I thinking about the requirement 1.3.1 Info and Relationships: Information, structure, and relationships conveyed through presentation can be programmatically determined or are available in text. (Level A)
I habe specially poblems with the requirememt of having headers in tables. I even don't know whether it is possible to reach this aim.
For a example of a simple table I took this one https://developer.android.com/guide/topics/ui/layout/grid.html
Thanks in advance.
Best regards
Petra Ritter, accessibility consultant, Foundation access for all
http://www.access-for-all.ch/en/
It is very simple.
Declare a Popupwindow and View as below.
PopupWindow layoutWindow;
View javaLayoutView;
JavaClassName _activity = this;
Write a Java Void method.
In this method add code below.
below will increase speed. if PopupWindow has a somthing method will not go longer else will go further.
if(layoutWindow != null)
return;
Assiggn Activity to this thread.
_activity = this;
Calling the method from Native side will not run directly because our activity is Native Activity since. So it will execute in Ui Thread.
this.runOnUiThread(new Runnable(){
#Override
public void run(){
// Make a Inflator Layout. Our XML Layout will work on this.
LayoutInflater lF =
(LayoutInflator)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
javaLayoutView = lF.inflate(R.layout.layoutfile, null);
layoutWindow = new PopupWindow( javaLayoutView, LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LinearLayout mainLayout = new LinearLayout(_activity);
MarginLayoutParams params = new
MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.setMargin(0, 0, 0, 0);
_activity.setContentView(mainLayout, params);
layoutWindow.showAtLocation(mainLayout, Gravity.TOP, 0, 0);
layoutWindow.update();
}
});
Here your layout file will be executed on LinearLayout doesn't matter what layout you used in XML. Here by changing LayoutParams.MATCH_PARENT you can change the width and height.
The line layoutWindow.showAtLocation(mainLayout, Gravity.TOP, 0, 0); will make render your layout over Native Activity,
Remember that If you change anything after layoutWindow.update() must recall it to take effect.
This will damn sure work !!!

View.setLayoutParams() has no effect even after layout completed

I'm working on an Android TV app and need to make a progress bar to indicate how much content has been played already. I feel like what I've written should be working, but it isn't.
I've got a LinearLayout with android:orientation="horizontal" and android:weightSum=100 as a container for the progress bar, and the actual progress bar is a FrameLayout inside the container, with android:layout_width="0dp" and android:layout_weight="0" to make sure the progress bar isn't drawn if there isn't any progress.
In the activity code I'm attempting to change the weight of the progress bar like this:
ViewGroup.LayoutParams lp = progressView.getLayoutParams();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(lp.width, lp.height, progress);
progressView.setLayoutParams(params);
(progressView, naturally, is the progress bar object I'm trying to manipulate, and progress is the intended new weight)
I was initially running this code in the activity's onCreate() method, until I remembered you can't do anything about a view's dimensions until it has been laid out properly. Since then I've tried running this in onResume() with postDelayed like this:
#Override
public void onResume() {
super.onResume();
progressView.postDelayed(new Runnable() {
#Override
public void run() {
setProgress();
}
}, 1000);
}
and with a ViewTreeObserver like this during onCreate():
progressView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (!progressBarRefreshed) {
setProgress();
}
}
});
and I've even tried running the code as an onClick event attached to a button in the same activity (in both cases setProgress() is the function that performs the weight change using the code posted above). I've verified (with log messages I removed from the code to make it more readable) that the initial width is 0, height is -1 (match_parent) and weight is 0, and that the new LayoutParams object has width 0, height -1 and weight equal to whatever I set it to. But after the setLayoutParams() call the view still has weight 0 and there is no change on the screen either. What blatantly trivial thing am I missing here?
Try calling progressView.requestLayout() after setting the layout params.

Removing layout exits my application

I have this code for initial/default layout
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Retrieve the shared preferences
mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
glView = new GLSurfaceView(this);
glView.setRenderer(this);
glView.setZOrderMediaOverlay(false);
glView.setId(123); // set id
layout = new RelativeLayout(this);
// create a fake view with zero size and place it to center of RelativeLayout
fakeView = new View(this);
fakeView.setId(24736);
fakeparams = new RelativeLayout.LayoutParams(0, 0);
fakeparams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, fakeView.getId()); // set position is above fakeView
layout.addView(glView, params);
setContentView(layout);
Say we are in main menu and in some menu when user click it, i'll inflate new layout which I prepared in res/layout folder
myStoryView = getLayoutInflater().inflate(R.layout.story2, layout, false);
layout.addView(myStoryView);
This goes well, I get the result, new layout appear as it should.
later I call this code when user want to close story screen and return to main menu:
layout.removeView(myStoryView);
But what happened is I lose all my layout. It exits entire app, though in my galaxy tab when I hold center button, I can see my app is there, maybe it's not entirely closed. I only have no layout to display.
sorry, i just need to
setContentView(layout)

How to set Background of an Class Activity

I don't have any XML-Layout's. I directly start my Activity like that
super.onCreate(savedInstanceState);
setContentView(new DrawingPanel(this));
It's a drawing App and the default Background is Black. I want to change the Background to an ImageBackground. How can I do that?
EDIT: IT'S NOT ABOUT JUST SETTING A BACKGROUND. I´ve got a SurfaceView (DrawingPanel) with a black Background by default. I want to change the black background to a Picture, so I can draw ON the picture.
// try this way,hope this will help you...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout parent = new LinearLayout(this);
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
parent.setBackgroundResource(R.drawable.ic_launcher); // here set your background
parent.addView(new DrawingPanel(this));
setContentView(parent);
}
I support #haresh, I think the problem occur because you are trying to load big images. Here is a good tutorial how to load large bitmaps.
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.mypicture);
scaled = Bitmap.createScaledBitmap(background, width, height, true);
and then using
canvas.drawBitmap(scaled, 0, 0, null);
solved that problem!
Below code is working fine to set the background image.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
setContentView(imageView);
}
this works fine for me
logo = new ImageView(this);
logo.setImageResource(R.drawable.sync_cloud_logo);
logo.setBackgroundColor(getResources().getColor(R.color.drawer_bkground));// your color
setContentView(logo);

Adding View to Relative Layout behind an existing view causes screen flicker

I'm trying to insert a View behind another view that is taking up the full screen and then later removing the view in the front to reveal the only remaining view. Functionally, everything is working as expected but the problem is that when I call View.addView() to add the second view, specifying to add it at index 0 so it is behind the first view, the screen flickers. It's almost as if the view is actually getting added in front of the first view for a fraction of a second and then it is hidden again as it is moved behind it.
Here's what I'm doing:
When the Activity is created I add an ImageView to a RelativeLayout and make the RelativeLayout instance the Activity's content view:
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
m_layout = new RelativeLayout(this);
m_layout.setBackgroundColor(Color.BLACK);
m_splashImage = new ImageView(this);
m_splashImage.setImageResource(R.drawable.splash);
m_splashImage.setScaleType(ScaleType.FIT_XY);
m_layout.addView(m_splashImage,
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
setContentView(m_layout);
}
When the Activity is started, I created and add the GLSurfaceView to the RelativeLayout at index 0, so it is behind the ImageView:
protected void onStart() {
super.onStart();
m_layout.addView(new MyGLSurfaceView(), 0,
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
}
Later, after all of the loading is done and the GLSurfaceView is ready to continuously render,
the splash ImageView is removed and cleaned up.
public void hideSplashScreen() {
if (m_splashImage != null) {
m_layout.removeView(m_splashImage);
m_splashImage = null;
}
}
Is there a better way to do this that doesn't require creating the GLSurfaceView before the onStart() is called?
Have you tried using view.setVisibility(View.GONE) on of the view that you adding behind? Of course before you are adding it.

Categories

Resources