Force Main-Layout to Inflate - android

I'll try another shot. Hopefully now i get the answere i totally Need.
Just imagine:
I have a widget which calls(after onClick) a blank activity with no hardcoded code, just a Relative Layout with some views.(Layout provided by XML-layout-file).
My Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
When i add a for-loop to the onCreate-Method which adds 50 Buttons(take no care about layoutparams, orientation and so on). Just 50 simply Buttons
Like:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
for(int i = 0; i<50; i++) {
Button btn = new Button(this);
myLayout.add(btn);
}
I recognize that he only Shows the activity on the Screen until he finished to add all the Buttons.
My Question is: How can i prevent this!? How can i Show up the Activity with the Content from the XML-layout file and then(ONLY then) add one Button after another to the Layout.
Is this possible? If so, do i neew to redraw the whole activity and so on. Please give me advise to my issue.

Related

Layout and Toast not displaying

My MainActivity layout consists of 4 embedded LinearLayouts, all declared Visibility:GONE in the XML.
The first layout contains just a TextView. I set this layout to VISIBLE, and then try to display a Toast in it. But neither the Toast nor the TextView are displayed--just a blank screen.
This same hiding/unhiding technique works in other areas in the activity, but I can't see why it doesn't here.
For Context for the Toast, I've tried this, MainActivity.this, and getApplicationContext(). Nothing works. (The program works, it just doesn't display the TextView or the Toast)
[code]
#BindView(R.id.layout_configuration) LinearLayout linearLayoutConfiguration;
#BindView(R.id.layout_appupdater) LinearLayout linearLayoutAppUpdater; //Added to handle app-updating clause
#BindView(R.id.layout_user) RelativeLayout linearLayoutUser;
#BindView(R.id.layout_capture) LinearLayout linearCapture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // contains the above 4 layouts, all initialized to GONE visibility
ButterKnife.bind(this);
BusProvider.getInstance().register(this);
init(); // unrelated to display
}
...
if (versionMismatch) {
setDisplayAppUpdating();
Log.d(TAG, "Trying to update app...");
//(doesn't print) if we've detected a newer version in the cloud, get it.
Toast.makeText(MainActivity.this, "Updating app...", Toast.LENGTH_SHORT).show();
apkDownloadInteractor = new DownloadApkInteractor (
ThreadExecutor.getInstance(),
MainThread.getInstance(),
Patient.getInstance());
apkDownloadInteractor.execute();
...
protected void setDisplayAppUpdating() {
linearLayoutConfiguration.setVisibility(View.GONE);
linearLayoutUser.setVisibility(View.GONE);
linearCapture.setVisibility(View.GONE);
linearLayoutAppUpdater.setVisibility(View.VISIBLE);
}

difference instantiation of android's button

i' m new on android, i want to know what are the differences to declare a button like :
#Override
protected void onCreate(Bundle savedInstanceState) {
button = ((Button) findViewById(R.id.button));
button.setOnClickListener(this);
}
or
#Override
protected void onCreate(Bundle savedInstanceState) {
button = new Button(this);
button.setId(..);
button = ((Button) findViewById(R.id.button));
button.setOnClickListener(this);
}
thanks in advance.
If you declare the Button in xml then you should provide a layout to the Activity using setContentView(int) and initialize the Button like this
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.your_layout);//a button with id button should present in this layout
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
If you want to create a Button programmatically then you should do initialization like this
#Override
protected void onCreate(Bundle savedInstanceState) {
button = new Button(this);
button.setId(1);//some random integer value
setContentView(view);//some view
button.setOnClickListener(this);
}
setContentView(View view) for 2nd case
If you use first declaration, you have to define a button from xml. In the second one you are creating a button programmatically. You can also define it's position in the view etc.
The difference is that the second time you declare the button in the code. Then you add an id to it, presumably a new one.
If you want to run the first code your button has to be declared in the xml layout resource file. This is the fastest way since it is optimized on the android framework to read the layouts from xml (it is actually converted to java, but in an optimized way).
The second code does not require the layout file, although you do not add the button to a layout so the button as it is now is not usable, as it is not 'on screen'.
The difference is that
xml based layout is comparatively fast
xml based code at the end it converted in to java code by the compiler
and if you will use the second approach you have to do allot of things like setting the
width and height, position where it to be shown and by doing that
Your code will be messy
So it its better to use xml based layouts

How to make Only Some Views touchable while making the others (like RelativeLayout) not touchable

Window w = getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
The above code is to make the whole window not touchable.
However, the button inside this window is also being not Touchable, while I want it to be clickable.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Window w = getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
b = (Button)findViewById(R.id.button1);
b.setClickable(true);
b.setOnClickListener(this);
}
How can I make on that button touchable?
The other views in your layout shouldnt register onclicks unless you register them to. Whats the behavior you expected from setting the entire window to not be touchable?
EDIT: this code can be used to set any settings on just about any view as long as you want them all the same. For setting a number of views to be non clickable just throw them all into a view[] array and itterate through them like this
TextView a,b,c;
EditText d,e,f;
RelativeLayout g,h,i;
#Override
protected void onCreate(){
//intstatiate and inflate all your views....you should know how to do that
View[] viewHolder = {a,b,c,d,e,f,g,h,i};
for(int i = 0; viewHolder.length>i; i++){
viewHolder[i].setClickable(false);
}
This will do exactly what you want but unless the other views are taking focus you shouldnt have to do that. But it will work.
By using this code:
Window w = getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
The whole window will be set as not touchable. Alternatively, you can use onClickListener for each View you added in your XML. even layouts can have their own click listener

How to change color of backGround on app load(onCreate())?

I am trying to change r=the color of the back ground on app load. For this I have used something like this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View view= new View(getApplicationContext());
view.post(new Runnable() {
#Override
public void run() {
view.setBackgroundColor(Color.BLACK);
}
});
addListenerOnButton();
}
This does not work. See the emulator screen shot below :
As you can see the back ground color is still white . Any ideas on what i can do to rectify this ?
Simple. Try this way.
this.findViewById(android.R.id.content).setBackgroundColor(Color.BLACK);
For ex:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.findViewById(android.R.id.content).setBackgroundColor(Color.BLACK);
}
Here findViewById(android.R.id.content) will return ContentView of current activity. Then you can set background for this view.
I hope this will help you.
First of all, view.post() is not necessary, because onCreate() already runs in the UI thread. Secondly, you are creating a View and setting the background of it, but you never set the view to anywhere, it's just an Object that never gets drawn. There are 2 solutions:
either:
View view= new View(this);
view.setBackgroundColor(Color.BLACK);
setContentView(view);
or, probably better:
setContentView(R.layout.activity_main);
View view = findViewById(R.id.root_laout); // set the root_layout in the layout xml file
view.setBackgroundColor(Color.BLACK);
This should do the trick:
this.findViewById(android.R.id.content).setBackgroundColor(Color.BLACK);

Show progress dialog while adding views dynamically to scrollview in android

In the start of an activity in OnCreate method, I'm trying to add some text views dynamically to a scrollview of the layout file. But during the time of this operation the screen remains blank. Is it possible to show a progress dialog when adding views dynamically to a scrollview?
You should use a AsyncTask for that. Please post some code you're having in your onCreate method at the moment.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lottery);
for(i = 0; i < 20; i++) {
TextView txt = new TextView(this);
scrolview.addView(txt);
}
}
The code is similar to this...can u explain how to put a progress dialog...in this sample using AsyncTask...where should i perform this operation doInBackground method or on postExecute?

Categories

Resources