Button in custom dialog has no function - android

i have an issue related to customdialog.The Button in custom dialog has no function. setting_dialog.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:background="#color/exitdialog_background">
<TextView
android:id="#+id/setting_title"
android:layout_height="32dp"
android:layout_width="match_parent"
android:text="Setting"
android:textSize="20dp"
android:textColor="#color/While"
android:paddingTop="4dp"
android:background="#ff40c4ff"
android:typeface="sans"
android:textStyle="bold"
android:gravity="center" />
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_below="#+id/setting_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp">
<Switch
android:id="#+id/volume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:checked="true"
android:textSize="10sp" />
<TextView
android:id="#+id/txtVolume"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:text="Volume"
android:textColor="#color/While"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayoutBtns"
android:layout_below="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp">
<Button
android:id="#+id/btnSave"
android:layout_width="125dp"
android:layout_height="35dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:gravity="center"
android:text="Save"
android:textColor="#color/While"
android:textSize="14dp"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
Setting class
public class SettingDialog extends Dialog implements View.OnClickListener
{
private Activity c;
private Button btnSave;
private TextView txtTitle;
private int layoutResID;
private OnSettingDialogClickListener mSaveClickListener;
public static interface OnSettingDialogClickListener {
public void onClick(SettingDialog settingDialog);
}
public SettingDialog(Activity a, int layoutResID) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
this.layoutResID = layoutResID;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResID);
btnSave = (Button) findViewById(R.id.btnSave);
txtTitle = (TextView) findViewById(R.id.setting_title);
this.setCancelable(true);
this.setCanceledOnTouchOutside(false);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setCancelable(Boolean cancelable)
{
this.setCancelable(cancelable);
}
public void setTitle(String title) {
this.txtTitle.setText(title);
}
public SettingDialog setSaveClickListener(String textName, OnSettingDialogClickListener listener) {
mSaveClickListener = listener;
btnSave.setText(textName);
return this;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSave:
if (mSaveClickListener != null) {
mSaveClickListener.onClick(SettingDialog.this);
}
break;
default:
break;
}
}
}
Call Setting custom dialog
private void ShowSetting() {
SettingDialog settingDialog = new SettingDialog(PlayActivity.this, R.layout.setting_dialog);
settingDialog.setTitle("Setting");
settingDialog.setSaveClickListener("Save", new SettingDialog.OnSettingDialogClickListener() {
#Override
public void onClick(SettingDialog sDialog) {
LogHelper.d("PlayActivity", " Save button ");
sDialog.dismiss();
}
});
settingDialog.show();
}
Setting custom dialog apear and i touch save button but it has no function.
Could you please help me?

Woa, i've just found solution
Mising : btnSave.setOnClickListener(this);

Related

How to fetch data related to a clicked item in a recyclerView from room database to a new activity?

My app has a RawMaterialFragment that displays raw materials data in a recyclerView from room database.
I am trying to build a detail activity(MetrialItemView) to show up the details of an individual raw material by clicking or selecting the raw material from the recyclerView.
my problem is how to send the data from the adapter and how to receive the data in the MetrialItemView Activity and display it.
MaterialListAdapter:
public class MaterialListAdapter extends RecyclerView.Adapter<MaterialListAdapter.ViewHolder> {
private final LayoutInflater mInflater;
private FragmentRawMaterials mContext;
private List<RawMaterialsEntity> mMaterial; // Cached copy of Materials
RawMaterialsEntity mCurrent;
public MaterialListAdapter(FragmentRawMaterials context) {
mInflater = LayoutInflater.from(context.getActivity());
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView materialName;
private final TextView materialBrand;
private final TextView materialQuantity;
LinearLayout parentLayout;
private ViewHolder(View itemView) {
super(itemView);
materialName = itemView.findViewById(R.id.raw_material_name);
materialBrand = itemView.findViewById(R.id.raw_material_brand);
materialQuantity = itemView.findViewById(R.id.raw_material_quantity);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (mMaterial != null) {
mCurrent = mMaterial.get(position);
holder.materialName.setText(mCurrent.getRawMaterialName());
holder.materialBrand.setText(mCurrent.getRawMaterialBrand());
holder.materialQuantity.setText(String.valueOf(mCurrent.getRawMaterialQuantity()));
} else {
// Covers the case of data not being ready yet.
holder.materialName.setText("Name NA");
holder.materialBrand.setText("Brand NA");
holder.materialQuantity.setText("Quantity NA");
}
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
mContext.startActivity(intent);
}
});
}
public void setMaterial(List<RawMaterialsEntity> materials){
mMaterial = materials;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
#Override
public int getItemCount() {
if (mMaterial != null)
return mMaterial.size();
else return 0;
}
}
MaterialItemView:
public class MaterialItemView extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.material_item_view);
}
}
material_list_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw Material Name:"
style="#style/OtherTextViews"/>
<TextView
android:id="#+id/material_name_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw Material Brand:"
style="#style/OtherTextViews"/>
<TextView
android:id="#+id/material_brand_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit Weight:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2000"
style="#style/OtherTextViewsBody"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gm"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit Cost:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cost per gm/ml:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.1"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Available Quantity:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1000"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Cost:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50000"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Name:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pandah"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Email:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pandah#panadh.com"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Phone:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+966555699517"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
</LinearLayout>
FragmentRawMaterials:
public class FragmentRawMaterials extends Fragment{
private RawMaterialViewModel mMaterialViewModel;
private static final int NEW_MATERIAL_ACTIVITY_REQUEST_CODE = 1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_raw_materials, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Setup any handles to view objects here
//FloatingActionButton fab to insert recipes
TextView emptyViewText = view.findViewById(R.id.empty_raw__materials_view);
FloatingActionButton fab = view.findViewById(R.id.fab_raw_materials);
fab.setOnClickListener(view1 -> {
Intent intent = new Intent(getActivity(), RawMaterialsEditor.class);
startActivityForResult(intent, NEW_MATERIAL_ACTIVITY_REQUEST_CODE);
});
RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
//Decoration to add a line divider between recyclerView items
DividerItemDecoration decoration =
new DividerItemDecoration(Objects.requireNonNull(this.getActivity()),
R.drawable.border_line);
recyclerView.addItemDecoration(decoration);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
final MaterialListAdapter adapter = new MaterialListAdapter(this);
recyclerView.setAdapter(adapter);
// Check if adapter list is empty, if so empty text view will appear.
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onChanged() {
super.onChanged();
if (adapter.getItemCount() == 0) {
recyclerView.setVisibility(View.GONE);
emptyViewText.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
emptyViewText.setVisibility(View.GONE);
}
}
});
mMaterialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
// Update the cached copy of the words in the adapter.
mMaterialViewModel.getAllMaterials().observe(this, adapter::setMaterial);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NEW_MATERIAL_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
RawMaterialsEntity material = new RawMaterialsEntity(data
.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_NAME),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_BRAND),
Float.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_WEIGHT)),
Float.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_COST)),
Integer.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_QUANTITY)),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_NAME),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_EMAIL),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_PHONE),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_UOM));
mMaterialViewModel.insertMaterial(material);
mMaterialViewModel.costPerGm();
mMaterialViewModel.totalCost();
} else {
Toast.makeText(
Objects.requireNonNull(getActivity()).getApplicationContext(),
R.string.editor_insert_rm_failed,
Toast.LENGTH_LONG).show();
}
}
}
each row of dataModel() must have ID and then use putExtra() whan clicked It happens
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
intent.putExtra("ID",mCurrent.getID());
mContext.startActivity(intent);
}
});
and use getIntent() in detailActivity
int id =getIntent().getIntExtra("ID",-1);
and then get a row data in detail activity from database(viewModel)by ID and parse it

AVLoadingIndicatorView Library not visible in app

added the library for showing loading effects but it is not visible or any effect is not seen. Although no error occurs but there is no effect seen when the network call begins. The show method doesnot have any effect. Currently im using using android version 4.1.2.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/signin_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center"
android:background="#drawable/ic_arrow_back_black_24dp"></ImageView>
</FrameLayout>
<TextView
android:id="#+id/txt_signIn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="#color/colorPrimary"
android:gravity="center"
android:padding="12dp"
android:text="#string/sign_in"
android:textColor="#color/white"
android:textSize="18dp"></TextView>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Sign in to TruePay"
android:textColor="#color/black"
android:textSize="20dp"
android:textStyle="bold"></TextView>
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp">
<EditText
android:id="#+id/signin_email_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimary"
android:hint="Username"
android:inputType="textEmailAddress"></EditText>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_password"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/signin_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:hint="#string/signin_password"
android:inputType="textPassword"></EditText>
</android.support.design.widget.TextInputLayout>
<TextView
android:id="#+id/forgot_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="#string/forgot_password"
android:textColor="#color/black"
android:textSize="18dp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/use_device_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:padding="10dp"
android:text="#string/device_code"
android:textColor="#color/black"
android:textSize="18dp"
android:textStyle="bold"></TextView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<com.wang.avi.AVLoadingIndicatorView
android:id="#+id/avi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:indicatorName="BallPulseIndicator" />
</RelativeLayout>
</LinearLayout>
java code
public class SignIn extends BaseSupportFragment {
private View view;
private ImageView backPage;
private TextView signInBtn;
private TextView forgotPassword;
private TextView deviiceCodes;
TextInputLayout emailLayout,passwordLayout;
EditText email,password;
private static SessionManagement sessionManagement;
AVLoadingIndicatorView avi;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.sign_in_fragment, container, false);
initiateUI();
setListener();
return view;
}
public void getTokenFromServer()
{
WeakHashMap<String, String> param = new WeakHashMap<>();
param.put("username",email.getText().toString());
param.put("password",password.getText().toString());
Log.e("param...", String.valueOf(param));
RetrofitInterface apiService = RetrofitClient.getClient().create(RetrofitInterface.class);
Observable<TokenModel> call = apiService.postFishDetails(param)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
call.subscribe(new Observer<TokenModel>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
//handle error
Log.e("access",e.toString());
try {
if (e instanceof HttpException) {
if (((HttpException) e).code() == 401) {
// GlobalBus.getBus().post(new TokenExpirationNotification("Token Expired"));
}
}
if (e instanceof IOException) {
}
} catch (Exception e1) {
}
}
#Override
public void onNext(TokenModel response)
{
Constant.ACCESS_TOKEN = "Bearer" + response.getAccessToken();
avi.hide();
Intent intent = new Intent(getActivity(), DrawerAct.class);
getActivity().startActivity(intent);
getActivity().finish();
}
});
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
private void setListener() {
backPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().popBackStack();
}
});
signInBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(TextUtils.isEmpty(email.getText().toString()))
{
emailLayout.setError("Enter Username");
requestFocus(email);
}
else if(TextUtils.isEmpty(password.getText().toString()))
{
passwordLayout.setError("Enter Password");
requestFocus(password);
}
else
{
avi.show();
getTokenFromServer();
}
}
});
forgotPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment(R.id.frame_layout, new ForgotPassword());
}
});
deviiceCodes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment(R.id.frame_layout, new DeviceCode());
}
});
}
private void initiateUI()
{
backPage = (ImageView) view.findViewById(R.id.signin_back_button);
signInBtn = (TextView) view.findViewById(R.id.txt_signIn);
forgotPassword = (TextView) view.findViewById(R.id.forgot_password);
deviiceCodes = (TextView) view.findViewById(R.id.use_device_code);
emailLayout=(TextInputLayout)view.findViewById(R.id.input_layout_email);
passwordLayout=(TextInputLayout)view.findViewById(R.id.input_layout_password);
email=(EditText)view.findViewById(R.id.signin_email_address);
password=(EditText)view.findViewById(R.id.signin_password);
avi=(AVLoadingIndicatorView)view.findViewById(R.id.avi);
}
}
Resolved the issue. Forgot to mention the indiacator color which is white by default so the loader was actually working but wasnt visible;
XML code
<com.wang.avi.AVLoadingIndicatorView
android:id="#+id/avi"
style="#style/AVLoadingIndicatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
app:indicatorColor="#color/colorPrimaryDark"
app:indicatorName="LineSpinFadeLoaderIndicator" />
Your parent layout is LinearLayout, try wrapping your main layout with a RelativeLayout and put the AVLoadingIndicatorView outside the LinearLayout
Just add
app:indicatorColor="#color/grovery_blue" - set your color here
that's all. Enjoy your coding...

No view found for id 0x7f0c0078 for the fragment while calling fragment from activity

In my app i have to call a fragment from activity. so that i am using Frgament mangaer. While i am running that code it throws the above exception.
this is my main activity
public class UserDashBoardActivity extends DrawerActivity {
private Context context;
private ImageButton searchBtn;
private ImageButton favBtn;
private ImageButton profileBtn;
private ImageButton reminderBtn;
private ImageButton logoutBtn;
private ImageButton notificationBtn;
private ImageView seatchIcon;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
#Override
protected void onStart() {
super.onStart();
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
#Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
#Override
protected void onResume() {
super.onResume();
AppActivityStatus.setActivityStarted();
}
#Override
protected void onStop() {
super.onStop();
AppActivityStatus.setActivityStoped();
}
#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_user_dash_boad, menu);
return true;
}
// delete the selected event from event list added here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
return true;
case R.id.action_favourite:
return true;
case R.id.action_navigation:
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_dash_board);
context = getApplicationContext();
searchBtn = (ImageButton) findViewById(R.id.search_btn);
favBtn = (ImageButton) findViewById(R.id.fav_btn);
profileBtn = (ImageButton) findViewById(R.id.profile_btn);
reminderBtn = (ImageButton) findViewById(R.id.reminder_btn);
notificationBtn = (ImageButton) findViewById(R.id.notification_btn);
logoutBtn = (ImageButton) findViewById((R.id.logout_btn));
final EditText Search = (EditText) findViewById(R.id.emailAddress);
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent regAct = new Intent(getApplicationContext(), SearchActivity.class);
// Clears History of Activity
regAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(regAct);
}
});
favBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//here i am calling the fragment
MyFavouritesFragment fragment = new MyFavouritesFragment();
if (fragment != null) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}
});
profileBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent tabAct = new Intent(getApplicationContext(), AboutCollegeFragment.class);
tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabAct);
}
});
}
}
this is my fragment to be called
public class MyFavouritesFragment extends Fragment {
private FavouriteDelegates favouriteDelegates = new FavouriteDelegates();
private Gson gson = new Gson();
private Context context;
private List<CollegeMO> collegeMOs = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View view = inflater.inflate(R.layout.my_favourites_list_view, container, false);
return view;
}
private class FavouriteCollege extends BaseAdapter {
LayoutInflater mInflater;
TextView collegeText;
FavouriteCollege() {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return collegeMOs.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
// show list values name and mobile number in contact page
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.my_favourites, null);
collegeText = (TextView) convertView.findViewById(R.id.clg_details);
collegeText.setText(collegeMOs.get(position).getCollegeName());
return convertView;
}
}
}
this is my main activity xml
<?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="match_parent"
android:layout_gravity="center"
android:background="#color/appblue"
android:orientation="vertical">
<ImageButton
android:id="#+id/search_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="150dp"
android:layout_marginTop="70dp"
android:background="#drawable/search_blue"
android:gravity="center" />
<TextView
android:id="#+id/searchCollege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/search_college"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/fav_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="50dp"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-167dp"
android:background="#drawable/fav_blue"
android:gravity="center" />
<TextView
android:id="#+id/myFavourites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="370dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-30dp"
android:text="#string/my_favourites"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/profile_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_marginLeft="90dp"
android:layout_marginRight="150dp"
android:layout_marginTop="30dp"
android:background="#drawable/profile_blue"
android:gravity="center" />
<TextView
android:id="#+id/myProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="105dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/my_profile"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/notification_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-165dp"
android:background="#drawable/notification_blue"
android:gravity="center" />
<TextView
android:id="#+id/notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="390dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/notification"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/reminder_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="55dp"
android:layout_marginRight="200dp"
android:layout_marginTop="20dp"
android:background="#drawable/reminder_blue"
android:gravity="center" />
<TextView
android:id="#+id/reminder"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginLeft="110dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/reminder"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/logout_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-220dp"
android:background="#drawable/logout_blue"
android:gravity="center" />
<TextView
android:id="#+id/logout"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="410dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/logout"
android:textColor="#color/green"
android:textSize="20sp" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="1000dp"
android:layout_height="1000dp"
android:layout_gravity="center"
>
</FrameLayout>
</LinearLayout>
this is fragment listview xml
<?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="match_parent"
android:background="#color/white"
android:orientation="vertical">
<ListView
android:id="#+id/course_detail_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbarStyle="outsideOverlay" />
</LinearLayout>
this is the item of myfavourite listview xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/appblue">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="40dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp"
android:background="#color/white">
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageView
android:id="#+id/clgImage"
android:src = "#drawable/ic_launcher"
android:layout_weight="1" android:background="#color/white"
android:padding="20dip" android:gravity="center"/>
<TextView
android:id="#+id/clg_details" android:text="Row 2 column 2"
android:layout_weight="1" android:background="#color/white"
android:textColor="#000000"
android:padding="20dip" android:gravity="center"/>
<ImageView
android:id="#+id/downloadImage"
android:src = "#drawable/ic_launcher"
android:layout_weight="1" android:background="#color/white"
android:padding="20dip" android:gravity="center"/>
</TableRow>
</LinearLayout>
</LinearLayout>
you dont have container (R.id.container) in your main activity xml where you should display your fragment into.
like
<framelayout android:id="#+id/container"/> // this snippet is just for idea.

Need to implement Custom Layout Alert Dialog on listView item click

I need suggestions.
I have a listview with items. I need to implement onitemclick in such a way that it ask for entering a value and value is also shown in that item(which i have clicked). Each row in listview contains a TextView and value should be updated upon clicking corresponding rows.
I am using listview in a fragment class.
I dont get any ideas. I need suggestions or ideas.
Thanks in advance.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "pressed" , Toast.LENGTH_SHORT).show();
}
how to get value from the custom dialog box which has an EditText 'Size' and 'OK' button. This value should be updated to the list.
Create a custom dialog in xml and show dialog in onitemclick for entering a value.
Try this code
My recommendation would be to DialogFragment and a simple, custom listener.
For custom dialog, use diaglogFragment.show(); from your onListItemClick.
To return values/update your listView, add a listener in your DialogFragment which is implemented by its calling Fragment.
Use this :
public class DialogAlert extends Dialog {
Context mContext;
Listeners mListeners;
String mTitle;
String mMessage;
public DialogAlert (Context mContext, Listeners mListeners, String mTitle,
String mMessage) {
// TODO Auto-generated constructor stub
super(mContext);
this.mContext = mContext;
this.mListeners = mListeners;
this.mTitle = mTitle;
this.mMessage = mMessage;
}
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.dialog_alert);
setCancelable(false);
Typeface mTypeface = Typeface.createFromAsset(mContext.getAssets(),
"fonts/helvetica.otf");
TextView mTitle = (TextView) findViewById(R.id.confirm_title);
TextView mMessage = (TextView) findViewById(R.id.confirm_message);
mTitle.setText(this.mTitle);
mMessage.setText(this.mMessage);
mTitle.setTypeface(mTypeface);
mMessage.setTypeface(mTypeface);
Button confirmOK = (Button) findViewById(R.id.confirm_ok);
confirmOK.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mListeners.OnClickOk();
}
});
Button dialogClose = (Button) findViewById(R.id.confirm_close);
dialogClose.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
mListeners.OnClickClose();
}
});
Button dialogCancel = (Button) findViewById(R.id.confirm_cancel);
dialogCancel
.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
mListeners.OnClickCancel();
}
});
confirmOK.setTypeface(mTypeface);
dialogCancel.setTypeface(mTypeface);
}
public interface Listeners {
void OnClickClose();
void OnClickCancel();
void OnClickOk();
}
XML File :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/close_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/dialog_left" />
<Button
android:id="#+id/confirm_close"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="17dp"
android:background="#null"
android:drawableTop="#drawable/dialog_close" />
</RelativeLayout>
<TableLayout
android:id="#+id/edit_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/confirm_title"
android:layout_margin="5dp"
android:layout_marginLeft="75dp"
android:minWidth="200dp"
android:layout_toRightOf="#+id/close_container" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/close_container"
android:layout_marginLeft="15dp"
android:layout_marginTop="30dp"
android:src="#drawable/dialog_alert" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/confirm_message"
android:layout_alignTop="#+id/imageView2"
android:layout_toRightOf="#+id/close_container"
android:orientation="horizontal" >
<Button
android:id="#+id/confirm_ok"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/button_alert"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_weight="1"
android:text="OK"
android:textColor="#ffffff"
android:textStyle="bold" />
<Button
android:id="#+id/confirm_cancel"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/button_alert"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_weight="1"
android:text="CANCEL"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/confirm_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/close_container"
android:layout_alignLeft="#+id/edit_container"
android:layout_marginBottom="40dp"
android:text="Are You Sure ?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/color_white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/confirm_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/confirm_title"
android:layout_centerVertical="true"
android:text="Are you Sure you want to exit dude?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/color_white"
android:textSize="20sp"
android:textStyle="bold" />
Check Dialogfragment class
It provides dialogs with custom layots like activity
here is examplpe how to use it
public class Dialog extends DialogFragment implements View.OnClickListener {
private TextView messageText;
private Button okButton;
private String title;
private String message;
public Dialog(String title,String message) {
this.title = title;
this.message = message;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog, container);
messageText = (TextView) view.findViewById(R.id.dialog_message);
okButton = (Button) view.findViewById(R.id.dialog_ok);
getDialog().setTitle(title);
messageText.setText(message);
okButton.setOnClickListener(this);
return view;
}
#Override
public void onClick(View view) {
dismiss();
}
}
Layout for dialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dialog_message"
android:textAppearance="?android:textAppearanceMedium"/>
<Button
android:id="#+id/dialog_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="#string/ok"
android:background="#drawable/button_blue"
android:textColor="#android:color/white"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
You can show your dialog using this code
Dialog d = new Dialog();
d.show(getSupportFragmetManager(), "Tag");
P.S if your app need to support earlier android versions like 2.2 you need to add appcompat library or sherlock library to your project

Code optimisation. (Architecture)

I'm making a quiz app. User has to finish the phrase shown on display and write the name of the car in the edittext, after pushing on button, if the answer right, edittext become green, if doesn't, become red. If all answers right (green), intent move on next activity.
The question is: how to optimize my code, I don't like how it's look like? If I decide to add some more options it wouldn't be readable.
public class MainActivity extends AppCompatActivity {
EditText et_one_one, et_one_two, et_one_three;
Button buttonCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_one_one = (EditText) findViewById(R.id.et_one_one);
et_one_two = (EditText) findViewById(R.id.et_one_two);
et_one_three = (EditText) findViewById(R.id.et_one_three);
buttonCheck = (Button) findViewById(R.id.buttonCheck);
buttonCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean allAnswersCorrect = true;
String t1 = et_one_one.getText().toString().toLowerCase();
String t2 = et_one_two.getText().toString().toLowerCase();
String t3 = et_one_three.getText().toString().toLowerCase();
if (t1.equals("maserati")){
et_one_one.setBackgroundColor(Color.GREEN);
}
else {
allAnswersCorrect = false;
et_one_one.setBackgroundColor(Color.RED);
}
if (t2.equals("mercedes")){
et_one_two.setBackgroundColor(Color.GREEN);
}
else{
allAnswersCorrect = false;
et_one_two.setBackgroundColor(Color.RED);
}
if (t3.equals("bmw")){
et_one_three.setBackgroundColor(Color.GREEN);
}
else{
allAnswersCorrect = false;
et_one_three.setBackgroundColor(Color.RED);
}
if(allAnswersCorrect) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
});
}
}
In my Layout I use ScrollView:
<LinearLayout 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" tools:context=".MainActivity"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/task1"
android:id="#+id/textView1"
android:textSize="20sp"
android:textColor="#010101" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_one"
android:id="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_one"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_two"
android:id="#+id/textView3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_two"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_three"
android:id="#+id/textView4" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_three"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_four"
android:id="#+id/textView5" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_four"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_five"
android:id="#+id/textView6" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_five"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/buttonCheck" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I would strongly suggest a nice library called Butterknife by JakeWharton :
http://jakewharton.github.io/butterknife/
In your case your code would look like this :
public class MainActivity extends AppCompatActivity {
#Bind(R.id.et_one_one) EditText et_one_one;
#Bind(R.id.et_one_two) EditText et_one_two;
#Bind(R.id.et_one_three) EditText et_one_three;
#Bind(R.id.buttonCheck) Button buttonCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
...
}
#OnClick(R.id.submit)
public void submit(View view) {
// check code here
}
Also you can group all your edit texts in a group :
#Bind({ R.id.et_one_one, R.id.et_one_two, R.id.et_one_three })
List<EditText> nameViews;
And apply some setters or actions on them :
...
ButterKnife.apply(nameViews, LOWERCASE);
...
static final ButterKnife.Action<View> LOWERCASE= new ButterKnife.Action<View>() {
#Override public void apply(View view, int index) {
// TODO set the text of the view to lowercase and disable textview
}
};
You can use RecyclerView to implement this. With it, you can dynamically add as much EditText for cars as you want. The sample is shown as below:
In your Activity:
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private String[] answerArray;
Button buttonCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
answerArray = new String[]{
"maserati",
"mercedes",
"bmw"
... (you can add as much as you want)
}
buttonCheck = (Button) findViewById(R.id.buttonCheck);
}
For RecyclerView.Adapter
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
String[] mAnswerArray;
public static class ViewHolder extends RecyclerView.ViewHolder {
public EditText editText;
public ViewHolder(View v) {
super(v);
editText = (EditText) v.findViewById(R.id.editText);
}
}
public MyRecyclerAdapter(String[] answerArray) {
this.mAnswerArray = answerArray;
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position){
super.onBindViewHolder(holder, position);
final String answer = mAnswerArray.get(position);
if ( holder.editText.getText().toString().toLowerCase().equals(answer) ) {
holder.editText.setBackgroundColor(Color.GREEN);
} else {
holder.editText.setBackgroundColor(Color.RED);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_list_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public int getItemCount() {
return mAnswerArray.size();
}
}
For "my_list_item.xml", you just need to put inside and as for the ButtonCheck you can also pass an array or flag into adapter to record the state of each answer (correct or wrong) in order to decide whether to go to SecondActivity.

Categories

Resources