I have an app that shows a listView inside a fragment ok
now I need to animate the list view out
but the animation
animate()
is not working
here the code,
public class PhoneMainView extends SherlockFragment{
ListView listView ;
TranslateAnimation mAnimation;
//testeo
private Button btnNewEmpresa;
RelativeLayout lLayoutFrgValidate;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
Log.d("mensa", "ONCREATE");
lLayoutFrgValidate=(RelativeLayout) inflater.inflate(R.layout.activity_main, container, false);
btnNewEmpresa=(Button) lLayoutFrgValidate.findViewById(R.id.button_anima);
btnNewEmpresa.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("mensa", "sikas");
animate();
}
});
return lLayoutFrgValidate;
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Log.d("mensa", "onActivityCreated");
listView = (ListView) getView().findViewById(R.id.listViewCats);
String[] values = new String[] { "C0","C1","C2","C3", "C4", "C5", "C6","C7"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
}
public void animate() {
mAnimation = new TranslateAnimation(0, 0, 0, 599);
mAnimation.setDuration(10000);
mAnimation.setFillAfter(true);
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.REVERSE);
listView.setAnimation(mAnimation);
}
}
You need to start the animation.
listView.startAnimation(mAnimation); // Use this
Related
I am new to Java/android develop and trying to follow instructions from android site but cannot find clear answer to implementing onclicklistener in fragment. Please help me why its keep crashing in runtime when I click.
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
try this
use this
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
instead of this
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
change your code as below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
return v;
}
Try this
public class TestFragment extends Fragment {
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
Move your code from onCreate() method to onCreateView() method and change the following code
From
(TextView)getActivity().findViewById(R.id.text_show);
to
(TextView)v.findViewById(R.id.text_show);
Basically the flow is like the following: when a fragment is created (or when the user swipes to refresh the layout), an AsyncTask is executed. The ASyncTask retrieves the info from a URL and setup an adapter which is then assigned to a ListView.
This is the code of the Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tasks_page, container, false);
mSwipeRefreshLayout = (SwipeRefreshLayout)
rootView.findViewById(R.id.list_container);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light, android.R.color.holo_red_light);
lv = (ListView) rootView.findViewById(android.R.id.list);
getTasks();
return rootView;
}
private void getTasks() {
new TasksRetriever(mSwipeRefreshLayout,tasksAdapter,lv).execute();
}
#Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(true);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getTasks();
}
}, 1000);
}
The TaskRetriever constructor is:
public TasksRetriever(SwipeRefreshLayout srl, TasksAdapter ta, ListView lv) {
adpt = ta;
this.srl = srl;
this.lv = lv;
}
and postExecute is:
#Override
protected void onPostExecute(List<TasksItem> result) {
super.onPostExecute(result);
dialog.dismiss();
if (adpt == null) adpt = new TasksAdapter(result);
else adpt.setItemList(result);
lv.setAdapter(adpt);
adpt.notifyDataSetChanged();
srl.setRefreshing(false);
}
Not sure yet if it works but I was wondering if I'm on the right track because it doesn't look clean to me. On the other hand, I can't create the adapter and assign it to the ListView until I actually have data for it... Is this considering OK or am I doing it wrong?
I would set the list adapter up in the onCreate() method under your list view and make this a instance variable (not a local one) so that the Async task can access it as follows i.e.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lv = (ListView) rootView.findViewById(android.R.id.list);
mAdpt = new TasksAdapter(null);
lv.setAdapter(mAdpt);
}
And then in your onPostExecute()
#Override
protected void onPostExecute(List<TasksItem> result) {
dialog.dismiss();
mAdpt.clear();
for( TaskItem ti : result )
{
mAdpt.add( ti );
}
mAdpt.notifyDataSetChanged();
srl.setRefreshing(false);
}
Also make sure you account for the null value being passed into your adapter in the getView() method.
Hope this helps.
you CAN create an empty adapter and fill it later - in AsyncTask
your onPostExecute() should look like:
#Override
protected void onPostExecute(List<TasksItem> result) {
dialog.dismiss();
if (adpt == null){
adpt = new TasksAdapter(result);
lv.setAdapter(adpt);
}else{
adpt.clear();
}
for( TaskItem ti : result ) adpt.add( ti );
adpt.notifyDataSetChanged();
srl.setRefreshing(false);
}
I have a Spinner in my class TelaEntrada.java loading normally.
public class TelaEntrada extends Fragment {
public Fragment newInstance(Context context) {
TelaEntrada telaEntrada = new TelaEntrada();
return telaEntrada;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.telaentrada, container, false);
DbEntrada banco = new DbEntrada(getActivity().getApplicationContext());
List<Tipo> tipos = banco.todosTipos();
List<String> item = new ArrayList<String>();
for(Tipo tipo : tipos){
item.add(tipo.getDescricao().toString());
}
final Spinner spn_Entrada = (Spinner) root.findViewById(R.id.spnEntradaTipo);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this.getActivity(),
android.R.layout.simple_spinner_dropdown_item, item);
spn_Entrada.setAdapter(adapter2);
I too have one DialogFragment in another class. Code is below.
public class DialogTipo extends DialogFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View root = (View) inflater.inflate(R.layout.dialog_tipo, container, false);
final EditText txtDialogTipo = (EditText)root.findViewById(R.id.txtDialogTipo);
Button btnDialogTipoAdd = (Button) root.findViewById(R.id.btnDialogTipoAdd);
Button btnDialogTipoCancel = (Button) root.findViewById(R.id.btnDialogTipoCancel);
btnDialogTipoAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DbEntrada banco = new DbEntrada(getActivity().getApplicationContext());
banco.insereTipo(txtDialogTipo.getText().toString());
//I want update my spinner here!
dismiss();
}
});
getDialog().setTitle("Inserir Novo Tipo");
return root;
}
}
I wanted to add a new item, the spinner was directly updated the database to display the new information in the User spinner. Thanks. Sorry for my english!
I am trying to display text view with 20 seconds delay in a each view page. The view page also display 1 minute delay.means,in each view page i want show three text views, on next page it will show another three text.
please help me out.
private int[] ImageIds={R.drawable.image_asana,R.drawable.image_a,R.drawable.image_c,R.drawable.image_d,
R.drawable.image_e,R.drawable.end_image};
public static ScreenSlideFragment create(int PageNumber){
ScreenSlideFragment fragment=new ScreenSlideFragment();
Bundle args=new Bundle();
args.putInt(ARG_PAGE, PageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlideFragment(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mPageNumber=getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ViewGroup rootView=(ViewGroup)inflater.inflate(R.layout.fragment_screenslide_page, container, false);
((ImageView)rootView.findViewById(R.id.image3)).setImageResource(ImageIds[mPageNumber]);
if (mPageNumber==ImageIds.length) {
handler.removeCallbacks(null);
}
final Runnable textUpdtae = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
((TextView) rootView.findViewById(R.id.imagetextview)).setText(ImageTextIds[count++]);
}
};
newTimer = new Timer();
newTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
handler.post(textUpdtae);
}
}, 0, 5000);
return rootView;
}
You can try setting layoutanimation to viewpager object. This animation will be applied to every view that view pager contains.
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(500);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 50.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(1000);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
<ViewPager>.setLayoutAnimation(controller);
I am a newbie in Android programming and I have this problem:
In this simple example, when I click on start button, I run a fragment. When I click on alarm button, I write in a log file.
Now, if I click first the start button, later the alarm button don't run. I think the problem is the fragment don't leave the interaction with user. Can you help me?
best regads
A.
public class MainActivity extends FragmentActivity {
private static final String FRAG1_TAG = MainActivity.class.getCanonicalName() +".fragment1";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.i("exemple", "start button Start !!!!!!");
goFragment();
}
});
Button buttonAllarm = (Button) findViewById(R.id.buttonAllarm);
buttonAllarm.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.i("exemple", "start button allarm !!!!!!");
}// fine onClick
});// fine onClickListner
}
void goFragment() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction xact = fm.beginTransaction();
if (null == fm.findFragmentByTag(FRAG1_TAG)) {
xact.replace(android.R.id.content, new ListFramment(), FRAG1_TAG);
xact.addToBackStack (null); // questo serve devi metterlo
xact.commit();
}
}
this is the fragment
public class ListFramment extends ListFragment {
// onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, null);
AppLog.logString("Parte onCreateView");
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppLog.logString("Parte onActivityCreated");
ArrayList<Map<String, String>> list = buildData();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(getActivity(), list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Android", "Mobile"));
list.add(putData("iPhone", "iPhone"));
return list;
// TODO Auto-generated method stub
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
}
Your problem is here:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, null);
AppLog.logString("Parte onCreateView");
return view;
}
Your Fragment is inflating the same layout as the Activity that created it. But the Fragment does not have any onClickListeners. I suspect that the Fragment layout is covering up the Activity layout, duplicating it exactly, with non-functional buttons. That would explain why the button click works only the first time.
It would be normal for the Fragment to have its own layout, different from the Activity.
Not seeing your layout file does not help. But I suspect it got nothing to do with fragments.