So I'm having a little bit of trouble trying to change the text of a TextView in another layout. My MainActivity.java sets the content view as activity_main.xml, which <include>s app_bar_main.xml, which <include>s content_main.xml, where my main app layout is displayed (it's a Navigation Drawer Activity in Android Studio).
Part of my code in MainActivity.java is shown below:
String user, organisation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Default code to include navigation drawer here
if (savedInstanceState == null) {
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
user = bundle.getString("user");
organisation = bundle.getString("organisation");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View hamburgerMenu = inflater.inflate(R.layout.nav_header_main, null);
TextView navUsername = (TextView) hamburgerMenu.findViewById(R.id.nav_username),
navOrgGroup = (TextView) hamburgerMenu.findViewById(R.id.orgGroup);
navUsername.setText(user);
navOrgGroup.setText(organisation);
}
}
}
And my nav_header_main.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="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:contentDescription="#string/user"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:id="#+id/nav_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="#string/user"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/orgGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/email" />
</LinearLayout>
But for some reason the TextViews in nav_header_main.xml never changes. It remains as the default values set in the XML file. Why is this so? Thanks in advance!
Since my comment helped solve:
In my code I'm using somewhat of a different strategy to change the TextView in the navigation drawer. View v = navigationView.getHeaderView(0); TextView emailTextView = (TextView) v.findViewById(R.id.emailTextView);
As for the explanation, you might take a look at this question. It'd be redundant that I copy the text here.
your hamburgerMenu has inflated but not add to any viewGroup to show it
Related
After trying to create a CardView programmatically, a NullPointerException but I've used an if statement and the warning is gone, but I'm not sure what needs to go in else part of the if statement.
Method invocation 'inflate' may produce java.lang.NullPointerException
Java
//start of CardView
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
if(inflater1 != null) {
View inflatedCardviewLayout = inflater.inflate(R.layout.my_cardview, new RelativeLayout(getContext()), false);
inflatedCardviewLayout.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.green));
TextView cv_tv1a = inflatedCardviewLayout.findViewById(R.id.cardview_titleA);
cv_tv1a.setText(getString(R.string.titleA));
ImageView imgPictogram = inflatedCardviewLayout.findViewById(R.id.image_pictogram);
imgPictogram.setImageResource(R.drawable.ic_pictogram);
TextView cv_tv1b = inflatedCardviewLayout.findViewById(R.id.cardview_titleB);
cv_tv1b.setText(getString(R.string.titleB));
} else {
//do something else
}
//end of CardView
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/tools"
android:id="#+id/my_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/cardview_titleA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image_pictogram"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/cardview_titleB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
You should fix the NullPointerException since getting an inflater is a required use for your code. I mean fix by using LayoutInflator.from(parent context) instead of asking the system for one.
You should make a class so that you extend CardView, then use the context of when you create the view object to get the inflater. Also make sure to merge the CardView with the inflated layout!
I'm working on an application that has a navigation drawer and where the user is required to login.
So far, I've got the whole user login/registration thing working and I've built the navigation drawer.
What I want to do now is to make the TextViews on the navigation drawer display the user's full name and his/her email.
I've tried to do so with the LayoutInflater, but the TextViews don't change.
I've also tried passing the user information to LogCat and it displays it correctly.
I don't know where my problem is.
Here is my code:
DrawerActivity.java
public class DrawerActivity extends AppCompatActivity{
public static String[] userInfo = LoginActivity.userInfo;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.navDrawerTheme);
setContentView(R.layout.activity_drawer_layout);
//Declare instances.
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
mNavigationDrawer = (LinearLayout) findViewById(R.id.navigationDrawer);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View drawerHeaderView = inflater.inflate(R.layout.drawer_header, null);
TextView tvFullName = (TextView) drawerHeaderView.findViewById(R.id.name);
TextView tvEmail = (TextView) drawerHeaderView.findViewById(R.id.email);
ImageView imgProfile = (ImageView) drawerHeaderView.findViewById(R.id.imgProfile);
tvFullName.setText(userInfo[0] + " " + userInfo[1]);
tvEmail.setText(userInfo[2]);
Log.d("First name", "First name: " + userInfo[0]);
Log.d("Last name", "Last name: " + userInfo[1]);
Log.d("Email", "Email: " + userInfo[2]);
...
EDIT
drawer_header.xml
<?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="120dp"
android:background="#444444">
<RelativeLayout
android:id="#+id/userInfoWrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toEndOf="#+id/imgProfile"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"
android:singleLine="true"
android:textSize="14sp"
android:textStyle="bold"
android:layout_marginTop="40dp"/>
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:singleLine="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:textStyle="normal"
android:layout_below="#id/name"/>
</RelativeLayout>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="75dip"
android:layout_height="75dip"
android:src="#drawable/ic_face_white_48dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:id="#+id/imgProfile"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
EDIT 2
activity_drawer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Activity Content-->
<FrameLayout
android:id="#+id/activity_frame"
android:layout_height="match_parent"
android:layout_width="match_parent">
</FrameLayout>
<LinearLayout
android:id="#+id/navigationDrawer"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="270dp">
<include
android:id="#+id/drawer_header"
layout="#layout/drawer_header" />
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:groupIndicator="#android:color/transparent"
android:divider="#android:color/transparent"
android:background="#444444"/>
</LinearLayout>
Thank you for your time.
I think you do not need to use LayoutInflater, simply try this and let me know
TextView tvFullName = (TextView)findViewById(R.id.name);
TextView tvEmail = (TextView)findViewById(R.id.email);
ImageView imgProfile = (ImageView)findViewById(R.id.imgProfile);
tvFullName.setText(userInfo[0] + " " + userInfo[1]);
tvEmail.setText(userInfo[2]);
for your clarification it worked because it is already defined in a layout so you can access it directly. LayoutInflater is used in case you want to add some views runtime using defined layout.
You could try calling
drawerHeaderView.invalidate();
after setting the text to refresh the views, since the layout has already been inflated.
I'm probably wrong but it could potentially be an issue with the way you are getting an instance of LayoutInflater. Try
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
Attach the inflated view to a parent view like this:
RelativeLayout userInfoRL = (RelativeLayout)findViewById(R.id.userInfoWrapper);
userInfoRL.addView(drawerHeaderView);
//just in case use the userInfoRL.invalidate();
Try it and let me know if it works.
I am attempting to create shared element transition between two Activities. This appears to work find between two layouts, each containing a single ImageView. However, I am having issues getting this transition to work with a ListView.
The issue is when the user taps on the first row, the transition will work as expected. However, when the second row is tapped, the origin of the transition appears to be the image of the first row as well!
I have created a simple list view with a custom row layout:
row_detailed.xml
<?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"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Daft Punk"
android:id="#+id/primary_textview"
android:layout_gravity="center_horizontal"
android:layout_weight="0.07"
android:layout_alignTop="#+id/primary_imageview"
android:layout_toRightOf="#+id/primary_imageview" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Random Access Memories"
android:id="#+id/textView4"
android:layout_gravity="center_horizontal"
android:layout_weight="0.07"
android:layout_alignBottom="#+id/primary_imageview"
android:layout_toRightOf="#+id/primary_imageview" />
<ImageView
android:layout_width="84dp"
android:layout_height="84dp"
android:id="#+id/primary_imageview"
android:src="#drawable/ram"
android:layout_weight="0.07" />
</RelativeLayout>
The layout for the secondary Activity, activity_transition_secondary.xml:
<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="legiocode.com.tapstrprototype.activity.TransitionSecondaryActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/secondary_imageview"
android:src="#drawable/ram"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
The Primary Activity, TransitionPrimaryActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_primary);
ListView listView = (ListView)findViewById(R.id.listView2);
ArrayAdapter<String> adapter = new ArrayAdapter(this,
R.layout.row_detailed,
R.id.primary_textview,
items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String transitionName = getString(R.string.transition_image) + String.format("_%d", i);
ImageView imageView = (ImageView)findViewById(R.id.primary_imageview);
imageView.setTransitionName(transitionName);
Intent intent = new Intent(TransitionPrimaryActivity.this, TransitionSecondaryActivity.class);
intent.putExtra(TransitionSecondaryActivity.TRANSITION_NAME, transitionName);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(TransitionPrimaryActivity.this,
imageView, // The view which starts the transition
transitionName // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(TransitionPrimaryActivity.this, intent, options.toBundle());
}
});
}
The Secondary Activity, TransitionSecondaryActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_secondary);
String transitionName = getIntent().getStringExtra(TRANSITION_NAME);
ImageView imageView = (ImageView)findViewById(R.id.secondary_imageview);
imageView.setTransitionName(transitionName);
}
I have tried to implement the transition names dynamically, but that doesn't seem to make much of a difference. Am I setting up the transition correctly?
You have to access your actually clicked View (in this case parameter view) in
onItemClick(AdapterView<?> adapterView, View view, int i, long l)
With
ImageView imageView = (ImageView)findViewById(R.id.primary_imageview);
you always access the first element in the list.
PS: Put the transition names inside the xml. Makes it much more easy to read.
I am just starting out android programming, and I ran into this error while trying to set the text in a TextView. I have researched other people's problems, but I have seemed to take care of the trouble issues that were pointed out by their answers.
Here is my 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.everythingmath.MainActivity$PlaceholderFragment" >
<GridLayout
android:id="#+id/problemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:columnCount="5"
android:rowCount="1" >
<TextView
android:id="#+id/firstNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Number"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" + "
android:textSize="30sp" />
<TextView
android:id="#+id/secondNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Number"
android:textSize="20sp" />
<EditText
android:id="#+id/numTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Text Goes Here"
android:textSize="20sp"/>
</GridLayout>
</RelativeLayout>
Here is my pertinent Java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
int firstNumber = createRandomNumber(10);
int secondNumber = createRandomNumber(10);
TextView firstNumText = (TextView)findViewById(R.id.firstNumber);
TextView secondNumText = (TextView)findViewById(R.id.secondNumber);
firstNumText.setText(Integer.toString(firstNumber));
secondNumText.setText(Integer.toString(secondNumber));
}
It is the lines in which I try to set the text that throw the error. The random number generator works fine also.
Localize PlaceholderFragment class inside MainActivity class.
Change public View onCreateView method into
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
int firstNumber = createRandomNumber(10);
int secondNumber = createRandomNumber(10);
TextView firstNumText = (TextView)findViewById(R.id.firstNumber);
TextView secondNumText = (TextView)findViewById(R.id.secondNumber);
firstNumText.setText(Integer.toString(firstNumber));
secondNumText.setText(Integer.toString(secondNumber));
return rootView;
}
You will probably need to move createRandomNumber method to PlaceholderFragment class. And in case you have changed Fragment's name from fragment_main to something else, change it also in this line
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Your current code doesn't work because inside
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[..]
}
you can only find by ID view (findViewById) which was declared inside activity_main layout. And your TextView views are probably declared inside fragment_main layout.
I'm starting to build a simple Android app so when I press my video button it calls a fragment which contains the video player, but I'm always getting: No view found for id 0x7f090005...
I wonder if you give me a hand by figuring out what is wrong in the code that I'm writing.
Here is my MainActivity.java file:
public class MainActivity extends Activity {
private Button mVideoButton;
Fragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mVideoButton = (Button)findViewById(R.id.videoButton);
// Video Button
mVideoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentVideo sf = new FragmentVideo();
ft.add(R.id.fragmentVideo, sf);
ft.commit();
}
});
}
}
Then I have my main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activityStart" >
<ImageView
android:src="#drawable/armstrong_on_moon"
android:contentDescription="#string/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:layout_weight="1" />
<TableRow
android:gravity="center|bottom"
android:layout_weight="0">
<Button
android:id="#+id/videoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/video" />
</TableRow>
Next, my FragmentVideo.java class:
public class FragmentVideo extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState ){
View v = inflater.inflate(R.layout.fragment_video, parent, false);
return v;
}
}
And at last, the fragment_video.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"
android:id="#+id/fragmentVideo" >
<VideoView
android:id="#+id/video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
<ProgressBar
android:id="#+id/prog"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center" />
</LinearLayout>
Please let me know if I'm doing something wrong or if I'm missing something as I can see where the issue lays.
Thanks very much for any help in advance.
EGMWEB
On ft.add() the int parameter refers to the container to which you want to add the Fragment; you are trying to add the Fragment to the Fragment's own layout (Android doesn't find the View because it is not inflated yet, if it did it will throw and Exception in this case; you cannot add a Fragment to its own layout). You want to replace R.id.fragmentVideo with R.id.activityStart to add the Fragment to the current TableLayout that is part of the already inflated main_activity layout
Could you check whats the equivalent view for the id '0x7f090005' in the R.java file. It should be under gen folder of your project. The framework is not able to find this under the resource file that you have in this java class.