Android - The function getLayoutResource()? - android

I saw this some code :
....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected int getLayoutResource() {
return R.layout.activity_home;
}
....
In which they used getLayoutResource() instead of setContentView in order to add View Layout.
I don't understand, why it is so. Does getLayoutResource() before or alongside with onCreate(Bundle savedInstanceState).
I read some about getLayoutResource() (Gets the layout resource that will be shown as the View for this Preference.) in android developer document but i didn't understand .
Is there anyone to explain more.
I want to know where and where we should use getLayoutResource().

While I can't tell from your code snippet, I don't think that you are talking about Preference, since Preferences don't have onCreate (as far as I know). So, I am assuming that you are talking about a custom method named getLayoutResource in an activity. As such, getLayoutResource likely just returns the layout resource ID, without doing anything to it. The returned layout resource ID could then be used in onCreate by setContentView, which actually inflates the resource. We use it this way:
public abstract class BaseActivity {
#Override
onCreate(Bundle savedInstanceState) {
setContentView(getLayoutResource);
}
protected abstract int getLayoutResource();
}
public class MyActivity extends BaseActivity {
#Override
protected int getLayoutResource() {
return R.layout.my_activity_layout;
}
}
In short, you use getLayoutResource anywhere where you need the layout resource ID for a view.
To read more about setContentView, check out this: https://developer.android.com/reference/android/app/Activity.html#setContentView .
This answer discusses the difference between getLayoutResource and setContentView: What is the different between setContentView and getLayoutResource in android?

Related

Android: How to execute a method before the layout is initiated/created in an activity

I need to execute a method before initiating the layout in an activity. If I call the method I need to execute inside onCreate(), would it be executed before the layout is set?
The reason is because I need the method to return a piece of information that is displayed in the layout before initiating it. Would love some feedback on this.
You can do whatever you like before setContentView like so:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int i = 0;
setContentView(R.layout.main);
}
}
As long as you do not interact with views that have not been inflated yet
For example this is an error:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ERROR, CAN'T TOUCH UI ELEMENTS
ImageView img = (ImageView)findViewById(R.id.img);
setContentView(R.layout.main);
}
}
Default activity created with Android Studio contains following code
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here is code, that executed before layout inflated
setContentView(R.layout.example_activity); //This line inflates layout
}
BTW, you can even remove setContentView and inflate layout programmaticaly.
Do it in onCreate(), preferably before calling setContentView().
However, if the data you want to receive comes from the network, then it will be obtained on a separate Thread (as no network calls can be done on the main Thread). In this situation the layout will almost certainly display before the data is obtained.
A solution would be to obtain the piece of data before you start the Activity, pass it in the Intent as extra and then retrieve in onCreate() using getIntent().getStringExtra()
You are probably inflating your layout in Activity.onCreate() with setContentView(), so you need to put your function call in that method before the call to setContentView().
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
yourFunctionCallHere();
setContentView(R.layout.act_main);
}

Butterknife not detecting the source of event

I have just started working with Butterknife library and written the following code:
class myActivity extends AppCompatActivity
{
#BindView(R.id.button) Button app1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
public void selectApp(View b)
{
Button clicked=(Button)b;
if(clicked==app1)
Toast.makeText(this,"First App clicked",Toast.LENGTH_LONG).show();
}
}
here selectApp is attached through onClick in the xml view file.
But the problem is clicked==app1 is returning false even when pressing app1. The method is being called but the if condition is coming false.
Can anybody clarify.
Thanks
I think this would work:
if(clicked.getId()==R.id.button)
Also, you can use View b and not parse into a button:
if(b.getId()==R.id.button)
¿Is this your actual code? seems to lack an annotation on the method.

Android application closes on click

I do my android app , but I have a bug I don't know how to fix it.
My code is below :
This is my Main Activity :
public class MainActivity extends Activity {
private ImageView imgHot;
public final static String EXTRA_MESSAGE="com.cuonglm.KhoHinh.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgHot=(ImageView)findViewById(R.id.imageViewHot);
imgHot.setOnClickListener(toContentHot);
}
View.OnClickListener toContentHot=new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent content=new Intent(MainActivity.this,ContentActivity.class);
String signal="1";
content.putExtra(EXTRA_MESSAGE,signal);
startActivity(content);
}
};
And this is my second Activity :
public class ContentActivity extends Activity {
private TextView viewMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
setContentView(R.layout.activity_content);
}
I want to click on the image on the Main Activity , string "1" or number "1" will send to the Second Activity via Intent and view on the TextView.
But my app will be close "Unfortunately..."
Thanks
Change to
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
In your ContentActivity
findViewById looks for a view with the id for the current infalted layout. SO you need to set the content of your layout to the activity first and then initialize your views.
You are probably gettting NullPointerException coz your initialization fails.
You need to call setContentView() in your second Activity before trying to access any of the Views in that layout. Change it to
public class ContentActivity extends Activity {
private TextView viewMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
}
If this doesn't fix your problem then please post your logcat so we can see the error. Also always post logcat in the future when your app crashes. They aren't always this easy to see.
Also, I'm not sure you understand how putExtra() works. It is a key, value pair so when you put EXTRA_MESSAGE as the key then that is what you would use to retrieve the value added in the second param. So the way you are doing it may work if the Activity gets destroyed but it looks really strange to me and probably not realy safe or efficient. I would change it to something like
content.putExtra("message",signal);
in your first Activity then get it with
String messageReceive = content.getStringExtra("message");
in your second Activity
You need to set the layout before trying to reference the Views associated with it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_content);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
}

#Override on my own methods?

I have a setup similar to what you see below. I have just noticed that "YourClass" is actually implementing some of my logic from "MyClass." Ugh. I tried to throw an #Override above setupViews() in "YourClass" but it won't compile stating, "The method setupViews() of type DataManagerActivity must override a superclass method"
Code changed. It was an example. I just typed the wrong thing. Same question. How can I keep YourClass that extends MyClass from implementing setupViews() from MyClass?
public class MyClass extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setupViews();
...
}
private void setupViews() {
....
}
}
public class YourClass extends MyClass {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setupViews();
...
}
private void setupViews() {
....
}
}
#Override instructs the compiler to fail unless the method underneath it overrides a method in the superclass (the one you extend from) or one of the interfaces it implements.
Edit: sorry I may have misinterpreted what you meant. The reason why you can't override setupViews() is that it's private in MyClass so that you cannot access or override it from any subclasses.
If that's what you want, then you want your method to be protected - as in, accesible and overrideable in subclasses of the class it's defined in, but not accessible from outside.
Edit 2': so bottom line:
If you want setupViews() to be overridable in subclasses (such as YourClass), make it protected. Otherwise, make it private.
I think you want to use
this.setupViews();

What is Android's icicle parameter?

I've noticed in some coding people use icicle with the onCreate method, and I was wondering what it is exactly:
public class About extends Activity {
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.whatup);
}
Is this the same thing as savedInstanceState?
"icicle" is sometimes used as the name of the parameter because onSaveInstanceState() used to be called onFreeze().
The name isn't magic. It's just a placeholder for one of the formal parameters. As shown by the API, onCreate takes one Bundle parameter. It's up to you what to call it.

Categories

Resources