Add editTexts to a layout dynamically using an add button in android - java

I am having a layout with a single editText in it,below the editText I added a add button. Now my question is when I click the add button I need to get another editText below the editText and has to repeat the same.Please anyone help, Thank you.

activity_main.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">
<LinearLayout
android:id="#+id/ll_edit_texts_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
and put following code in your Activity
final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container);
Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(getActivity());
//editText.setId(); you should set id smartly if you wanted to use data from this edittext
llEditTextsContainer.addView(editText);
}
});

First of all create a container view in your main layout which is going to hold the new Edittexts.
<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="com.example.vuclip.dynamictextedit.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addTextView"
android:text="Add EditText"
android:onClick="onClick"
/>
<TableLayout
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
Here, on pressing the button, new Edittexts will be created and added into the TableLayout.
Now create a new Layout which will hold your edittext(I called this file new_layout.xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/newEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Now add this layout into your main activity.
public class MainActivity extends AppCompatActivity {
TableLayout container;
static int rowIndex = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (TableLayout) findViewById(R.id.containerLayout);
}
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRowView = inflater.inflate(R.layout.new_layout,null);
container.addView(newRowView,rowIndex);
rowIndex++;
}}

Related

How to resolve ViewSwitcher not showing the second View

I am trying to switch between two WebViews in my layout with Android Java. The two WebViews load different urls when the oncreate method is called. The only problem am having is that the first webview shows up with the site content when oncreate is called but when I click the button that is supposed to show the next webview, it removes the current webview from view and then shows white screen. Help me resolve that, below is my layout file and the android java code behind file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ViewSwitcher
android:layout_height="wrap_content"
android:id="#+id/viewSwitcher1"
android:layout_width="wrap_content">
<LinearLayout
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.webkit.WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.webkit.WebView
android:id="#+id/webView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ViewSwitcher>
<Button
android:id="#+id/button1"
android:text="Show Next"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
and Android Java code for the behavior
public class MainActivity : AppCompatActivity
{
ViewSwitcher viewSwitcher;
private Button next;
protected WebView mywebview,webview2;
protected int mycount=0;
protected View view1, view2;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
viewSwitcher = FindViewById<ViewSwitcher>(Resource.Id.viewSwitcher1);
next = FindViewById<Button>(Resource.Id.button1);
mywebview = FindViewById<WebView>(Resource.Id.webView1);
webview2 = FindViewById<WebView>(Resource.Id.webView2);
view1 = FindViewById<View>(Resource.Id.view1);
view2 = FindViewById<View>(Resource.Id.view2);
WebViewClient client = mywebview.WebViewClient;
mywebview.LoadUrl("https://www.upwork.com");
webview2.LoadUrl("https:www.sololearn.com");
//set the in and out animation for the viewswitcher
viewSwitcher.SetOutAnimation(this, Resource.Animation.abc_slide_out_top);
viewSwitcher.SetInAnimation(this, Resource.Animation.design_bottom_sheet_slide_in);
next.Click += Next_Click;
//hide the action bar
}
private void Next_Click(object sender, System.EventArgs e)
{
if (viewSwitcher.CurrentView != view1)
{
viewSwitcher.ShowPrevious();
}else if(viewSwitcher.CurrentView != view2)
{
viewSwitcher.ShowNext();
}
}
}

Android onClick on Linear Layout

I have following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="true"
android:onClick="openGithubUrl">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/github_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I want to add a onCLick listener on LinearLayout(#+id/list-item). Is this possible? After the onClick fired I would like to get the content of the child Textview #+id/github_url.
Yes you can set an onClickListener to the linear layout, in your use case,
listItem.setOnClickListener() in the activity.
LinearLayout listItem;
TextView tvGithubUrl;
tvGithubUrl = (TextView) findViewById(R.id.github_url)
listItem = (LinearLayout) findViewById(R.id.list_item)
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Get the github_url text here
tvGithubUrl.getText().toString();
}
});

In Android Studio how can I change the photo of the google account with the rounded border?

I put the photo of the account in the navigation header. The border is rectangular, I would like to make it rounded, how can I do that?
In this code I used Glide to display the image.
my_header_navigation.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"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#color/common_google_signin_btn_text_dark_focused"
android:id="#+id/navigation_header">
<ImageView
android:id="#+id/photoImageView"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="18dp"
android:background="#color/colorAccent"
android:visibility="invisible" />
</RelativeLayout>
and in the Main Activity:
private ImageView photoImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
View hView = navigationView.getHeaderView(0);
photoImageView = hView.findViewById(R.id.photoImageView);
if(user != null){
Glide.with(List.this).load(user.getPhotoUrl()).into(photoImageView);
photoImageView.setVisibility(View.VISIBLE);
}
else{
photoImageView.setVisibility(View.INVISIBLE);
}
Your code i'ts right, you need implement CircleImageView, I show you how to implement.
CircleImageView - GitHub
nav_header_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="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"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/profile_image"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
app:civ_border_color="#EEEEEE"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#8BC34A"/>
</LinearLayout>
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CircleImageView circleImageView = (CircleImageView) findViewById(R.id.profile_image);
Glide.with(List.this).load(user.getPhotoUrl()).into(circleImageView);
}
My NavigationDrawer with CircleImageView
Link Image
NOTE
Remember, your image need proportional dimensioned, example (100x100, 200x200), like a perfect square, I hope I have help you.

EditText not showing text

Dictionary app with listview, Button and EditText.
EditText show keyboard in real device on the first click but it is not showing words and
then it doesn't appear again.
I tried many times to find a solution in stack overflow but none of them worked for me.
Any one can help?
Thanks in advance.
public class MainActivity extends AppCompatActivity {
private ListView listView;
List<WordTranslation> quotes;
Button searchBtn;
EditText editText;
DatabaseAccess databaseAccess;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onStart() {
super.onStart();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
AlertDialog alert = alertDialog.show();
alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBtn = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.EditText);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String word= editText.getText().toString();
databaseAccess.getwordtranslation(word);
}
});
this.listView = (ListView) findViewById(R.id.ListView);
final DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
databaseAccess.getQuotes();
quotes = databaseAccess.ShowDictionaryDetails();
databaseAccess.close();
ArrayAdapter<String> adapter = new Adapter(this, R.layout.activity_list_view_item, quotes);
listView.setAdapter(adapter);
}
xml file
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.future.DictionaryEdition.MainActivity"
android:focusableInTouchMode="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/line2"
android:layout_below="#+id/line1">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ListView">
</ListView>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="True"
android:focusableInTouchMode="true"
android:id="#+id/line1">
<!--android:layout_alignRight="#+id/line3">-->
<!--<SearchView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_weight="1"-->
<!--android:id="#+id/search_view"/>-->
<EditText
android:hint="#string/search_word_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/EditText"
android:focusable="True"
android:focusableInTouchMode="true"
android:layout_weight="3"
android:textColor="#color/colorAccent"
android:inputType="text" />
<Button
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_weight="1" />
</LinearLayout> </RelativeLayout>
Because as you can see in the xml file you write nothing in the android:text field. so it give you nothing written. So change it.
<EditText
android:text="balabalbla"
android:hint="#string/search_word_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/EditText"
android:focusable="True"
android:focusableInTouchMode="true"
android:layout_weight="3"
android:inputType="text" />
and write something on android:text="baablaabla"

Define TextView TypeFace from xml?

I have a ListView that uses SimpleAdapter, each row has 2 TextViews, and I would like to use an external font to one of the two TextView.. Here is the code of the class:
public class QuranEnglish extends Activity {
<...>
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quran_english);
listview=(ListView) findViewById(R.id.QuranEnglishListView);
<..add into list..>
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.quran_english_row,
new String[] {"Arabic","English"},
new int[] {R.id.QuranEnglishTextViewArabic,R.id.QuranEnglishTextViewEnglish});
listview.setAdapter(adapter);
xml for each row (quran_english_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout android:gravity="right" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewArabic" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewEnglish" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
xml for the layout (quran_english.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<ListView android:id="#+id/QuranEnglishListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>
So how can I put the external typeface of the TextView that is in the row (quran_english_row.xml)? I tried using this line though it crashed my application:
((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
R.id.QuranEnglishTextViewArabic
is not an id of one of your views here. It is an id of a example view, which is used to fill the list. In order to change the font, you need to get one view of your list. This works if you have chosen one of your views:
TextView view = getSelectedView();
if (view != null) {
view.setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
}

Categories

Resources