I have a button and imageview in my layout. When button is clicked imageview supposed to become visible.I.e. the algorithm is:
1) click button
2) imageview becomes visible
3) other actions.
However it doesn't work always like that. Sometimes on button click the button stays yellow and does nothing, no exceptions thrown either. When I tap anywhere on the screen it continues on step 3 and button becomes deselected again.
What might be the reason of such an odd behaviour?
Edit: Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="fill_parent"
android:id="#+id/water_room_layout" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ViewFlipper android:id="#+id/water_room_flipper"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<include layout="#layout/water_room_wall1" android:id="#+id/first" />
<include layout="#layout/water_room_wall2" android:id="#+id/second" />
<include layout="#layout/water_room_wall3" android:id="#+id/third" />
<include layout="#layout/water_room_wall4" android:id="#+id/fourth" />
</ViewFlipper>
<com.example.room.CustomView
android:id="#+id/customView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
This is water_room_wall1.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/wall1Layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/wall1withkey"
tools:context=".FireRoomActivity"
>
<ImageView
android:id="#+id/zoomed_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/key_zoomed"
android:visibility="gone" />
<Button
android:id="#+id/key"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="150dp"
android:layout_marginTop="144dp"
android:onClick="zoomImage"
/>
</RelativeLayout>
Here is my activity:
int wall;
RelativeLayout parentLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.water_room_view_flipper);
wall=1;
rl = (CustomView) findViewById(R.id.customView);
vf = (ViewFlipper) findViewById(R.id.water_room_flipper);
}
public void zoomImage(View view)
{
parentLayout = (RelativeLayout)findViewById(R.id.first);
if(view.getId()==R.id.key){
zoomedImage= (ImageView)parentLayout.findViewById(R.id.zoomed_image);
((BitmapDrawable)parentLayout.getBackground()).getBitmap().recycle();
//set Alpha
Drawable wall1withoutkey = getResources().getDrawable(R.drawable.wall1withoutkey);
wall1withoutkey.setAlpha(100);
parentLayout.setBackgroundDrawable(wall1withoutkey);
parentLayout.findViewById(R.id.key).setVisibility(View.INVISIBLE);
}
zoomedImage.setVisibility(View.VISIBLE);
zoomedImage.setClickable(true);
zoomedImage.setOnClickListener(this);}
}
Related
I have a scrollview in my android app that supports overscroll and has a nice bounce effect. What I would like to do is add a view that is initially hidden to the user, but if they scroll up beyond the initial view, then they can see it. How can I do this? Is it possible to do this using just xml?
You can place the initial view and the additional view in a LinearLayout, and when the scroll view is created, you can immediately scroll downwards to the initial view. You can set the initial scroll offset using the xml attribute android:scrollY.
By code you can definitely achieve this. In this sample code I have 15 buttons in srollview. And hide 1st button for initial display.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.sview);
hscrollViewMain.post(new Runnable() {
public void run() {
Button bt2 = (Button)findViewById(R.id.button2);
int nY_Pos = bt2.getTop();
// scroll to top of bt2
hscrollViewMain.scrollTo(0,nY_Pos);
}
});
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:id="#+id/bt1"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
.
.
.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 15" />
</LinearLayout>
</ScrollView>
I'm learning Android and I was trying out FrameLayout containing ImageViews, I tried to do a little app that switchs between two images when you click on them the code is the following:
My xml looks like this:
>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Hola" >
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1" />
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2" />
</FrameLayout>
And my main program:
public class Hola extends Activity {
ImageView primera;
ImageView segunda;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teta_layout);
primera = (ImageView) findViewById(R.id.primera);
segunda = (ImageView) findViewById(R.id.segunda);
}
public void metodoprimera (View view){
primera.setVisibility(View.GONE);
segunda.setVisibility(View.VISIBLE);
}
public void metodosegunda (View view){
segunda.setVisibility(View.GONE);
primera.setVisibility(View.VISIBLE);
}
}
This program should show an image and as soon as you click on it it should hide that image and show the other and so on.
The thing is that this won't work , but as soon as I switch the imageview order in the xml, it works, and i don't really understand why it should not work this way.
Thank you guys in advance
Try adding:
android:visibility="gone"
to the XML for one of the ImageView's.
I'll try to go more explicit with the explanation, here is the thing:
If I have the XML like this it will show the images (depending on visibility) but the oClick thing will not work:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Hola" >
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1"
android:visibility="gone" />
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2"
android:visibility="visible" />
</FrameLayout>
If I swap the ImageView's order in the XML It will work perfectly:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Hola" >
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2"
android:visibility="visible" />
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1"
android:visibility="gone" />
</FrameLayout>
It's not about the visibility of each Imageview since it does not matter what value I put on them It won't work the first way but i will work the second way.
Seems like a weird issue since the oder of the imageviews should not matter if you set them visible or gone ...
As the title shows, I'm trying to make a button in my Android app to change a TextView but it ain't working properly... Here's the code, I hope you guys see what's going wrong. FYI, I'm using NetBeans with the Android 2.3.3 emulator connected as I will be running on my phone (2.3.6) later.
Main java:
public class Rooster extends Activity
{
private Button buttonSearch;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
this.buttonSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textRooster = (TextView)findViewById(R.id.textRooster);
textRooster.setText("some text");
}
});
}
Well here is my main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="42px"
android:layout_weight="1"
android:gravity="center_vertical" >
<EditText
android:id="#+id/editSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="2"
android:gravity="center"
android:hint="Zoekopdracht" />
<Button
android:id="#+id/buttonSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="1"
android:gravity="center"
android:text="Zoek" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="39px"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="#+id/buttonChangeWijzigingen"
android:layout_width="fill_parent"
android:layout_height="37px"
android:gravity="center"
android:text="Zet wijzigingen aan" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="12" >
<TextView
android:id="#+id/textRooster"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Geen rooster opgezocht" />
</LinearLayout>
</LinearLayout>
I think the button that you think your clicking is actually getting drawn over. try changing
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
to
this.buttonSearch = (Button)this.findViewById(R.id.buttonChangeWijzigingen);
It doesnt actually fix the problem but just makes more clear what the issue is.
I'm trying to start an application and I'm kind of lost on how to do what I want.
This is what I want
Both red panels should be able to slide to the sides and the white one should expand if one of them (or both) them is collapsed, occupying that panel space.
When I have the red panels on the screen, the white panel show all the content and not be under any of the panels.
What have I tried so far:
I started by trying with two SlidingDrawers, but the white panel got behind the red ones, so, no luck there.
After that I tried with 2 LinearLayouts (red panels) and a RelativeLayout (white panel), and tried to change width of the layouts with buttons (like is on the image on top). This caused always problems that I didn't know how to resolve.
Suggestions?
Edit:
xml of the SlidingDrawer example:
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="bottom"
android:background="#FFFFFFFF">
<SlidingDrawer
android:layout_width="100dip"
android:id="#+id/SlidingDrawer"
android:handle="#+id/slideHandleButton"
android:content="#+id/contentLayout"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/contentLayout"
android:orientation="horizontal"
android:background="#C0C0C0"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Meio"
android:layout_toRightOf="#+id/buttonEsq" />
</LinearLayout>
<Button
android:layout_width="30dip"
android:layout_height="30dip"
android:id="#+id/slideHandleButton"
android:background="#drawable/arrowup">
</Button>
</SlidingDrawer>
</LinearLayout>
And the xml of the LinearLayout example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_toLeftOf="#+id/linearLayout3"
android:layout_toRightOf="#+id/linearLayout1"
android:background="#FFFFFFFF">
<Button
android:id="#+id/buttonEsq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="v--" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Meio"
android:layout_toRightOf="#+id/buttonEsq" />
<Button
android:id="#+id/buttonDir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView1"
android:text="--v" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:background="#FFFF0000">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Esquerda" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#FFF00000">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Direita" />
</LinearLayout>
</RelativeLayout>
The android code (the commented part is the code of the 1st example):
public class Main extends Activity {
Button slideHandleButton;
Button slideHandleButtonLeft;
Button slideHandleButtonRight;
SlidingDrawer slidingDrawer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayouts);
/* slideHandleButton = (Button) findViewById(R.id.slideHandleButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
slideHandleButton.setBackgroundResource(R.drawable.arrowdown);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
slideHandleButton.setBackgroundResource(R.drawable.arrowup);
}
});*/
slideHandleButtonLeft = (Button) findViewById(R.id.buttonEsq);
slideHandleButtonLeft.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LinearLayout lll = (LinearLayout) findViewById(R.id.linearLayout1);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(50, LinearLayout.LayoutParams.FILL_PARENT);
lll.setLayoutParams(params);
}
});
}
}
SlidingDrawer can only slide in from the right or the bottom, it is unable to slide in from the top or the left. You can try the answer from this question: Android SlidingDrawer from top? or you can get try rewrite the source code http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/SlidingDrawer.java.
However, a SlidingDrawer wont allow
the white panel show all the content and not be under any of the panels
as it will slide over the white panel
The only solution I can think of is to use a series of scale/translate, translate the left or right panel off-screen and set visibility to gone, while at the same time scale the center panel out in the direction of whichever red panel is being removed.
I tried to make a similar prototype some time ago and used this method but the scale animations looked terrible, not to mention the onAnimationEnd listener doesn't work, you'll have to extend a LinearLayout and override onAnimationEnd there for it to work.
I want to create a following kind of layout in android , rite now i have created with fixed height of listview which is causing a problem with different dimension of screens of mobiles.
Initially when this screen appears a button will be at the bottom side only with empty listview and as an when the items will get added in a list view , it grows but button will remain steady at the bottom.
so far i have written following code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/cText"
android:src="#drawable/c_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dip"
/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
/>
<ImageButton
android:id="#+id/cBtn"
android:layout_width="150dip"
android:layout_height="52dip"
android:src="#drawable/preview"
android:layout_alignParentTop="#+id/list"
android:layout_gravity="center_horizontal"/>
</RelativeLayout>
Try this
< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageButton
android:id="#+id/cBtn"
android:layout_centerHorizontal="true"
android:layout_width="150dip"
android:layout_height="52dip"
android:src="#drawable/preview"
android:layout_alignParentBottom="true"
/>
<ListView
android:id="#+id//listview01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/cBtn"
android:layout_marginBottom="10dip"
/>
< /RelativeLayout>
You can add a Button as a footer to List View. The sample code sinppet is shown below
Step1: Create a Button
<Button android:id="#+id/footer" android:gravity="center"
android:layout_gravity="center"
android:text="My Footer"
android:layout_width="wrap_content"
android:layout_height="0dp" android:layout_weight="1"/>
Step2: Make it as Footer to the List View
ListView myList;
View footerView;
ViewGroup footerViewOriginal = (ViewGroup) findViewById(R.id.footer);
footerView = inflater2.inflate(R.layout.footer,footerViewOriginal);
footerView.findViewById(R.id.footer);
myList.addFooterView(footerView, null, true);
Step3: Create on ClickListener and write the action what you want to perform
footerView.findViewById(R.id.footer).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}