if (type=="dialer") {
String timestamp = list.get(position).get(Constants.DATE);
holder.txtTimestamp.setVisibility(View.VISIBLE);
holder.imgDelete.setVisibility(View.GONE);
holder.txtTimestamp.setText(getDate(Long.parseLong(timestamp),"dd/MM/yyyy hh:mm:ss"));
if (Integer.parseInt(callType) == CallLog.Calls.BLOCKED_TYPE)
holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.block));
if (Integer.parseInt(callType) == CallLog.Calls.BLOCKED_TYPE)
holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.rejected));
if (Integer.parseInt(callType) == CallLog.Calls.OUTGOING_TYPE)
holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.outgoing_call));
if (Integer.parseInt(callType) == CallLog.Calls.INCOMING_TYPE)
holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.incoming_call));
if (Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE)
holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.missed_call));
if (Integer.parseInt(callType) == CallLog.Calls.ANSWERED_EXTERNALLY_TYPE)
holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.call_received));
}else if (type=="contact") {
holder.txtTimestamp.setVisibility(View.GONE);
holder.imgCallType.setVisibility(View.GONE);
holder.imgDelete.setVisibility(View.GONE);
}else if (type=="favourite"){
holder.txtTimestamp.setVisibility(View.GONE);
holder.imgCallType.setVisibility(View.GONE);
}
// listener
Bitmap finalBitmap = imgBitmap;
holder.imgPic.setOnLongClickListener(view -> {
CustomDialog imageDialog = new CustomDialog(activity,R.layout.image_dialog,"","","", finalBitmap);
imageDialog.setCancelable(true);
imageDialog.show();
return false;
});
holder.itemView.setOnLongClickListener(view->{
if (type=="dialer"){
}else if (type=="contact"){
Intent intent = new Intent(context, ContactDetailsActivity.class);
intent.putExtra("id",list.get(position).get(Constants.ID));
context.startActivity(intent);
}else if (type=="favourite"){
Intent intent = new Intent(context, FavouriteContactDetailsActivity.class);
intent.putExtra(Constants.FAVOURITE_ID,list.get(position).get(Constants.FAVOURITE_ID));
intent.putExtra(Constants.ID,list.get(position).get(Constants.ID));
ActivityOptions anim = ActivityOptions.makeSceneTransitionAnimation(activity);
context.startActivity(intent, anim.toBundle());
}
return false;
});
holder.imgDelete.setOnClickListener(v->{
SqliteFavourite sqliteFavourite = new SqliteFavourite(context.getApplicationContext());
boolean check = sqliteFavourite.deleteData("",list.get(position).get(Constants.ID));
if (check)
Snackbar.make(v,"Successfully deleted",Snackbar.LENGTH_SHORT).show();
else Snackbar.make(v,"Error occurred when deleting",Snackbar.LENGTH_SHORT).show();
});
holder.itemView.setOnClickListener(view->{
call(activity,phoneNumber);
});
I was using these code in an adapter.
#Override
public int getItemCount() {
return list.size();
}
#Override
public int getItemViewType(int position) {
return position;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtContactName,txtContactNumber,txtTimestamp;
ImageView imgPic,imgCallType,imgDelete;
ConstraintLayout contactItem;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
txtContactName = itemView.findViewById(R.id.txtName);
txtContactNumber = itemView.findViewById(R.id.txtContactNumber);
txtTimestamp = itemView.findViewById(R.id.txtTimestamp);
imgPic = itemView.findViewById(R.id.imgContact);
imgCallType = itemView.findViewById(R.id.imgCallType);
contactItem = itemView.findViewById(R.id.contactItem);
imgDelete = itemView.findViewById(R.id.imgDeleteContact);
}
}
Whenever I am clicking on first or second item then I can hear the clickListener. But when I click on 3rd 4th or higher item then I can't hear the clickListener. Even I had set a selector there but the selector is only working for 1st and 2nd
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/contactItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector"
android:clickable="true">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imgContact"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/user_profile"
app:civ_border_color="#FF000000"
app:civ_border_width="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:id="#+id/txtName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Name"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/txtContactNumber"
app:layout_constraintEnd_toStartOf="#+id/imgCallType"
app:layout_constraintStart_toEndOf="#+id/imgContact"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtContactNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Contact Number"
app:layout_constraintBottom_toTopOf="#id/txtTimestamp"
app:layout_constraintEnd_toStartOf="#+id/imgCallType"
app:layout_constraintStart_toEndOf="#+id/imgContact"
app:layout_constraintTop_toBottomOf="#+id/txtName" />
<TextView
android:id="#+id/txtTimestamp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="TextView"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/txtContactNumber"
app:layout_constraintStart_toEndOf="#+id/imgContact"
app:layout_constraintTop_toBottomOf="#id/txtContactNumber" />
<ImageView
android:id="#+id/imgCallType"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/call_received"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imgDeleteContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/ic_menu_delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
When I scroll down then I can hear the listener but whenever I am on top at that layout then I can't access the listener in 3rd or more. Why it's happening? I am not getting any error in logcat. Although I can't click on some items why? I had tried to use holder.contactItem.setOn.... but it wasn't working also.
When I scroll down I can listen the click. But whenever I am at top I can't listen. But I wonder I can click on Image. I meant holder.imgPic.setOnLongClickListener......
I have set onTouchListener to itemView but it's working. It's only not working for onCLickListener and onLongClickListener (As I said earlier it's working when I scroll down).
I have tried
android:focusable="false"
android:clickable="false"
android:longClickable="false"
although I was facing the same issue. I had used them in Constraint Layout which I declared as contactItem. Then I had tried holder.contactItem instead of holder.itemView
Related
I am making chat app but when I send message recycler view does not show first 2 messages because it is up I want something like whatsapp if I open keyboard recycler view is shown from start
I tried following but it sticks chats to end even after I close keyboard:
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
. following is my code:
public class UserChat extends AppCompatActivity {
private RecyclerView recyclerView;
private Button btnSend;
private EditText messageBox;
private TextView name_txt;
final ArrayList<ChatModel> chatModels = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_chat);
recyclerView = findViewById(R.id.chat_list);
messageBox = findViewById(R.id.et_chat_box);
btnSend = findViewById(R.id.btn_chat_send);
name_txt = findViewById(R.id.name_txt);
String username = "username not set";
Bundle extras = getIntent().getExtras();
if(extras!=null){
username = extras.getString("username");
}
name_txt.setText(username);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int i = 1;
i++;
ChatModel chatModel = new ChatModel();
chatModel.setId(i);
chatModel.setMe(true);
chatModel.setMessage(messageBox.getText().toString().trim());
chatModels.add(chatModel);
ChatAdapter chatAdapter = new ChatAdapter(chatModels, getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(chatAdapter);
messageBox.setText("");
}
});
}
}
following is my XML :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UserChat">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/chat_list"
android:layout_width="406dp"
android:layout_height="611dp"
android:paddingStart="15dp"
android:paddingTop="15dp"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintVertical_bias="0.974" />
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e0e0e0"
app:layout_constraintBottom_toTopOf="#+id/layout_gchat_chatbox" />
<RelativeLayout
android:id="#+id/layout_gchat_chatbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="#+id/et_chat_box"
android:layout_width="333dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="0dp"
android:layout_toStartOf="#+id/btn_chat_send"
android:background="#android:color/transparent"
android:hint="Enter Message"
android:inputType="text"
android:maxLines="6"
tools:ignore="Autofill" />
<Button
android:id="#+id/btn_chat_send"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="0dp"
android:background="?attr/selectableItemBackground"
android:text="Send"
android:textColor="#color/teal_700" />
</RelativeLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="408dp"
android:layout_height="64dp"
android:background="#2E2A2A"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/name_txt"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="#FAF8F8"
android:textSize="21sp"
app:layout_constraintEnd_toEndOf="#+id/toolbar"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="#+id/toolbar"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="67dp"
android:layout_height="53dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="#+id/name_txt"
app:layout_constraintHorizontal_bias="0.673"
app:layout_constraintStart_toStartOf="#+id/toolbar"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/female" />
</androidx.constraintlayout.widget.ConstraintLayout>
int i = 1;
i++;
always u send i=2
There was a problem in height of the recycler view. I saw it here: https://github.com/stfalcon-studio/ChatKit/issues/103. #andriizhumela said that you need to put match parent in width and height of recycler view . so now it is solved
I'm trying to create a form to capture data, I need to access the camera to take pictures. My problem is when I try to call the camera with imagenButton, it's not working but the code doesn't show any error.
Here's my fragment to declare form:
public class EspecimenesInsertarFragment extends Fragment implements
Response.Listener<JSONObject>, Response.ErrorListener {
private EspecimenesViewModel especimenesViewModel;
RequestQueue rq;
JsonRequest jrq;
final int COD_SELECCIONA=10;
final int COD_FOTO=20;
private static final int REQUEST_IMAGE_CAMERA=101;
private static final int REQUEST_PERMISSION_CAMERA=101;
Button btnGuardar, btnCancelar;
ImageButton btnCamara, btnGaleria;
ImageView imageView;
public static final int MY_DEFAULT_TIMEOUT = 50000;
public EspecimenesInsertarFragment() {
// Required empty public constructor
}
public static EspecimenesInsertarFragment newInstance() { return new EspecimenesInsertarFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view;
view = inflater.inflate(R.layout.fragment_colecciones_insertar, container, false);
imageView = (ImageView)view.findViewById(R.id.imageView);
btnCamara=(ImageButton) view.findViewById(R.id.tomarFoto);
btnGuardar = (Button) view.findViewById(R.id.buttonIngresarEspecimenes);
rq = Volley.newRequestQueue(getContext());
/* if(validaPermisos()){
btnCamara.setEnabled(true);
}else{
btnCamara.setEnabled(false);
} */
if (ContextCompat.checkSelfPermission(getContext(), WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA}, 1000);
}
btnGuardar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
registrar_especimen("http://localhost/BIO-UES-APP/EspecimenesController.php");
}
});
static final int REQUEST_TAKE_PHOTO=1;
public void tomarFoto(View view){
Intent takePictureInent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Log.d("entra","");
if(takePictureInent.resolveActivity(getActivity().getPackageManager())!= null){
File photoFile=null;
try {
photoFile=createImageFile();
}catch (IOException ex){
}
if(photoFile!=null){
Uri photoUri=FileProvider.getUriForFile(getActivity(),"com.example.luvin.drawercero",photoFile);
takePictureInent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
getActivity().startActivityForResult(takePictureInent,REQUEST_TAKE_PHOTO);
}
}
}
And this is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Especimenes.EspecimenesConsultarFragment">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="-7dp"
android:layout_marginRight="-7dp"
android:layout_marginBottom="9dp"
tools:layout_editor_absoluteX="393dp"
tools:layout_editor_absoluteY="79dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageButton
android:id="#+id/tomarFoto"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="100dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:clickable="true"
android:src="#android:drawable/ic_menu_camera" />
<ImageButton
android:id="#+id/seleccionarDesdeGaleria"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="200dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:src="#android:drawable/ic_menu_gallery" />
</FrameLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="88dp"
android:orientation="vertical">
<Button
android:id="#+id/buttonCancelarEspecimenes"
android:layout_width="129dp"
android:layout_height="41dp"
android:layout_toRightOf="#+id/buttonIngresar"
android:backgroundTint="#96A6A8"
android:gravity="center|left"
android:text="CANCELAR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.783"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.659" />
<Button
android:id="#+id/buttonIngresarEspecimenes"
android:layout_width="129dp"
android:layout_height="41dp"
android:layout_marginStart="68dp"
android:text="INGRESAR"
app:backgroundTint="#00BCD4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/buttonCancelar"
app:layout_constraintHorizontal_bias="0.85"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.659" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
Logcat message when button is pressed:
com.example.luvin.drawercero D/ViewRootImpl#6731d38[MainActivity]: ViewPostIme pointer 1
To complete my question, I have the permissions in the manifest. I already tried to call the camera in another way, tried creating the method in the MainActivity to create the OnClick event in the XML Layout, but nothing works.
If anyone knows how to solve this issue please help.
Thanks.
You forgot to add the on click listener to your photo button. This should solve your problem:
btnCamara = view.findViewById(R.id.tomarFoto);
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tomarFoto(v);
}
});
I am trying to add Text views programmatically to Relative layout parent one below other. However, the first view is not aligning correctly while the rest of the view got aligned correctly. See attached image.
My code:
private void setupQuestionChoices(int position) {
question.setText(questionList.get(position).question());
choicesTextViewsList.clear();
for (int i = 0; i < questionList.get(position).choices().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setBackgroundResource(R.drawable.qa_button_background);
textView.setMinWidth(300);
textView.setGravity(Gravity.CENTER);
textView.setText(questionList.get(position).choices().get(i));
textView.setId(i);
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
layoutParams.addRule(RelativeLayout.BELOW, question.getId());
} else {
layoutParams.addRule(RelativeLayout.BELOW, choicesTextViewsList.get(i - 1).getId());
}
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.setMargins(20, 20, 20, 20);
relativeLayout.addView(textView, layoutParams);
choicesTextViewsList.add(textView);
choicesTextViewsList.get(i).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
answerSelected = true;
}
});
}
}
Relative layout xml where I add my views to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_parent" //here is where I add views to
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:gravity="center">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_back"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Back"
android:textColor="#android:color/darker_gray" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_next"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Next"
android:textColor="#android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I read some posts related to this issue and followed the guidelines from them but that did not help.
How to lay out Views in RelativeLayout programmatically?
Create a new TextView programmatically then display it below another TextView
I was using the index of the for loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.
I changed the line
textView.setId(i)
to
textView.setId(ViewGroup.generateViewId())
Thanks.
I am trying to make a popup window in an activity. The popup window shows up when we click a button in the activity. The layout for popup window is to show a radio group containing 4 radio buttons, a button and a seek bar and has to return a value to the main activity based on the selected radio button when the button in the pop up window is pressed.
When i run the app and open the pop up window it is giving we this error:
"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference "
The code for my main activity is:
public class CustomMenuActivity extends Activity
{
String ingredientName;
String ingredientQuantity;
private PopupWindow pw;
Button setQuantity;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_menu);
setQuantity = (Button) findViewById(R.id.btnSetQty);
setQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
initiatePopupWindow();
}
});
}
private void initiatePopupWindow()
{
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) CustomMenuActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.set_quantity_popup,
(ViewGroup) findViewById(R.id.popupElementid));
// create a 600px width and 570px height PopupWindow
pw = new PopupWindow(layout, 600, 570, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroupid);
final String[] tempQtyVar = new String[1];
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rbqty30id:
tempQtyVar[0] = "30";
break;
case R.id.rbqty60id:
tempQtyVar[0] = "60";
break;
case R.id.rbqty90id:
tempQtyVar[0] = "90";
break;
case R.id.rbqtycutomid:
tempQtyVar[0] = "120";
break;
}
}
});
Button setQtyBtn = (Button) layout.findViewById(R.id.buttonOkSetQtyid);
setQtyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredientQuantity = tempQtyVar[0];
Toast.makeText(getApplicationContext(),ingredientQuantity,Toast.LENGTH_LONG).show();
pw.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
The layout.xml for my pop up window is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#fd050505"
android:padding="30dp"
android:id="#+id/popupElementid">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SET QUANTITY"
android:textColor="#84e9e6"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/popupTitleid"/>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popupTitleid"
android:orientation="horizontal"
android:background="#drawable/btn_rounded_boarder"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroupid"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" 30ml "
android:textColor="#84e9e6"
android:id="#+id/rbqty30id"
android:checked="true" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 60ml "
android:id="#+id/rbqty60id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 90ml "
android:id="#+id/rbqty90id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" Custom Value "
android:id="#+id/rbqtycutomid"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true">
<SeekBar
android:id="#+id/customqtySBid"
android:layout_height="fill_parent"
android:layout_width="600dp"
android:textColor="#84e9e6"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
android:paddingBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonOkSetQtyid"
android:textColor="#84e9e6"
android:text="OK"
android:background="#drawable/btn_rounded_boarder"
android:layout_marginTop="275dp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I tried looking for answer online but didn't really find solution specific to my problem. Please suggest any solutions.
Thanks in advance.
You seem to be inflating the wrong layout. Change the correct layout.xml in
setContentView(R.layout.activity_custom_menu);
I have problem wit changing backgroud color in my project. I'm just trying to change list background color with condition in ViewBinder. But color of backgroung didn't changing.
My ViewBinder
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
LinearLayout ll = (LinearLayout) findViewById(R.id.listViewTopLinearLayout);
String voted = cursor.getString(VOTED_COLLUMN_INDEX);
if (columnIndex == cursor.getColumnIndex(AssetsTableHelper.COLUMN_VOTED)) {
boolean is_checked = voted.equals("true");
if (is_checked) {
((View) ll.getParent()).setBackgroundResource(R.color.votedColor);
((View) view.getParent()).setBackgroundResource(R.color.votedColor);
} else {
((View) view.getParent()).setBackgroundResource(R.color.notVotedColor);
}
return true;
}
return false;
};
onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
integrator = new IntentIntegrator(this);
datasource = new AssetsDataSource(this);
Cursor cursor = datasource.getCursorForAllAssets();
adapter = new SimpleCursorAdapter(this, R.layout.listview_item_row,cursor, UI_BINDING_FROM,UI_BINDING_TO, 0);
adapter.setViewBinder(new CustomViewBinder());
setListAdapter(adapter);
List_item_row.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_marginLeft="5dp"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvHomeNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
I think, problem is in argument of setBackgroundResource(R.color.votedColor). Argument should be a resource id for example some R.drawable.votedColor.
Take a look setBackgroundResource and to make a drawable xml file take a look at Drawable Resource.