How to use a base layout and custom it in android - java

I'am trying to create a Linear Layout and an editText base on a layout.xml file here is the java code :
private EditText editText(int _intID) {
EditText editText = (EditText)findViewById(R.id.player_name);
editText.setId(_intID);
editText.setHint("Element "+_intID);
editTextList.add(editText);
return editText;
}
private LinearLayout linearlayout(int _intID)
{
LinearLayout LLMain= (LinearLayout)findViewById(R.id.linear_player_base);
LLMain.setId(_intID);
LLMain.addView(editText(_intID));
LLMain.setOrientation(LinearLayout.VERTICAL);
linearlayoutList.add(LLMain);
return LLMain;
}
Here is the xml layout I want to use as a base :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="5dp"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:id="#+id/linear_player_base">
<EditText
android:id="#+id/player_name"
android:background="#drawable/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionSend"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
/>
</LinearLayout>
I don't understand why when I do :
LinearLayout LLMain= (LinearLayout)findViewById(R.id.linear_player_base);
and then :
LLMain.setId(_intID);
I have this error : Attempt to invoke virtual method 'void android.widget.LinearLayout.setId(int)' on a null object reference
why LLMain is null ?

You must inflate a custom view with you layout and the find the linear layout in this view...
Do it like this:
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.playerLayout, null);
LinearLayout LLMain= (LinearLayout) v.findViewById(R.id.linear_player_base);

Have you loaded this xml file that you want to use as base ??
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayoutNameHere);

Related

Edit Custom Views programmatically

I created a layout like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rlMenu"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_centerVertical="true">
<RelativeLayout
android:background="#drawable/dark_rectangle_bord"
android:id="#+id/rl1dia"
android:elevation="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_width="#dimen/sizeCard"
android:layout_height="#dimen/sizeCard"
android:layout_marginBottom="#dimen/padding_bottom_cards"
android:layout_marginEnd="#dimen/padding_end_cards"
android:layout_marginRight="#dimen/padding_end_cards"
android:layout_marginLeft="#dimen/padding_start_cards"
android:layout_marginStart="#dimen/padding_start_cards"
android:layout_marginTop="#dimen/padding_top_cards">
<TextView
android:text="1ยบ Dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1dia"
android:textStyle="normal|bold"
android:textColor="#color/Branco"
android:layout_alignParentBottom="true"
android:padding="10dp"
android:textSize="#dimen/texto_pequeno"
android:gravity="center"
android:fontFamily="sans-serif"
android:layout_centerHorizontal="true"/>
<ImageView
app:srcCompat="#drawable/ic_calendario_1"
android:id="#+id/iv1dia"
android:layout_width="#dimen/sizeImage"
android:layout_height="#dimen/sizeImage"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
After that, I had included it on my MainActivity.
<include
android:id="#+id/my_custom_view"
layout="#layout/custom_view></include>
But in this Custom View there are a RelativeLayout, a ImageView and a TextView. I need to create some Custom Views dynamically, sample as below.
How can i create this Custom View Programatically?
Ex.:
Button bt = new Button(MainActivity.this);
And how can I change the TextView, RelativeLayout, background and the ImageView programatically? Like:
CustomView cv = new CustomView(MainActivity.this);
cv.setImage(R.drawable.chips);
cv.setRlBackground(Color.WHITE);
cv.setText("Hello, World!");
You can use LayoutInflater for that
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = (View) inflater.inflate(R.layout.custom_view, null);
View email = (View) mView.findViewById(R.id.email);
yourParentView.addView(mView);
But make sure when you add view to its parent call removeAllview() method of yourParentView
Maybe you want to create an actual custom View, using include will not create a custom View but will only inject in your layout that particular xml. So, create a class (in this case extends RelativeLayout but you can use whatever suits you here)
public class CustomView extends RelativeLayout {
private TextView textView;
private ImageView imageView;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context, R.layout.custom_view, this);
imageView = (ImageView) findViewById(R.id.image_view);
textView = (TextView) findViewById(R.id.text_view);
}
public void setImage(int resId){
imageView.setImageResource(resId);
}
public void setText(String text){
textView.setText(text);
}
}
an xml of the layout of your custom View (custom_view.xml), you might want to use merge instead of include since you already have a parent layout (RelativeLayout in this case)
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/image_view"
android:layout_width="48dp"
android:layout_height="48dp"
/>
</merge>
add your custom view to the Activity layout like this, please note that you need to use the full name of the custom View class you're using, in this case com.example.lelloman.dummy.CustomView
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<com.example.lelloman.dummy.CustomView
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
and in your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
CustomView customView = (CustomView) findViewById(R.id.custom_view);
customView.setImage(R.drawable.some_icon);
customView.setText("Hello world");
}

addView to layout with java

I am trying to understand how to add views programatically (with java) in Android applications. I created a simple layout (below) to practice that but I struggle to solve the following error:
java.lang.IllegalStateException: Could not find method addingView() in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
Screenshot from the app:
My 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:id="#+id/activity_main"
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.android.view_create_test_1.MainActivity">
<LinearLayout
android:id="#+id/top"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#555555">
</LinearLayout>
<RelativeLayout
android:id="#+id/bottom"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#116677">
<Button
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerInParent="true"
android:onClick="addingView"/>
</RelativeLayout>
</LinearLayout>
My JAVA:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addingView (){
LinearLayout top = (LinearLayout) findViewById(R.id.top);
LinearLayout newLayout = new LinearLayout (this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
newLayout.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, 50));
top.addView(newLayout,params);
}
}
Please help me out how to solve the error. Thanks
Your method should come like this
public void addingView (View view){
LinearLayout top = (LinearLayout) findViewById(R.id.top);
LinearLayout newLayout = new LinearLayout (this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
newLayout.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, 50));
top.addView(newLayout,params);
}
When you specify an onclick method in Xml, it must contain the corresponding method in your Activity. However you must include the correct parameters. THe View parameter is required.
Change your addingView method declaration to:
public void addingView (View v){
Your error is however of changeColor(View), which i assume else where, you have done the same thing to the changeColor declaration. Add the View parameter to this method also

Change TextView on content_main.xml in MainActivity.java

I'm using the Android Studio standard template "Navigation Drawer Activity" which contains the content_main.xml layout.
On this layout is my textView which I want to change the text from my MainActivity.java.
I tried following but nothing changes:
LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.content_main, null);
TextView txt = (TextView)view.findViewById(R.id.txt_head);
txt.setText("sdfsdf");
content_main.xml:
<?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:id="#+id/relativelayout_for_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.w4ter.ledcontroldesign.MainActivity"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical"
android:paddingTop="10dp">
<TextView
android:text="Presets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:id="#+id/txt_head" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#61000000" />
</LinearLayout>
there is no need of LayoutInflater. Simply use:
TextView textView = (TextView) findViewById(R.id.txt_head);
texView.setText("Hello");
Hope this helps.
You just need to access the TextView component the normal way. With that template the content_main.xml is included inside the main layout, so you sould do as follows in your main activity (maybe inside the onCreate method or wherever you need):
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = (TextView)view.findViewById(R.id.txt_head);
txt.setText("sdfsdf");
}
//...
}
Assuming that you have a app_bar_main.xml inside the main layout wich contains the content_main.xml. In the end everything is accessible from the main activity directly since the layouts are included in the main layout.
Hope it helps!

Getting Child from linear layout

I created layout file, consist of one linearlayout and two nested linearlayout inside the main one. When I using getParent method, it select the second nested linearlayout. My target was the first one nested linearlayout. So I gives the first nested linearlayout a ID called linear_top. Then I declared in the onCreateView, test and debug, I have no luck it shows that is null.
My target was AnimatedGifImageView
<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/linear_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/music_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Love Story"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.music.flow.lib.AnimatedGifImageView
android:id="#+id/music_anim"
android:layout_width="match_parent"
android:layout_height="76dp"
android:scaleType="fitXY"
android:contentDescription="Animation"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RatingBar
android:id="#+id/music_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:numStars="4"
android:rating="3.5" />
<ImageView
android:id="#+id/btn_playmusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:src="#drawable/resume" />
</LinearLayout>
OnCreateView
linearTop = (LinearLayout) v.findViewById(R.id.linear_top);
AnimatedGifImageView animatedGifImageView =
(AnimatedGifImageView) linearTop.getChildAt(1); /* Null Exception */
Child views are indexed from 0. In other words, the first child view is 0, the second child view is 1... and so on.
Also, why don't you just get the the ImageView by it's ID?
AnimatedGifImageView musicAnim = (AnimatedGifImageView) findViewById(R.id.music_anim);
#Override
public View onCreateView(View v, String name, Context context,
AttributeSet attrs) {
LinearLayout linearTop = (LinearLayout) v.findViewById(R.id.linear_top);
ImageView animatedGifImageView = (ImageView) linearTop.getChildAt(1); // NullPointer
return super.onCreateView(v, name, context, attrs);
}
Does your code look like this? You are getting a NullPointerException because in onCreateView, the parameter View v may be null, as mentioned in the docs here. Are you using fragments? If not, then don't use onCreateView. Place your code instead in onCreate like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearTop = (LinearLayout) findViewById(R.id.linear_top);
ImageView animatedGifImageView = (ImageView) linearTop.getChildAt(1);
setContentView(R.layout.activity_nested_linearlayout);
}
also, as what Sound Conception mentioned, you could just try to get the view directly by using its id instead of manually getting it using an index.

Android - How to set the right on click event to a button that was made programmatically

I've made the next layout xml code which I call it layout_one -
<?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/layout_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Testing 1"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/info_layout"
android:visibility="gone">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
As you can see there are two layout in it - one with textview and button and one with textview.
The layout with the only textview - is gone, And i want by a click on the button, from the visible layout, it will be shown.
Now at the activity i wrote the next code -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
LinearLayout newView0 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView1 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView2 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView3 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
ll.addView(newView0);
ll.addView(newView1);
ll.addView(newView2);
ll.addView(newView3);
setContentView(sv);
newView1.findViewById(R.id.layout_one).setBackgroundColor(0xff0000ff);
TextView tv3 = (TextView) newView3.findViewById(R.id.textView1);
tv3.setText("Suprise Suprise");
infoLay = (View) findViewById(R.id.info_layout);
ll.addView(newView3);
}
Now there is something I don't understad - How could I set an on click listener to the button that will know which layout to show?
Tanks for any kind of help
Actually, there are three linear layouts, which seems redundant.
You can set the onclick listener by checking if textview of each layout is empty or not.
First off, have a look at this http://developer.android.com/training/improving-layouts/reusing-layouts.html. Use that <include> tag instead of dynamically creating all these layouts in your Activity class. Define your ScrollView, and all your LinearLayouts in your test_layout.xml file.
Then, in your Activity, all you have to do is get a reference to those views that's you've defined using findViewById.
Once you've got that working we can address your question as follows:
setContentView(R.layout.test_layout);
LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout3);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View view) {
((View)view.getParent().getParent()).findViewById(R.id.textView2).setVisibility(View.VISIBLE);
}
});
layout1.findViewById(R.id.button1).setOnClickListener(listener);
layout2.findViewById(R.id.button1).setOnClickListener(listener);
layout3.findViewById(R.id.button1).setOnClickListener(listener);
I'd also add that it seems like you're trying to create a list here, in which case you should use a ListView instead of many LinearLayouts.

Categories

Resources