How to make a layout of an activity visible through programming? - android

I have a layout as indicated below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<LinearLayout
android:layout_height="match_parent"
...
The associated activity is like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_login);// here is the layout
}
I'm trying to make it visible in the following way:
LinearLayout layoutActLogin = (LinearLayout) findViewById(R.layout.act_login);
layoutActLogin.setVisibility(View.VISIBLE);
But Android Studio told me that there is an error about R.layout.act_login

findViewById is for views, not layouts.
You should put an ID in your view like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<LinearLayout
android:layout_height="match_parent"
...
Then get the view reference and make it visible
View viewActLogin = findViewById(R.id.my_view);
viewActLogin.setVisibility(View.VISIBLE);

You can not change visibility of Layout. you can only change visibility of Views inside.
You can assign id to you view by using android:id tag in you layout. You can read more about this here.
In your case. Just give some Id to your View/ViewGroup, and reference that View from your Activity and using findViewById method and change its visibility.

You're misunderstanding how layouts and views work. Layouts define what is shown on-screen to a user during an activity, set by calling setContentView() within the Activity's onCreate() method. Views are individual elements within the layout, which are accessed with the R.id prefix using findViewById().
In your example you'll need to apply an ID to the root ConstraintLayout (using android:id) to be able to access it:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
Then you can access it with:
ConstraintLayout layoutActLogin = (ConstraintLayout) findViewById(R.id.parent_layout);
layoutActLogin.setVisibility(View.VISIBLE);
EDIT Looking at your code I now realise you want to control the root ConstraintLayout, which makes my answer almost identical to Eduardo Herzer's. Leaving my answer up due to the added explanation at the beginning.

Related

findViewById() returns null - what should I do?

I am using a bottomSheetBehavior in my android project. See codes below:
onlineGame.java:
// get the bottom sheet view
ConstraintLayout llBottomSheet = findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);
// init the bottom sheet behavior
end_of_online_game_popup = BottomSheetBehavior.from(llBottomSheet);
avtivity_online_game.xml:
.
.
.
<android.support.design.widget.CoordinatorLayout 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="com.androidsample.BottomSheetActivity">
<!-- include bottom sheet -->
<include
android:id="#+id/includeBottomSheetBehavior"
layout="#layout/test_end_of_online_game_popup" />
</android.support.design.widget.CoordinatorLayout>
.
.
.
test_end_of_online_game_popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/end_of_online_game_bottom_sheet_behavior_cl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cardview_light_background"
android:visibility="gone"
app:behavior_hideable="false"
app:behavior_peekHeight="120dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
.
.
.
The problem is this line:
ConstraintLayout llBottomSheet = findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);
is returning null. I even changed the code place to onResume, but it did not work. When I get another element in test_end_of_online_game_popup, it works well and not null.
What is the problem?
tnx
Two choices:
ConstraintLayout llBottomSheet = findViewById(R.id. includeBottomSheetBehavior);
<include layout="#layout/test_end_of_online_game_popup" />
Try this:
View view = findViewById(R.id.includeBottomSheetBehavior);//firstly get the root view ID
ConstraintLayout llBottomSheet = view.findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);
As you are using included layouts :
Use the find view on the id you have given to the include tag
findViewById(R.id.includeBottomSheetBehavior)
Or you can omit the id in the layout tag so that its not overridden.
In the < include > tag, only the layout attribute is required. This attribute, is a reference to the layout file you wish to include. This tag also lets you override a few attributes of the included layout.
The above example shows that you can use android:id to specify the id of the root view of the included layout; it will also override the id of the included layout if one is defined. Similarly, you can override all the layout parameters.
Source : http://www.curious-creature.com/2009/02/25/android-layout-trick-2-include-to-reuse/

Reuse a rendered layout in some other layout at run time

I am using a RecylerView inside a layout l1.xml. I am including this l1.xml inside l2.xml using include tag.
I update this RecyclerView after an api call but l2.xml is not showing the updated RecyclerView.
Is there a way to forcibly ask the parent to refresh?
invalidate(), refreshDrawableState(); on the parent layout didn't help?
Is there a smarter way to use a rendered layout in multiple places?
l1.xml
...
...
<LinearLayout
android:id="#+id/feed"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#color/light_primary_background">
<include layout="#layout/events_list"/>
</LinearLayout>
...
...
events_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/events_recycler_view"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
I update the events_recycler_view after an API call and the events_list.xml is updated but the include in l1.xml is not updated
Yes you can always use LayoutInflater to inflate a view, but the view must have the ids and type matching the id and type defined in your java code.
Check out this link for how to use layout inflater
http://www.programcreek.com/java-api-examples/android.view.LayoutInflater

Set onClickListener on view from included layout not working

I have one layout which includes other layout inside. I tried to set onClikeListener on ImageView inside the included layout, but it's not working. But when I set background drawable it works. I don't know why. Here is my code:
//custom header
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sticky_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_header_close"
android:layout_width="60dp"
android:layout_height="55dp"
android:background="#drawable/big_cross_icon" />
</LinearLayout>
//activity_detail
<?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"
android:background="#f1f2f6">
<include
android:id="#+id/header"
layout="#layout/custom_header"/>
</RelativeLayout>
//in Activity
View header = findViewById(R.id.header);
iv_header_close = (ImageView)header.findViewById(R.id.iv_header_close);
iv_header_close.setBackgroundDrawable(getResources().getDrawable(R.drawable.big_edit_icon));
iv_header_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish(); //not working
}
});
I wonder why I can access to child view inside included layout but can't set OnClickListner. Thank you so much :)
Create your iv_header_close like this:
ImageView iv_header_close = (ImageView) findViewById(R.id.iv_header_close);
Instead of calling finish() Try to call YourActivity.this.finish()
What's happening is that, inside the onClick of your imageview, you don't have access to your activity so you have to access your activity using YourActivity.this if you want to access the methods of your activity.
In case anybody is having the same issue five years later...
Using the onClick attribute on the view (ImageView, Button... mine was a button) inside the included layout file fixes the problem. However, note that the onClick attribute is now deprecated

How To Use Same LinearLayout In Different Activities

I want to use same LinearLayout in different activities. Selected LinearLayout's id in picture is previewLinear. What I want to do is, use previewLinear in a different activity. I have A and B activities. Firstly I do some changes for previewLinear in activity A (adding border, slice it to pieces etc.) programmatically and I want to copy whole previewLinear to B activity.
pencerebol.xml
What I tried is;
A Activity: (container is static)
...
setContentView(R.layout.pencerebol);
...
container = (LinearLayout) findViewById(R.id.previewLinear);
container.setBackground(getResources().getDrawable(R.drawable.border));
B Activity:
...
setContentView(R.layout.pencerebol);
container = (LinearLayout) findViewById (R.id.previewLinear);
container = A.container; // I know that this line is completely wrong but this is what i want to do.
Thanks in advice.
You can use the <include> property in your XML files, you only create one layout and include it in your other activities/fragments, this is an example of that:
create a xml for your previewLinear layout, name it as:
layout_preview_linear.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/previewLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
and then in your activity A or B, you can include it as the following:
activity_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_a_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/previewLayout"
layout="#layout/layout_preview_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
You also can use that layout in your JAVA code as well, hope this will help you.
If I got it right you want to make LinearLayout from B activity look same as in A activity.
You can create method in A activity
static void createLinearLayout(LinearLayout layout){
layout.setBackground(getResources().getDrawable(R.drawable.border));
...
}
Then call it in B activity
A.createLinearLayout(container);

How to get a view from a resource?

I have a UI, I build it dynamically. I should want to put some component in a xml resource file. So I do :
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/titreItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
... in a file res/layout/titreitem.xml as I see anywhere. But I don't understand how get it to put inside my UI. So, inside activity.onCreate, I want to do something like :
RelativeLayout myBigOne = new RelativeLayout(this);
TextView thingFromXML = [what here ? ];
myBigOne.addView(thingFromXML);
setContentView(myBigOne);
Use a LayoutInflater....The Entire Layout can be inflated, without dynamically creating it....
LayoutInflater li = LayoutInflater.from(context);
View theview = li.inflate(R.layout.whatever, null);
Approach seems to be little incorrect. You should put RelativeLayout to the xml as your TextView made, and inflate the whole xml. Afterwards, you will be free to add views to your layout. So, do this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+androi:id/relLayout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/titreItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
In your activity:
setContentView(R.layout.titreitem);
RelativeLayout layout = (RelativeLayout)findViewByid(R.id.relLayout);
layout.addView(...);

Categories

Resources