Few includes in the same space - android

As you can see in the image, the profile_content Layout takes the whole space available and when I press the "Profile" button the information display correctly.
But when I press the "Messages" button it's not display anything. As you can se in the image, the "blue rectangle" does not fill the whole space like the profile_content Layout.
Both layouts have the same value (wrap_content) in android:layout_widt parameter.
I have some code like:
public void onClick(View v) {
switch(v.getId()){
case R.id.bProfile:
Log.d(TAG, "onClick, buttonProfile pressed");
//Hide previous layout
activeLayout = "bProfile";
profileContent.setVisibility(View.VISIBLE);
//Access for extras passed in from login activity
tUserName.setText(getIntent().getStringExtra("tProfileName"));
break;
case R.id.bMessages:
Log.d(TAG, "onClick, buttonMessages pressed");
profileContent.setVisibility(View.INVISIBLE);
messagesContent.setVisibility(View.VISIBLE);
activeLayout = "bMessages";
test.setText("Test");
break;
}
}
Maybe I should use FragmentLayout for this particular functionality?
Thanks in advance.

LinearLayout isn't gonna help you in this situation. Try using RelativeLayout, something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/buttonsMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QWERTY" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QWERTY" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ASDFG" />
</LinearLayout>
<RelativeLayout
android:layout_toRightOf="#id/buttonsMenu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/profile_content"
layout="#layout/profile_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<include
android:id="#+id/profile_content"
layout="#layout/messages_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>

Related

How to call a layout inside the linear layout after clicking the buttons?

I'm learning android and And I'm facing some troubles. I kindly request you to solve these problems.
I have 2 linear layouts inside a linear layout, both are placed horizontally. The left side section contains some buttons and if we click the button the respective layout must come on the right-side section. As of now, if I click the button the specific layout is appearing but not inside the second linear layout. And also, I want to set a default layout to appear on the right side layout. For ex, here I've added the "breakfastdishes.xml", which I want as a default right-side layout, and when I click on the buttons from the left-side layout, according to id the right-side layout must change. Can you please help me to achieve it?
Here is my code:
MenuSection.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuSection"
android:background="#drawable/gradient"
android:padding="10dp">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/breakfast"
android:layout_width="220dp"
android:layout_height="65dp"
android:text="#string/breakfast"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#AC8E0D"/>
<Button
android:id="#+id/lunch"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:text="#string/lunch"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#0A5FAA"/>
<Button
android:id="#+id/eveningSnacks"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:text="#string/snacks"
android:textSize="20dp"
android:textStyle="bold"
app:backgroundTint="#DF5124" />
<Button
android:id="#+id/dinner"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:text="#string/dinner"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#14A61A"/>
</LinearLayout>
</ScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:id="#+id/itemsDisplay">
<LinearLayout
android:id="#+id/menuDispArea"
android:layout_width="508dp"
android:layout_height="412dp">
<!-- <include-->
<!-- layout="#layout/breakfastdishes"-->
<!-- android:layout_width="508dp"-->
<!-- android:layout_height="412dp" />-->
</LinearLayout>
</ScrollView>
</LinearLayout>
BreakfastDishes.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:paddingStart="12dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingEnd="12dp"
android:paddingRight="10dp"
android:paddingBottom="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical">
<ImageView
android:id="#+id/burger"
android:layout_width="155dp"
android:layout_height="102dp"
android:layout_x="10dp"
android:layout_y="10dp"
app:srcCompat="#mipmap/burger_breakfast" />
<TextView
android:id="#+id/burgerTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/burger"
android:layout_alignTop="#+id/burger"
android:layout_alignRight="#+id/burger"
android:layout_alignBottom="#+id/burger"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/burger"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:orientation="vertical">
<ImageView
android:id="#+id/eggOmlet"
android:layout_width="160dp"
android:layout_height="104dp"
android:layout_x="233dp"
android:layout_y="7dp"
app:srcCompat="#mipmap/egg_omlet" />
<TextView
android:id="#+id/eggOmletTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/eggOmlet"
android:layout_alignTop="#+id/eggOmlet"
android:layout_alignRight="#+id/eggOmlet"
android:layout_alignBottom="#+id/eggOmlet"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/eggOmlet"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
MenuSection.java
package com.example.restaurant;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MenuSection extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_section);
Button breakfast = (Button) findViewById(R.id.breakfast);
Button lunch = (Button) findViewById(R.id.lunch);
breakfast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MenuSection.this, BreakfastDishes.class);
startActivity(intent);
}
});
lunch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MenuSection.this, LunchDishes.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed(){
//super.onBackPressed();
}
}
Akshay, here are a few things you have to keep in mind ;
In the button click listener you are launching new activity instead of updating
the existing layout. When you use Intent it will launch new activity and
close the current activity. So in your case do not use startActivity
Use findViewById for the menuDispArea layout and add update
the content when user clicks on the button.
never use dp for text size, use sp
you can divide the layout using LinearLayout weights to split your layouts on
all the devices instead of hardcoding the height and width
https://developer.android.com/guide/topics/ui/layout/linear#Weight
edit: Added sample
LayoutInflater inflater = LayoutInflater.from(MenuSection.this);
LinearLayout parentLayout = findViewById(R.id.menuDispArea);
View menuLayout= inflater.inflate(R.layout.BreakfastDishes, parentLayout, false);
parentLayout.addView(menuLayout);
You can use this to inflate any XML in your layout dynamically. Make sure you are doing this inside button onClick for your use case.

2 overlayed EditText fields appearing independently

I have a problem with my layout of my App. I have 2 Fragments which must be displayed in an Activity. It works fine, but the Layout somehome displays an EditText twice and also behind each other. You can see the hint of the EditText is darker and once I type something in the TextEdit I can see the other EditText independently still visible in the background. Also the TextView and Switch is displayed twice as you can see from the darkness of the appearance:
The Layout files:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".QuickSteuerActivity">
<fragment
android:id="#+id/calculation_fragment"
android:name="de.immozukunft.quicksteuer.CalculationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="#+id/settings_overview_fragment"
android:name="de.immozukunft.quicksteuer.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/> </LinearLayout>
fragment_calculation.xml (This is where the EditText and Switch etc. is)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_calculation"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/turnover"
android:defaultValue="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/turnover_tax"
android:inputType="numberDecimal"
android:textSize="36sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/turnover_vattxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="#string/turnover_vattxt"
android:visibility="visible" />
<Switch
android:id="#+id/turnover_vat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:foregroundGravity="top"
android:title="#string/industrial_tax"
android:visibility="visible" />
</LinearLayout>
<EditText
android:id="#+id/costs"
android:defaultValue="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/costs"
android:inputType="numberDecimal"
android:textSize="36sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/costs_vattxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="#string/turnover_vattxt"
android:visibility="visible" />
<Switch
android:id="#+id/costs_vat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:title="#string/industrial_tax"
android:visibility="visible" />
</LinearLayout>
<Button
android:id="#+id/btncalculate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/calculate"
android:textSize="24sp" />
</LinearLayout>
</FrameLayout>
fragment_settings_overview.xml (2.Fragment)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_settings_overview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/current_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:padding="10dp"
android:text="#string/overview_settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
I can also provide the Java-files if necessary or any additional files. I just didn't want to bloat the post.
If anybody has got any other additional advices (coding style etc.) please let me know, I am relatively new to Android programming :)
thanks in advance
THE-E
EDIT:
I think I am messing up the layout with the fragment manager. But I am not sure. Because now I see even some PreferenceFragment.
onCreate() of the Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Layout
manager = getSupportFragmentManager(); //Fragment-Manager
CalculationFragment calculationFragment = new CalculationFragment(); //create calculation fragment
SettingsOverviewFragment settingsOverviewFragment = new SettingsOverviewFragment(); //create settings overview fragment
calculationFragment.setArguments(getIntent().getExtras()); //I don't know what happens here
settingsOverviewFragment.setArguments(getIntent().getExtras()); //I don't know what happens here either
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.calculation_fragment, calculationFragment); //add calculation fragment to the fragment transaction
transaction.add(R.id.settings_overview_fragment, settingsOverviewFragment); //add settings overview fragment to the fragment transaction
transaction.commit(); //commit the transaction
}
Change height and width of both the fragments to wrap_content and see if it works for you ?
I've used Hint in the second text and TEXT in the first
android:hint="#string/turnover_tax"
To display the text dimly use the Hint instead of the Netscape
android:hint="Your String"
android:text="Your String"
I've used Hint in the second text and TEXT in the first
android:hint="#string/turnover_tax"
To display the text dimly use the Hint instead of the Netscape
android:hint="Your String"
android:text="Your String"
Using the Hint means a tip which must be written in the EditText
When you type Netscape in the Netscape EditText, the embedded grid will appear in the EditText and can be set or changed
But when you write a hint it will disappear once you start typing in the EditText that contains it
I found my mistake. I was adding in the onCreate of the main Activity a Fragment on top of the already set Fragment using the FragmentManager. The Fragment was already set in the layout ( itself, that is why I don't need to use a FragmentManager (fragment_calculation.xml).
All I had to do is set the layout of the activity and I was done.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Layout
}
this was my onCreate()-Method.
thanks for your help! :)

How to create overlay at the bottom of view pager for image slider in android

I want to make layout for Image Slider, so have to create the view pager as well as have to make a overlay at the bottom of view pager.
How can I create that layout for Image Slider.
As i mentioned above image like that want to make layout in android
Thanks in Advance
I used a component like this before.
Basicly those codes should be enough for you. It doesn't have to be complicated realy.
These are your layout codes. I'm sure you will manage to use them in java codes according to your needs.
Here is the link for horizontalListView:
https://github.com/sephiroth74/HorizontalVariableListView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffAAAAAA" />
<!--You may use here-->
<!--HorizontalListView or-->
<!--RecyclerView with horizontal layoutManager-->
<!--I would use HorizontalListView it's easier-->
<!--You may find it by searching in google-->
<HorizontalListView
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:layout_height="150dp"
android:background="#66FFffFF" />
</FrameLayout>
Happy coding.
this is xml code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<ImageView
android:id="#+id/selected"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/gallery_relative_layout"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:layout_marginTop="30dip"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"
android:layout_marginTop="300dp"
android:layout_above="#+id/gallery_relative_layout"
/>
<RelativeLayout
android:id="#+id/gallery_relative_layout"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:paddingTop="20dp">
<HorizontalScrollView
android:id="#+id/hor_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im1"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im2"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im3"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im4"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im5"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im6"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im7"
android:onClick="biggerView"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
</RelativeLayout>
this is MainActivity.java code :
public class MainActivity extends Activity {
ImageView im;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void biggerView(View v)
{
im=(ImageView)findViewById(R.id.selected);
switch (v.getId())
{
case R.id.image1: im.setImageResource(R.drawable.im1);
break;
case R.id.image2: im.setImageResource(R.drawable.im2);
break;
case R.id.image3: im.setImageResource(R.drawable.im3);
break;
case R.id.image4: im.setImageResource(R.drawable.im4);
break;
case R.id.image5: im.setImageResource(R.drawable.im5);
break;
case R.id.image6: im.setImageResource(R.drawable.im6);
break;
case R.id.image7: im.setImageResource(R.drawable.im7);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
here is output :
When you click any image it display as follow.
NOTE : Here I have used my Image used your Image and see the result.
You can use Recycler View at Bottom. and add a LinearLayoutManager with Horizontal scrolling. add a a single item image view in your adapter. It will definitely help you :)

Blank space remains after removing some content from layout

Xml Code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/trip2"
android:orientation="vertical">
<com.cengalabs.flatui.views.FlatTextView
android:id="#+id/Rpickdtetx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Return Pick Up Date"
/>
<com.cengalabs.flatui.views.FlatEditText
android:id="#+id/Rpickdte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:maxLines="1"
android:textSize="15dp" />
</LinearLayout>
</RelativeLayout>
<!--Return Pick up time-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.cengalabs.flatui.views.FlatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Return Pick Up Time"
android:textSize="15dp"
flatui:fl_theme="#array/grass"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
<com.cengalabs.flatui.views.FlatEditText
android:id="#+id/Rpicktme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:maxLines="1"
android:gravity="center_vertical"
android:hint="Pick-Up Time"
android:includeFontPadding="true"
android:drawableLeft="#drawable/time"
flatui:fl_fieldStyle="fl_box"
/>
</LinearLayout>
</RelativeLayout>
.java code
Triptype.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.triptype1:
view.findViewById(R.id.trip2).setVisibility(View.INVISIBLE);
view.findViewById(R.id.trip21).setVisibility(View.INVISIBLE);
/* view.findViewById(R.id.Rpickdte).setVisibility(View.INVISIBLE);
view.findViewById(R.id.Rpickdtetx).setVisibility(View.INVISIBLE);*/
break;
case R.id.triptype2:
ReturnTrip(view);
break;
}
}
});
I am newbie to android,I am using radio button on toggling,have to add some text boxes(above xml code of text boxes),My query making visible/invisible text box even though i wrap_content, can see a empty space.Can anyone help me how to resolve that.
view.findViewById(R.id.trip2).setVisibility(View.GONE);
view.findViewById(R.id.trip21).setVisibility(View.GONE);
/* view.findViewById(R.id.Rpickdte).setVisibility(View.GONE);
view.findViewById(R.id.Rpickdtetx).setVisibility(View.GONE);*/
break;
set them to "Gone" instead of "invisible" will help to retain space
Try to use View.GONE instead of View.INVISIBLE.
The documentation says:
View.GONE: This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE: This view is invisible, but it still takes up space for layout purposes.
If you want to know more, you can look at this question.

View Gone when clicked outside that view

I wanted to hide the view when clicking outside that view, for example,
<Framelayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/imgButtonToOpenGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="#drawable/open_grid_img"
/>
<RelativeLayout
android:id="#+id/containerGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
>
<Button
android:id="#+id/button1grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/favourites"
/>
<Button
android:id="#+id/button2grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/recent"
android:layout_toRightOf="#+id/button1grid"
/>
<Button
android:id="#+id/button3grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/gridview"
android:layout_toRightOf="#+id/button2grid"
/>
</RelativeLayout>
</FrameLayout>
What I am doing in my app is oncreate i am hiding the RelativeLayout view with id="containerGrid" and making that relativeLayout view visible when I click on the imageview.
So my requirement is I wanted to hide the container RelativeLayout with id="containerGrid" when I click outside that container.
I tried to get the framelayout container and when clicked on that container checking if relativelayout container is shown or not , if shown then made its view gone. Here is the code i tried,
containerMap = (FrameLayout)findViewById(R.id.container);
containerMap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(rltvContainer.isShown()){
rltvContainer.setVisibility(View.GONE);
}
}
});
Is there anywhere i am going wrong in above thing.
Actually i have map too in that framelayout container so here i am expecting when i click on a map the relativelayout container should go.
Add another coverView which is transparent in front of the map fragment
<Framelayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="#+id/coverView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#00000000"
android:visibility="gone"
/>
<ImageView
android:id="#+id/imgButtonToOpenGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="#drawable/open_grid_img"
/>
<RelativeLayout
android:id="#+id/containerGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
>
<Button
android:id="#+id/button1grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/favourites"
/>
<Button
android:id="#+id/button2grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/recent"
android:layout_toRightOf="#+id/button1grid"
/>
<Button
android:id="#+id/button3grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/gridview"
android:layout_toRightOf="#+id/button2grid"
/>
</RelativeLayout>
</FrameLayout>
When the containerGrid is opened(which means its' visibility is View.VISIBLE, set the coverView's visibility to View.VISIBLE.
If you want to close containerGrid, which is that you want to click outside containerGrid, you actually click on the coverView, set OnClickListener to the coverView:
coverView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(rltvContainer.isShown()){
rltvContainer.setVisibility(View.GONE);
}
coverView.setVisibility(View.Gone);
}
});
set both coverView and containerGrid to View.GONE.
Try setting dependent focusability false in framelayout and it will work
The problem is frame layout is not getting the click event
Descendent focus ability false give you ability disable click event in child views if a parent vieegroup

Categories

Resources