I have a layout in XML as follows:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="myButton.performClick" />
myButton is a member of this activity, is it possible use a class member method here?
is it possible use a class member method here?
According to the API - no. The API says:
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View.
No, as the docs say (emphasis mine):
you must declare a public void sayHello(View v) method of your context
(typically, your Activity)
If you wanted to execute a method on a field, you could wrap it I suppose.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="performClick" />
Then:
public void performClick(View view) {
myButton.performClick(view);
}
but that almost defeats the purpose of the XML onClick attribute (making things more concise). Is there a good reason for performClick to be on your button instead of your activity anyway?
Lastly, doing this on the LinearLayout is fine. The attribute is supported for all Views.
onClick doesn't go in the Linear Layout tag. It goes in the tag of the button whose on click you want to set.
So you do it like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="methodName"/>
</LinearLayout>
Related
Is it possible to add multiple TextView inside one ImageButton with colour background ?
The core need is to have a button with the action text on it, and a subtext nearby explaining the action or giving other information related to the action. This subtext can vary from time to time.
Considering this requirement, one solution is to have a normal button and a subtext below, not clickable. But I find it messy. A better approach which I like is, on iOS for instance, to have a clickable UIView containing the action as bold text and the explanation as light text. See the image bellow containing 4 buttons :
How to achieve the same on Android with Java ? The closest I can have is to have an ImageButton bellow a TextView, and it does not sound right.
Is possible to nest TextViews inside an ImageButton ? If not, what is the best alternative ?
I hope this may be useful it explains how to position a textView within and in front of a imageView in the XML.
TextView inside of ImageButton/ImageView XML - Android Dev
Obviously make sure each view has a unique id/name which you can assign as shown here on this link
Sorry I cannot explain specifically myself but it has been a while since developing in Java for Android.
I dont know why you want this behaviour but you can make a container for your views and add a click listener to the whole view. you can also use it anywhere.
an example of this would be.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:background="#drawable/container_background"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
add a selector background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/text_pressed" />
<item android:color="#color/normal" />
</selector>
and the listener
findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I am programming an app which has many views, and then based on which view you click, it will update the views. When I try to find the layout to update the views, findViewById returns null. FYI: I am calling findViewById from a function that onCreate() calls. And yes, I am calling setContentView before calling that function.
MainActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updateViews();
}
public void updateViews(){
LinearLayout layout = (LinearLayout) findViewById(R.id.list_layout);
layout.removeAllViews(); //NPE: Attempt to invoke virtual method 'android.widget.LinearLayout.removeAllViews()' on a null object reference
}
What can I do to fix this issue? I suspect it has something to do with layout inflation, but I am not sure.
Personally, I see only two reasons of NPE (Null Pointer Exception):
First. You don't have already LinearLayout with R.id.list_layout which is a really silly, because your project is making and it returns NPE.
You said that you have that view with this android:id. Let's say you have a TextView with this id. I swear that in that case Android Studio won't say about NPE, but about wrong view attributes like:
Hey rpurohit! There's no TextView matched with this id. Check please correctness of your configuration.
Second. You said also that you have only one view, which is this LinearLayout. I can say only
Congratulations! You've found an issue!
and explain you that you're trying to remove all views from your master ViewGroup, but like you said:
The LinearLayout is the only thing that is inside the XML file.
so your IDE tries to remove any view from its parent, but as there's no ChildViews, it tells you about Null Pointer Exception.
I'm sure that now you understand your simple mistake :-)
When you use layout.removeAllViews(). that means remove all child
into the layout and dose not contain parent layout.
If you have this code:
<Linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#android:color/holo_blue_dark">
<ToggleButton
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</Linearlayout>
and call layout.removeAllViews() method. this remove textView and toggleButton but dose not remove parent layout(layout id).
If you wantremove child and parent view . You can use like this:
((ViewGroup)layout.getParent()).removeAllViews();
I'm creating an app which used a GridView to display a set of items. Each of these items is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.myapp.customviews.Tile
android:layout_width="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
android:layout_height="match_parent"
android:id="#+id/tile" />
</LinearLayout>
In my OnItemClickHandler I want to check if the type of the View with id R.id.tile (My custom View class mentioned above) is com.example.myapp.customviews.EmptyTile. This class is a child of the Tile class.
Currently I'm checking if the class is an EmptyTile as follows:
LinearLayout tileLayout = (LinearLayout) gridView.getChildAt(position);
if (EmptyTile.class.isInstance(tileLayout.findViewById(R.id.tile))) {
Log.d("myappDebug", "Correct instance!");
}
I have tried checking using instanceof and tileLayout.findViewById(position).getClass() == EmptyTile.class but these also don't work. At runtime, my view is just a Tile View and not an EmptyTile View.
How can I check if my View is actually an instance of the EmptyTile class?
instanceof must work. But, in your XML, you define the view to be of type com.example.myapp.customviews.Tile, so why do you expect it to be an EmptyTile?
If you use EmptyTile (with the correct package of course) in the XML, the instanceof check and the other options will work.
I need to make a simple progress dialog with a square with only a circle inside
like this but on an dark back ground. 50x50 dp.
I tried this. But my app is crashes on the show()
Display progressdialog without text Android
here is my code :
utils.java
protected void startLoading() {
ProgressDialog proDialog = new ProgressDialog(this.getActivity());
proDialog.setCancelable(false);
proDialog.setContentView(R.layout.progressdialog);
proDialog.show();
}
.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/transparent" >
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
what am i doing wrong?
og : 10-18 12:28:26.924: E/AndroidRuntime(809): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
This is not because of your progressBar.
You probably are calling a requestFeature method (for hiding your windowBar, for making your app fulscreen or something else) after your setContentLayout in your onCreate method. Please, put this call before your setContentLayout and this would work ;)
Found the answer :
need to switch between
proDialog.show();
and then to set Content.
proDialog.setContentView(R.layout.progressdialog);
I have been sitting for at least 4 hours trying to solve this problem.
To understand this there are 3 files you need to know about:
eggCatcher.java which extends Activity, this class is not used for much more than
saving gamestate and showing the optionsmenu.
eggCatcherView.java which extends SurfaceView and contains "the game".
eggCatcher_layout.xml which is shown below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layouten">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<easter.fun.EggCatcherView
android:id="#+id/eggcatcher"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<TextView android:text="Score: "
android:id="#+id/totalscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</TextView>
<TextView android:text="Bonus: "
android:id="#+id/bonus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
As shown in the xml file, EggCatcherView is put in the xml file.
When the applications i started the onCreate calls setContentView(layout.eggcatcher_layout);
My question now is:
how can i, from EggCatcherView.java access and edit the TextViews defined in the xmlfile?
if it was in EggCatcher.java it would be easy, just use findViewById(id.bonus), but from
inside the surfaceView appears to be a little more difficult.
I hope i have made everything clear, if you dont understand just ask!
//micke
I think you should get the parent view and then from there on you can use findViewById() (are you sure you can't just use that method anyway since SurfaceView is a subclass of View and inherits findViewById() from it?).
For using the parent you do something like:
ViewParent vp = eggCatcherView.getParent();
FrameLayout fl = (FrameLayout) vp;
TextView tx = (TextView) fl.findViewById(R.id.bonus);
Of course you need to check if the ViewParent is indeed an instance of FrameLayout.
I found this the best way:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:id="#+id/test"/>
</LinearLayout>
TextView test = (TextView) findViewById(R.id.test);
test.setText("test");
If I understand correctly, you want to access the views in the surrounding activity? That seems like poor architecture. I think it would be better to either pass a callback to the EggCatcherView that can trigger methods in the Activity which in turn operate on the TextViews or fire some kind of events upwards.