This RadioGroup layout or its LinearLayout parent is useless - java

I am trying to embed RadioGroup with LinearLayout, so to add elements dynamically to my Layout. My code goes here
`
<RadioButton
android:id="#+id/dailyCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/dailyCheck"/>
<RadioButton
android:id="#+id/selectedCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/selectedCheck" />
<RadioButton
android:id="#+id/specificCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/specificCheck" />
`
Doing this shows an exception:
This RadioGroup layout or its LinearLayout parent is useless
Please suggest me what to do.
Why can't LinearLayout and RadioGroup be together in one file.

package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
public class Test extends Activity implements
RadioGroup.OnCheckedChangeListener {
RadioGroup orientation;
RadioGroup gravity;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
orientation = (RadioGroup) findViewById(R.id.orientation);
orientation.setOnCheckedChangeListener(this);
gravity = (RadioGroup) findViewById(R.id.gravity);
gravity.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.horizontal:
orientation.setOrientation(LinearLayout.HORIZONTAL);
break;
case R.id.vertical:
orientation.setOrientation(LinearLayout.VERTICAL);
break;
case R.id.left:
gravity.setGravity(Gravity.LEFT);
break;
case R.id.center:
gravity.setGravity(Gravity.CENTER_HORIZONTAL);
break;
case R.id.right:
gravity.setGravity(Gravity.RIGHT);
break;
}
}
}
//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="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup android:id="#+id/orientation"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px">
<RadioButton
android:id="#+id/horizontal"
android:text="horizontal" />
<RadioButton
android:id="#+id/vertical"
android:text="vertical" />
</RadioGroup>
<RadioGroup android:id="#+id/gravity"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px">
<RadioButton
android:id="#+id/left"
android:text="left" />
<RadioButton
android:id="#+id/center"
android:text="center" />
<RadioButton
android:id="#+id/right"
android:text="right" />
</RadioGroup>
</LinearLayout>

Related

EditText autoscroll doesn't work after showing dialog

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.

Cant see all of my buttons on a phone

**I am making some app and cant see a button that I have created (this is when running the app on my phone) ,but if I run it on the emulator I can see everything - the button that I can't see on my phone is the clicked button (its id is "startBtn").
**
Here is my layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:background="#798"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="unleashed.myprefs.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="hello"
android:background="#ff14"
/>
<Button
android:id="#+id/startBtn"
android:text="start"
android:layout_below="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addOne"
/>
<Button
android:id="#+id/resetBtn"
android:text="restart"
android:layout_below="#+id/textView"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="restart"
/>
</RelativeLayout>
And this is my main activity
package unleashed.myprefs;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button restartNums;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("main", MODE_PRIVATE);
}
private int getNum(){
return prefs.getInt("num",1);
}
private void setNum(int num){ // put the number into my shared preference
prefs.edit().putInt("num", num).apply();
}
public void addOne(View v) { //adding number to the starting button
int num = getNum();
((Button)v).setText("Clicked " + num);
setNum(num + 1);
}
public void restart(View v){
restartNums = (Button) findViewById(R.id.startBtn);
prefs.edit().remove("num").apply();
restartNums.callOnClick();
}
}
I will appreciate any help.
AlignParentEnd should be used in ResetButton.( Not sure )
(Alternative) If you trying to have two buttons in the same row, you can try this layout..
`
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="hello"
android:background="#ff14"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation = "horizontal"
android:weightsum="2"
android:layout_below="#+id/textView">
<Button
android:id="#+id/startBtn"
android:text="start"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="addOne"
/>
<Button
android:id="#+id/resetBtn"
android:text="restart"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="restart"/>
`

Dynamically Remove EditText Field in LinearLayout

My problem is that once my editText fields are created, I don't know how to remove them one by one with the other button in my app. I don't really understand the android:id element as it relates to setID() in my java code. I guess my main question is, how do I figure out how to target dynamically created editText views with their id's if I don't know their id's. Here is my code, any help is really appreciated:
Java:
package com.zdowning.decisionmaker;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.lang.*;
import static com.zdowning.decisionmaker.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setId(numberOfLines++);
et.setText("Enter Answer #" + numberOfLines++);
ll.addView(et);
numberOfLines++;
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void Remove_Line() {
}
public void getAnswer() {
String[] options = new String[numberOfLines];
EditText text = (EditText)findViewById(R.id.editText2);
options[0] = text.getText().toString();
text = (EditText)findViewById(R.id.editText3);
options[1] = text.getText().toString();
text = (EditText)findViewById(R.id.editText4);
options[2] = text.getText().toString();
int number = (int)(Math.random() * 3);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Your Question Here"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:id="#+id/linearLayoutDecisions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #2" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #1" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/add_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="+"
android:textColor="#android:color/background_light"
android:textSize="18sp"/>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="#+id/remove_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="-"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_margin="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="DECIDE!"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_centerInParent="true" />
</RelativeLayout>
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:textColor="#android:color/black"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
There is no relationship between android:id and your setID.
Check this answer to unsderstand more about that.
Anyway, one approach to achieve to remove a generated line could be based on the children index of your LinearLayout like this:
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
...
}
public void addLine() {
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
mEditTextContainer.addView(et);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void removeLine() {
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}

when I click on Register here, it does not go to the next activity

when I click on Logout button it goes to the next activity but, On the next activity when I click on the "Register here" to go to the next activity it says unfortunately , Login has stopped. Please help me out.
Main Activity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button blogout;
EditText uname1, name1, age1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname1 =(EditText) findViewById(R.id.uname1);
name1 = (EditText) findViewById(R.id.name1);
age1 = (EditText) findViewById(R.id.age1);
blogout = (Button) findViewById(R.id.blogout);
blogout.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.blogout:
startActivity(new Intent(this, Login.class));
break;
}
}
}
Login Activity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends AppCompatActivity implements View.OnClickListener {
Button blogin;
EditText uname, pass;
TextView registerLink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
uname =(EditText) findViewById(R.id.uname);
pass = (EditText) findViewById(R.id.pass);
registerLink = (TextView) findViewById(R.id.registerLink);
blogin = (Button) findViewById(R.id.blogin);
blogin.setOnClickListener(this);
registerLink.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.registerLink:
startActivity(new Intent(this, Register.class));
break;
case R.id.blogin:
break;
}
}
}
Register Acitvity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Register extends AppCompatActivity implements View.OnClickListener {
Button bregister;
EditText uname2, pass1, name2, age2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
uname2 =(EditText) findViewById(R.id.uname2);
pass1 = (EditText) findViewById(R.id.pass1);
name2 = (EditText) findViewById(R.id.name2);
age2 = (EditText) findViewById(R.id.age2);
bregister = (Button) findViewById(R.id.blogin);
bregister.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.bregister:
break;
}
}
}
activity_login layout file
<?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"
android:padding="10dp"
android:background="#28e317"
android:backgroundTint="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/password"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/blogin"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Here."
android:id="#+id/registerLink"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="italic" />
activity_main layout file
<?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:orientation="vertical"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/name"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/name2"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:id="#+id/age"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/age2"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname2"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Logout"
android:id="#+id/blogout"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"/>
activity_register layout file
<?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:orientation="vertical"
android:padding="10dp"
android:background="#28e317"
android:backgroundTint="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/name"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/name1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:id="#+id/age"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/age1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/password1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass1"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:id="#+id/bregister"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"/>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.namrata.login">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/title_activity_login"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Register"
android:label="#string/title_activity_register"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
In register activity xml you have android:id="#+id/bregister"
but in RegisterActivity you searching for login button
bregister = (Button) findViewById(R.id.blogin);
which is null that is why you are getting null pointer exception i think change it to
bregister = (Button) findViewById(R.id.bregister);
In your layout file for register activity (activity_register) you have:
android:id="#+id/bregister"
but in Register Activity.java you're doing:
bregister = (Button) findViewById(R.id.blogin);
so register is set to null. Hence it crashes on:
bregister.setOnClickListener(this);
Your onClick method in MainActivity is not the overrided one that's why it's not getting called automatically.Create it as you have create onClick method in LoginActivity
In your First Activity class MainActivity you are using this layout class
setContentView(R.layout.activity_main)
You should use
setContentView(R.layout.activity_login)
and in this activity_login layout you should
change TextView is Register here To Button
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Here."
android:id="#+id/registerLink"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="italic" />
Remove this and use button instead of TextView.

How to manage layout with checkbox in Android

I have multiple layout in a xml file and I want with checkbox from another xml file to show or hide the layout who is checked or unchecked.
This is the main layout and I want to show or hide Linearlayout1 or LinearLayout2 etc.
So I click on the button "Layout" and I can activate or desactivate layout by checking a checkbox
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout 1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout 2" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout"
android:layout_below="#+id/linearLayout" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout 3" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:text="Layout" />
</RelativeLayout>
With the java code:
package com.dlayout;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.dlayout.Checkbox"));
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.dlayout.Checkbox"));
}
}
and my other xml file with the checkbox
<?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" >
<CheckBox
android:id="#+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox1" />
<CheckBox
android:id="#+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox2" />
<CheckBox
android:id="#+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox3" />
</LinearLayout>
And my java code of my checkbox class:
package com.dlayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
public class Checkbox extends MainActivity {
CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
LinearLayout linearLayout1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
}
// *******************I know, this code is not working***************
public void onCheckboxClicked(View view){
boolean checked = ((CheckBox) view) .isChecked();
switch(view.getId()){
case R.id.cb1:
if (checked)
//linearLayout1.setVisibility(View.INVISIBLE);
else
//linearLayout1.setVisibility(View.VISIBLE);
break;
case R.id.cb2:
if (checked)
//LinearLayout2.setVisibility(View.INVISIBLE);
else
//linearLayout2.setVisibility(View.VISIBLE);
break;
case R.id.cb3:
if (checked)
//LinearLayout3.setVisibility(View.INVISIBLE);
else
//linearLayout2.setVisibility(View.VISIBLE);
break;
//**************************************************************
}
}
}
So when the checkbox1 is checked, the layout1 is visible when isn't, the layout1 is invisible.
Can you suggest me any ideas?
If this is all in one Activity and you want to show/hide a layout from a different xml file then you will first need to inflate that layout so you have a reference to it. Then you can use a checkChangedListener to know when the Checkbox state has changed and change the visibility of that layout.
If it is going from one Activity to another then you can simply check the state of the CheckBox in your onClick() and send some type of extra data in your intent to check in the next Activity to know to show/hide the layout.

Categories

Resources