This question already has answers here:
findViewByID returns null
(33 answers)
Closed 3 years ago.
I'm trying to change the text of a NavigationView's child. I am using findViewById to get directly to the TextView. It works in the MainActivity, but in every other Activity findViewById returns a null reference.
By the way this is my first submitted question and I hope I respected the guidelines.
I can't seem to figure this out so I don't know where to start.
This is the NavigationView located in the layout of every activity
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:visibility="visible"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
nav_header_main is the component that has the said TextView, it is referenced in the code above. This is nav_header_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_header"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<TextView
android:id="#+id/nav_header_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Angheluta Filip"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#android:color/white" />
</LinearLayout>
This code is from MainActivity, where finding the said text view doesn't return a null reference
NavigationManager navigationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Downloads the database and than proceeds to setup the Activity
//onDataChange gets called when the download is finished and dataSnapshot is the downloaded
//data
final AppCompatActivity context = this;
FirebaseHandler.getFirebaseHandler().getReference().addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
FirebaseHandler.getFirebaseHandler().setData(dataSnapshot);
DataSnapshot userSnapshot = dataSnapshot.child("Users").child(FirebaseHandler.getFirebaseHandler().getAuth().getUid());
User.initializeCurrentUser(userSnapshot);
//Add side bar to this activity
navigationManager = new NavigationManager(context);
navigationManager.createNavBar();
//Recycler view setup
RecyclerManager recyclerManager = new RecyclerManager();
recyclerManager.createRecyclerWithLargeElements(context, Voluntariat.getDataSet());
//Sort spinner setup
SortSpinnerManager sortSpinnerManager = new SortSpinnerManager();
sortSpinnerManager.createSortSpinnerManager(context, recyclerManager);
//Search bar setup
SearchBarManager searchBarManager = new SearchBarManager();
searchBarManager.createSearchBar(context, recyclerManager, Voluntariat.getDataSet());
TextView textViewHeader = findViewById(R.id.nav_header_textView);
Log.d("textHeader", textViewHeader.getText().toString());
textViewHeader.setText(User.currentUser.lastName + " " + User.currentUser.firstName);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
But this is from any other Activity in the onCreate method
RecyclerManager recyclerManager;
NavigationManager navigationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toate_vol);
//Add side bar to this activity
navigationManager = new NavigationManager(this);
navigationManager.createNavBar();
//RECYCLERVIEW SETUP
recyclerManager = new RecyclerManager();
recyclerManager.createRecyclerWithSmallElements(this, Voluntariat.getDataSet());
//Search bar managing
SearchBarManager searchBarManager = new SearchBarManager();
searchBarManager.createSearchBar(this, recyclerManager, Voluntariat.getDataSet());
TextView textViewHeader = findViewById(R.id.nav_header_textView);
Log.d("textHeader", textViewHeader.getText().toString());
textViewHeader.setText(User.currentUser.lastName + " " + User.currentUser.firstName);
}
When I access any activity other than the MainActivity I get NullPointerException on the textViewHeader.
EDIT: I added more code and i changed it so the findViewById is called straight from the Activity file rather than from the NavigationManager file. The error is the same.
EDIT2: Added some more code from activity_toate_vol.xml:
It seems like this layout doesn't have any view with the problematic id, but the id is located in the nav_header_main.xml file which is set in the activity_toate_vol.xml file with the headerLayout attribute:
app:headerLayout="#layout/nav_header_main"
This NavigationView is a direct child of the root layout and the rest of the code is irrelevant, This same navigation view is found in the MainActivity as well.
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:visibility="visible"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgdeconctare"
android:layout_width="27dp"
android:layout_height="27dp"
android:alpha=".6"
android:src="#drawable/ic_logout"
app:srcCompat="#drawable/ic_logout"
tools:srcCompat="#drawable/ic_logout" />
<TextView
android:id="#+id/logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:gravity="center|start"
android:text="Deconectare"
android:textColor="#android:color/black" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
EDIT 3: I came out with a workaround and also identified the problem. Turns out I have to somehow wait for that NavigationView to inflate before I update it's text. Can anyone help me with the proper way to do this? Cause I am not happy with this.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try {
wait(150);
}
catch(Exception e){
}
TextView textViewHeader = findViewById(R.id.nav_header_textView);
Log.d("textHeader", textViewHeader.getText().toString());
textViewHeader.setText(User.currentUser.lastName + " " + User.currentUser.firstName);
}
}, 100);
Welcome! When findViewById returns null, that can mean 2 things.
1) The layout that you pass to 'setContentView' does not have a view with that id...
2) The layout does have that view, but the view itself is not fully inflated yet. To wait until a view is fully inflated, you can use a GlobalLayoutListener
in java:
setContentView(R.layout.activity_other);
final NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
TextView text = navigationView.findViewById(R.id.nav_header_textView);
text.setText("Hello StackOverflow");
navigationView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
and when you switch to kotlin:
setContentView(R.layout.activity_main)
val view = findViewById<NavigationView>(R.id.nav_view)
view?.viewTreeObserver?.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener{
override fun onGlobalLayout() {
val header = findViewById<TextView>(R.id.nav_header_textView)
header.text= "Hallo Main"
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
Related
I am trying to create a simple PopupWindow with a text entry field.
I know there are many questions of this type, but none seem to solve my problem. This is the XML of my popup layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:background="#color/white"
android:gravity="center"
android:padding="15dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dodaj produkt po identyfikatorze"
android:textSize="15sp"
android:gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:textSize="20sp"
android:hint="Identyfikator"
android:minWidth="250dp"
android:id="#+id/insert_edit"
android:gravity="center"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:id="#+id/insert_ok"
android:text="OK"/>
<Button
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:id="#+id/insert_cancel"
android:text="Anuluj"/>
</LinearLayout>
</LinearLayout>
Now, I want to create this layout in my main activity when a FAB is pressed. This is how I do it:
public class ScrollingActivity extends AppCompatActivity {
private ActivityScrollingBinding binding;
private int ACTIVITY_LOAD_DATABASE = 1;
AbstractDatabase database;
RowAdapter rowAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = Room.databaseBuilder(getApplicationContext(), AbstractDatabase.class, "magazyn_db")
.allowMainThreadQueries().build();
binding = ActivityScrollingBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
FloatingActionButton fab = binding.addCode;
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.println(Log.INFO, "VIEWs", "Showing popup");
PopupWindow codePopup;
View popupView = getLayoutInflater().inflate(R.layout.insert_code, binding.getRoot());
codePopup = new PopupWindow(popupView);
codePopup.setFocusable(true);
codePopup.setOutsideTouchable(true);
View cancelButton = codePopup.getContentView().findViewById(R.id.insert_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
codePopup.dismiss();
}
});
View acceptButton = codePopup.getContentView().findViewById(R.id.insert_ok);
acceptButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View editView = codePopup.getContentView().findViewById(R.id.insert_edit);
String id = ((EditText)editView).getText().toString().trim();
insertNewElement(id);
codePopup.dismiss();
}
});
}
});
[...]
For an unknown reason my popup doesn't go dismissed, both when I click outside of it, or press the cancel button (when I tried to print some logs from the handler, they worked). I have tried many solutions, also inflating it with no root, but then 'showAtPosition' doesn't even show it, and no solutions here on stackoverflow seem to help me even though there are many of them.
Is anyone able to point the problem here?
public void dismiss () Disposes of the popup window. This method can be invoked only after showAsDropDown(android.view.View) has been executed. Failing that, calling this method will have no effect.
I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
Here is my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my activity_main.xml
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}
I tried to make ImageView(bul1) disappear when ImageView(Seethrough) is pressed. I get a nullpointer error when i try to run this code. What is wrong with it?
JAVA code
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
final ImageView view1 = (ImageView) findViewById(R.id.bul1);
seethrough1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(view1.getVisibility() == View.VISIBLE)
{
view1.setVisibility(View.INVISIBLE);
}
}
});
}
XML code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="#drawable/gun"
android:clickable="true"
android:id="#+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet"
android:id="#+id/bul1"
/>
</LinearLayout>
You need to reconcile seethrough's onClickListener with its onClick XML attribute. I'd suggest removing this line from the xml:
android:onClick="next"
and placing the code inside your next method (if you have one)
public void next (View v){
some code
}
behind or before your visibility checking if, whichever suits you more:
#Override
public void onClick(View v) {
//place some code here
if(view1.getVisibility() == View.VISIBLE){
view1.setVisibility(View.INVISIBLE);
}
//or here
}
I think their is problem with out xml code, please try writing xml as follows,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="#drawable/gun"
android:clickable="true"
android:id="#+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet"
android:id="#+id/bul1"
/>
</LinearLayout>
If it return NullPointerExeption, I think your ImageView is Null, because
setContentView(R.layout.activity_main);
and activity_main.xml is not like contents of your post, check name of layout and try again.
I found out that I was supposed to declare the imageviews inside the method instead of before.
like this
public void onClick(View v) {
ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
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.
this is my last desperate attempt to fix this problem.
i am using dialogfragment to make my dialog window MyDialog. the window is supposed to show a score I have sent with and the user can then write in their name and confirm. i am then supposed to be able to retrieve their name.
My first problem is that the edittext that are supposed to show the score, doesn't want to show the score i set the edittext to be (it will only show new text if it was in the xml file). I can however still retrieve the correct score(text) afterwards from the same edittext.
The second problem is that the text that the user writes in can't be retrieved since the edittext isn't updating. However the text that was in the field from the xml file can be retrieved.
It seems like the edittext doesn't want to refresh after they are made in the xml file.
the dialog class (imports taken out):
public class MyDialog extends DialogFragment
{
private EditText scoreEdit;
private EditText userEdit;
private DialogClickListener callback;
public interface DialogClickListener
{
public void onFinishedClick();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
callback = (DialogClickListener) getActivity();
}catch(ClassCastException e)
{
throw new ClassCastException("called class must implement interface");
}
}
public static MyDialog newInstance(int title)
{
MyDialog frag = new MyDialog();
Bundle args = new Bundle();
args.putInt("tittel",title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View inflatedView = inflater.inflate(R.layout.dig_lay, null);
scoreEdit = (EditText) inflatedView.findViewById(R.id.dialogScoreEdit);
userEdit = (EditText) inflatedView.findViewById(R.id.dialogUsernameEdit);
scoreEdit.setText("score supposed to change");
// scoreEdit (edittext) is not changing
dialog.setView(inflater.inflate(R.layout.dig_lay, null));
dialog.setTitle("Title");
dialog.setPositiveButton("reg1", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int whichButton)
{
System.out.println("new text supposed to be: " + userEdit.getText().toString());
//old text from xml shows
callback.onFinishedClick();
}
});
setCancelable(false);
return dialog.create();
}
}
the XML file for the dialog window:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialogLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp">
<TextView
android:text="score"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/dialogScoreEdit"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="score here"
android:inputType="none"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif">
<TextView
android:text="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/dialogUsernameEdit"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="retrive this"
android:inputType="none"/>
</LinearLayout>
</LinearLayout>
picture of the dialogwindow: http://i.stack.imgur.com/EvwSe.png
I have tried calling onResume() and putting the new score in. but the edittext still doesn't change.
I appreciate any answer and sorry for any bad English :)
It looks like there are a few issues there. The ones I have noticed are that when you set your view you are reinflating the view, versus setting it to the view you have already set up when you do
dialog.setView(inflater.inflate(R.layout.dig_lay, null));
you want to set that instead to
dialog.setView(inflatedView);
Also, since we can't see the actual data you are putting into the EditText, we can't see if that might be null, etc.
Finally, I never see you run a scoreEdit.getText(), so you wouldn't be retrieving any changes the user made to the edittext.