How to let marquee text to show in every activity. Just write at one place and the marquee text should be reflect in every activity. How to do this..please help
here is one way to do that.
first create a BaseActivity that will be extended by every activity whose marquee is similar for every activity.
public abstract class BaseWaalaMarquee extends Activity {
FrameLayout flBaseActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_layout_marquee);
appendView();
}
private void appendView() {
flBaseActivity = (FrameLayout) findViewById(R.id.flBaseActivity);
View mainView = getLayoutInflater().inflate(getLayout(), null);
flBaseActivity.addView(mainView);
}
public abstract int getLayout();
}
create a base layout for this activity named base_layout_marquee as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/fact"
android:layout_width="200dp"
android:layout_height="40dip"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Loading... More text to see if it spans or not and want more">
<requestFocus
android:duplicateParentState="true"
android:focusable="true"
android:focusableInTouchMode="true" />
</TextView>
<FrameLayout
android:id="#+id/flBaseActivity"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
now create your own layout for the activity like below
<?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:background="#f00f0f"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your custom Layout" />
</LinearLayout>
finally create you activity that extends BaseWaalaMarquee as below
package ram.materialnavigation.opengltravis;
import ram.materialnavigation.R;
/**
* Created by view9 on 12/16/15.
*/
public class ExtendedBaseActivity extends BaseWaalaMarquee {
#Override
public int getLayout() {
return R.layout.your_custom_layout;
}
}
Now invoke ExtendedBaseActivity and see the result.
I guess this will be quite helpful for you. cheers
Simple..Take a global string variable and change it accordingly and use it in all the activities.
Related
Hello i'm stcuk by a little problem,
I want to add a new CardView with same set of the first CardView underneath the first Cardview only when i click on my button and this action can be repetable endlessly.
Because i'm beginner in android and i have litterally no idea how to make that.
If anyone can Copy Past this code and help me i will be very happy.
I know the spinner is void but its for after i just want to duplicate this.
my activity :
package com.example.lif.test;
public class testActivity extends AppCompatActivity {
Spinner idSpinnerLance;
EditText idEdtTempsLance;
TextView idTextViewPuissance, textViewTitre;
Button idButtonAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
idSpinnerLance = findViewById(R.id.idSpinnerLance);
idEdtTempsLance = findViewById(R.id.idEdtTempsLance);
textViewTitre = findViewById(R.id.textViewTitre);
idTextViewPuissance = findViewById(R.id.idTextViewPuissance);
idButtonAdd = findViewById(R.id.idButtonAdd);
idButtonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
and my xml code :
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".test.testActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="10dp"
android:layout_margin="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ajouter une lance :"
android:textColor="?android:textColorPrimary"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/idSpinnerLance"
android:layout_width="150dp"
android:layout_height="47dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temps :"
android:textColor="?android:textColorPrimary"
android:textSize="15dp"
android:textStyle="bold" />
<EditText
android:id="#+id/idEdtTempsLance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="#+id/idButtonAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add" />
</LinearLayout>
</ScrollView>
thank you
If those CardView will contain different data you can use a RecyclerView and on button click you can add an item in the list and notify the adapter this will add a new item(here item is your CardView created in a different XML file) on screen.
This could be helpful
and this too
I am new in android and tried to learn how to make an add button which add Views in a certain view dynamically when clicked. But i meet the problem that not sure how to set id for each item in the layout which i want to insert into another view.
Here is the main_view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/container_layout"
android:text="add"
/>
</RelativeLayout>
Here is my code:
public class MainActivity extends AppCompatActivity {
public int index_num;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add_button = findViewById(R.id.add_button);
final LayoutInflater layoutInflater = getLayoutInflater();
final ViewGroup insertPoint = findViewById(R.id.container_layout);
index_num = 0;
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
insertPoint.addView(new_view);
index_num++;
}
});
}
}
And here is the layout which i want to insert the main view which includes 3 Edittext:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
</LinearLayout>
Can someone teach me how to set different id for the three edittext when i pressed the add button?
As #Rasoul Miri said, you can use View.generateViewId() to set id for newly inserted views, and you should use a list to record them. And also you should set the id for those three items.
like these:
for the inserted view
<EditText
android:id="#+id/one_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:id="#+id/two_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:id="#+id/three_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
for the MainActivity
public ArrayList<InsertedView> list;
public void addView() {
View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
insertPoint.addView(new_view);
index_num++;
InsertedView insertedView = new InsertedView(View.generateViewId(), View.generateViewId(), View.generateViewId());
new_view.findViewById(R.id.one_view).setId(insertedView.firstId);
new_view.findViewById(R.id.two_view).setId(insertedView.secondId);
new_view.findViewById(R.id.three_view).setId(insertedView.thirdId);
list.add(insertedView);
}
class InsertedView {
public int firstId;
public int secondId;
public int thirdId;
public InsertedView(int one, int two, int three) {
firstId = one;
secondId = two;
thirdId = three;
}
}
you can use these ways
use View
view.setId(View.generateViewId());
use ViewCompat
ViewCompat.generateViewId()
use ConstraintLayout
ConstraintLayout.generateViewId()
We always have heard using multiple fragments with one activity. Is opposite possible? I am curious about this. Can we use same fragment for multiple activities. Please give ONE EXAMPLE.
How to reuse one Fragment in multiple Activities
The green background with two buttons is a single fragment that is reused among multiple activities.
1. Make your fragment class and layout
MyFragment.java
import android.support.v4.app.Fragment;
public class MyFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myLayout = inflater.inflate(R.layout.my_fragment_layout, container, false);
// add click listeners to the buttons in the fragment
Button buttonOne = myLayout.findViewById(R.id.button_1);
Button buttonTwo = myLayout.findViewById(R.id.button_2);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
return myLayout;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1:
Toast.makeText(getContext(), "Button One", Toast.LENGTH_SHORT).show();
break;
case R.id.button_2:
Toast.makeText(getContext(), "Button Two", Toast.LENGTH_SHORT).show();
break;
}
}
}
my_fragment_layout.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:background="#android:color/holo_green_dark"
android:orientation="vertical">
<Button
android:id="#+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="#+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
2. Add the fragment to your activities
activity_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToRedActivityButtonClick"
android:text="Go to red activity"/>
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity_red.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff3636"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToYellowActivityButtonClick"
android:text="Go to yellow activity"/>
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f9f478"
android:orientation="vertical">
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Notes
For simplicity we added the fragment directly to the xml. You can also load fragments dynamically in code. See the documentation for help with that.
Yes, it is possible to have one fragment with multiple activities.
But you will need to program the layout with java using LayoutParams and embed them in every fragment instance.
On every activity, you need to call this fragment
Create your UI Components in Java, add them to the layout dynamically from Java Class i.e. Your Activities.
I would suggest this approach will not be easy to maintain, if you are not super comfortable with Java exclusively. You will need to forget XML for this approach as nothing will be there in it at all, everything will be done with Java classes only.
generic_error_msg_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/White" >
<TextView
android:id="#+id/error_message_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/font_size_16sp" />
<Button
android:id="#+id/error_button_handler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="left|center_vertical"
android:textSize="#dimen/font_size_16sp"
/>
</RelativeLayout>
GenericErrorFragment.Java
public class GenericErrorFragment extends Fragment{
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View genericView = inflater.inflate(R.layout.generic_error_msg_fragment, null);
TextView errorText = (TextView) genericView.findViewById(R.id.error_message_textview);
errorText.setText("error msg" );
Button errorbutton = (Button) genericView.findViewById(R.id.error_button_handler);
errorbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your logic launch some other activity
}
});
return genericView;
}
}
you can load this fragment in any activity and can define your custom text for error and button handler
I will not write whole code but I can give you exact example you are looking for
Think of an application in which a person can register either as admin or as user
Now while working on it you make 3 fragments in admin registration activity asking
1.) personal information
2.) academic information
3.) admin details
Now for user registration activity say you make 2 fragments to get the following information
1.) personal information
2.) user details
here you used personal information fragment 2 times for 2 activities
code for this is child's play main thing is the concept
I have an abstract class extended from the class View, and 4 subclasses to perform a series of vectorial drawings. Then I have a layout with four Buttons and a View to show the drawings. Depending on which button is clicked, different figures should be drawn in that View. The problem is that when I click one button, the figures are drawn filling the entire screen, the buttons disappear. How could this problem be solved?
public abstract class DrawFiguresWithDimensions extends View{
public DrawingFiguresWithDimensions(Context context)
//...
}
public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawTWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawDobleTWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawBoxWithDimensions extends DrawFiguresWithDimensions {..}
Here is the "activity_main.xml":
<LinearLayout
android:id="#+id/mainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="10"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4"
>
<Button
android:id="#+id/btnSecRec"
android:text="Rec"
android:onClick="btnSecRec"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/btnSecT"
android:text="T"
android:onClick="btnSecT"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/btnSecDobleT"
android:text="DT"
android:onClick="btnSecDobleT"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/btnSecCajon"
android:text="Box"
android:onClick="btnSecCajon"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<View class="com.tari.drawfigureswithdimensions.DrawFiguresWithDimensions"
android:id="#+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
"MainActivity.java"
public class MainActivity extends Activity {
DrawRectangleWithDimensions sec1;
DrawTWithDimensions sec2;
DrawDoubleTWithDimensions sec3;
DrawBoxWithDimensions sec4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnSecRec(View view){
sec1 = new DrawRectangleWithDimensions(this);
sec1.setFigure(10, 40);
setContentView(sec1);
}
public void btnSecT(View view){
sec2 = new DrawTWithDimensions(this);
sec2.setFigure(100, 20, 20, 70);
setContentView(sec2);
}
public void btnSecDobleT(View view){
sec3 = new DrawDoubleTWithDimensions(this);
sec3.setFigure(100, 20, 20, 90, 50, 25);
setContentView(sec3);
}
public void btnSecCajon(View view){
sec4 = new DrawBoxWithDimensions(this);
sec4.setFigure(100, 20, 20, 80);
setContentView(sec4);
}
You can't expect to instantiate an abstract class by referencing it in your xml layout.
You'll need to specify a concrete implementation in order for it to work properly.
So, instead, you might provide a ViewGroup of some sort in your layout to serve as the parent for whatever concrete DrawFiguresWithDimensions implementation you want to show based on what button is pressed.
Then, dynamically remove the current child associated with your parent ViewGroup and add the one corresponding to your pressed button.
Essentially, you'll want to replace this in your activity_main.xml layout:
<View class="com.tari.drawfigureswithdimensions.DrawFiguresWithDimensions"
android:id="#+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
with this:
<RelativeLayout
android:id="#+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And then when handling the button click, do something like:
#Override
public void onClick(View v)
{
int id = v.getId();
if (id == R.id.btnSecCajon)
{
RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
parent.removeAllViews();
parent.addView(new DrawBoxWithDimensions(this));
}
}
Your custom view will be instantiate as soon as the layout will be load so NO abstract class allowed !
Here an exemple to include a custom view in your xml.
http://developer.android.com/training/custom-views/create-view.html
Have a DashBoardActivity that extends a custom MainActivity but i get NullPointerException when trying to access certain views from the DashBoardActivity. Any idea why this could be happening or something am not seeing here?. Thanks
MainActivity:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_layout);
linearlayout = (LinearLayout)findViewById(R.id.footer_container_id);
actionlayout = (LinearLayout)findViewById(R.id.actionbar_container_id);
mainboard = (ImageView)findViewById(R.id.image_dashboard_id);
dashboard = (ImageView)findViewById(R.id.image_comparison_id);
graph = (ImageView)findViewById(R.id.image_graph_id);
contact = (ImageView)findViewById(R.id.image_contact_id);
actionbar = getSupportActionBar();
actionbarView = getLayoutInflater().inflate(R.layout.actionbar, null);
actionbar.setCustomView(actionbarView, new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
public void setSelectedDashBoard(){
mainboard.setSelected(true);
}
DashBoardActivity:
public class DashBoardActivity extends MainActivity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
int orientation = getResources().getConfiguration().orientation;
sessionmanager = new SessionManager(this);
prefs = getSharedPreferences(PREFS_NAME,0);
String cached_username = prefs.getString(USERNAME, "");
mainboard.setSelected(true); // NullPointerException here.
dashboard_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:id="#+id/footer_container_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<include layout="#layout/footer"></include>
</LinearLayout>
<FrameLayout
android:id="#+id/dashboard_fragment_container_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/footer_container_id"
android:layout_alignParentTop="true">
</FrameLayout>
</RelativeLayout>
part of footer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_footer_id" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:background="#292929" android:orientation="horizontal">
<LinearLayout android:id="#+id/ytd_container_id"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:orientation="vertical">
<ImageView android:id="#+id/image_dashboard_id"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:src="#drawable/footer_icons_ytd" />
<TextView android:id="#+id/ytd_text_id" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_gravity="center"
android:layout_marginBottom="5dp" android:layout_marginLeft="5dp"
android:clickable="false" android:paddingLeft="10dp" android:text="Year to Date"
android:textColor="#FFFFFF" android:textSize="10sp">
</TextView>
"
</LinearLayout>
Update
if i use the method setSelectedDashBoard() instead, which i just added from the MainActivity class, i don't get the NullPointerException. don't understand it..
dashboard = (ImageView)findViewById(R.id.image_comparison_id);
the only reason for dashboard to be null is R.id.image_comparison_id does not belongs to R.layout.dashboard_layout
It looks like a typical error of subclass member variable obscuration. You should check if you re-declared the variable dashboard in DashBoardActivity. The subclass is just substituting the inherited variable with a local one which is not initialised.
It is the character " , unless it is a typo.
</TextView>
**"**
</LinearLayout>