In My Activity i have a Edit Text and Button When the user enter the button " Add " the checklist want to create Automatically with the name of the user entered in the edit text
how to create a checkbox in java program based on the user input
when i click the Add Button the list was not updated ( the userinput was not converted into checkbox and it doesnot display the name in bottom )
Here is My XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name to Add in the list"
android:id="#+id/ed_name"
android:layout_marginTop="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/btn_add"
android:layout_below="#+id/ed_name"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
/>
</RelativeLayout>
here is My java code
public class MainActivity extends AppCompatActivity {
EditText uinput;
Button btnadd;
CheckBox cbname;
ScrollView sv;
RelativeLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv = new ScrollView(this);
ll = new RelativeLayout(this);
// ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
uinput = findViewById(R.id.ed_name);
btnadd = findViewById(R.id.btn_add);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String Name = uinput.getText().toString();
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(Name);
ll.addView(cb);
}
});
}
I suggest you to add everything that is static in the activity_main.xml layout file as :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity2">
<EditText
android:id="#+id/ed_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Enter name to add in the list"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ed_name" />
<ScrollView
android:id="#+id/scrollview"
android:layout_width="409dp"
android:layout_height="612dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_add">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Now, Checkbox are the one which we will generate programmatically and add them to the LinearLayout which is the only child of ScrollView(it supports single child only). A sample code for MainActivity doing that is here :
public class MainActivity extends AppCompatActivity {
// declare the required views variable
private EditText uInput;
private LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// initializes required view variable
uInput = findViewById(R.id.ed_name);
linearLayout = findViewById(R.id.linear_layout);
Button btnAdd = findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = uInput.getText().toString();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
linearLayout.addView(checkBox);
} else
Toast.makeText(MainActivity.this, "The name cannot be empty!", Toast.LENGTH_LONG).show();
}
});
}
}
It also checks whether or not the text in EditText is empty and if it is generates a suitable Toast message to not permit the user create a empty Checkbox. Hope, this helps!
You didn't add the ScrollView to your layout, So the R.layout.activity_main know nothing about your added views.
So, inflate your root ViewGroup of your layout, add the ScrollView to it and set constraints/attributes according to the type of your root ViewGroup.
Add an id to the root layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/root"
tools:context=".MainActivity">
Replace the ScrollView inner RelativeLayout with a LinearLayout and add the ScrollView with an attribute param RelativeLayout.BELOW to be at the bottom of the btn_add
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
EditText uinput = findViewById(R.id.ed_name);
Button btnadd = findViewById(R.id.btn_add);
RelativeLayout rootLayout = findViewById(R.id.root);
// Set constraints/attributes to the sv
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, btnadd.getId());
rootLayout.addView(sv, params);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String Name = uinput.getText().toString();
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(Name);
ll.addView(cb);
}
});
This code will add a checkbox in your view
since you don't have any provided code, ill give you an idea to come up on your own strategies or style
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
Button b = new Button(this);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(myTextBoxText.getText());
ll.addView(cb);
}
});
Related
I am trying to set the text of a Check Box from an Edit Text but it's not working.
I've created an Edit Text with a Button that when clicked creates a Check Box and gets the text for the Check Box from the Edit Text. The app crahes when I click on the Button.
XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<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:id="#+id/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/new_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="New Task"
android:inputType="textCapSentences" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Create" />
</LinearLayout>
</ScrollView>
JAVA
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout linearLayout = findViewById(R.id.rootContainer);
EditText newTask = findViewById(R.id.text);
CheckBox checkBox = new CheckBox(MainActivity.this);
String task = newTask.getText().toString();
newTask.setText(task);
checkBox.setText(task);
// Add Checkbox to LinearLayout
if (linearLayout != null) {
linearLayout.addView(checkBox);
}
}
});
}
id of EditText in your layout is android:id="#+id/new_task"
whereas in Activity you using
EditText newTask = findViewById(R.id.text);
So you should use same id in Activity as in xml
Remove below line from onClick event
LinearLayout linearLayout = findViewById(R.id.rootContainer);
and add that line in onCreate() method.
Because linearLayout is your root container.
you don't need to find every time.
And same for the button.
Also there is issue related id.
You are finding text id but you mentation new_task in xml.
So below line should be EditText newTask = findViewById(R.id.new_task);
I'm trying to create a 'favourites' view dynamically in android by recording whether the user has favoured a specific sound bite in my app and then inserting the sub view into a parent view with the appropriate info about a particular soundbite. Essentially, creating a custom playlist of soundbites.
I'm having problems with the layoutInflator though. If I add more than one sub view into my parent view, only the last sub view added will have the appropriate parameters entered, whereas the rest have the default values from the sub view xml layout.
Here is my java code displaying the general idea behind adding subviews:
LayoutInflater l = getLayoutInflater();
LinearLayout ll = (LinearLayout)findViewById(R.id.favlist);
if(getIntent().getExtras()!=null)
{
Bundle extras = getIntent().getExtras();
Intent test = getIntent();
if(test.hasExtra("favThunder")){
String favValues = extras.getString("favThunder");
View v = l.inflate(R.layout.sublayout, null);
ll.addView(v);
ImageView favImage = (ImageView)findViewById(R.id.favimage);
favImage.setImageResource(R.drawable.thundercats);
TextView tv = (TextView) findViewById(R.id.favtitle);
tv.setText(R.string.thundercats);
TextView tvClicks = (TextView) findViewById(R.id.timesplayed);
tvClicks.setText("Times played: " + favValues);
favImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.stop();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.thunder);
mp.start();
}
});
}
if(test.hasExtra("favSkeletor")){
String favValues = extras.getString("favSkeletor");
View v = l.inflate(R.layout.sublayout, null);
ll.addView(v);
ImageView favImage = (ImageView)findViewById(R.id.favimage);
favImage.setImageResource(R.drawable.skeletor);
TextView tv = (TextView) findViewById(R.id.favtitle);
tv.setText(R.string.skeletor);
TextView tvClicks = (TextView) findViewById(R.id.timesplayed);
tvClicks.setText("Times played: "+favValues);
favImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.stop();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.skeletor);
mp.start();
}
});
}
And here is my sub view and parent view xml respectively
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/favimage"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="200dp"
android:src="#drawable/thundercats"
android:scaleType="fitCenter"/>
<LinearLayout
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/favtitle"
android:layout_weight="0.4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A TEST TITLE"/>
<TextView
android:layout_weight="0.3"
android:id="#+id/timesplayed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Times played: Heck!"/>
</LinearLayout>
</LinearLayout>
Parent xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context="com.example.markohare.ohareb00716779_cw1.MainActivity"
>
<LinearLayout
android:id="#+id/favlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
</LinearLayout>
</ScrollView>
What am I doing wrong? Is my subview too complex or is there something wrong in my java code?
Thanks
I want to display underneath my AutoCompleteTextView what the user entered when they press the button beside it. So basically adding a textview in response to button clicks. I have "hello world" displaying where i want them to be right now, but am stuck on how to do this inside a button listener. Any help is appreciated thank you.
Here is an image example of what I am trying to accomplish exactly.
sample display
my onCreate so far:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setText("hello world");
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
//LayoutParams is a class to specify layout properties when adding a view to a layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, layoutParams);
}
}
XML for layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:orientation="vertical"
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="com.example.tester.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:text="AutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/autoCompleteTextView"
android:layout_weight="1" />
<Button
android:text="PRESS ME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Try this
Button button1 = (Button) findViewById(R.id.button2);
AutocompleteTextView autoTv = (autocompleteTextView) findViewById(R.id.autocompleteTextView);
button1.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String enteredText = autoTv.getText().toString();
if (!enteredText.isEmpty()) {
TextView textView = new TextView(this);
textView.setText(enteredText);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
//LayoutParams is a class to specify layout properties when adding a view to a layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, layoutParams);
}
}
});
So I have an app with a listview. When you click a listview item it opens a new activity. I placed a checkbox in this new activity where when its checked, it should put a checkmark next to the listview item on the previous screen. I'm running into this error I'm guessing because I'm trying to set the checkbox from the second activity that has a different content view than the listview. I hope I explained that well.
Heres my RouteDetails.java with the checkbox code
public class RouteDetails extends AppCompatActivity {
ImageView routeImage;
String routeName;
CheckBox routeCheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//back button for route details view
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
///////checkbox///////////////////////////////////////////
routeCheckBox = (CheckBox) findViewById(R.id.routeCheckBox);
final ImageView checkImageView = (ImageView) findViewById(R.id.checkImageView);
routeCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
if (routeCheckBox.isChecked())
{
checkImageView.setImageResource(R.drawable.birdsboroicon);
}
}
});
/////////////////////////////////////////////
//sets actionbar title
routeName = getIntent().getExtras().getString("routeName");
getSupportActionBar().setTitle(routeName);
//TextView for route details
final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
routeDetailsView.setText(getIntent().getExtras().getCharSequence("route"));
//ImageView for route details
routeImage = (ImageView) findViewById(R.id.routeImage);
final int mImageResource = getIntent().getIntExtra("imageResourceId", 0);
routeImage.setImageResource(mImageResource);
and heres the custom_row.xml that contains the imageview im trying to set based on the checkbox state.
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="51dp"
android:id="#+id/routeText"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center_vertical"
android:textColor="#android:color/black"
android:typeface="normal"
/>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/checkImageView" />
So I want the checkmark to be next to the listview item after the back button is pressed.
Ill include this other code if it is relavent
heres my RouteDetails.xml that contains the checkbox
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_route_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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.zach.listview.RouteDetails">
<CheckBox
android:text="Route Climbed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/routeCheckBox"
android:gravity="center" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/routeDetailsView"
android:textSize="18sp"
android:textAlignment="center"
android:textColor="#android:color/black"
android:layout_below="#id/routeCheckBox"/>
<ImageView
android:layout_below="#id/routeDetailsView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/routeImage"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true" />
</RelativeLayout>
</ScrollView>
My Custom adapter.java which creates each listview row
class CustomAdapter extends ArrayAdapter<CharSequence>{
public CustomAdapter(Context context, CharSequence[] routes) {
super(context, R.layout.custom_row ,routes);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater routeInflater = LayoutInflater.from(getContext());
View customView = convertView;
if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}
CharSequence singleRoute = getItem(position);
TextView routeText = (TextView) customView.findViewById(R.id.routeText);
routeText.setText(singleRoute);
return customView;
}
my mainavtivity.java which is where the listview is populated
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Action Bar customization
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.actionbar);
setContentView(R.layout.activity_main);
///// fill listview numbers I want to add
final String[] routeListviewNumbers = getResources().getStringArray(R.array.routeNumbers);
//fill list view with xml array of routes
final CharSequence[] routeListViewItems = getResources().getTextArray(R.array.routeList);
//fills route detail text view with xml array info
final CharSequence[] routeDetail= getResources().getTextArray(R.array.routeDetail);
//fills route detail image view with xml array of images
final TypedArray image = getResources().obtainTypedArray(R.array.routeImages);
//image.recycle();
//custom adapter for list view
ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems);
final ListView routeListView = (ListView) findViewById(R.id.routeListView);
routeListView.setAdapter(routeAdapter);
routeListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CharSequence route = routeListViewItems[position];
int imageId = (int) image.getResourceId(position, -1);
if (route.equals(routeListViewItems[position]))
{
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[position]);
intent.putExtra("imageResourceId", imageId);
intent.putExtra("routeName", routeListViewItems[position]);
startActivity(intent);
}
}
}
);
}
}
and my activitymain.xml which is the listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="0dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="0dp"
tools:context="com.example.zach.listview.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/routeListView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Edit: So I added this to my RouteDetails.java
routeCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
if (routeCheckBox.isChecked())
{
//checkImageView.setImageResource(R.drawable.checkmark);
Intent check = new Intent(RouteDetails.this,CustomAdapter.class);
check.putExtra("checkImageResource", R.drawable.checkmark);
startActivity(check);
}
}
});
and this to my customAdapter.java
////////trying to set checkmark/////
ImageView checkImageView = (ImageView) customView.findViewById(R.id.checkImageView);
checkImageView.setImageResource(((Activity) getContext()).getIntent().getIntExtra("checkImageResource",0));
////////////////////////////////////////
but this is not working either. It says fatal exception unable to find explicit activity class. It's doing this I guess since CustomAdapter is not an activity, but customadapter is linked to my custom_row which contains the imageview I want to add the checkbox to. How would I do this? I'm not sure what else to try
Edit: I still haven't found a solution if anyone has any suggestions!
I want to change images in my popupwindow. I am trying to change ImageView image source which is in test.xml while my contentView is activity_main. When I try to just call imageView.setImageResource(android.R.drawable.ic_menu_help); it gives me nullpointerexception I realized i have to use inflate method, but when I use it it doesn't do what I want.I know there is a lot of questions regarding this, but none of them really helped me.
This is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.popupwindow.MainActivity">
<Button
android:text="Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:id="#+id/test1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear1">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#android:drawable/btn_star_big_on"
android:id="#+id/imageView1"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
my main.java:
public class MainActivity extends AppCompatActivity {
private PopupWindow popupWindow;
private RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test = (Button) findViewById(R.id.button);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
final View view1 = getLayoutInflater().inflate(R.layout.test, null);
ImageView imageView = (ImageView) view1.findViewById(R.id.imageView1);
imageView.setImageResource(android.R.drawable.ic_menu_help); //Image source is not changed in my popupWindow,
// but it is changed when I call relativeLayout.addView(view1);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//relativeLayout.addView(view1); // <-- this does the trick, but its not what i want
View container = getLayoutInflater().inflate(R.layout.test, null);
popupWindow = new PopupWindow(container, android.app.ActionBar.LayoutParams.MATCH_PARENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT, true);
popupWindow.showAtLocation(relativeLayout, Gravity.BOTTOM, 0, 0);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popupWindow.dismiss();
return true;
}
});
}
});
}
}
You're inflating two separate views. Obviously what you do in one will have no impact on the other. You should set the popup view to your originally inflated view:
popupWindow = new PopupWindow(view1,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT,
true);