how to hide and show custom view in android development? - java

I have two view class that only contains drawing. I want to change the view class from one class to another just by tap the 'door' (on specific coordinates).
Here is my layout:
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.insidehouse.InsideHouseView
android:id="#+id/insideHouseView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<com.example.insidehouse.BedRoomView
android:id="#+id/bedRoomView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_centerVertical="false" />
</RelativeLayout>

Try using a ViewSwitcher. http://developer.android.com/reference/android/widget/ViewSwitcher.html
You can surround your two views with a viewswitcher and have Android smoothly switch between views. You can even use animations to give your user more eye candy

You can hide a view by setting its visibility to VIEW.GONE.

Add a onClickListener in the view and use setVisibility to show/hide the door/bedroom
to go in (onClickListener of the door):
insideHouseView1.setVisibility(View.GONE);
bedRoomView1.setVisibility(View.VISIBLE);
to go out (onClickListener of the door of the bed?):
insideHouseView1.setVisibility(View.VISIBLE);
bedRoomView1.setVisibility(View.GONE);
There are other views which allows you to switch between views, but i never tried it.. ViewFlipper maybe.

Related

Child LinearLayouts not displaying properly

I have one LinearLayout - horizontal, and it has 3 vertical LinearLayouts in it.
I add TextViews to those LinearLayouts programmaticaly. This is my xml:
<LinearLayout 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"
android:id="#+id/hlkurva"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity$PadyFragment">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:id="#+id/cesky">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/pomlcka"
android:layout_weight="0.2"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:id="#+id/latinsky"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
This is how it looks like on Android Studio preview.
[
However, when I run the app I get this:
[
When I rotate screen, it persist. I tried different layout weights, I tried it different way with no layout weight, I tried everything I can but I never get 3 Linear Layouts displayed horizontal to each other how it looks like on the preview. I am really desperate, please help.
This looks to me like you've passed the wrong view id to a findViewById() call in your activity. Maybe something like this:
LinearLayout left = findViewById(R.id.cesky);
LinearLayout middle = findViewById(R.id.latinsky); // wrong id here
LinearLayout right = findViewById(R.id.latinsky);
Then, later when you add all the - characters to the middle column, you're actually adding them to the right column because you've accidentally assigned the wrong view to middle.

Resizing Image Buttons and Image Views on a XML Page

So I have this XML layout page, but when i put it on a bigger screen nothing resizes, i was wondering how this would be done. Can it be done through the XML or Java, or both?
This is my layout code
<RelativeLayout 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainPage"
android:background="#drawable/appbackground3"
android:id="#+id/HomeLayout">
<ImageButton
android:layout_width="120dp"
android:layout_height="120dp"
android:id="#+id/PlayBtn"
android:background="#drawable/mybutton2"
android:src="#drawable/playbutton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="500dp"
android:layout_height="120dp"
android:id="#+id/TitleImg"
android:background="#drawable/title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
I believe it is because you are already setting the height and width of the images. What do you expect the system will do? I would rather use wrap_content, match_parent, Fill_parent depending what you want the UI to look like. That way your app will behave differently in every layout and screen sizes. I highly recommend you look into fragments, as they will give you much more flexibility.

What can i use instead of a Sliding drawer

im making a launcher app. in the tutorial it uses a Sliding Drawer which deprecated in Api 17. what could i use instead of this? i tried using the Slideup Panel from one of the posts on here link Below to the gitHub below
gitHubLinkhttps://github.com/umano/AndroidSlidingUpPanel
. but it wont allow me to use it in the way i want. i want the apps to appear in the drawer not in the main content. for some reason the second view only allows me to create a Text view which is for the handle to slide it up. any advice on what other methods i could implment to do the same job. it must slide up from the bottom. thanks for all the help
Ok guys Sussed it. Using The GitHub I posted in the question. what it did is because im aloud as many layouts as i like but only 2 children. i used a relative layout as my first layout with a gridView for my homescreen apps inside and closed it off. then i used anther Relative Layout Below with a textview inside then another Relative layout set with 80dp Padding at the top (the Size of my Text Handle) inside this one i had another gridview for my apps and closed it all off.
the Xml for it looks like this
<RelativeLayout 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=".MainActivity"
android:id="#+id/Mainlayout"
>
<com.sothree.slidinguppanel.SlidingUpPanelLayout
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoPanelHeight="80sp"
sothree:umanoShadowHeight="4dp"
sothree:umanoDragView ="#+id/dragView"
sothree:umanoScrollableView="#+id/list"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content1"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="50dp"
android:horizontalSpacing="50dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:clickable="true"
>
</GridView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/handle"
android:text="b"
android:textSize="80sp"
android:textColor="#17ff1c"
android:gravity="center|top"
android:clickable="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="80sp"
android:id="#+id/dragView"
android:clickable="true"
android:focusable="false">
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="80sp"
android:id="#+id/content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="50dp"
android:horizontalSpacing="50dp"
android:stretchMode="columnWidth"
android:gravity="center"
>
</GridView>
</RelativeLayout>
</RelativeLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
</RelativeLayout>

Android button layout adapting to screen size

I am new in Android development.I created a simple project with a simple button.I thought that the
button will look the same on different screen sizes but when i previewed all screens eclipse displayed this
http://imgur.com/kjEMhHx
This is the xml code
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.businessideas.MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Button" />
</RelativeLayout>
Can you please help me and tell me how can i design that button look the same on all screen sizes?
Thanks
You'll need to use the weight attribute when the button's parent is LinearLayout. That way it will force the button to have a size proportional to the screens width:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:text="Button"
android:layout_weight="2"
android:layout_width="0dp"/>
</LinearLayout>
Notice that if the LinearLayout's orientation is horizontal, width will be adjusted by android (implicitly) therefore the children have width of 0dp.
Also note that now the button will take 20% (2/10) of the LinearLayout's (the parent's) width.
On the button part change:
android:layout_width="20dp"
android:layout_height="20dp"
Use specific DP sizes to keep items the same physical size on different screen (replace 20 with your desired size).
If you want your button to be of the same size in all different screen than you have to manually give the DP size.
like
andoird:layout_width = "20dp"
android:layout_height = "20dp"
You can also place your button at specific location by changing your relative layout to linear layout.

HorizontalScrollView swipe/fling guesture listener

I know this is a common question , but I am completely messed with those different examples and tutorials available on the web.
I have a horizontalScrollView where I add some view dynamically , and I want to set a guestureListener for swiping through the Views . Each view's with is equal to the width of the screen.
You can use the View Pager instead of the horizontalScrollView. http://developer.android.com/reference/android/support/v4/view/ViewPager.html
To keep the View pager at half the screen use something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<android.support.v4.view.ViewPager
android:id="#+id/pagerPromo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicatorPromo"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical">
This View Pager has a circle indicator at the bottom but you can remove that if you like. Just play around with the weights and see what works best for you.

Categories