Draw 50 button dynamically after full activity loaded - android

This is on create of my activity and after setting content view , I am calling LoadButton().
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Loadbuttons();
}
public void LoadButtonss()
{
Here I am fetching data from database ,and plotting 50 buttons dynamically
}
So My Problem is : It is taking time in loading activity.
Any Idea how to LoadButtons() after Loading full activity. It should not wait for the ButtonLoad() function.
Any Idea how to do that?

paste this code in your activity class block:
#Override
protected void onStart() {
// TODO Auto-generated method stub
LoadButtons();
super.onStart();
}

According to the activity life cycle, you must call LoadButtons in onStart overrided method. Because activity will be visible in this state.

You could set the buttons to invisible and if you fetch the data you can update the UI through an AsyncThread. It is not the nicest way, but it works. Otherwise use instead of Buttons a CustomListView.

I would try this: Load buttons in onCreate and set View invisible. in onResume make it visible again.
View v;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v= findViewById(R.layout.main);
v.setVisibility(View.INVISIBLE);
Loadbuttons();
}
#Override
protected void onResume() {
v.setVisibility(View.VISIBLE);
super.onResume();
}
public void LoadButtons()
{
FrameLayout layout = (FrameLayout) findViewById(R.id.MarkerLinearlayout);
//Here I am fetching data from database ,and plotting 50 buttons dynamically
}

Related

How to remove headers from BrowseFragment?

I am working on an android TV app and I am using the leanback library.
I want to customize the app layout "BrowseFragment". I want to remove the header view and only display the card list "rows".
Is it possible to do that or is there any other solution to achieve that?
I want to remove that
The above call actually needs to be in the OnCreate method instead of OnActivityCreated.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHeadersState(HEADERS_DISABLED);
}
You have to set the HeaderState like this:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHeadersState(HEADERS_DISABLED); // Add this line
}
There are two options:
/** The headers fragment is enabled and hidden by default. */
HEADERS_HIDDEN
/** The headers fragment is disabled and will never be shown. */
HEADERS_DISABLED
OnCreate, you have to set the Header:
setHeadersState(HEADERS_DISABLED); //To Diable the Header
setHeadersState(HEADERS_HIDDEN); //To Hide the Header
When you change the setHeaderState(HEADERS_DISABLED), the rows would be disabled and hidden too.
One way to do it is setHeaderPresenterSelector()
private void setupUIElements() {
setHeadersState(HEADERS_DISABLED);
setHeaderPresenterSelector(new PresenterSelector() {
#Override
public Presenter getPresenter(Object item) {
return new CustomPresenter();
}
});
}
you just need to override the getPresenter() method, and return new customized presenter that you need to implement.

How to save instances of classes like LinearLayout or Button, in onSaveInstanceState?

If my activity has member variables that are a Button or a LinearLayout, like so:
public class MyActivity extends AppCompatActivity {
LinearLayout lLayout;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
lLayout = new lLayout(this);
for (int i = 0; i < 2; i++) {
button = new Button(this);
lLayout.addView(button);
}
}
}
The above is just basic pseudocode, but illustrates what I want to achieve - creating a dynamic row of buttons.
What exactly should I do in the onSaveInstanceState() function so that I can save the values of lLayout and button? I.e, so that when lLayout is displayed after a configuration change, it is still a row of buttons as before. I noticed that the Bundle class does not have any methods to add classes like these to a hash map, like it does for ints or String, so is there some other way to do this?
you have to overwrite the onSavedInstanceState method
Here try this :
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save whatever you want to save.
savedInstanceState.putInt("KEYINT", 1);
savedInstanceState.putString("SavedString", "value you want to save");
// etc.
}
then you can either get these value inside onCreate or override onRestoreInstanceState:
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState
int savedInt= savedInstanceState.getInt("KEYINT");
String savedString= savedInstanceState.getString("SavedString");
}
Hope it helps

Textview.text change example

I have 2 layouts. First layout has 2 textviews. And second layout has 1 button. I want to do when i pres buton textviews.text change. But i get null error about textviews.
textviews on musicactivity, button on homeactivity.
my first activity class --> homeactivity,secon activity class --> Musicactivity,textview-->ad1,textview-->ad2,textviews on musiclayouts,Button on homeactivity.
public class HomeActivity extends Activity {
Button btn;
TextView ad1;
TextView ad2;
private MusicActivity music;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
btn=(Button)findViewById(R.id.button1);
ad1=(TextView)findViewById(R.id.textView1);
ad2=(TextView)findViewById(R.id.textView2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
ad1.setText(null);
ad2.setText(null);
}
}
});
}
The error message you posted is for line 40, which is doing a close of v1. I don't know what a veritabani is, but it appears that error is happening there. I suspect the rest of the error trace might say more.
You are doing it wrong. You can't get the textViews if they are not in the layout. You say yourself they are not in the layout. You have to pass the values you want to set to the new activity (musicActivity) via an Intent. Then in musicActivity's you can get the textViews, you have the values, you set them.

Android: How can I reference layout items outside the Main Activity?

My onCreate method sets the content view and sets up an image button:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton mGetClickTime = (ImageButton) findViewById(R.id.clicker);
}
But I want to create a method that changes the background of the ImageButton:
public void mUpdateBackground() {
if (backgroundPic) {
randomImageId = R.drawable.bg1;
}
else {
randomImageId = R.drawable.bg0;
}
mGetClickTime.setImageResource(randomImageId);
}
The problem is, the mUpdateBackground method doesn't know about the layout. If I declare the layout in the method it resets all the changes made programatically in the MainActivity.
Any ideas how I can solve this problem?
the mUpdateBackground method doesn't know about the layout
I am going to translate that to mean:
the mUpdateBackground method doesn't know about the ImageButton
If that is correct, make ImageButton mGetClickTime be a data member of your activity, instead of a local variable within onCreate(), and have mUpdateBackground() be a method on that same activity.
You need to move the ImageButton declaration to your Activity scope instead of your onCreate() method scope. Try like this:
public class YourActivity extends Activity{
ImageButton mGetClickTime;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGetClickTime = (ImageButton) findViewById(R.id.clicker);
}
public void mUpdateBackground() {
if (backgroundPic) {
randomImageId = R.drawable.bg1;
}
else {
randomImageId = R.drawable.bg0;
}
mGetClickTime.setImageResource(randomImageId);
}
}

setContentView and Listeners

I'm changing my ContentView of the Activity at some point. (to View2).
After changing it back to View1, the listeners are not more working.
I already tried putting the Listener in the onResume() method.
Is it common anyway to use setContentView() to display e.g. a Progress screen/please wait,...(while an asyncTask is running).
Or should you only have ONE mainView for each Activity? (and replacing the content dynamically).
//EDIT: To be more specific:
I am looking for something like
LinearLayout item = (LinearLayout) findViewById(R.id.mainView);
View child = getLayoutInflater().inflate(R.layout.progress, null);
item.addView(child);
but instead of adding the "progress.xml", it should remove the current layout and ONLY show "progress.xml".
Do I need an "container" and show/hide mainView/progress?
But that doesn't seem very proper to me...
See also code below (stripped)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
}
});
}
setContentView(R.layout.view2);
[...]
setContentView(R.layout.view1);
//Listener not more working
Thank you all, for your reply. You made me realize, the onClickListeners are getting lost when I remove or replace (using setContentView()) the main view. I ended up this way now:
onCreate:
setContentView(R.layout.parse);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(getLayoutInflater().inflate(R.layout.dialog, null));
container.addView(getLayoutInflater().inflate(R.layout.progress, null));
onStartDoingSomething:
findViewById(R.id.dialog).setVisibility(View.INVISIBLE);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
onEndDoingSomehting:
findViewById(R.id.dialog).setVisibility(View.VISIBLE);
findViewById(R.id.progress).setVisibility(View.INVISIBLE);
I might change View.INVISIBLE to View.GONE, like nmr said, but since I have never used View.GONE, I have to check the Android doku first ;)
Assuming you use 'findViewById' to initialize 'button', you would need to do that every time that you do setContentView(R.layout.view1);
You can only have one UI for each Activity, you should create another activity and in its onCreate method set the content view
Steps:
Create Activity
Use the following code to set the content View:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view2);
}
Define the Activity in the applications manifest as such :
Create an intent in the first Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view1);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
intent();
}
});
}
void intent(){
Intent intent = new Intent();intent.setClass(Activity1.this, Activity2.class);
startActivity(intent);
}
And there you go c:

Categories

Resources