Implement existing Activity(within there extends/implements) in Fragments (Sherlock) - android-activity

I have developed an app with some activities. Now I would implenent a Navigation with fragments and Sherlock.
I found a lot of examples, but I don't know how to implement my exiting activities.
Since 3 days I'm looking for an solution...
So it would be great if someone can give me a solution or hint.
Thank you!
These are fragment files:
MainActivity
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class MainActivity extends SherlockFragmentActivity {
public static Context appContext;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ActionBar gets initiated
ActionBar actionbar = getSupportActionBar();
//Tell the ActionBar we want to use Tabs.
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//initiating both tabs and set text to it.
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
//create the two fragments we want to use for display content
Fragment PlayerFragment = new AFragment();
Fragment StationsFragment = new BFragment();
//set the Tab listener. Now we can listen for clicks.
PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
StationsTab.setTabListener(new MyTabsListener(StationsFragment));
//add the two tabs to the actionbar
actionbar.addTab(PlayerTab);
actionbar.addTab(StationsTab);
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
//Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
}
AFragment
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.afragment, container, false);
}
}
BFragment
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.bfragment, container, false);
}
}
And here is an exiting Activity
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Admin_Activity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin);
//Something else
//Database / Cursor actions
//...
//Listener Button
Button button_xy = (Button) this.findViewById(R.id.button_xy);
button_xy.setOnClickListener(this);
//...
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonxy:
//intent...
break;
}
}
}

Related

How to use Shared Preference from a Button in another activity?

Im trying to implement SharedPreferences but, since this is all so new to me, Im confused on how to actually do this. I have an activity that contains Two EditTexts and a Button; those EditTtexts are subject and grade, and the button sends it to a fragment activity, which contains a Recyclerview with its cardview etc. I have no trouble getting the input and displaying it in the Recyclerview list. When I try applying the Shared Preferences to save the custom data from the recyclerview, I have no idea where I should input the code.
I tried several tutorials, and I used gson to set a saveData, but I havent been able to call the list, making my app crash.
Activity with the Button
package com.example.repomax;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Objects;
public class Binary extends AppCompatDialogFragment {
private PersonAdapter adapter;
private static final String TAG = "Binary Dialog";
public Binary(PersonAdapter adapter) {
this.adapter = adapter;
}
private void onClick(View v) {
if (classtup.getText().toString().isEmpty() || Sectiones.getText().toString().isEmpty()) {
Toast.makeText(getContext(), "Llene todos los campos", Toast.LENGTH_LONG).show();
} else {
ApplicationClass.materias.add(new Lasmaterias(classtup.getText().toString().trim(),
Sectiones.getText().toString().trim()));
adapter.addMaterias(new Lasmaterias(classtup.getText().toString().trim(),
Sectiones.getText().toString().trim()));
Toast.makeText(getContext(), "Clase Registrada", Toast.LENGTH_SHORT).show();
classtup.setText(null);
Sectiones.setText(null);
saveData();
dismiss();
}
}
public void saveData
public interface OnInputSelected {
void SendInput(String input);
}
public OnInputSelected monInputSelected;
//Widgets
private
RecyclerView.Adapter myAdapter;
TextView Title;
EditText classtup, Sectiones;
Button addud;
Planes planes;
FragmentManager fragmentManager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_binary, container, false);
myAdapter = new PersonAdapter(getContext(), ApplicationClass.materias);
classtup = view.findViewById(R.id.classtup);
Sectiones = view.findViewById(R.id.Sectiones);
fragmentManager = getParentFragmentManager();
planes = (Planes) fragmentManager.findFragmentById(R.id.Planes);
addud = view.findViewById(R.id.addud);
addud.setOnClickListener(this::onClick);
return view;
}
}
FragmentActivity with the RecyclerView:
package com.example.repomax;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import java.util.ArrayList;
public class Planes extends Fragment {
private static final String TAG = "Planes fragment";
RecyclerView recyclerView;
RecyclerView.Adapter myAdapter;
RecyclerView.LayoutManager layoutManager;
View view;
Button fab;
public Planes() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_planes, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);
myAdapter = new PersonAdapter(getContext(), ApplicationClass.materias);
recyclerView.setAdapter(myAdapter);
fab = view.findViewById(R.id.fab);
fab.setOnClickListener(v -> {
Log.d(TAG, "onClick: Abriendo Ventanilla");
Binary dialog = new Binary((PersonAdapter) myAdapter);
dialog.setTargetFragment(Planes.this, 1);
dialog.show(getParentFragmentManager(), "Binary");
});
}
}
And finally the adapter:
package com.example.repomax;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> {
private ArrayList<Lasmaterias> materias;
private Context context;
private PersonAdapter adapter;
public PersonAdapter(Context context, ArrayList<Lasmaterias> list) {
this.context = context;
this.materias = list;
}
public void addMaterias(Lasmaterias lasmaterias) {
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
TextView tvGroup;
public ViewHolder(#NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tvName);
tvGroup = itemView.findViewById(R.id.TvPass);
itemView.setOnClickListener(v -> {
});
}
}
#NonNull
#Override
public PersonAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.class_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull PersonAdapter.ViewHolder viewHolder, int position) {
viewHolder.itemView.setTag(materias.get(position));
viewHolder.tvName.setText(materias.get(position).getName());
viewHolder.tvGroup.setText(materias.get(position).getTelNr());
}
#Override
public int getItemCount() {
return materias.size();
}
}

Passing User input from mainactivity to a second activity in eclipse

I am bran new to this but I have successfully created a functional button.
I am attempting to pass user input (editText1 on activity_main) to Recipe (my second activity) The first code is my functional button. What am I doing wrong in the second round of code in my attempt at passing information to be displayed on the second activity layout?
package com.example.andrew;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v){
startActivity(new Intent(MainActivity.this, Recipe.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Second round of code: My attempt at passing information. First activity
package com.example.andrew;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Edit Text et = (Edit Text) findViewById(R.id.editText1);
String theText = et.getText().toString();
Intent i = new Intent(this, Recipe.class);//Recipe is my second class
i.putExtra("text_label", theText);//what is "text_label"? Where should it be?
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v){
startActivity(new Intent(MainActivity.this, Recipe.class));
}
});
}
}
And in my second activity I have:
public class Recipe extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_layout);
Intent i = getIntent();
uriStringi = i.getStringExtra("text_label");//is text_label on actv. 1?
startActivity(i);
}
You are adding data in the intent but while starting the activity you are not using it. Instead creating new intent.
Solution:
In your current Activity, create a new Intent:
Intent i = new Intent(this, Recipe.class);
i.putExtra("text_label", theText);
startActivity(i); // Write this in onClick()
Then in the Recipe, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("text_label");
}

Unfortunately newsample4 stopped in android

1-When i run it,emulator does not allow to show my button.
2-this is my code:
package fatemi.prj.newsample4;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.util.Date;
public class MainActivity extends Activity { /*main activity*/
Button btn=new Button(this);
private OnClickListener btnOnclickListener= new OnClickListener() {
public void onClick(View v) { /* click button for showing date*/
updateTime();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(btnOnclickListener);
updateTime();
setContentView(btn);
}
private void updateTime() { /* defne update function*/
btn.setText(new Date().toString());
}
}

Eclipse WebView

I would like to open url´s in webview instead of in a webbrowser. I´m no programmer and can´t find a solution. Help would be greatly apreciated. I would like the url to open in a new activity called: activity_webview. Thanks in advance
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.casovi.luxury_new.R;
public class MainCars extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cars);
}
public void GoKoenigsegg(View view) {
goToUrl("http://www.koenigsegg.com/");
}
public void GoMercedes(View view) {
goToUrl("http://www.mercedes-amg.com/#/home");
}
private void goToUrl(String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
}
}
Make your main activity like this:
first create a webview,
second enable webviewclients,
third enable contentview,
finally load your desired url.
All the best..
public class MainCars extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webView1);
web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.koenigsegg.com/");
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient());
setContentView(web);
}

id cannot be resolved or is not a field again

Ok this is the code in my single java file:
package text.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
public class TexttestActivity extends Activity {
/** Called when the activity is first created. */
Button btn;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
EditText piaf=(EditText)findViewById(R.id.fieldd);
piaf.setText("Inception" + "The lord of the Rings");
}
}
I keep getting that error! WTF is wrong?! i tried to google for an answer but nothing worked!
should
EditText piaf=(EditText)findViewById(R.id.fieldd);
be
EditText piaf=(EditText)findViewById(R.id.field);
??