[postlink]
https://the-best-way-of-life-is-islam.blogspot.com/2015/07/how-to-make-button-go-to-another-page.html
[/postlink]
Okay so I have a section_home layout that has this button code.
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:text="Apply"
android:textColor="@color/white"
android:elevation="0dp"
android:id="@+id/button"
android:layout_alignTop="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
What I want is when someone click's that "Apply" button for it to go to my other layout that's called section_apply
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/window_bg"
android:clipToPadding="false"
android:fitsSystemWindows="true">
<ListView
android:id="@+id/launcherslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/light_bg"
android:divider="@color/transparent" />
Here is my java class for my home layout its called HomeFragment.
public class HomeFragment extends Fragment {
Button button;
private static final String MARKET_URL = "http://ift.tt/OQdlpn";
private String PlayStoreDevAccount, PlayStoreListing, AppOnePackage, AppTwoPackage, AppThreePackage;
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.section_home, container, false);
PlayStoreDevAccount = getResources().getString(R.string.play_store_dev_link);
PlayStoreListing = getActivity().getPackageName();
AppOnePackage = getResources().getString(R.string.app_one_package);
AppTwoPackage = getResources().getString(R.string.app_two_package);
AppThreePackage = getResources().getString(R.string.app_three_package);
ActionBar toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (toolbar != null) {
toolbar.setTitle(R.string.app_name);
}
ObservableScrollView content = (ObservableScrollView) root.findViewById(R.id.HomeContent);
TextView ratebtn = (TextView) root.findViewById(R.id.rate_button);
ratebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent rate = new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_URL + PlayStoreListing));
startActivity(rate);
}
});
//FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.apply_btn);
//fab.setColorNormal(getResources().getColor(R.color.accent));
//fab.setColorPressed(getResources().getColor(R.color.accent_pressed));
//fab.setColorRipple(getResources().getColor(R.color.semitransparent_white));
//fab.show(true);
//fab.attachToScrollView(content);
//fab.setOnClickListener(new View.OnClickListener() {
// @Override
//public void onClick(View v) {
// ((MainActivity) getActivity()).result.setSelectionByIdentifier(3);
//((MainActivity) getActivity()).switchFragment(3, getResources().getString(R.string.section_three), "Apply");
// }
//});
return root;
}
private boolean AppIsInstalled(String packageName) {
final PackageManager pm = getActivity().getPackageManager();
boolean installed;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
}
This is my java class for the apply layout called ApplyFragment
public class ApplyFragment extends Fragment {
private static final String MARKET_URL = "http://ift.tt/OQdlpn";
private String intentString;
private final List<Launcher> launchers = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.section_apply, container, false);
// Splits all launcher arrays by the | delimiter {name}|{package}
String[] launcherArray = getResources().getStringArray(R.array.launchers);
for (String launcher : launcherArray)
launchers.add(new Launcher(launcher.split("\\|")));
ActionBar toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (toolbar != null)
toolbar.setTitle(R.string.section_three);
ListView launcherslist = (ListView) root.findViewById(R.id.launcherslist);
LaunchersAdapter adapter = new LaunchersAdapter(launchers);
launcherslist.setAdapter(adapter);
launcherslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (launchers.get(position).name.equals("Google Now Launcher"))
gnlDialog();
else if (LauncherIsInstalled(launchers.get(position).packageName))
openLauncher(launchers.get(position).name);
else
openInPlayStore(launchers.get(position));
}
});
return root;
}
private boolean LauncherIsInstalled(String packageName) {
final PackageManager pm = getActivity().getPackageManager();
boolean installed;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
private void openLauncher(String name) {
final String className = "com.jahirfiquitiva.paperboard" + ".launchers."
+ Character.toUpperCase(name.charAt(0))
+ name.substring(1).toLowerCase().replace(" ", "").replace("launcher", "")
+ "Launcher";
Class<?> cl = null;
try {
cl = Class.forName(className);
} catch (ClassNotFoundException e) {
Log.e("LAUNCHER CLASS MISSING", "Launcher class for: '" + name + "' missing!");
}
if (cl != null) {
Constructor<?> constructor = null;
try {
constructor = cl.getConstructor(Context.class);
} catch (NoSuchMethodException e) {
Log.e("LAUNCHER CLASS CONS",
"Launcher class for: '" + name + "' is missing a constructor!");
}
try {
if (constructor != null)
constructor.newInstance(getActivity());
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void openInPlayStore(final Launcher launcher) {
intentString = MARKET_URL + launcher.packageName;
final String LauncherName = launcher.name;
final String cmName = "CM Theme Engine";
String dialogContent;
if (LauncherName.equals(cmName)) {
dialogContent = launcher.name + getResources().getString(R.string.cm_dialog_content);
intentString = "http://ift.tt/1bsK4w9";
} else {
dialogContent = launcher.name + getResources().getString(R.string.lni_content);
intentString = MARKET_URL + launcher.packageName;
}
new MaterialDialog.Builder(getActivity())
.title(launcher.name + getResources().getString(R.string.lni_title))
.content(dialogContent)
.positiveText(R.string.lni_yes)
.negativeText(R.string.lni_no)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog dialog) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(intentString));
startActivity(intent);
}
}).show();
}
public class Launcher {
public final String name;
public final String packageName;
public Launcher(String[] values) {
name = values[0];
packageName = values[1];
}
}
class LaunchersAdapter extends ArrayAdapter<Launcher> {
final List<Launcher> launchers;
LaunchersAdapter(List<Launcher> launchers) {
super(getActivity(), R.layout.item_launcher, R.id.launchername, launchers);
this.launchers = launchers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
LauncherHolder holder;
if (item == null) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
item = inflater.inflate(R.layout.item_launcher, parent, false);
holder = new LauncherHolder(item);
item.setTag(holder);
} else {
holder = (LauncherHolder) item.getTag();
}
// Turns Launcher name "Something Pro" to "l_something_pro"
int iconResource = getActivity().getResources().getIdentifier(
"ic_" + launchers.get(position).name.toLowerCase().replace(" ", "_"),
"drawable",
getActivity().getPackageName()
);
holder.icon.setImageResource(iconResource);
holder.launchername.setText(launchers.get(position).name);
if (LauncherIsInstalled(launchers.get(position).packageName)) {
holder.isInstalled.setText(R.string.installed);
holder.isInstalled.setTextColor(getResources().getColor(R.color.green));
} else {
holder.isInstalled.setText(R.string.noninstalled);
holder.isInstalled.setTextColor(getResources().getColor(R.color.red));
}
return item;
}
class LauncherHolder {
final ImageView icon;
final TextView launchername;
final TextView isInstalled;
LauncherHolder(View v) {
icon = (ImageView) v.findViewById(R.id.launchericon);
launchername = (TextView) v.findViewById(R.id.launchername);
isInstalled = (TextView) v.findViewById(R.id.launcherinstalled);
}
}
}
private void gnlDialog() {
final String appLink = MARKET_URL + getResources().getString(R.string.extraapp);
new MaterialDialog.Builder(getActivity())
.title(R.string.gnl_title)
.content(R.string.gnl_content)
.positiveText(R.string.lni_yes)
.negativeText(R.string.lni_no)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog dialog) {
super.onPositive(dialog);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(appLink));
startActivity(intent);
}
}
).show();
}
}
Basically what I want is for the button in the home layout to take you to this ApplyFragment/section_apply layout.
Enregistrer un commentaire