EditText autoscroll doesn't work after showing dialog - java

I'm developing an app for users to share their books. To achieve this, I'm getting different data about the book from the user. The problem is, horizontal autoscrolling of all EditTexts are working until Dialog has been shown and dismissed. Once a dialog has been dismissed on the fragment, horizontal autoscrolling of the EditText's on this layout won't work.
I added:
android:focusable="true"
android:focusableInTouchMode="true"
to parent layout of the edittext but doesn't work.
Also to make sure that autoscrolling is enabled I've added:
android:scrollHorizontally="true"
But none of above helped.
Here is XML code of the fragment layout.
<?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="match_parent"
android:background="#color/white"
android:orientation="vertical"
android:paddingStart="#dimen/dp25"
android:paddingEnd="#dimen/dp25"
tools:context=".UI.Fragments.SharePostFragments.Fragment1.OverViewFragment">
<LinearLayout
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:id="#+id/edit_text_name_of_book"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:fontFamily="#font/segoe_ui_semi_bold"
android:hint="#string/name_of_book"
android:inputType="text"
android:maxLength="50"
android:maxLines="1"
android:scrollHorizontally="true"
android:textAlignment="textStart"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="#dimen/font22" />
<TextView
android:id="#+id/text_view_number_of_characters"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="#dimen/dp10"
android:fontFamily="#font/segoe_ui_light"
android:text="#string/_0_50"
android:textColor="#color/colorAccent"
android:textSize="#dimen/font16"
tools:ignore="RtlSymmetry" />
<EditText
android:id="#+id/edit_text_name_of_author"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:layout_marginTop="#dimen/dp30"
android:background="#drawable/round_text_box_gray"
android:fontFamily="#font/segoe_ui_regular"
android:hint="#string/name_of_writer"
android:inputType="text"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<RelativeLayout
android:id="#+id/constraint_layout_1"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:fontFamily="#font/segoe_ui_regular"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/price_of_book"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/azn_sign"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<EditText
android:id="#+id/edit_text_price_of_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/textView2"
android:background="#color/white"
android:clickable="false"
android:hint="#string/_0_0"
android:inputType="text|numberDecimal"
android:maxLength="6"
android:singleLine="true"
android:textColorHint="#android:color/black" />
</RelativeLayout>
<LinearLayout
android:id="#+id/constraint_layout_conditions_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center_vertical"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/book_condition_placeholder"
android:textColor="#color/colorPrimaryDark" />
<ImageButton
android:id="#+id/image_button_conditions"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/dp10"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_spinner" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear_layout_conditions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/text_view_new"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/_new"
android:textColor="#color/dark_gray_text_color" />
<TextView
android:id="#+id/text_view_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/normal"
android:textColor="#color/dark_gray_text_color" />
<TextView
android:id="#+id/text_view_old"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/old"
android:textColor="#color/dark_gray_text_color" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/constraint_layout_languages_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center_vertical"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/language"
android:textColor="#color/colorPrimaryDark" />
<ImageButton
android:id="#+id/image_button_languages"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/dp10"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_spinner" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_languages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Fragment Java code:
package org.kitapp.UI.Fragments.SharePostFragments.Fragment1;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import org.kitapp.R;
import org.kitapp.UI.Dialogs.ConditionDialog.ConditionOfBookDialog;
import org.kitapp.UI.Dialogs.LanguageDialog.LanguageDialog;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
public class OverViewFragment extends Fragment implements OverViewContractor.View {
private final String TAG = OverViewFragment.class.getSimpleName();
private Context mContext;
private OverViewCallback mListener;
private OverViewPresenter mPresenter;
#BindView(R.id.constraint_layout_conditions_root)
LinearLayout conditionRoot;
#BindView(R.id.constraint_layout_languages_root)
LinearLayout languagesRoot;
#BindView(R.id.linear_layout_conditions)
LinearLayout conditions;
#BindView(R.id.root_layout)
LinearLayout rootLayout;
#BindView(R.id.text_view_number_of_characters)
TextView numberOfChars;
#BindView(R.id.edit_text_name_of_book)
EditText nameOfBook;
public interface OverViewCallback {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_over_view, container, false);
ButterKnife.bind(this, view);
new OverViewPresenter(this);
//rootLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
return view;
}
////// CONTRACTOR METHODS //////
#Override
public void setPresenter(OverViewPresenter presenter) {
this.mPresenter = presenter;
}
////// LISTENERS //////
#Override
#OnClick(R.id.image_button_conditions)
public void showConds() {
ConditionOfBookDialog conditionOfBookDialog = new ConditionOfBookDialog(mContext);
conditionOfBookDialog.show();
}
#Override
#OnClick(R.id.image_button_languages)
public void showLangs() {
LanguageDialog languageDialog = new LanguageDialog(mContext);
languageDialog.show();
}
#Override
#SuppressLint("SetTextI18n")
#OnTextChanged(R.id.edit_text_name_of_book)
public void onNameOfBookChange() {
int len = nameOfBook.getText().toString().trim().length();
numberOfChars.setText(len + "/50");
if (len > 50) {
nameOfBook.setText(nameOfBook.getText().toString().substring(0, 50));
nameOfBook.setSelection(nameOfBook.getText().length());
}
}
////// FRAGMENT METHODS //////
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
if (context instanceof OverViewCallback) {
mListener = (OverViewCallback) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OverViewCallback");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
UI Design and Dialog
In the image above, there are EditTexts Name Of Book and Name of Author. Before showing a dialog, both of them work like a charm. When writing something, the cursor automatically scrolls to the end. Once Dialog has been shown and dismissed, none of them works properly, and the cursor stays at the end of the EditText but still the characters are being written continues in the hidden part.
In this UI it cannot be seen because of the issue, but I've written.
Roses are red, violets are blue, Stackoverflow I love u
But only Roses are red, violets are blue, Stac can be seen.
Weird buggy layout UI image
Please, help me to solve this problem. Thanks in advance.

As a solution, I don't know the underlying reason, but the problem solved once I removed Butterknife library from my project.

Related

Android (Java): Programatically adding a custom TableRow View with cells of equal width does not result in cells of equal width

In my Android application, I am attempting to programmatically add a custom TableRow view (CustomTableRow) to a TableLayout. CustomTableRow has cells of equal length (the first two cells have text, and the third one has a edit and delete button, as shown in the diagram below):
However, when I programmatically add a CustomTableRow to my TableLayout, this is the result I get (the cells are all jumbled together instead of occupying equal width):
This seems strange, considering that I have specified that all cells are to have weight 1 in the XML layout that CustomTableRow inflates. Furthermore, I even programatically set the cells in CustomTableRow to have weight 1 as an extra measure. And yet, the cells are still jumbled together.
Below is my code:
activity_main.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="wrap_content">
<include
android:id="#+id/housemate_data_table"
layout="#layout/table" />
</LinearLayout>
table.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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rent_form"
android:layout_marginTop="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:elevation="4dp"
android:padding="8dp"
android:background="#ffffff"
android:orientation="vertical">
<TableLayout
android:id="#+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TableRow android:background="#d6d7d7" android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Area"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textColor="#000000" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
custom_table_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/col1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="#000000" />
<TextView
android:id="#+id/col2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Area"
android:textColor="#000000" />
<LinearLayout
android:id="#+id/col3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:text="E" />
<Button
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:text="D" />
</LinearLayout>
</TableRow>
MainActivity.java:
package com.example.programaticallyaddtablerowtotablelayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve of the table layout.
TableLayout table = (TableLayout) findViewById(R.id.table_layout);
// Create the housemate's custom TableRow.
CustomTableRow customTableRow = new CustomTableRow(getApplicationContext(), "Name", "Area");
customTableRow.findViewById(R.id.col1).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.findViewById(R.id.col2).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.findViewById(R.id.col3).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
table.addView(customTableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}
CustomTableRow.java:
package com.example.programaticallyaddtablerowtotablelayout;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TableRow;
import androidx.annotation.Nullable;
public class CustomTableRow extends TableRow {
public CustomTableRow(Context context) {
super(context);
initUI();
}
public CustomTableRow(Context context, String name, String areaText) {
super(context);
initUI();
}
public CustomTableRow(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
initUI();
}
// Initiate the custom TableRow's UI by inflating the template XML layout.
private void initUI() {
inflate(getContext(), R.layout.custom_table_row, this);
}
}
In custom_table_row.xml, changing TableRow to the merge tag resolved the problem.

onClickListener is not working in Recycler Adapter class

I am using Recycler view to show the list but setOnclicklistener is not woking in Recylerview Adapter class . Below is my Adapter class code & xml code.
i m just simply implementing setOnClickListener to adapter same setOnClickListener was working one day ago and
same issue i met with few days ago but i made anothers adapter and it did the trick but again today i met same problem
though i googled but did get the answer and i m posting here question to what could be the reason i meet often where i m making mistake and keep this in mind for further codes.
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.chat.Activities.Statedata;
import com.example.chat.Models.StateCases;
import com.example.chat.R;
import com.example.chat.databinding.StateDataItemBinding;
import java.util.ArrayList;
public class StatelistAdapter extends RecyclerView.Adapter<StatelistAdapter.StatelistHolder> {
Context context;
ArrayList<StateCases> stateCases;
public StatelistAdapter(Context context, ArrayList<StateCases> stateCases) {
this.context = context;
this.stateCases = stateCases;
}
#NonNull
#Override
public StatelistHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.state_data_item, parent, false);
return new StatelistHolder(view);
}
#Override
public void onBindViewHolder(#NonNull StatelistHolder holder, int position) {
StateCases scase = stateCases.get(position);
holder.binding.states.setText(scase.getStatename());
holder.binding.sconfirmed.setText(scase.getTodaycases());
holder.binding.stotay.setText(scase.getTotalcases());
holder.binding.todayreovered.setText(scase.getTodayrecovered());
holder.binding.srecovered.setText(scase.getRecovered());
holder.binding.sdeaths.setText(scase.getDeaths());
holder.binding.todaydeath.setText(scase.getTodaydeaths());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, Statedata.class);
intent.putExtra("totalcase", scase.getTotalcases());
intent.putExtra("todaycase", scase.getTodaycases());
intent.putExtra("statename", scase.getStatename());
intent.putExtra("todayrecovered", scase.getTodayrecovered());
intent.putExtra("totalrecovered", scase.getRecovered());
intent.putExtra("todaydeaths", scase.getTodaydeaths());
intent.putExtra("totaldeaths", scase.getDeaths());
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return stateCases.size();
}
public class StatelistHolder extends RecyclerView.ViewHolder {
StateDataItemBinding binding;
public StatelistHolder(#NonNull View itemView) {
super(itemView);
binding = StateDataItemBinding.bind(itemView);
}
}
}
XML
<?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"
android:stretchColumns="1,2,3,4,5"
tools:context=".Activities.Statewiselist">
<com.romainpiel.shimmer.ShimmerTextView
android:id="#+id/lastime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Loading..." />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/lastime"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#318797"
android:stretchColumns="1,2,3,4,5">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/textView25"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="States"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Today Cases"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latest Reco."
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Recovered"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latest deat."
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginEnd="5dp"
android:text="Deaths"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<com.cooltechworks.views.shimmer.ShimmerRecyclerView
android:id="#+id/strecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:shimmer_demo_angle="20"
app:shimmer_demo_child_count="10"
app:shimmer_demo_grid_child_count="2"
app:shimmer_demo_layout="#layout/statelistitem"
app:shimmer_demo_layout_manager_type="linear_vertical"
tools:listitem="#layout/state_data_item">
</com.cooltechworks.views.shimmer.ShimmerRecyclerView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Thanks in Advance.
In this case the reason may be in StateDataItemBinding.
Is your root view in state_data_item.xml is clickable?

I am having problems programming java on aide app

The app is supposed to call one screen from another and use the other screen's layout components. Whenever I try to call a component, such as ImageButton, it says that member id is unknown, I told my teacher and he says that it shouldn't happen. I have tried renaming the components and even made a new project.
How can I fix it?
the error is in R.id
package com.movil.ejemploventanas;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
public class DatosPersonales extends Activity {
EditText vEdtNom,vEdtEdad;
ImageButton vBtnOrigen,vBtnSantuario;
Intent intnEnvio;
Bundle bndContenedorEnvio;
String strNom;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.datospersonales);
vEdtNom=(EditText)findViewById(R.id.edtNom);
vEdtEdad=(EditText)findViewById(R.id.edtEdad);
vBtnOrigen=(ImageButton)findViewById(R.id.btnOrigen);
vBtnSantuario=(ImageButton)findViewById(R.id.btnSantuario);
intnEnvio=new Intent(DatosPersonales.this,Caracteristicas.class);
bndContenedorEnvio=new Bundle();
vBtnOrigen.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0){
strNom=vEdtNom.getText().toString();
bndContenedorEnvio.putString("nom",strNom);
bndContenedorEnvio.putString("edad", vEdtEdad.getText().toString());
bndContenedorEnvio.putString("opc","1");
intnEnvio.putExtras(bndContenedorEnvio);
startActivity(intnEnvio);
}
});
vBtnSantuario.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0){
strNom=vEdtNom.getText().toString();
bndContenedorEnvio.putString("nom",strNom);
bndContenedorEnvio.putString("edad",vEdtEdad.getText().toString());
bndContenedorEnvio.putString("opc","2");
intnEnvio.putExtras(bndContenedorEnvio);
startActivity(intnEnvio);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtLetrero"
style="style/titulo"
android:layout_centerHorizontal="true"
android:text="#string/letMariposa"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtLetrero"
android:id="#+id/linearlayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp">
<TextView
android:id="#+id/txtNom"
style="style/letreros"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="string/nom"/>
<EditText
android:id="#+id/edtNom"
style="style/datos"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1"
android:id="#+id/linearlayout2"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/txtEdad"
style="style/letreros"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="#string/edad"
android:textAlignment="gravity"/>
<EditText
android:id="#+id/edtEdad"
style="style/datos"
android:layout_width="220dp"
android:ems="10"
android:layout_height="wrap_content"
android:inputType="number"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/origen"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/linearLayout2"
android:layout_marginLeft="30dp"
android:layout_marginTop="200dp"
android:id="#+id/textView4"
style="style/letreros"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="#id/textView4"
android:layout_marginLeft="80dp"
android:id="#+id/btnOrigen"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sant"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/btnOrigen"
android:layout_marginLeft="120dp"
android:id="#+id/textView5"
style="style/letreros"/>
<ImageButton
android:id="#+id/btnSantuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_marginLeft="180dp"/>
</RelativeLayout>

Method is missing or has incorrect signature

I am making a simple app in which context view changes and then a toast message is displayed as input given by user. I don't understand why the app keeps on crashing when changing the context view.
Also Android studio gives this warning:
Method "Toaster" is missing in "FirstActivity" or has incorrect signature.
Here is my code:
activity_me_clicked.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/welcome"
android:textSize="36sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text="#string/intro"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="7dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/named"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="MainProcess"
android:text="#string/done" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
tools:ignore="UselessLeaf"></LinearLayout>
</LinearLayout>
</LinearLayout>
FirstActivity.Java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class FirstActivity extends AppCompatActivity {
public static String owner_string;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
}
public void MainProcess(View view) {
final String TAG="DEBUG";
Log.d(TAG,"At least process started");
EditText owner = (EditText) findViewById(R.id.name);
owner_string = owner.getText().toString();
Log.d(TAG,"owner name stored");
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
setContentView(R.layout.activity_main_screen);
Log.d(TAG,"content shown");
}
}
activity_main_screen.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:orientation="vertical">
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/what_would_you_like_me_to_do_today"
android:textSize="18sp" />
<Button
android:id="#+id/Cam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/camera" />
<Button
android:id="#+id/Mus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/music" />
<Button
android:id="#+id/QR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/scanQR" />
<EditText
android:id="#+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/order"
android:inputType="textPersonName" />
<Button
android:id="#+id/toaster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:onClick="toaster"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/toast"
android:layout_marginEnd="100dp"
android:layout_gravity="center"/>
</LinearLayout>
MainScreen.java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
}
public void toaster(View view){
EditText toast = (EditText)findViewById(R.id.toastText);
String final_toast = toast.getText().toString();
Toast.makeText(getApplicationContext(), final_toast, Toast.LENGTH_SHORT).show();
}
}
EDIT: As suggested, I moved the toaser function to FirstActivity.Java and deleted the MainScreen.java file as it becomes pointless to keep it. But the major problem is when I press the button (id named) the app keeps stopping.
EDIT2: I found that setContentView(R.layout.activity_main_screen) in FirstActivity.Java needs to be above this code
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
so that Activity has access to all the layout components. Thanks solved :)
You replaced the content view in the first activity with a layout that include the onClick attribute, but you have no public void toaster(View view) method there.
So, either don't use setContentView a second time, or implement that method on both Activities.
The recommended way to replace the view is Fragments, by the way
This is a wrong way to change the contents of your Activity, The main problem here is that you have onClick attribute set to "toaster", And you don't have a function called "toaster" in your FirstActivity.
Beside that, The MainScreen Activity in your code will never be used.
So, Your problem is that you set the FirstActivity contents to "activity_main_screen.xml", And FirstActivity doesn't have "toaster" method in it, When you change the context, the Activity will try to find toaster method and the app will crash because the method doesn't exist in FirstActivity.
You can solve this problem by making a method called "toaster" inside FirstActivity, But
This is a very bad practice, You can use Fragments instead, Or you can use Intent to move to another activity.
Some useful links about Fragments:
https://developer.android.com/guide/components/fragments.html
https://www.tutorialspoint.com/android/android_fragments.htm
https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/
Why should I use fragment in Android?

Android ActionBar with custom layout does not occupy whole width of parent

This is my xml fil
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal"
android:gravity="fill_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:background="#fff556ff"
android:id="#+id/actionbar_relativelayout"
>
<ImageView
android:clickable="true"
android:id="#+id/imgMenu"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:paddingLeft="10dp"
android:src="#drawable/menu_dashboard" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="22dp"
android:layout_toEndOf="#+id/imgMenu"
android:layout_toRightOf="#+id/imgMenu"
android:text="#string/app_name"
android:textColor="#ffffff"
android:textSize="14sp" />
<ImageView
android:id="#+id/imgUser"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:src="#drawable/user" />
<ImageView
android:id="#+id/imgMessage"
android:layout_width="40dp"
android:clickable="true"
android:layout_height="40dp"
android:layout_marginTop="15dp"
android:layout_toLeftOf="#+id/imgUser"
android:src="#drawable/message_dashboard"
/>
<ImageView
android:id="#+id/imgInfo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginRight="80dp"
android:layout_marginTop="15dp"
android:src="#drawable/ic_action_search"
android:clickable="true"/>
</RelativeLayout>
</LinearLayout>
This is my Activity
RelativeLayout custom_RelativeLayout = (RelativeLayout)findViewById(R.id.actionbar_relativelayout);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM );
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
I want to its occupy whole space of action bar but it does not occupy i also read many answers on stackoverflow but didn't work.
1) I want match parent space in action bar
Edit This is my main Avtivity
package com.example.qasim.cashmanagement;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class DashboardActivity extends ActionBarActivity implements View.OnClickListener {
View viewAction;
ImageView imgUserbutton;// Actionbar
ImageView imgMessagebutton;// Actionbar
ImageView imgInfobutton;// Action bar
ImageView imgMenubutton;// Actionbar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_two);
android.support.v7.app.ActionBar actionbar = getSupportActionBar();
GetActionBarDetails();// Method for inflating ActionBar
/*
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);*/
/* ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View actionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(actionBarView, lp);*/
// actionBar.setCustomView(R.layout.custom_actionbar);
/* getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
viewAction = getSupportActionBar().getCustomView();*/
}
#Override
protected void onResume() {
super.onResume();
}
private void GetActionBarDetails()
{
RelativeLayout custom_RelativeLayout = (RelativeLayout)findViewById(R.id.actionbar_relativelayout);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM );
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
imgUserbutton = (ImageView) findViewById(R.id.imgUser);
imgInfobutton = (ImageView) findViewById(R.id.imgInfo);
imgMessagebutton = (ImageView)findViewById(R.id.imgMessage);
imgMenubutton = (ImageView)findViewById(R.id.imgMenu);
imgMenubutton.setOnClickListener(this);
imgMessagebutton.setOnClickListener(this);
imgInfobutton.setOnClickListener(this);
imgUserbutton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
If you want to get the height of native action bar then use ?android:attr/actionBarSize in XML.
Now your XML will look like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#fff556ff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="#+id/imgMenu"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.4"
android:text="#string/app_name"
android:textColor="#ffffff"
android:textSize="20sp" />
<ImageView
android:id="#+id/imgUser"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imgMessage"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imgInfo"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</RelativeLayout>

Categories

Resources