I have an activity file which uses a ViewPager to show different pages - each which are fragments with a video. I want some of the pages to have buttons which when I press turn into a pop up to display some information - is this possible?
I've tried this code below:
Here is the code in my fragment java:
public class MassageandBeauty extends Fragment implements View.OnClickListener {
Button btnPopup;
showPopup showPopup;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_fragment, container, false);
btnPopup = (Button)v.findViewById(R.id.cavern);
btnPopup.setOnClickListener(this);
return v;
}
//#Override
public void onViewCreated(View v) {
btnPopup = (Button)v.findViewById(R.id.cavern);
btnPopup.setOnClickListener(this);
}
//#Override
#Override
public void onClick(View parent) {
new showPopup(getActivity().getApplicationContext()).cavern(parent);
}
Here is the code in my popup:
public class showPopup extends ActionBarActivity {
Context ctx;
public showPopup(Context ctx){
this.ctx = ctx;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_layout);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Black.ttf");
TextView myTextView = (TextView)findViewById(R.id.textViewtop);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myTextView.setTypeface(myTypeface);
}
public void cavern(View parent) {
final PopupWindow popup = new PopupWindow(ctx);
ViewGroup.LayoutParams para = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tvMessage = new TextView(ctx);
Button btnDismiss = new Button(ctx);
tvMessage.setLayoutParams(para);
btnDismiss.findViewById(R.id.close);
btnDismiss.setLayoutParams(para);
btnDismiss.setWidth(20);
btnDismiss.setHeight(10);
btnDismiss.setText("x");
popup.setContentView(btnDismiss);
popup.setWidth(1200);
popup.setHeight(800);
popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);
popup.update();
btnDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_popup_layout, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}
}
Here is the xml of my PopUp
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/showpopup"
android:layout_height="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="#8000"
android:orientation="vertical"
tools:context="morzineappy.morzineappy.showPopup">
<TextView
android:id="#+id/textViewtop"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="#FFF"
android:textSize="30sp"
android:text="The Cavern" />
<Button
android:id="#+id/close"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="1200dp"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x" />
<ImageView
android:id="#+id/poster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cavern"
android:layout_marginTop="20dp"
android:layout_marginLeft="800dp"
android:layout_marginRight="20dp"
android:layout_gravity="right" />
<TextView
android:id="#+id/textViewtop"
android:layout_width="800dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="20dp"
style="#style/BusinessFont"
android:text="Text"
/>
For some reason it won't display the text in the popup- I want to add the layout into the popup but I'm not sure how- any ideas?!
First create your popup or message dialog with extending DialogFragment, then on button click:
MyDialogFragment dialog = new MyDialogFragment(....);
dialog.show(getActivity().getSupportFragmentManager(), "name of fragment here");
Your question is not very clear, but I'll assume that you're trying to use something like an AlertDialog. Typically when creating these, you need a Context object as an argument. In an Activity, you can usually create an AlertDialog like so:
AlertDialog d = new AlertDialog(this);
But I'm guessing that your issue is that since you are in a fragment, the this object is not a Context. So for you, it would be more like so:
AlertDialog d = new AlertDialog(getActivity());
Related
Currently , I am programatically creating a popupmenu which displays a list of floors and a title. However, changing the background color of just the title and adding a close button to title is turning out to be a nightmare.
I want to replace this popupmenu with a list popup window so I can add an XML file with background attribute for the title with a black color as the background and a close button on the right and white background for items in the menu. Is there a way I can achieve this with list popup window? Here's my code for that:
private void floorMenu(ImageView btnFloorMenu){
MapData data = new MapDao(MyPlugin.mapId);
final List<Floor> flList = dao.getFloors();
// set popupMenu
final PopupMenu floorsPm = new PopupMenu(MapViewActivity.this,btnFloorMenu);
MenuItem titleItem = floorsPm.getMenu().add(Menu.NONE, Menu.NONE, Menu.NONE, "Floors");
int i = 1;
for(Floor fl : flList)
{
floorsPm.getMenu().add(Menu.NONE, i,i, fl.getName());
if(i>3)
break;
i++;
}
// add popup listener
floorsPm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
// onClick
#Override
public boolean onMenuItemClick(MenuItem item){
// get floorname
int flOrder = item.getOrder();
if(flOrder == Menu.NONE )
return true;
flOrder--;
final String floorId = flList.get(flOrder).getMapId();
// set camera to floor
runOnUiThread(new Runnable() {
#Override
public void run() {
floorsPm.dismiss();
mapFragment.getMapManager().setCameraLayer(floorId, false);
Log.d(TAG, "post cameraLayer set");
changedSteps = true;
pauseNav();
}
});
return true;
}
});
floorsPm.show();
}
Here is my example to create show a ListPopupWindow
First, create layout item_list_popup_window for each item of ListPopupWindow
<?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="wrap_content"
android:background="#e4e4e4"
android:paddingTop="1dp"
android:orientation="horizontal">
<TextView
android:id="#+id/text_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="#+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
</LinearLayout>
Second, create an Adapter for your ListPopupWindow like
public class ListPopupWindowAdapter extends BaseAdapter{
private Activity mActivity;
private List<String> mDataSource = new ArrayList<>();
private LayoutInflater layoutInflater;
private OnClickDeleteButtonListener clickDeleteButtonListener;
ListPopupWindowAdapter(Activity activity, List<String> dataSource, #NonNull OnClickDeleteButtonListener clickDeleteButtonListener){
this.mActivity = activity;
this.mDataSource = dataSource;
layoutInflater = mActivity.getLayoutInflater();
this.clickDeleteButtonListener = clickDeleteButtonListener;
}
#Override
public int getCount() {
return mDataSource.size();
}
#Override
public String getItem(int position) {
return mDataSource.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.item_list_popup_window, null);
holder.tvTitle = (TextView) convertView.findViewById(R.id.text_title);
holder.btnDelete = (Button) convertView.findViewById(R.id.button_delete);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
// bind data
holder.tvTitle.setText(getItem(position));
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickDeleteButtonListener.onClickDeleteButton(position);
}
});
return convertView;
}
public class ViewHolder{
private TextView tvTitle;
private Button btnDelete;
}
// interface to return callback to activity
public interface OnClickDeleteButtonListener{
void onClickDeleteButton(int position);
}
}
Third, You create a function for create and show ListPopupWindow
private void showListPopupWindow(View anchorView) {
final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
listPopupWindow.setWidth(600);
List<String> sampleData = new ArrayList<>();
sampleData.add("A");
sampleData.add("B");
sampleData.add("CCCCCCCCCCCCCC");
sampleData.add("D");
sampleData.add("EEEEEEEEE");
listPopupWindow.setAnchorView(anchorView);
ListPopupWindowAdapter listPopupWindowAdapter = new ListPopupWindowAdapter(this, sampleData, new ListPopupWindowAdapter.OnClickDeleteButtonListener() {
#Override
public void onClickDeleteButton(int position) {
Toast.makeText(MainActivity.this, "Click delete " + position, Toast.LENGTH_SHORT).show();
listPopupWindow.dismiss();
}
});
listPopupWindow.setAdapter(listPopupWindowAdapter);
listPopupWindow.show();
}
Finally, you can show the ListPopupWindow by
showListPopupWindow(v);
for example, if you want to show it when click button
anyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showListPopupWindow(v);
}
});
Full Demo is here
Please Try this code, Maybe you wont like this
private void floorMenu(ImageView btnFloorMenu){
final Dialog customDialog = new Dialog(this);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.item_dialog_coustom_design);
TextView clickItem = (TextView)customDialog.findViewById(R.id.item_click);
TextView clickItem1 = (TextView)customDialog.findViewById(R.id.item_click1);
TextView clickItem2 = (TextView)customDialog.findViewById(R.id.item_click2);
Button btnClose = (Button)customDialog.findViewById(R.id.btn_close);
clickItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
// wright your Button Action
}
});
clickItem1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
// wright your Button Action
}
});
clickItem2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
// wright your Button Action
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
customDialog.show();
}
Create Linearlayout layout_width="280dp" layout_height="wrap_content"
android:orientation="vertical" file name item_dialog_coustom_design.xml then put this code
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Title"
android:background="#000"
android:textColor="#fff"
android:padding="12dp"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="#fff"
android:padding="10dp"
android:text="Your Item"
android:id="#+id/item_click"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="#fff"
android:padding="10dp"
android:text="Your Item"
android:id="#+id/item_click1"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="#fff"
android:padding="10dp"
android:text="Your Item"
android:id="#+id/item_click2"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="#fff"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_close"
android:text="Close"/>
</LinearLayout>
I'm using dialog fragment and everything is fine until I'm trying to remove title bar. There is a code:
Dialog XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f0f0f0">
<GridView
android:id="#+id/gv_registration_avatars"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="#dimen/avatar_layout_margin"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:focusable="true"
android:clickable="true"
/>
<Button
android:id="#+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/avatar_layout_margin"
android:layout_marginRight="#dimen/avatar_layout_margin"
android:layout_below="#id/gv_registration_avatars"
android:text="Ok"
android:layout_gravity="right"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar_avatars"
android:layout_centerInParent="true"
android:visibility="gone"/>
DialogFragment class is :
public class AvatarPickDialogFragment extends DialogFragment {
private GridView mGridView;
private ProgressBar mProgressBar;
private GridViewAvatarAdapter mGridAdapter;
private ArrayList<String> mGridData;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.avatar_pick_dialog_fragment, container, false);
mGridView = (GridView) rootView.findViewById(R.id.gv_registration_avatars);
mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressBar_avatars);
mGridData = new ArrayList<>();
mGridAdapter = new GridViewAvatarAdapter(getActivity(), R.layout.grid_item_avatar_layout, mGridData);
mGridView.setAdapter(mGridAdapter);
mProgressBar.setVisibility(View.VISIBLE);
String item;
for (int i = 0; i < 10; i++) {
item = new String();
item= new String("string");
mGridData.add(item);
}
mProgressBar.setVisibility(View.GONE);
mGridAdapter.setGridData(mGridData);
Button dismiss = (Button) rootView.findViewById(R.id.dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
return rootView;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
When I am using dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) title bar disappear, but my dialog window width changes to very short and I don't know why, because with title bar it is fine. Any suggestions?
You can try setting fragment width in fragment's onStart() method
#Override
public void onStart()
{
super.onStart();
if (getDialog() == null)
return;
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
Hi here you can see my code.
My probleme is : with this code my EditText and my Button field are null so i can't do anithing with them.
Plz can you help me to access to this button.
For the activity it's autogenerated by eclipse.
public class MainActivity extends ActionBarActivity {
private EditText commentaire;
private Button addReleve;
#Override
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();
}
this.commentaire = (EditText) findViewById(R.id.commentaire);
this.addReleve = (Button) findViewById(R.id.addReleve);
addReleve.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast toast = Toast.makeText(getApplicationContext(), "LOL", Toast.LENGTH_LONG);
toast.show();
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void resetInterface(){
this.commentaire.setText("");
this.addReleve.setEnabled(true);
}
}
XML Layout (fragment_main.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: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.relevegps.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Relevé GPS"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/addReleve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="92dp"
android:text="Ajouter le relevé" />
<EditText
android:id="#+id/commentaire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/commentaire"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:text="Commentaire" />
</RelativeLayout>
try this also.
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
EditText commentaire = (EditText) rootView.findViewById(R.id.commentaire);
Button addReleve = (Button) rootView.findViewById(R.id.addReleve);
commentaire.setText("");
addReleve.setEnabled(true);
return rootView;
}
// Also this goes in this class
public void resetInterface(){
this.commentaire.setText("");
this.addReleve.setEnabled(true);
}
}
Your problem is that those two views are in your Framgent's xml and you can only find those in that fragment. So just move the commentaire and addReleve specific stuff to your fragment class.
public static class PlaceholderFragment extends Fragment {
// Variables moved from MainActivity to here
private EditText commentaire;
private Button addReleve;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
// To get views in fragment you need the base view (=rootView)
// Example
this.commentaire = (EditText) rootView.findViewById(R.id.commentaire);
// Put the code in here !
// ...
return rootView;
}
// Also this goes in this class
public void resetInterface(){
this.commentaire.setText("");
this.addReleve.setEnabled(true);
}
}
I'm new on Android and I'm trying to do my first app.
I've read a lot of posts here about the same problem I got, but couldn't figure it out.
I hope you can help me.
My app has several Activities. In this case in particular, I'm sending a string which I want to put in a textview. When I tried to do this, the app crashed. So, I read a lot of posts and "catched" the null pointer. But that meant my setContentView is not working.
I'm working with ADT downloaded on 2014-06-21.
Tried to clean Eclipse, but it remains the same.
Tried to change the name of tvGrupo (before, it was textView1).
Also, I'm not very good at english, as you can tell and more important, since I'm new in Android I'm not use to all the words used commonly to describe process... :(
So, please be very specific.
Thanks for all the help!
This is my code:
public class DisplayGrup extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_grup);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
TextView tvgrupo = (TextView) findViewById(R.id.tvGrupo);
if (tvgrupo == null) {
// this is how i know i received a null pointer.
Toast.makeText(getApplicationContext(), "Puntero nulo", Toast.LENGTH_SHORT).show();
} else {
tvgrupo.setText("hola");
Toast.makeText(getApplicationContext(), "Obteniendo dato:" + tvgrupo.toString(), Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_grup, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_grup,
container, false);
return rootView;
}
}
public void clickBorrar(View view) {
// This works just fine.
TextView tv = (TextView) findViewById(R.id.tvGrupo);
tv.setText("hhhh");
}
}
This is my 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:gravity="top"
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.mula_saltadora.DisplayGrup$PlaceholderFragment" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="clickBorrar"
android:text="Borrar" />
<TextView
android:id="#+id/tvGrupo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/button1"
android:text="Grupo"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignParentRight="true"
android:text="Agregar" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvGrupo"
android:layout_below="#+id/tvGrupo"
android:layout_above="#+id/editText1"
>
</ListView>
</RelativeLayout>
Your textview is in fragment layout so put your code in onCreateView instead in onCreate:
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_display_grup,
container, false);
TextView tvgrupo = (TextView) rootView.findViewById(R.id.tvGrupo);
if (tvgrupo == null) {
// this is how i know i received a null pointer.
Toast.makeText(getActivity(), "Puntero nulo",
Toast.LENGTH_SHORT).show();
} else {
tvgrupo.setText("hola");
Toast.makeText(getActivity(), "Obteniendo dato:" +
tvgrupo.toString(), Toast.LENGTH_SHORT).show();
}
return rootView;
}
public void clickBorrar(View view) {
// This works just fine.
TextView tv = (TextView) rootView.findViewById(R.id.tvGrupo);
tv.setText("hhhh");
}
Edit:
Change getApplicationContext() to getActivity() and make rootView global variable in fragment and use it in function clickBorrar too. See edited code.
Make your rootView accessible to all your fragment, and use it to call findViewById. Its something like this:
public static class PlaceholderFragment extends Fragment {
View rootView;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_display_grup,
container, false);
return rootView;
}
}
public void clickBorrar(View view) {
// This works just fine.
TextView tv = (TextView) rootView.findViewById(R.id.tvGrupo);
tv.setText("hhhh");
}
}
In your case, In your activity cannot have textview, so you cannot call findViewById. so you must call rootView caused Fragment.
e.g :
in activity findViewByID.......
in fragment rootView......
or outside rootView if you using fragment you can using getView().
Hi guys I have a problem with the cool lib from nhaarman
I integrated successfully his lib with an activity extends ListActivity
public class MainActivity extends ListActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setDivider(null);
MyAdapter adapter = new MyAdapter();
//setListAdapter(adapter);
ScaleInAnimationAdapter swingRightInAnimationAdapter = new ScaleInAnimationAdapter(adapter);
swingRightInAnimationAdapter.setAbsListView(getListView());
setListAdapter(swingRightInAnimationAdapter);
for(int i = 0 ; i<10; i++)
{
adapter.add("task" + String.valueOf(i));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyAdapter extends ArrayAdapter<String>
{
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView) convertView;
if (tv == null) {
tv = new TextView(MainActivity.this);
}
tv.setText(getItem(position));
return tv;
}
}
}
This code above is working fine. The list_rows are fading in the screen.
But now I want to integrate this lib into a fragment with a listview within:
public class FragmentTask extends Fragment{
ListView listView;
MyAdapter adapter;
View view;
public FragmentTask() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(new Exception().getStackTrace()[0].getClassName(), new Exception().getStackTrace()[0].getMethodName());
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_tasks, container, false);
Button button = (Button) view.findViewById(R.id.btn_newTask);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
adapter.add("task");
//adapter.notifyDataSetChanged();
}
});
listView = (ListView) view.findViewById(R.id.taskListView);
listView.setDivider(null);
adapter = new MyAdapter(view.getContext());
ScaleInAnimationAdapter swingRightInAnimationAdapter = new ScaleInAnimationAdapter(adapter);
swingRightInAnimationAdapter.setAbsListView(listView);
listView.setAdapter(swingRightInAnimationAdapter);
return view;
}
private class MyAdapter extends ArrayAdapter<String>
{
Context context;
private MyAdapter(Context context) {
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_row,parent, false);
TextView title = (TextView) row.findViewById(R.id.textView);
title.setText(getItem(position));
return row;
}
}
Here is the xml fragment_tasks:
<?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:layout_width="match_parent" android:layout_height="match_parent"
android:background="#ffc7c7c7">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Task"
android:id="#+id/btn_newTask"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/taskListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btn_newTask"
/>
</RelativeLayout>
Now I have the effect that the rows only fade in if i "spam" new rows so that I have to scroll down. So the list items which were hided appear now with the fade-effect. But the other list items which have room in the listview without scrolling are not fading.
Do you understand me?
Please help me :-)
I had the same problem, but the problem is not the fragment. The height of list_view must be match_parent.
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/taskListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btn_newTask"
/>
description of the problem
Hope that helps