I have mainActivity, that has xml that include another xml . Both activitymain layout has a button, and the included layout has another button. Both should display into the main activity.
Now , i need to change text of both button. I can get reference to first button by findviewByID , and it works. for the second button that is into the second layout, i get view by inflating the layout, It gets properly refence to the view and the findviewbyID gets reference to this button. But when changing the text, it does not change.
activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools">
<Button android:id="#+id/buttonvvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
xmlns:android="http://schemas.android.com/apk/res/android" />
<include
layout="#layout/layouttest"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
layouttest.xml
<?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">
<Button
android:id="#+id/buttonaaa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
---Mainactivity.java----------------------------------
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) view.findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
}
Results
buttonvvv text is updated and becomes ssssssss
buttonaaa text is Not updated and still Button
No need for inflating the included layout, you can just findViewById like you did with b button. It's already included with activity_main.xml layout
Replace this:
View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) view.findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
With :
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
From Docinclude:
The root View should be exactly how you'd like it to appear in each
layout to which you add this layout.
BTW, check this answer
You don't have to inflate the layout, just find it like:
LinearLayout layout =(LinearLayout) findViewById(R.id.layouttest);
and then
Button bb =(Button) layout.findViewById(R.id.buttonaaa);
Related
Front endI have three fragment one will be the main fragment and the other two fragments for some tasks, I want to display the remaining fragment by clicking the buttons in main fragment but the main fragment should not be replaced so that if I click the other button the previous one should be replaced by the the new button , this should continue based on the buttons I click
enter code here
MainActivity:
<?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:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/headline_fragment"
android:name="com.example.fragments.headlinefragment"
android:layout_weight="1"></fragment>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/baseline"
android:name="com.example.fragments.baseline"
android:layout_weight="1"></fragment>
</LinearLayout>
fragment1.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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="headline" />
</android.support.constraint.ConstraintLayout>
similarly for other two
fragment.java:
package com.example.fragments;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class headlinefragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.headline_fragment,container,false);
return view;
}
}
-similar for other two
ok this isnt a great solution, but you can just show or hide the view using set visibility, the actual way to do this is to inflate your fragment using a tag and then check if its in the layout has been added etc before deciding whether to show or remove it, but if your only showing a small fragment that doesnt do much you can include it in your layout like you have already except inside a frame layout and then call fragmentHolder.setVisibility(View.VISIBLE); or View.INVISIBLE to toggle it on and off
I am trying to write code , where after loading the Layout , i want to know which are the view elements are related to the layout like TextView, EditText , Checkbox etc.
MyCode :
MainActivity.java
package com.example.myview;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
// How to get to know about the UI Elements related to this layout //
}
}
demo_layout.xml
<?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:orientation="vertical" >
<LinearLayout
android:id="#+id/lnr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ddccee"
android:orientation="vertical">
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
</LinearLayout>
This layout has TextView , EditText , so , i want to know how to get UI elements are related to this layout programmatically.
So , please suggest me some solution.
You can use getChildCount REF to get the count of views added toViewGroup and then use getChildAt REF to get the View at given index.
You are using LinearLayout as base layout which already extends the ViewGroup Just change below line.
LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
After you've inflated your demo_layout just call findViewById on your view instance like this:
View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
TextView txt1 = (TextView) view.findViewById(R.id.txt1);
Elements of view must be retrieved by view.findViewById().
Except in activity you use findViewById() directly to retrieve view elements of the contentView you set in setContentView().
Ok so I followed the Android Tutorials at the developer.android.com to build my first app. So to create a simple user interface I added a button and text field given in the tutorial. But when I run it on my phone, I don't see the buttons or text field.
package com.example.lookforbuttons;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv= new TextView(this);
tv.setText("Buttons");
setContentView(tv);
}
}
The .xml file where I describe the layout is this:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
and the strings.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Buttons</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
The target android version is 4.03 since I am testing it on 4.03 phone. When I run this I only "Buttons" printed and no button or text field. Thanks.
You call setContentView twice. When you do this, the second time is what you will see on the screen because it will overwrite whatever layout you called in the first call to setContentView(). So since you call
setContentView(tv);
last you only have the TextView. Remove that line and you should see your EditText and your Button.
In your code you are setting setContentview() twice. That means you are changing the layout which contains Button and TextView with second setContentview().
If you want to dynamically add new TextView to your layout. Remove the second setContentView() and assign an id to your LinearLayout in xml. Then find it in your Java code and say linearlayout.add(textview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lv"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edittext"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
Linearlayout lv=(Linearlayout) findViewById(R.id.lv);
lv.add(textview);
I know how to do this via a surfaceView and I'm wondering if I should go down that rout.
I'm simply trying to create a splashscreen that has a fullscreen image with an opaque image laid over the top (after a short delay). I can't work out how this is done in XML code.
This is what I have......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/lightDot"
android:src="#drawable/splashlight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#null"
/>
<ImageView
android:id="#+id/bGround"
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
So my 'lightDot' object is semi-transparent and I want to overlay this on top of my bGround resource after a short delay.
This is my code:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SplashAct extends Activity implements OnClickListener{
Button mainButton;
Button lightDot;
ImageView background;
ImageView light;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
background = (ImageView)findViewById(R.id.bGround);
light = (ImageView)findViewById(R.id.lightDot);
background.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
//finish();
Intent toMainGame = new Intent(this, ActOptions.class);
startActivity(toMainGame);
}
}
Thank you.
What you want is a FrameLayout.
Child views are drawn in a stack, with the most recently added child on top.
You can get more fancy with where the overlays go using layout_gravity, but it sounds like this is all you need.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/image" >
</ImageView>
<ImageView
android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/overlay"/>
</FrameLayout>
Use Relative layout not linear layout.
This allows you to place views ontop of one another, as opposed to in a linear list.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/lightDot"
android:src="#drawable/splashlight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#null"
/>
<ImageView
android:id="#+id/bGround"
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
To make the Dot appear after time, use android:visiblity="INVISIBLE" in the xml. (so it starts invisible)
then use light.setVisiblity(View.VISIBLE); in your code.
You could use a LayerDrawable
Define a simple LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splash_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
In your Activity class:
LinearLayout layout = (LinearLayout) findViewById(R.id.splash_layout);
Drawable drawableLayers[] = new Drawable[] {getResources().getDrawable(R.drawable.splash), getResources().getDrawable(R.drawable.splashlight)};
// ^ The order of drawables is important: The above line overlays splashlight on top of splash.
LayerDrawable layerDrawable = new LayerDrawable(drawableLayers);
layout.setBackgroundDrawable(layerDrawable);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
***android:background="#drawable/splash"*** >
// Try this for invisible
iv.getBackground().setAlpha(0);
//Try this for visible
iv.getBackground().setAlpha(255);
//What great about this is that you could create a loop
//to slowly increment the alpha, creating a fade effect instead of
//invisible to visible.
Hello there people of stack overflow, I have just started developing with android and have run into a bit of a problem. I am trying to run a very simple app that has a single button, which when clicked updates the text to the current time. However, the app does not even start up. I am following a book. "Beginning Android 2", and am just learning about XML layouts. The same app worked without an XML layout, so it may be something to do with my xml.
Anyways, I'm running it on a droid 2, if that helps.
Heres the codes.
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class FirstApp extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button btn;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(this);
updateTime();
}
public void onClick(View view) {
updateTime();
}
private void updateTime() {
btn.setText(new Date().toString());
}
}
And the XML file
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Thanks in advance.
You should enclose the button inside a layout (linear, relative, etc):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Both answers are incorrect. The only line missing in that xml is the first one:
<?xml version="1.0" encoding="utf-8"?>
Of course you will need some kind of ViewGroup to make any meaningful UI, but setContentView can take any View as parameter
The problem is that your root layout must be a ViewGroup. Change the layout to something like this for example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>