Remove and add layout dynamically by click button in Android - android

I'm working on a big project and I can not solve the problem of a dynamically adding of components.I want to add layout into other layout by click on a button ADD. After this I want to remove it by click a button REMOVE.
Specially for stackoverflow I build a small example of what I want to do.
To ADD it's not a problem but remove it's a problem.When I click a "remove" button this remove not what I need (I want remove parent of "remove" button).
After this I want to ask something more important.I will need save all this data to the DB.So I don't know how to get data from each Text Fields and put it into list (or something else) because all this Text Fields have same ID.
So I see two way of solution:
1)Change there ID dynamically
2)Something else))
Thank you very much!!!
This is
sub_fields.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/detailsLayout"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAddd"
android:onClick="onClickAddd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REMOVE"
android:id="#+id/btnRemove"
android:onClick="onClickAddd" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/generalLayout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:gravity="center"
android:background="#7d65258a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="FILL FIELDS"
android:id="#+id/textView" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/subLayoutFields">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAdd"
android:onClick="onClickAdd" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package andrey.adddinamicallycontrolsapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickAdd(View view) {
LayoutInflater ltInflater = getLayoutInflater();
final LinearLayout subLayoutFields = (LinearLayout) findViewById(R.id.subLayoutFields);
final View view1 = ltInflater.inflate(R.layout.sub_fields, subLayoutFields, true);
Button buttonRemove = (Button)view1.findViewById(R.id.btnRemove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
subLayoutFields.removeView((LinearLayout)(v.getParent().getParent()));
}});
}

Before adding some view by addView(childView) you should save reference to that. In that case removeView(childView) will work. Getting that view by traveling in view tree by getParent() is not a good idea.
By the way, where is your addView call?

maybe try subLayoutFields.removeView(view1) - view1 is final
if you want to somehow differ your widgets (for storing in db, iterating or whatever you want) you may set for them dynamic ids using View.generateViewId() (API17) or using custom method from most voted answer here

Thank you guys!!!
The answer is :
public void onClickAddPanel(View view) {
LayoutInflater ltInflater = getLayoutInflater();
LinearLayout subLayoutFieldsForBtnAdd = (LinearLayout) findViewById(R.id.productDetails);
View view1 = ltInflater.inflate(R.layout.product_details, subLayoutFieldsForBtnAdd, true);
}
public void onClickRemovePanel(View v) {
View v1 = (View) v.getParent();
LinearLayout subLayoutFieldsForBtnRemove = (LinearLayout) findViewById(R.id.productDetails);
subLayoutFieldsForBtnRemove.removeView(v1);
}

Related

Want Tick and Cross Image on Button

Iam Developing an android app in which one of its module is of Quiz.I want to show Tick image on button when someone click the right answer and wrong when wrong ans is seleceted, I want to show these tick and cross images on the right most of my button.Right now Iam displaying the toast for right and wrong answer.Also I want to show the next quiz question on the same activity when user click the next or previous button.I dont want to create another activity for next question rather want to show the next question on the same activity.
the sample image like what I want to do is like this :
enter image description here
Code of Quiz xml is :
<?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:background="#drawable/empty"
tools:context=".activity.QuizActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="vertical">
<TextView
android:id="#+id/quiz_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Quiz"
android:textStyle="bold"
android:textSize="#dimen/_30ssp"
android:textColor="#FFF"
android:gravity="center_horizontal"/>
<TextView
android:layout_below = "#id/quiz_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Which was the first non test playing country to beat India in an international match?"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/afghan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Afghanistan"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"
android:background="#drawable/button_border"/>
<Button
android:layout_below = "#id/afghan"
android:id="#+id/bang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Bangladesh"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"></Button>
<Button
android:id="#+id/srilanka"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Srilanka"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"
android:background="#drawable/button_border"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:background="#drawable/left_arrow"
android:layout_gravity="bottom"
android:layout_margin="#dimen/_20sdp"/>
<ImageView
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:background="#drawable/right_arrow"
android:layout_gravity="bottom"
android:layout_marginBottom="#dimen/_20sdp"
android:layout_marginLeft="#dimen/_150sdp"/>
</LinearLayout>
</LinearLayout>
Code for Activity is :
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.vshine.neuron.riseshine.R;
public class QuizActivity extends AppCompatActivity {
Button btn1, btn2, btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
btn1 = findViewById(R.id.afghan);
btn2 = findViewById(R.id.bang);
btn3 = findViewById(R.id.srilanka);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Sorry! Wrong Answer",Toast.LENGTH_LONG).show();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Sorry! Wrong Answer",Toast.LENGTH_LONG).show();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Congratulation! Right Answer",Toast.LENGTH_LONG).show();
}
});
getSupportActionBar().setTitle("Quiz");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)
}
return super.onOptionsItemSelected(item);
}
}
I'd suggest you do couple of things.
Instead of hardcodding your buttons' onclick methods, try determining which button was clicked and was it the write answer or not.
You can make a method which will check if the clicked button holds the right answer or not.
For reusing the activity, you can simply update the textview (that holds the question) and the button's texts.

How Whatsapp attachment's window can override keyboard?

I'm working on a similiar problem developing my chat and I was checking on how whatsapp solves it to get an idea to work on it.
On mobile and inside a conversation, when your keyboard is up on the screen and you click on attachments, the attachments window overrides the keboard like the image below. It also look likes the activity under it freezes ( you can look at the slash "|" on the EditText, it seems that is freeze ).
I was wondering how WP could override the keyboard and why it looks like the activity under it is freezing, thanks in advance!
/Use the below code Snipet without using any library/
action_attach.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
private void openDialog() {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the custom popup layout
final View inflatedView;
inflatedView = layoutInflater.inflate(R.layout.custom_dialog_options_menu, null, false);
LinearLayout layoutGallery;
layoutGallery = (LinearLayout) inflatedView.findViewById(R.id.layoutGallery);
layoutGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FloatingView.dismissWindow();
}
});
FloatingView.onShowPopup(this, inflatedView);
}
/**Sample Layout for Pop Up window ie: custom_dialog_options_menu
and change layout according to your choice***/
<?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="wrap_content"
android:orientation="vertical"
android:paddingTop="82dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutGallery"
android:layout_width="0dp"
android:layout_marginTop="18dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/gallery_attach_icon_selector"/>
<TextView
android:id="#+id/tvGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Gallery"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutPhoto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="18dp"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/camera_attach_icon_selector"/>
<TextView
android:id="#+id/tvPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Camera"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutVideo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:layout_weight="1"
android:layout_marginTop="18dp"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/video_attach_icon_selector"/>
<TextView
android:id="#+id/tvVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Video"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
/**FloatingView Class**/
public class FloatingView
{
/* Floating view is used to display a custom view for attachments in the chat screen */
import android.app.Activity;
import android.graphics.Point;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
private static PopupWindow popWindow;
private FloatingView() {
}
public static void onShowPopup(Activity activity, View inflatedView) {
// get device size
Display display = activity.getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
// fill the data to the list items
// set height depends on the device size
popWindow = new PopupWindow(inflatedView, size.x, ViewGroup.LayoutParams.WRAP_CONTENT,
true);
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(activity.getResources().getDrawable(
R.drawable.comment_popup_bg));
// make it focusable to show the keyboard to enter in `EditText`
popWindow.setFocusable(true);
// make it outside touchable to dismiss the popup window
popWindow.setOutsideTouchable(true);
popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
// show the popup at bottom of the screen and set some margin at
// bottom ie,
popWindow.showAtLocation(activity.getCurrentFocus(), Gravity.BOTTOM, 0,
0);
}
public static void dismissWindow() {
popWindow.dismiss();
}

Soft Keyboard doesn't appear when tapping on EditText

I got a really weird problem. I am working on an Android app. If I use my layout on an Activity everything works as expected. But, if I use my layout in a Fragment the Soft Keyboard doesn't show anymore when I focus an EditText.
Here is the code of my fragment:
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import mariohenkel.com.familycheck.R;
import mariohenkel.com.familycheck.fragment.api.BaseFragment;
/**
* Created by Developer on 15.06.2015.
*/
public class SuchenFragment extends BaseFragment {
public static SuchenFragment newInstance() {
return new SuchenFragment();
}
private View v;
EditText etOrt;
public SuchenFragment(){ }
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("SuchenFragment", "onCreate");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_suchen_neu, container, false);
loadViewComponents();
loadInfoView();
return v;
}
private void loadViewComponents() {
etOrt = (EditText) v.findViewById(R.id.editText_Ort);
etOrt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("onClick", "onClick");
}
});
}
private void loadInfoView() {
}
}
The onClick() Event of the EditText fires correctly. I tried to force show the keyboard within the onClick() with following code
InputMethodManager mImm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.showSoftInput(SearchEdit, InputMethodManager.SHOW_IMPLICIT);
but without success. Any ideas what I am doing wrong?
EDIT: I am using this project https://github.com/halysongoncalves/Material-Design-Example and I am trying to use the second tab. The first one is working as expected.
EDIT2: This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<TableLayout
android:id="#+id/tl_views"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow
android:background="#color/accent"
android:minHeight="60dp">
<TextView
android:id="#+id/titel_ort"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textAppearance="?android:textAppearanceLarge"
android:layout_gravity="center_vertical"
android:text="Ort: "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="Ort eingeben"
android:id="#+id/editText_Ort"
android:paddingRight="16dp">
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:background="#color/gruen"
android:minHeight="60dp">
<TextView
android:id="#+id/titel_kategorie"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textAppearance="?android:textAppearanceLarge"
android:layout_gravity="center_vertical"
android:text="Kategorie: "/>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_Kategorie"
android:layout_gravity="center_vertical"
android:paddingRight="16dp"
android:spinnerMode="dropdown" />
</TableRow>
<TableRow
android:background="#color/gelb"
android:minHeight="60dp">
<TextView
android:id="#+id/titel_suchbegriff"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textAppearance="?android:textAppearanceLarge"
android:layout_gravity="center_vertical"
android:text="Suchbegriff: "/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText_Suchbegriff"
android:layout_gravity="center_vertical"
android:hint="Suchbegriff"
android:paddingRight="16dp"/>
</TableRow>
</TableLayout>
<RelativeLayout
android:layout_below="#+id/tl_views"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/theme_dialer_primary"
android:gravity="center">
<TextView
android:id="#+id/titel_suchen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textAppearance="?android:textAppearanceLarge"
android:text="Jetzt suchen"/>
</RelativeLayout>
</RelativeLayout>
Update: One more weird thing. If I open another activity and go back to the activity which hosts the fragments the keyboard is working as expected...
In your xml use, in your EditText tags
<requestFocus />
source : EditText request focus
Also checkout : Android: show soft keyboard automatically when focus is on an EditText
P.S. Little exploration on SO or Google would have the problem, and will save your time of writing the question.
After recreating my project step by step I found my mistake.
In my first fragment I used an AsyncTask with .get() to aquire a SID from a server. After I removed that .get() and changed my following logic everything worked as expected.

Setting a ProgressBar to VISIBLE on click in android

I am trying to make my ProgressBar turn VISIBLE when the LogIn button is clicked. Here's my XML for the Activity:
<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:gravity="center_vertical"
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" >
<EditText
android:id="#+id/txtView_UserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="#string/login_username_hint">
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtView_Password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtView_UserName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:hint="#string/login_password_hint"
android:inputType="textPassword" />
<Button
android:id="#+id/button_LogIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtView_UserName"
android:layout_below="#+id/txtView_Password"
android:layout_marginTop="10dp"
android:onClick="logIn"
android:text="#string/login_button" />
<CheckBox
android:id="#+id/checkbox_RememberPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtView_Password"
android:layout_below="#+id/button_Login"
android:layout_marginTop="15dp"
android:text="#string/remember_my_password" />
<CheckBox
android:id="#+id/checkbox_LogMeInAutomatically"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkbox_RememberPassword"
android:layout_below="#+id/checkbox_RememberPassword"
android:layout_marginTop="-5dp"
android:text="#string/log_me_in_automatically" />
<ProgressBar
android:id="#+id/progressBar_LogIn"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_LogMeInAutomatically"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:visibility="invisible" />
<TextView
android:id="#+id/txtView_Version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:text="#string/version_number" />
</RelativeLayout>
And here is where I am trying to change it to VISIBLE in the code.
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Get Views and Buttons
EditText txt_Username = (EditText) findViewById(R.id.txtView_UserName);
EditText txt_Password = (EditText) findViewById(R.id.txtView_Password);
Button btn_LogIn = (Button) findViewById(R.id.button_LogIn);
CheckBox chkbox_RememberMyPassword = (CheckBox) findViewById(R.id.checkbox_RememberPassword);
CheckBox chkbox_LogMeInAutomatically = (CheckBox) findViewById(R.id.checkbox_LogMeInAutomatically);
ProgressBar progressbar_LogIn = (ProgressBar) findViewById(R.id.progressBar_LogIn);
public void logIn() {
progressbar_LogIn.setVisibility(0);
}
}
The program is crashing without rendering my Activity's Views. Any ideas?
I have a feeling its because you have an onClick for login, but login doesn't take a view parameter. Try setting the onclick for the button programmatically, instead of using the xml.
Edit: Try changing
public void logIn() {
progressbar_LogIn.setVisibility(0);
}
to
public void logIn(View v) {
progressbar_LogIn.setVisibility(0);
}
or to set it programatically
btn_LogIn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
//CODE GOES HERE
}
});
Further Edit:
Also noticed that theres no sign of an onCreateView so make sure that all that view code is inside of, or atleast inside of onCreate:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
}

Android sliding menu to change content on page can this be done?

The main page has a horizontalScrollView across the top and a number of TextView text fields within it. I would like to be able to scroll and click on any on the TextView text and it change the RelativeLayout to match the selection made in the HorizontalScrollView.
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"
>
<HorizontalScrollView android:id="#+id/horizontalScrollView1" android:layout_height="wrap_content" android:scrollbars="none" android:layout_width="wrap_content">
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent" android:id="#+id/linearLayout1">
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTOne" android:text="tOne"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTTwo" android:text="tTwo"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTThree" android:text="tThree"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFour" android:text="tFour"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFive" android:text="tFive"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSix" android:text="tSix"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSeven" android:text="tSeven"></TextView>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/relativeLayout1"></RelativeLayout>
</LinearLayout>
I have a second layout page called tone which would be linked to the menu option tOne in the HorizontalScrollView.
tone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is tOne"></TextView>
</LinearLayout>
The main activity will flag if one of the first 4 options are selected by writing to the LogCat. Is it possible to display the tone.xml within the relativelayout if the user clicks the tOne option from the HorizontalScrollView? Then if the user selects tTwo display ttwo.xml in the RelativeLayoutView etc?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlideMenu extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tOne, tTwo, tThree, tFour, tFive, tSix, tSeven, tEight, tNine, tTen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout vSpace = (RelativeLayout)findViewById(R.id.relativeLayout1);
tOne = (TextView)findViewById(R.id.EditTOne);
tTwo = (TextView)findViewById(R.id.EditTTwo);
tThree = (TextView)findViewById(R.id.EditTThree);
tFour = (TextView)findViewById(R.id.EditTFour);
tFive = (TextView)findViewById(R.id.EditTFive);
tSix = (TextView)findViewById(R.id.EditTSix);
tSeven = (TextView)findViewById(R.id.EditTSeven);
tOne.setOnClickListener(this);
tTwo.setOnClickListener(this);
tThree.setOnClickListener(this);
tFour.setOnClickListener(this);
tFive.setOnClickListener(this);
tSix.setOnClickListener(this);
tSeven.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
//--------------
case R.id.EditTOne:
System.out.println("Text One pressed");
break;
case R.id.EditTTwo:
System.out.println("Text Two pressed");
break;
case R.id.EditTThree:
System.out.println("Text Three pressed");
break;
case R.id.EditTFour:
System.out.println("Text Four pressed");
break;
}
}
}
I believe it is fairly simple. I added
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.tone, vSpace);
after you print "Text One Pressed". Be sure to make vSpace a field first.
To do this with all of them, call
vSpace.removeAllViews()
before you inflate each view.

Categories

Resources